blob: 10ec60d4d4c350bc3a1dcb7c132977ed5648667c [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 Elliott53622a72017-03-28 11:06:33 -0600350 bool VK_KHR_incremental_present_enabled;
351
Ian Elliottd940ca22017-03-22 08:31:27 -0600352 bool VK_GOOGLE_display_timing_enabled;
353 bool syncd_with_actual_presents;
354 uint64_t refresh_duration;
355 uint64_t refresh_duration_multiplier;
356 uint64_t target_IPD; // image present duration (inverse of frame rate)
357 uint64_t prev_desired_present_time;
358 uint32_t next_present_id;
359 uint32_t last_early_id; // 0 if no early images
360 uint32_t last_late_id; // 0 if no late images
361
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600363 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600365 VkQueue graphics_queue;
366 VkQueue present_queue;
367 uint32_t graphics_queue_family_index;
368 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600369 VkSemaphore image_acquired_semaphores[FRAME_LAG];
370 VkSemaphore draw_complete_semaphores[FRAME_LAG];
371 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600372 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600373 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600374 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
Tony Barbourda2bad52016-01-22 14:36:40 -0700376 uint32_t enabled_extension_count;
377 uint32_t enabled_layer_count;
378 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600379 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700380
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600382 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600383 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600384
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700385 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
386 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
387 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
388 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600389 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
390 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
391 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
392 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
393 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600394 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
395 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600396 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600397 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700398 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600399 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600400 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600401 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600402
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800403 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600404 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600405
406 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600408
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600409 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800410 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500411 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600412 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600413 } depth;
414
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600415 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600416 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600417
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700418 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500419 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600420 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600421 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800422 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600423 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600424
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600425 mat4x4 projection_matrix;
426 mat4x4 view_matrix;
427 mat4x4 model_matrix;
428
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600429 float spin_angle;
430 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600431 bool pause;
432
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600433 VkShaderModule vert_shader_module;
434 VkShaderModule frag_shader_module;
435
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600436 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800437
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600438 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600439 int32_t curFrame;
440 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600441 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600442 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600443 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600444 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700445 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
446 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
447 VkDebugReportCallbackEXT msg_callback;
448 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600449
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600450 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600451 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600452};
453
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700454VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
455 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600456 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600457 char *message = (char *)malloc(strlen(pMsg) + 100);
458
459 assert(message);
460
Cody Northropaf28a532016-09-27 10:50:57 -0600461 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
462 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600463 validation_error = 1;
464 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600465 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
466 validation_error = 1;
467 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600468 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600469 validation_error = 1;
470 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
471 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
472 validation_error = 1;
473 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
474 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600475 validation_error = 1;
476 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600477 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600478 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600479 }
480
481#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600482
Tony Barbour602ca4f2016-09-12 14:02:43 -0600483 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600484 struct demo *demo = (struct demo*) pUserData;
485 if (!demo->suppress_popups)
486 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600487 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600488
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600489#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600490
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600491 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600492 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600493 } else if (msgFlags & VK_DEBUG_REPORT_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_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600496 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600497 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600498 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600499 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600500 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600501 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600502 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600503 }
Cody Northropaf28a532016-09-27 10:50:57 -0600504
lenny-lunargfbe22392016-06-06 11:07:53 -0600505#else
Cody Northropaf28a532016-09-27 10:50:57 -0600506
lenny-lunargfbe22392016-06-06 11:07:53 -0600507 printf("%s\n", message);
508 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600509
lenny-lunargfbe22392016-06-06 11:07:53 -0600510#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600511
lenny-lunargfbe22392016-06-06 11:07:53 -0600512 free(message);
513
Cody Northropaf28a532016-09-27 10:50:57 -0600514 //clang-format on
515
lenny-lunargfbe22392016-06-06 11:07:53 -0600516 /*
517 * false indicates that layer should not bail-out of an
518 * API call that had validation failures. This may mean that the
519 * app dies inside the driver due to invalid parameter(s).
520 * That's what would happen without validation layers, so we'll
521 * keep that behavior here.
522 */
523 return false;
524}
525
Ian Elliottd940ca22017-03-22 08:31:27 -0600526bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
527 // The desired time was the earliest time that the present should have
528 // occured. In almost every case, the actual time should be later than the
529 // desired time. We should only consider the actual time "late" if it is
530 // after "desired + rdur".
531 if (actual <= desired) {
532 // The actual time was before or equal to the desired time. This will
533 // probably never happen, but in case it does, return false since the
534 // present was obviously NOT late.
535 return false;
536 }
537 uint64_t deadline = actual + rdur;
538 if (actual > deadline) {
539 return true;
540 } else {
541 return false;
542 }
543}
544bool CanPresentEarlier(uint64_t earliest,
545 uint64_t actual,
546 uint64_t margin,
547 uint64_t rdur) {
548 if (earliest < actual) {
549 // Consider whether this present could have occured earlier. Make sure
550 // that earliest time was at least 2msec earlier than actual time, and
551 // that the margin was at least 2msec:
552 uint64_t diff = actual - earliest;
553 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
554 // This present could have occured earlier because both: 1) the
555 // earliest time was at least 2 msec before actual time, and 2) the
556 // margin was at least 2msec.
557 return true;
558 }
559 }
560 return false;
561}
562
Ian Elliottcf074d32015-10-16 18:02:43 -0600563// Forward declaration:
564static void demo_resize(struct demo *demo);
565
Karl Schultze4dc75c2016-02-02 15:37:51 -0700566static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
567 VkFlags requirements_mask,
568 uint32_t *typeIndex) {
569 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600570 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700571 if ((typeBits & 1) == 1) {
572 // Type is available, does it match user properties?
573 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
574 requirements_mask) == requirements_mask) {
575 *typeIndex = i;
576 return true;
577 }
578 }
579 typeBits >>= 1;
580 }
581 // No memory types matched, return failure
582 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600583}
584
Karl Schultze4dc75c2016-02-02 15:37:51 -0700585static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600586 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600587
Tony Barbource7524e2016-07-28 12:01:45 -0600588 // This function could get called twice if the texture uses a staging buffer
589 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600590 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600591 return;
592
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600593 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600594 assert(!err);
595
Tony Barbource7524e2016-07-28 12:01:45 -0600596 VkFence fence;
597 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
598 .pNext = NULL,
599 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700600 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
601 assert(!err);
602
Karl Schultze4dc75c2016-02-02 15:37:51 -0700603 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700604 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
605 .pNext = NULL,
606 .waitSemaphoreCount = 0,
607 .pWaitSemaphores = NULL,
608 .pWaitDstStageMask = NULL,
609 .commandBufferCount = 1,
610 .pCommandBuffers = cmd_bufs,
611 .signalSemaphoreCount = 0,
612 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600613
Tony Barbource7524e2016-07-28 12:01:45 -0600614 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600615 assert(!err);
616
Tony Barbource7524e2016-07-28 12:01:45 -0600617 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600618 assert(!err);
619
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600620 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600621 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600622 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600623}
624
Karl Schultze4dc75c2016-02-02 15:37:51 -0700625static void demo_set_image_layout(struct demo *demo, VkImage image,
626 VkImageAspectFlags aspectMask,
627 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700628 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600629 VkAccessFlagBits srcAccessMask,
630 VkPipelineStageFlags src_stages,
631 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600632 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600633
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600634 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600635 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600636 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700637 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800638 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600639 .oldLayout = old_image_layout,
640 .newLayout = new_image_layout,
641 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700642 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600643
Tony Barboureb5077b2016-10-19 15:23:36 -0600644 switch (new_image_layout) {
645 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600646 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600647 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
648 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600649
Tony Barboureb5077b2016-10-19 15:23:36 -0600650 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700651 image_memory_barrier.dstAccessMask =
652 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600653 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700654
Tony Barboureb5077b2016-10-19 15:23:36 -0600655 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700656 image_memory_barrier.dstAccessMask =
657 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600658 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700659
Tony Barboureb5077b2016-10-19 15:23:36 -0600660 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700661 image_memory_barrier.dstAccessMask =
662 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600663 break;
664
665 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
666 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
667 break;
668
669 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
670 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
671 break;
672
673 default:
674 image_memory_barrier.dstAccessMask = 0;
675 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600676 }
677
Tony Barbourf165b5d2016-10-03 16:01:41 -0600678
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600679 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600680
Karl Schultze4dc75c2016-02-02 15:37:51 -0700681 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
682 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600683}
684
Karl Schultze4dc75c2016-02-02 15:37:51 -0700685static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700686 const VkCommandBufferBeginInfo cmd_buf_info = {
687 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
688 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600689 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700690 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700691 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800692 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700693 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
694 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800695 };
696 const VkRenderPassBeginInfo rp_begin = {
697 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
698 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800699 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700700 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800701 .renderArea.offset.x = 0,
702 .renderArea.offset.y = 0,
703 .renderArea.extent.width = demo->width,
704 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600705 .clearValueCount = 2,
706 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700707 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800708 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600709
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600710 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600711 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800712 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700713 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
714 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700715 demo->pipeline_layout, 0, 1,
716 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
717 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600718 VkViewport viewport;
719 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700720 viewport.height = (float)demo->height;
721 viewport.width = (float)demo->width;
722 viewport.minDepth = (float)0.0f;
723 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700724 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600725
726 VkRect2D scissor;
727 memset(&scissor, 0, sizeof(scissor));
728 scissor.extent.width = demo->width;
729 scissor.extent.height = demo->height;
730 scissor.offset.x = 0;
731 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700732 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600733 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600734 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600735 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800736 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600737
Tony Barbour22e06272016-09-07 16:07:56 -0600738 if (demo->separate_present_queue) {
739 // We have to transfer ownership from the graphics queue family to the
740 // present queue family to be able to present. Note that we don't have
741 // to transfer from present queue family back to graphics queue family at
742 // the start of the next frame because we don't care about the image's
743 // contents at that point.
744 VkImageMemoryBarrier image_ownership_barrier = {
745 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
746 .pNext = NULL,
747 .srcAccessMask = 0,
748 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
749 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
750 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
751 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
752 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700753 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600754 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
755
756 vkCmdPipelineBarrier(cmd_buf,
757 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600758 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600759 0, NULL, 0, NULL, 1, &image_ownership_barrier);
760 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600761 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600762 assert(!err);
763}
764
Tony Barbour22e06272016-09-07 16:07:56 -0600765void demo_build_image_ownership_cmd(struct demo *demo, int i) {
766 VkResult U_ASSERT_ONLY err;
767
768 const VkCommandBufferBeginInfo cmd_buf_info = {
769 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
770 .pNext = NULL,
771 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
772 .pInheritanceInfo = NULL,
773 };
Tony Barboura72d9492017-01-11 13:35:09 -0700774 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600775 &cmd_buf_info);
776 assert(!err);
777
778 VkImageMemoryBarrier image_ownership_barrier = {
779 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
780 .pNext = NULL,
781 .srcAccessMask = 0,
782 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
783 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
784 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
785 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
786 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700787 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600788 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
789
Tony Barboura72d9492017-01-11 13:35:09 -0700790 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600791 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
792 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
793 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700794 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600795 assert(!err);
796}
797
Karl Schultze4dc75c2016-02-02 15:37:51 -0700798void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600799 mat4x4 MVP, Model, VP;
800 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600801 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600802 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600803
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600804 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600805
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700806 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600807 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700808 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
809 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600810 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600811
Tony Barboura72d9492017-01-11 13:35:09 -0700812 vkWaitForFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence,
813 VK_TRUE, UINT64_MAX);
814
815 err = vkMapMemory(demo->device,
816 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
817 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600818 assert(!err);
819
Karl Schultze4dc75c2016-02-02 15:37:51 -0700820 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600821
Tony Barboura72d9492017-01-11 13:35:09 -0700822 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600823}
824
Ian Elliottd940ca22017-03-22 08:31:27 -0600825void DemoUpdateTargetIPD(struct demo *demo) {
826 // Look at what happened to previous presents, and make appropriate
827 // adjustments in timing:
828 VkResult U_ASSERT_ONLY err;
829 VkPastPresentationTimingGOOGLE* past = NULL;
830 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600831
832 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
833 demo->swapchain,
834 &count,
835 NULL);
836 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600837 if (count) {
838 past = (VkPastPresentationTimingGOOGLE*) malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
839 assert(past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600840 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
841 demo->swapchain,
842 &count,
843 past);
844 assert(!err);
845
846 bool early = false;
847 bool late = false;
848 bool calibrate_next = false;
Ian Elliottd940ca22017-03-22 08:31:27 -0600849 for (uint32_t i = 0 ; i < count ; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600850 if (!demo->syncd_with_actual_presents) {
851 // This is the first time that we've received an
852 // actualPresentTime for this swapchain. In order to not
853 // perceive these early frames as "late", we need to sync-up
854 // our future desiredPresentTime's with the
855 // actualPresentTime(s) that we're receiving now.
856 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600857
Ian Elliottd940ca22017-03-22 08:31:27 -0600858 // So that we don't suspect any pending presents as late,
859 // record them all as suspected-late presents:
860 demo->last_late_id = demo->next_present_id - 1;
861 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600862 demo->syncd_with_actual_presents = true;
863 break;
864 } else if (CanPresentEarlier(past[i].earliestPresentTime,
865 past[i].actualPresentTime,
866 past[i].presentMargin,
867 demo->refresh_duration)) {
868 // This image could have been presented earlier. We don't want
869 // to decrease the target_IPD until we've seen early presents
870 // for at least two seconds.
871 if (demo->last_early_id == past[i].presentID) {
872 // We've now seen two seconds worth of early presents.
873 // Flag it as such, and reset the counter:
874 early = true;
875 demo->last_early_id = 0;
876 } else if (demo->last_early_id == 0) {
877 // This is the first early present we've seen.
878 // Calculate the presentID for two seconds from now.
879 uint64_t lastEarlyTime =
880 past[i].actualPresentTime + (2 * BILLION);
881 uint32_t howManyPresents =
882 (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
883 demo->last_early_id = past[i].presentID + howManyPresents;
884 } else {
885 // We are in the midst of a set of early images,
886 // and so we won't do anything.
887 }
888 late = false;
889 demo->last_late_id = 0;
890 } else if (ActualTimeLate(past[i].desiredPresentTime,
891 past[i].actualPresentTime,
892 demo->refresh_duration)) {
893 // This image was presented after its desired time. Since
894 // there's a delay between calling vkQueuePresentKHR and when
895 // we get the timing data, several presents may have been late.
896 // Thus, we need to threat all of the outstanding presents as
897 // being likely late, so that we only increase the target_IPD
898 // once for all of those presents.
899 if ((demo->last_late_id == 0) ||
900 (demo->last_late_id < past[i].presentID)) {
901 late = true;
902 // Record the last suspected-late present:
903 demo->last_late_id = demo->next_present_id - 1;
904 } else {
905 // We are in the midst of a set of likely-late images,
906 // and so we won't do anything.
907 }
908 early = false;
909 demo->last_early_id = 0;
910 } else {
911 // Since this image was not presented early or late, reset
912 // any sets of early or late presentIDs:
913 early = false;
914 late = false;
915 calibrate_next = true;
916 demo->last_early_id = 0;
917 demo->last_late_id = 0;
918 }
919 }
920
921 if (early) {
922 // Since we've seen at least two-seconds worth of presnts that
923 // could have occured earlier than desired, let's decrease the
924 // target_IPD (i.e. increase the frame rate):
925 //
926 // TODO(ianelliott): Try to calculate a better target_IPD based
927 // on the most recently-seen present (this is overly-simplistic).
928 demo->refresh_duration_multiplier--;
929 if (demo->refresh_duration_multiplier == 0) {
930 // This should never happen, but in case it does, don't
931 // try to go faster.
932 demo->refresh_duration_multiplier = 1;
933 }
934 demo->target_IPD =
935 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600936 }
937 if (late) {
938 // Since we found a new instance of a late present, we want to
939 // increase the target_IPD (i.e. decrease the frame rate):
940 //
941 // TODO(ianelliott): Try to calculate a better target_IPD based
942 // on the most recently-seen present (this is overly-simplistic).
943 demo->refresh_duration_multiplier++;
944 demo->target_IPD =
945 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600946 }
947
948 if (calibrate_next) {
949 int64_t multiple = demo->next_present_id - past[count-1].presentID;
950 demo->prev_desired_present_time =
951 (past[count-1].actualPresentTime +
952 (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600953 }
954 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600955}
956
Karl Schultze4dc75c2016-02-02 15:37:51 -0700957static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600958 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600959
szdarkhackd322ff02016-10-08 09:51:22 +0300960 // Ensure no more than FRAME_LAG presentations are outstanding
961 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
962 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600963
Ian Elliottaaae5352015-07-06 14:27:58 -0600964 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700965 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -0700966 demo->image_acquired_semaphores[demo->frame_index],
967 demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600968 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600969
Tony Barboura72d9492017-01-11 13:35:09 -0700970 demo_update_data_buffer(demo);
971
Ian Elliottcf074d32015-10-16 18:02:43 -0600972 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
973 // demo->swapchain is out of date (e.g. the window was resized) and
974 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600975 demo->frame_index += 1;
976 demo->frame_index %= FRAME_LAG;
977
Ian Elliottcf074d32015-10-16 18:02:43 -0600978 demo_resize(demo);
979 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600980 return;
981 } else if (err == VK_SUBOPTIMAL_KHR) {
982 // demo->swapchain is not as optimal as it could be, but the platform's
983 // presentation engine will still present the image correctly.
984 } else {
985 assert(!err);
986 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600987 if (demo->VK_GOOGLE_display_timing_enabled) {
988 // Look at what happened to previous presents, and make appropriate
989 // adjustments in timing:
990 DemoUpdateTargetIPD(demo);
991
992 // Note: a real application would position its geometry to that it's in
993 // the correct locatoin for when the next image is presented. It might
994 // also wait, so that there's less latency between any input and when
995 // the next image is rendered/presented. This demo program is so
996 // simple that it doesn't do either of those.
997 }
998
Tony Barbource7524e2016-07-28 12:01:45 -0600999 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001000 // that the image won't be rendered to until the presentation
1001 // engine has fully released ownership to the application, and it is
1002 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001003 VkPipelineStageFlags pipe_stage_flags;
1004 VkSubmitInfo submit_info;
1005 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1006 submit_info.pNext = NULL;
1007 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1008 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1009 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001010 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001011 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001012 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001013 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001014 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -07001015 vkResetFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence);
1016 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
1017 demo->swapchain_image_resources[demo->current_buffer].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018 assert(!err);
1019
Tony Barbour22e06272016-09-07 16:07:56 -06001020 if (demo->separate_present_queue) {
1021 // If we are using separate queues, change image ownership to the
1022 // present queue before presenting, waiting for the draw complete
1023 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001024 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001025 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1026 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001027 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001028 submit_info.commandBufferCount = 1;
1029 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -07001030 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001031 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001032 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001033 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1034 assert(!err);
1035 }
1036
1037 // If we are using separate queues we have to wait for image ownership,
1038 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001039 VkPresentInfoKHR present = {
1040 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001041 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001042 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06001043 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -06001044 ? &demo->image_ownership_semaphores[demo->frame_index]
1045 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001046 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001047 .pSwapchains = &demo->swapchain,
1048 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001049 };
1050
Ian Elliott53622a72017-03-28 11:06:33 -06001051 if (demo->VK_KHR_incremental_present_enabled) {
1052 // If using VK_KHR_incremental_present, we provide a hint of the region
1053 // that contains changed content relative to the previously-presented
1054 // image. The implementation can use this hint in order to save
1055 // work/power (by only copying the region in the hint). The
1056 // implementation is free to ignore the hint though, and so we must
1057 // ensure that the entire image has the correctly-drawn content.
1058 uint32_t eighthOfWidth = demo->width / 8;
1059 uint32_t eighthOfHeight = demo->height / 8;
1060 VkRectLayerKHR rect = {
1061 .offset.x = eighthOfWidth,
1062 .offset.y = eighthOfHeight,
1063 .extent.width = eighthOfWidth * 6,
1064 .extent.height = eighthOfHeight * 6,
1065 .layer = 0,
1066 };
1067 VkPresentRegionKHR region = {
1068 .rectangleCount = 1,
1069 .pRectangles = &rect,
1070 };
1071 VkPresentRegionsKHR regions = {
1072 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1073 .pNext = present.pNext,
1074 .swapchainCount = present.swapchainCount,
1075 .pRegions = &region,
1076 };
1077 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001078 }
1079
Ian Elliottd940ca22017-03-22 08:31:27 -06001080 if (demo->VK_GOOGLE_display_timing_enabled) {
1081 VkPresentTimeGOOGLE ptime;
1082 if (demo->prev_desired_present_time == 0) {
1083 // This must be the first present for this swapchain.
1084 //
1085 // We don't know where we are relative to the presentation engine's
1086 // display's refresh cycle. We also don't know how long rendering
1087 // takes. Let's make a grossly-simplified assumption that the
1088 // desiredPresentTime should be half way between now and
1089 // now+target_IPD. We will adjust over time.
1090 uint64_t curtime = getTimeInNanoseconds();
1091 if (curtime == 0) {
1092 // Since we didn't find out the current time, don't give a
1093 // desiredPresentTime:
1094 ptime.desiredPresentTime = 0;
1095 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001096 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1097 }
1098 } else {
1099 ptime.desiredPresentTime = (demo->prev_desired_present_time +
1100 demo->target_IPD);
1101 }
1102 ptime.presentID = demo->next_present_id++;
1103 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001104
1105 VkPresentTimesInfoGOOGLE present_time = {
1106 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1107 .pNext = present.pNext,
1108 .swapchainCount = present.swapchainCount,
1109 .pTimes = &ptime,
1110 };
1111 if (demo->VK_GOOGLE_display_timing_enabled) {
1112 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001113 }
1114 }
1115
Tony Barboura2a67812016-07-18 13:21:06 -06001116 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001117 demo->frame_index += 1;
1118 demo->frame_index %= FRAME_LAG;
1119
Ian Elliottcf074d32015-10-16 18:02:43 -06001120 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1121 // demo->swapchain is out of date (e.g. the window was resized) and
1122 // must be recreated:
1123 demo_resize(demo);
1124 } else if (err == VK_SUBOPTIMAL_KHR) {
1125 // demo->swapchain is not as optimal as it could be, but the platform's
1126 // presentation engine will still present the image correctly.
1127 } else {
1128 assert(!err);
1129 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001130}
1131
Karl Schultze4dc75c2016-02-02 15:37:51 -07001132static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001133 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001134 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001135
Ian Elliottb08e0d92015-11-20 11:55:46 -07001136 // Check the surface capabilities and formats
1137 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001138 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
1139 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001140 assert(!err);
1141
Ian Elliott8528eff2015-08-07 15:56:59 -06001142 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001143 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1144 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001145 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06001146 VkPresentModeKHR *presentModes =
1147 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001148 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001149 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1150 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001151 assert(!err);
1152
Ian Elliotta0781972015-08-21 15:09:33 -06001153 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001154 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1155 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1156 // If the surface size is undefined, the size is set to the size
1157 // of the images requested, which must fit within the minimum and
1158 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001159 swapchainExtent.width = demo->width;
1160 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001161
1162 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1163 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1164 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1165 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1166 }
1167
1168 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1169 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1170 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1171 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1172 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001173 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001174 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001175 swapchainExtent = surfCapabilities.currentExtent;
1176 demo->width = surfCapabilities.currentExtent.width;
1177 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001178 }
1179
Tony Barbour66a56c32016-09-21 13:10:56 -06001180 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001181 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001182 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001183
Ian Elliottb18fdde2016-10-03 12:11:12 -06001184 // There are times when you may wish to use another present mode. The
1185 // following code shows how to select them, and the comments provide some
1186 // reasons you may wish to use them.
1187 //
1188 // It should be noted that Vulkan 1.0 doesn't provide a method for
1189 // synchronizing rendering with the presentation engine's display. There
1190 // is a method provided for throttling rendering with the display, but
1191 // there are some presentation engines for which this method will not work.
1192 // If an application doesn't throttle its rendering, and if it renders much
1193 // faster than the refresh rate of the display, this can waste power on
1194 // mobile devices. That is because power is being spent rendering images
1195 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001196
Ian Elliottb18fdde2016-10-03 12:11:12 -06001197 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1198 // tearing, or have some way of synchronizing their rendering with the
1199 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001200 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1201 // generally render a new presentable image every refresh cycle, but are
1202 // occasionally early. In this case, the application wants the new image
1203 // to be displayed instead of the previously-queued-for-presentation image
1204 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001205 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1206 // render a new presentable image every refresh cycle, but are occasionally
1207 // late. In this case (perhaps because of stuttering/latency concerns),
1208 // the application wants the late image to be immediately displayed, even
1209 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001210
1211 if (demo->presentMode != swapchainPresentMode) {
1212
1213 for (size_t i = 0; i < presentModeCount; ++i) {
1214 if (presentModes[i] == demo->presentMode) {
1215 swapchainPresentMode = demo->presentMode;
1216 break;
1217 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001218 }
1219 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001220 if (swapchainPresentMode != demo->presentMode) {
1221 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1222 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001223
Tony Barbour84376fe2017-01-06 15:54:22 -07001224 // Determine the number of VkImages to use in the swap chain.
1225 // Application desires to acquire 3 images at a time for triple
1226 // buffering
1227 uint32_t desiredNumOfSwapchainImages = 3;
1228 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1229 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1230 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001231 // If maxImageCount is 0, we can ask for as many images as we want;
1232 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -07001233 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -06001234 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001235 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001236 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001237 }
1238
Tony Barbour9fd6d352015-09-16 15:01:32 -06001239 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001240 if (surfCapabilities.supportedTransforms &
1241 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001242 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001243 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001244 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001245 }
1246
Tony Barbour820b55b2017-03-14 08:55:46 -06001247 // Find a supported composite alpha mode - one of these is guaranteed to be set
1248 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1249 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1250 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1251 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1252 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1253 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1254 };
1255 for (uint32_t i = 0; i < sizeof(compositeAlphaFlags); i++) {
1256 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1257 compositeAlpha = compositeAlphaFlags[i];
1258 break;
1259 }
1260 }
1261
Tony Barboura2a67812016-07-18 13:21:06 -06001262 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001263 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001264 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001265 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001266 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001267 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001268 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001269 .imageExtent =
1270 {
1271 .width = swapchainExtent.width, .height = swapchainExtent.height,
1272 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001273 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001274 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001275 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001276 .imageArrayLayers = 1,
1277 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1278 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001279 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001280 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001281 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001282 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001283 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001284 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -06001285 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001286 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001287 assert(!err);
1288
Ian Elliottcf074d32015-10-16 18:02:43 -06001289 // If we just re-created an existing swapchain, we should destroy the old
1290 // swapchain at this point.
1291 // Note: destroying the swapchain also cleans up all its associated
1292 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001293 if (oldSwapchain != VK_NULL_HANDLE) {
Tony Barboura90a4f92017-03-23 13:25:36 -06001294 // AMD driver times out waiting on fences used in AcquireNextImage on
1295 // a swapchain that is subsequently destroyed before the wait.
1296 vkWaitForFences(demo->device, FRAME_LAG, demo->fences, VK_TRUE, UINT64_MAX);
Ian Elliottb08e0d92015-11-20 11:55:46 -07001297 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001298 }
1299
1300 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001301 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001302 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001303
Karl Schultze4dc75c2016-02-02 15:37:51 -07001304 VkImage *swapchainImages =
1305 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001306 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001307 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001308 &demo->swapchainImageCount,
1309 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001310 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001311
Tony Barboura72d9492017-01-11 13:35:09 -07001312 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001313 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001314 assert(demo->swapchain_image_resources);
1315
1316 VkFenceCreateInfo fence_ci = {
1317 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1318 .pNext = NULL,
1319 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1320 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001321
Ian Elliotta0781972015-08-21 15:09:33 -06001322 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001323 VkImageViewCreateInfo color_image_view = {
1324 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001325 .pNext = NULL,
1326 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001327 .components =
1328 {
1329 .r = VK_COMPONENT_SWIZZLE_R,
1330 .g = VK_COMPONENT_SWIZZLE_G,
1331 .b = VK_COMPONENT_SWIZZLE_B,
1332 .a = VK_COMPONENT_SWIZZLE_A,
1333 },
1334 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1335 .baseMipLevel = 0,
1336 .levelCount = 1,
1337 .baseArrayLayer = 0,
1338 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001339 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1340 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001341 };
1342
Tony Barboura72d9492017-01-11 13:35:09 -07001343 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001344
Tony Barboura72d9492017-01-11 13:35:09 -07001345 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346
Karl Schultze4dc75c2016-02-02 15:37:51 -07001347 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001348 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001349 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001350
Tony Barboura72d9492017-01-11 13:35:09 -07001351 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1352 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001354
Ian Elliottd940ca22017-03-22 08:31:27 -06001355 if (demo->VK_GOOGLE_display_timing_enabled) {
1356 VkRefreshCycleDurationGOOGLE rc_dur;
1357 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device,
1358 demo->swapchain,
1359 &rc_dur);
1360 assert(!err);
1361 demo->refresh_duration = rc_dur.refreshDuration;
1362
1363 demo->syncd_with_actual_presents = false;
1364 // Initially target 1X the refresh duration:
1365 demo->target_IPD = demo->refresh_duration;
1366 demo->refresh_duration_multiplier = 1;
1367 demo->prev_desired_present_time = 0;
1368 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001369 }
1370
Mark Young04fd3d82016-01-25 14:43:50 -07001371 if (NULL != presentModes) {
1372 free(presentModes);
1373 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374}
1375
Karl Schultze4dc75c2016-02-02 15:37:51 -07001376static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001377 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001378 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001379 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001381 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001382 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001385 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001386 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001387 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001388 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389 .flags = 0,
1390 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001391
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001392 VkImageViewCreateInfo view = {
1393 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001394 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001395 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001396 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001397 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1398 .baseMipLevel = 0,
1399 .levelCount = 1,
1400 .baseArrayLayer = 0,
1401 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001403 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001405
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001406 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001407 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001408 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001409
1410 demo->depth.format = depth_format;
1411
1412 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001413 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414 assert(!err);
1415
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001417 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001418
Chia-I Wuf48700b2015-11-10 16:21:09 +08001419 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001420 demo->depth.mem_alloc.pNext = NULL;
1421 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1422 demo->depth.mem_alloc.memoryTypeIndex = 0;
1423
Karl Schultze4dc75c2016-02-02 15:37:51 -07001424 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001425 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001426 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001427 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001429 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001430 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1431 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001432 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001434 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001435 err =
1436 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001437 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
1439 /* create image view */
1440 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001441 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443}
1444
Tony Barbour1a86d032015-09-21 15:17:33 -06001445/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001446bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001447 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001448
1449#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001450 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001451#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001452
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001453#ifdef __ANDROID__
1454#include <lunarg.ppm.h>
1455 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001456 cPtr = (char*)lunarg_ppm;
1457 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001458 return false;
1459 }
1460 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001461 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001462 if (rgba_data == NULL) {
1463 return true;
1464 }
1465 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001466 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001467 return false;
1468 }
1469 while(strncmp(cPtr++, "\n", 1));
1470
1471 for (int y = 0; y < *height; y++) {
1472 uint8_t *rowPtr = rgba_data;
1473 for (int x = 0; x < *width; x++) {
1474 memcpy(rowPtr, cPtr, 3);
1475 rowPtr[3] = 255; /* Alpha of 1 */
1476 rowPtr += 4;
1477 cPtr += 3;
1478 }
1479 rgba_data += layout->rowPitch;
1480 }
1481
1482 return true;
1483#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001484 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001485 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001486
Tony Barbour1a86d032015-09-21 15:17:33 -06001487 if (!fPtr)
1488 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001489
Tony Barbour1a86d032015-09-21 15:17:33 -06001490 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001491 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1492 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001493 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001494 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001495
Tony Barbour1a86d032015-09-21 15:17:33 -06001496 do {
1497 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001498 if (cPtr == NULL) {
1499 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001500 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001501 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001502 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001503
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001504 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001505 if (rgba_data == NULL) {
1506 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001507 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001508 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001509 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001510 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001511 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1512 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001513 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001514 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001515
Karl Schultze4dc75c2016-02-02 15:37:51 -07001516 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001517 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001518 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001519 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001521 rowPtr[3] = 255; /* Alpha of 1 */
1522 rowPtr += 4;
1523 }
1524 rgba_data += layout->rowPitch;
1525 }
1526 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001527 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001528#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001529}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001530
Karl Schultze4dc75c2016-02-02 15:37:51 -07001531static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001532 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001533 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001534 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001535 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001536 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001537 int32_t tex_width;
1538 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001539 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001540 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001541
Karl Schultze4dc75c2016-02-02 15:37:51 -07001542 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001543 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001544 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001545
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001546 tex_obj->tex_width = tex_width;
1547 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001548
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001549 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001550 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001551 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001552 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001553 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001554 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001555 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001556 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001557 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001558 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001559 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001560 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001561 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001562 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001563
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001564 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001565
Karl Schultze4dc75c2016-02-02 15:37:51 -07001566 err =
1567 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001568 assert(!err);
1569
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001570 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001571
Chia-I Wuf48700b2015-11-10 16:21:09 +08001572 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001573 tex_obj->mem_alloc.pNext = NULL;
1574 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1575 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001576
Karl Schultze4dc75c2016-02-02 15:37:51 -07001577 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1578 required_props,
1579 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001580 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001581
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001582 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001583 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001584 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001585 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001586
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001587 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001588 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001589 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001590
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001591 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001592 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001593 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001594 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001595 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001596 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001597 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001598 void *data;
1599
Karl Schultze4dc75c2016-02-02 15:37:51 -07001600 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1601 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001602
Karl Schultze4dc75c2016-02-02 15:37:51 -07001603 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1604 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001605 assert(!err);
1606
1607 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1608 fprintf(stderr, "Error loading texture: %s\n", filename);
1609 }
1610
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001611 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001612 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001613
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001614 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001615}
1616
Karl Schultze4dc75c2016-02-02 15:37:51 -07001617static void demo_destroy_texture_image(struct demo *demo,
1618 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001619 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001620 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1621 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001622}
1623
Karl Schultze4dc75c2016-02-02 15:37:51 -07001624static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001625 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001626 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001627 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001628
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001629 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001630
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001632 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001633
Karl Schultze4dc75c2016-02-02 15:37:51 -07001634 if ((props.linearTilingFeatures &
1635 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1636 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001637 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001638 demo_prepare_texture_image(
1639 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1640 VK_IMAGE_USAGE_SAMPLED_BIT,
1641 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1642 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001643 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1644 // shader to run until layout transition completes
1645 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1646 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1647 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1648 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001649 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001650 } else if (props.optimalTilingFeatures &
1651 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001652 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001653
Tony Barbour16156cb2016-10-19 14:15:49 -06001654 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001655 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001656 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001657 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1658 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1659 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001660
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661 demo_prepare_texture_image(
1662 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1663 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1664 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001665
Tony Barbour16156cb2016-10-19 14:15:49 -06001666 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001667 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001668 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001669 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001670 VK_ACCESS_HOST_WRITE_BIT,
1671 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1672 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001673
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001674 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001675 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001676 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001677 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001678 VK_ACCESS_HOST_WRITE_BIT,
1679 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1680 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001681
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001682 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001683 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1684 .srcOffset = {0, 0, 0},
1685 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1686 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001687 .extent = {demo->staging_texture.tex_width,
1688 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001689 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001690 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001691 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001692 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1693 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001694
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001695 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001696 VK_IMAGE_ASPECT_COLOR_BIT,
1697 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001698 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001699 VK_ACCESS_TRANSFER_WRITE_BIT,
1700 VK_PIPELINE_STAGE_TRANSFER_BIT,
1701 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001702
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001703 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001704 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1705 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001706 }
1707
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001708 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001709 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001711 .magFilter = VK_FILTER_NEAREST,
1712 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001713 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001714 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1715 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1716 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001718 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001719 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001720 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001721 .minLod = 0.0f,
1722 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001723 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001724 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001725 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001726
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001727 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001728 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001730 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001731 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001732 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001733 .components =
1734 {
1735 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1736 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1737 },
1738 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001739 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001741
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001743 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001744 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 assert(!err);
1746
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 /* create image view */
1748 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001749 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001750 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751 assert(!err);
1752 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001754
Tony Barboura72d9492017-01-11 13:35:09 -07001755void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001756 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001757 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001758 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001759 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001760 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001761 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001762 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001763 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001764
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001765 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001766 mat4x4_mul(MVP, VP, demo->model_matrix);
1767 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001768 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001769
Karl Schultza2615462017-01-20 13:19:20 -07001770 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001771 data.position[i][0] = g_vertex_buffer_data[i * 3];
1772 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1773 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001774 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001775 data.attr[i][0] = g_uv_buffer_data[2 * i];
1776 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001777 data.attr[i][2] = 0;
1778 data.attr[i][3] = 0;
1779 }
1780
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001781 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001782 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001783 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001784 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001785
Tony Barboura72d9492017-01-11 13:35:09 -07001786 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1787 err =
1788 vkCreateBuffer(demo->device, &buf_info, NULL,
1789 &demo->swapchain_image_resources[i].uniform_buffer);
1790 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001791
Tony Barboura72d9492017-01-11 13:35:09 -07001792 vkGetBufferMemoryRequirements(demo->device,
1793 demo->swapchain_image_resources[i].uniform_buffer,
1794 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001795
Tony Barboura72d9492017-01-11 13:35:09 -07001796 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1797 mem_alloc.pNext = NULL;
1798 mem_alloc.allocationSize = mem_reqs.size;
1799 mem_alloc.memoryTypeIndex = 0;
1800
1801 pass = memory_type_from_properties(
1802 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001803 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001804 &mem_alloc.memoryTypeIndex);
1805 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001806
Tony Barboura72d9492017-01-11 13:35:09 -07001807 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1808 &demo->swapchain_image_resources[i].uniform_memory);
1809 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001810
Tony Barboura72d9492017-01-11 13:35:09 -07001811 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1812 VK_WHOLE_SIZE, 0, (void **)&pData);
1813 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001814
Tony Barboura72d9492017-01-11 13:35:09 -07001815 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001816
Tony Barboura72d9492017-01-11 13:35:09 -07001817 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001818
Tony Barboura72d9492017-01-11 13:35:09 -07001819 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1820 demo->swapchain_image_resources[i].uniform_memory, 0);
1821 assert(!err);
1822 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001823}
1824
Karl Schultze4dc75c2016-02-02 15:37:51 -07001825static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001826 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001827 [0] =
1828 {
1829 .binding = 0,
1830 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1831 .descriptorCount = 1,
1832 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1833 .pImmutableSamplers = NULL,
1834 },
1835 [1] =
1836 {
1837 .binding = 1,
1838 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1839 .descriptorCount = DEMO_TEXTURE_COUNT,
1840 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1841 .pImmutableSamplers = NULL,
1842 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001843 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001844 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001845 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001846 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001847 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001848 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001849 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001850 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851
Karl Schultze4dc75c2016-02-02 15:37:51 -07001852 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1853 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001854 assert(!err);
1855
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001856 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001857 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1858 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001859 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001860 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001861 };
1862
Karl Schultze4dc75c2016-02-02 15:37:51 -07001863 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001864 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001866}
1867
Karl Schultze4dc75c2016-02-02 15:37:51 -07001868static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001869 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1870 // because at the start of the renderpass, we don't care about their contents.
1871 // At the start of the subpass, the color attachment's layout will be transitioned
1872 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1873 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1874 // the renderpass, the color attachment's layout will be transitioned to
1875 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1876 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001877 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001878 [0] =
1879 {
1880 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001881 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001882 .samples = VK_SAMPLE_COUNT_1_BIT,
1883 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1884 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1885 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1886 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001887 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001888 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001889 },
1890 [1] =
1891 {
1892 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001893 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001894 .samples = VK_SAMPLE_COUNT_1_BIT,
1895 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1896 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1897 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1898 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1899 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001900 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001901 .finalLayout =
1902 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1903 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001904 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001905 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001906 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001907 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001908 const VkAttachmentReference depth_reference = {
1909 .attachment = 1,
1910 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1911 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001912 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001913 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1914 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001915 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001916 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001917 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001918 .pColorAttachments = &color_reference,
1919 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001920 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001921 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001922 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001923 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001924 const VkRenderPassCreateInfo rp_info = {
1925 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1926 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001927 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001928 .attachmentCount = 2,
1929 .pAttachments = attachments,
1930 .subpassCount = 1,
1931 .pSubpasses = &subpass,
1932 .dependencyCount = 0,
1933 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001934 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001935 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001936
Chia-I Wu63636062015-10-26 21:10:41 +08001937 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001938 assert(!err);
1939}
1940
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001941//TODO: Merge shader reading
1942#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001943static VkShaderModule
1944demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001945 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001946 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001947 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001949 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1950 moduleCreateInfo.pNext = NULL;
1951
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001952 moduleCreateInfo.codeSize = size;
1953 moduleCreateInfo.pCode = code;
1954 moduleCreateInfo.flags = 0;
1955 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1956 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001957
1958 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001959}
1960
Karl Schultze4dc75c2016-02-02 15:37:51 -07001961char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001962 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001963 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001964 void *shader_code;
1965
Bill Hollingsf4352142017-02-14 22:58:56 -05001966#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001967 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001968#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001969
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001970 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001971 if (!fp)
1972 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001973
1974 fseek(fp, 0L, SEEK_END);
1975 size = ftell(fp);
1976
1977 fseek(fp, 0L, SEEK_SET);
1978
1979 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001980 retval = fread(shader_code, size, 1, fp);
1981 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001982
1983 *psize = size;
1984
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001985 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001986 return shader_code;
1987}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001988#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001989
Karl Schultze4dc75c2016-02-02 15:37:51 -07001990static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001991#ifdef __ANDROID__
1992 VkShaderModuleCreateInfo sh_info = {};
1993 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1994
1995#include "cube.vert.h"
1996 sh_info.codeSize = sizeof(cube_vert);
1997 sh_info.pCode = cube_vert;
1998 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1999 assert(!err);
2000#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002001 void *vertShaderCode;
2002 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002003
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002004 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08002005 if (!vertShaderCode) {
2006 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
2007 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002008
Karl Schultze4dc75c2016-02-02 15:37:51 -07002009 demo->vert_shader_module =
2010 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002011
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002012 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002013#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002014
2015 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002016}
2017
Karl Schultze4dc75c2016-02-02 15:37:51 -07002018static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002019#ifdef __ANDROID__
2020 VkShaderModuleCreateInfo sh_info = {};
2021 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
2022
2023#include "cube.frag.h"
2024 sh_info.codeSize = sizeof(cube_frag);
2025 sh_info.pCode = cube_frag;
2026 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
2027 assert(!err);
2028#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002029 void *fragShaderCode;
2030 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002031
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002032 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08002033 if (!fragShaderCode) {
2034 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
2035 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002036
Karl Schultze4dc75c2016-02-02 15:37:51 -07002037 demo->frag_shader_module =
2038 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002039
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002040 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002041#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002042
2043 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044}
2045
Karl Schultze4dc75c2016-02-02 15:37:51 -07002046static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002047 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002048 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002049 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002050 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002051 VkPipelineRasterizationStateCreateInfo rs;
2052 VkPipelineColorBlendStateCreateInfo cb;
2053 VkPipelineDepthStencilStateCreateInfo ds;
2054 VkPipelineViewportStateCreateInfo vp;
2055 VkPipelineMultisampleStateCreateInfo ms;
2056 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
2057 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06002058 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002059
Piers Daniellba839d12015-09-29 13:01:09 -06002060 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2061 memset(&dynamicState, 0, sizeof dynamicState);
2062 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2063 dynamicState.pDynamicStates = dynamicStateEnables;
2064
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002065 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002066 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002067 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002068
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002069 memset(&vi, 0, sizeof(vi));
2070 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2071
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002072 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002073 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002074 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002075
2076 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002077 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2078 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002079 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002080 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002081 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002082 rs.rasterizerDiscardEnable = VK_FALSE;
2083 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002084 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002085
2086 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002087 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2088 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002089 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002090 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002091 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002092 cb.attachmentCount = 1;
2093 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002094
Tony Barbour29645d02015-01-16 14:27:35 -07002095 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002096 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002097 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002098 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2099 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002100 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002101 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2102 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002103
2104 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002105 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002106 ds.depthTestEnable = VK_TRUE;
2107 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002108 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002109 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002110 ds.back.failOp = VK_STENCIL_OP_KEEP;
2111 ds.back.passOp = VK_STENCIL_OP_KEEP;
2112 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002113 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002114 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002115
Tony Barbour29645d02015-01-16 14:27:35 -07002116 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002117 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002118 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002119 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002120
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002121 // Two stages: vs and fs
2122 pipeline.stageCount = 2;
2123 VkPipelineShaderStageCreateInfo shaderStages[2];
2124 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2125
Karl Schultze4dc75c2016-02-02 15:37:51 -07002126 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2127 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002128 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002129 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002130
Karl Schultze4dc75c2016-02-02 15:37:51 -07002131 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2132 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002133 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002134 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002135
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002136 memset(&pipelineCache, 0, sizeof(pipelineCache));
2137 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002138
Karl Schultze4dc75c2016-02-02 15:37:51 -07002139 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
2140 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002141 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002142
Karl Schultze4dc75c2016-02-02 15:37:51 -07002143 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002144 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002145 pipeline.pRasterizationState = &rs;
2146 pipeline.pColorBlendState = &cb;
2147 pipeline.pMultisampleState = &ms;
2148 pipeline.pViewportState = &vp;
2149 pipeline.pDepthStencilState = &ds;
2150 pipeline.pStages = shaderStages;
2151 pipeline.renderPass = demo->render_pass;
2152 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002153
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002154 pipeline.renderPass = demo->render_pass;
2155
Karl Schultze4dc75c2016-02-02 15:37:51 -07002156 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
2157 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002158 assert(!err);
2159
Chia-I Wu63636062015-10-26 21:10:41 +08002160 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2161 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002162}
2163
Karl Schultze4dc75c2016-02-02 15:37:51 -07002164static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002165 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002166 [0] =
2167 {
2168 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07002169 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002170 },
2171 [1] =
2172 {
2173 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07002174 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002175 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002176 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002177 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002178 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002179 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002180 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002181 .poolSizeCount = 2,
2182 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002183 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002184 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002185
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
2187 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002188 assert(!err);
2189}
2190
Karl Schultze4dc75c2016-02-02 15:37:51 -07002191static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002192 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002193 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002194 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002195
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002196 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002197 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06002198 .pNext = NULL,
2199 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002200 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002201 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002202
2203 VkDescriptorBufferInfo buffer_info;
2204 buffer_info.offset = 0;
2205 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002206
Chia-I Wuae721ba2015-05-25 16:27:55 +08002207 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002208 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002209 tex_descs[i].sampler = demo->textures[i].sampler;
2210 tex_descs[i].imageView = demo->textures[i].view;
2211 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002212 }
2213
2214 memset(&writes, 0, sizeof(writes));
2215
2216 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002217 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002218 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002219 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002220
2221 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002222 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002223 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002224 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002225 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002226
Tony Barboura72d9492017-01-11 13:35:09 -07002227 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2228 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2229 assert(!err);
2230 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2231 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2232 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2233 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2234 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002235}
2236
Karl Schultze4dc75c2016-02-02 15:37:51 -07002237static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002238 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002239 attachments[1] = demo->depth.view;
2240
Chia-I Wuf973e322015-07-08 13:34:24 +08002241 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002242 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2243 .pNext = NULL,
2244 .renderPass = demo->render_pass,
2245 .attachmentCount = 2,
2246 .pAttachments = attachments,
2247 .width = demo->width,
2248 .height = demo->height,
2249 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002250 };
2251 VkResult U_ASSERT_ONLY err;
2252 uint32_t i;
2253
Tony Barbourd8e504f2015-10-08 14:26:24 -06002254 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002255 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002256 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002257 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002258 assert(!err);
2259 }
2260}
2261
Karl Schultze4dc75c2016-02-02 15:37:51 -07002262static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002263 VkResult U_ASSERT_ONLY err;
2264
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002265 const VkCommandPoolCreateInfo cmd_pool_info = {
2266 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06002267 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002268 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06002269 .flags = 0,
2270 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07002271 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
2272 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06002273 assert(!err);
2274
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002275 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002276 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002277 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002278 .commandPool = demo->cmd_pool,
2279 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002280 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002281 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002282 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2283 assert(!err);
2284 VkCommandBufferBeginInfo cmd_buf_info = {
2285 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2286 .pNext = NULL,
2287 .flags = 0,
2288 .pInheritanceInfo = NULL,
2289 };
2290 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2291 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002292
2293 demo_prepare_buffers(demo);
2294 demo_prepare_depth(demo);
2295 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002296 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002297
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002298 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002299 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002300 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002301
Ian Elliotta0781972015-08-21 15:09:33 -06002302 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002303 err =
Tony Barboura72d9492017-01-11 13:35:09 -07002304 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002305 assert(!err);
2306 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002307
Tony Barbour22e06272016-09-07 16:07:56 -06002308 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002309 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002310 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2311 .pNext = NULL,
2312 .queueFamilyIndex = demo->present_queue_family_index,
2313 .flags = 0,
2314 };
Karl Schultza2615462017-01-20 13:19:20 -07002315 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002316 &demo->present_cmd_pool);
2317 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002318 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002319 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2320 .pNext = NULL,
2321 .commandPool = demo->present_cmd_pool,
2322 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2323 .commandBufferCount = 1,
2324 };
2325 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2326 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002327 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002328 assert(!err);
2329 demo_build_image_ownership_cmd(demo, i);
2330 }
2331 }
2332
Chia-I Wu63ea9262015-03-26 13:14:16 +08002333 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002334 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002335
Chia-I Wuf973e322015-07-08 13:34:24 +08002336 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002337
Ian Elliotta0781972015-08-21 15:09:33 -06002338 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002339 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002340 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002341 }
2342
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002343 /*
2344 * Prepare functions above may generate pipeline commands
2345 * that need to be flushed before beginning the render loop.
2346 */
2347 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002348 if (demo->staging_texture.image) {
2349 demo_destroy_texture_image(demo, &demo->staging_texture);
2350 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002351
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002352 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002353 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002354}
2355
Karl Schultze4dc75c2016-02-02 15:37:51 -07002356static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002357 uint32_t i;
2358
2359 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002360 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002361
Tony Barbour66a56c32016-09-21 13:10:56 -06002362 // Wait for fences from present operations
2363 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002364 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002365 vkDestroyFence(demo->device, demo->fences[i], NULL);
2366 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2367 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2368 if (demo->separate_present_queue) {
2369 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2370 }
2371 }
2372
Tony Barbourd8e504f2015-10-08 14:26:24 -06002373 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002374 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002375 }
Chia-I Wu63636062015-10-26 21:10:41 +08002376 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002377
Chia-I Wu63636062015-10-26 21:10:41 +08002378 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2379 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2380 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2381 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2382 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002383
2384 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002385 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2386 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2387 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2388 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002389 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002390 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002391
Chia-I Wu63636062015-10-26 21:10:41 +08002392 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2393 vkDestroyImage(demo->device, demo->depth.image, NULL);
2394 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002395
Ian Elliotta0781972015-08-21 15:09:33 -06002396 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002397 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002398 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002399 &demo->swapchain_image_resources[i].cmd);
2400 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2401 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2402 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002403 }
Tony Barboura72d9492017-01-11 13:35:09 -07002404 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002405 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002406 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002407
Tony Barbour22e06272016-09-07 16:07:56 -06002408 if (demo->separate_present_queue) {
2409 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002410 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002411 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002412 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002413 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002414 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002415 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002416 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002417 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002418
Tony Barbour153cb062016-12-07 13:43:36 -07002419#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002420 XDestroyWindow(demo->display, demo->xlib_window);
2421 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002422#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002423 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002424 xcb_disconnect(demo->connection);
2425 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002426#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2427 wl_shell_surface_destroy(demo->shell_surface);
2428 wl_surface_destroy(demo->window);
2429 wl_shell_destroy(demo->shell);
2430 wl_compositor_destroy(demo->compositor);
2431 wl_registry_destroy(demo->registry);
2432 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002433#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002434#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002435}
2436
Karl Schultze4dc75c2016-02-02 15:37:51 -07002437static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002438 uint32_t i;
2439
Mike Stroyanbb60b382015-10-28 09:46:57 -06002440 // Don't react to resize until after first initialization.
2441 if (!demo->prepared) {
2442 return;
2443 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002444 // In order to properly resize the window, we must re-create the swapchain
2445 // AND redo the command buffers, etc.
2446 //
2447 // First, perform part of the demo_cleanup() function:
2448 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002449 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002450
2451 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002452 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002453 }
Chia-I Wu63636062015-10-26 21:10:41 +08002454 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002455
Chia-I Wu63636062015-10-26 21:10:41 +08002456 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2457 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2458 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2459 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2460 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002461
2462 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002463 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2464 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2465 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2466 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002467 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002468
Chia-I Wu63636062015-10-26 21:10:41 +08002469 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2470 vkDestroyImage(demo->device, demo->depth.image, NULL);
2471 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002472
Ian Elliottcf074d32015-10-16 18:02:43 -06002473 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002474 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002475 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002476 &demo->swapchain_image_resources[i].cmd);
2477 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2478 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2479 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002480 }
Chia-I Wu63636062015-10-26 21:10:41 +08002481 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002482 if (demo->separate_present_queue) {
2483 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2484 }
Tony Barboura72d9492017-01-11 13:35:09 -07002485 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002486
Ian Elliottcf074d32015-10-16 18:02:43 -06002487 // Second, re-perform the demo_prepare() function, which will re-create the
2488 // swapchain:
2489 demo_prepare(demo);
2490}
2491
David Pinedo2bb7c932015-06-18 17:03:14 -06002492// On MS-Windows, make this a global, so it's available to WndProc()
2493struct demo demo;
2494
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002495#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002496static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002497 if (!demo->prepared)
2498 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002499
Ian Elliott639ca472015-04-16 15:23:05 -06002500 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002501 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002502 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002503 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002504 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002505}
Ian Elliott639ca472015-04-16 15:23:05 -06002506
2507// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002508LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2509 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002510 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002511 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002512 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002513 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002514 // The validation callback calls MessageBox which can generate paint
2515 // events - don't make more Vulkan calls if we got here from the
2516 // callback
2517 if (!in_callback) {
2518 demo_run(&demo);
2519 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002520 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002521 case WM_GETMINMAXINFO: // set window's minimum size
2522 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2523 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002524 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002525 // Resize the application to the new window size, except when
2526 // it was minimized. Vulkan doesn't support images or swapchains
2527 // with width=0 and height=0.
2528 if (wParam != SIZE_MINIMIZED) {
2529 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002530 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002531 demo_resize(&demo);
2532 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002533 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002534 default:
2535 break;
2536 }
2537 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2538}
2539
Karl Schultze4dc75c2016-02-02 15:37:51 -07002540static void demo_create_window(struct demo *demo) {
2541 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002542
2543 // Initialize the window class structure:
2544 win_class.cbSize = sizeof(WNDCLASSEX);
2545 win_class.style = CS_HREDRAW | CS_VREDRAW;
2546 win_class.lpfnWndProc = WndProc;
2547 win_class.cbClsExtra = 0;
2548 win_class.cbWndExtra = 0;
2549 win_class.hInstance = demo->connection; // hInstance
2550 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2551 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2552 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2553 win_class.lpszMenuName = NULL;
2554 win_class.lpszClassName = demo->name;
2555 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2556 // Register window class:
2557 if (!RegisterClassEx(&win_class)) {
2558 // It didn't work, so try to give a useful error:
2559 printf("Unexpected error trying to start the application!\n");
2560 fflush(stdout);
2561 exit(1);
2562 }
2563 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002564 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002565 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002566 demo->window = CreateWindowEx(0,
2567 demo->name, // class name
2568 demo->name, // app name
2569 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002570 WS_VISIBLE | WS_SYSMENU,
2571 100, 100, // x/y coords
2572 wr.right - wr.left, // width
2573 wr.bottom - wr.top, // height
2574 NULL, // handle to parent
2575 NULL, // handle to menu
2576 demo->connection, // hInstance
2577 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002578 if (!demo->window) {
2579 // It didn't work, so try to give a useful error:
2580 printf("Cannot create a window in which to draw!\n");
2581 fflush(stdout);
2582 exit(1);
2583 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002584 // Window client area size must be at least 1 pixel high, to prevent crash.
2585 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2586 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002587}
Tony Barbour8295ac42016-08-08 11:04:17 -06002588#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002589static void demo_create_xlib_window(struct demo *demo) {
2590
Tony Barbouree9cd372017-01-31 16:09:58 -07002591 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002592 demo->display = XOpenDisplay(NULL);
2593 long visualMask = VisualScreenMask;
2594 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002595 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002596 vInfoTemplate.screen = DefaultScreen(demo->display);
2597 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2598 &vInfoTemplate, &numberOfVisuals);
2599
2600 Colormap colormap = XCreateColormap(
2601 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2602 visualInfo->visual, AllocNone);
2603
Rene Lindsay11612722016-06-13 17:20:39 -06002604 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002605 windowAttributes.colormap = colormap;
2606 windowAttributes.background_pixel = 0xFFFFFFFF;
2607 windowAttributes.border_pixel = 0;
2608 windowAttributes.event_mask =
2609 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2610
2611 demo->xlib_window = XCreateWindow(
2612 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2613 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2614 visualInfo->visual,
2615 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2616
2617 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2618 XMapWindow(demo->display, demo->xlib_window);
2619 XFlush(demo->display);
2620 demo->xlib_wm_delete_window =
2621 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2622}
2623static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2624 switch(event->type) {
2625 case ClientMessage:
2626 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2627 demo->quit = true;
2628 break;
2629 case KeyPress:
2630 switch (event->xkey.keycode) {
2631 case 0x9: // Escape
2632 demo->quit = true;
2633 break;
2634 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002635 demo->spin_angle -= demo->spin_increment;
2636 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002637 case 0x72: // right arrow key
2638 demo->spin_angle += demo->spin_increment;
2639 break;
2640 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002641 demo->pause = !demo->pause;
2642 break;
2643 }
2644 break;
2645 case ConfigureNotify:
2646 if ((demo->width != event->xconfigure.width) ||
2647 (demo->height != event->xconfigure.height)) {
2648 demo->width = event->xconfigure.width;
2649 demo->height = event->xconfigure.height;
2650 demo_resize(demo);
2651 }
2652 break;
2653 default:
2654 break;
2655 }
2656
2657}
2658
2659static void demo_run_xlib(struct demo *demo) {
2660
2661 while (!demo->quit) {
2662 XEvent event;
2663
2664 if (demo->pause) {
2665 XNextEvent(demo->display, &event);
2666 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002667 }
2668 while (XPending(demo->display) > 0) {
2669 XNextEvent(demo->display, &event);
2670 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002671 }
2672
Tony Barbour2593b182016-04-19 10:57:58 -06002673 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002674 demo->curFrame++;
2675 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2676 demo->quit = true;
2677 }
2678}
Tony Barbour153cb062016-12-07 13:43:36 -07002679#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002680static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002681 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002682 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002683 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002684 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002685 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002686 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002687 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002688 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2689 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002690 demo->quit = true;
2691 }
2692 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002693 case XCB_KEY_RELEASE: {
2694 const xcb_key_release_event_t *key =
2695 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002696
Karl Schultze4dc75c2016-02-02 15:37:51 -07002697 switch (key->detail) {
2698 case 0x9: // Escape
2699 demo->quit = true;
2700 break;
2701 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002702 demo->spin_angle -= demo->spin_increment;
2703 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002704 case 0x72: // right arrow key
2705 demo->spin_angle += demo->spin_increment;
2706 break;
2707 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002708 demo->pause = !demo->pause;
2709 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002711 } break;
2712 case XCB_CONFIGURE_NOTIFY: {
2713 const xcb_configure_notify_event_t *cfg =
2714 (const xcb_configure_notify_event_t *)event;
2715 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002716 demo->width = cfg->width;
2717 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002718 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002719 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002720 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002721 default:
2722 break;
2723 }
2724}
2725
Tony Barbour2593b182016-04-19 10:57:58 -06002726static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002727 xcb_flush(demo->connection);
2728
2729 while (!demo->quit) {
2730 xcb_generic_event_t *event;
2731
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002732 if (demo->pause) {
2733 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002734 }
2735 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002736 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002737 }
2738 while (event) {
2739 demo_handle_xcb_event(demo, event);
2740 free(event);
2741 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002742 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002743
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002744 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002745 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002746 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002747 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002748 }
2749}
2750
Tony Barbour2593b182016-04-19 10:57:58 -06002751static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002752 uint32_t value_mask, value_list[32];
2753
Tony Barbour2593b182016-04-19 10:57:58 -06002754 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002755
2756 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2757 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002758 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002759 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002760
Tony Barbour2593b182016-04-19 10:57:58 -06002761 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002762 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2763 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2764 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002765
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002766 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002767 xcb_intern_atom_cookie_t cookie =
2768 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2769 xcb_intern_atom_reply_t *reply =
2770 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002771
Karl Schultze4dc75c2016-02-02 15:37:51 -07002772 xcb_intern_atom_cookie_t cookie2 =
2773 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2774 demo->atom_wm_delete_window =
2775 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002776
Tony Barbour2593b182016-04-19 10:57:58 -06002777 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002778 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002779 &(*demo->atom_wm_delete_window).atom);
2780 free(reply);
2781
Tony Barbour2593b182016-04-19 10:57:58 -06002782 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002783
Karl Schultze4dc75c2016-02-02 15:37:51 -07002784 // Force the x/y coordinates to 100,100 results are identical in consecutive
2785 // runs
2786 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002787 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002788 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002789}
Tony Barbour6b131662016-08-25 13:14:45 -06002790// VK_USE_PLATFORM_XCB_KHR
2791#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002792static void demo_run(struct demo *demo) {
2793 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002794 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002795 demo->curFrame++;
2796 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2797 demo->quit = true;
2798 }
2799}
2800
2801static void handle_ping(void *data UNUSED,
2802 struct wl_shell_surface *shell_surface,
2803 uint32_t serial) {
2804 wl_shell_surface_pong(shell_surface, serial);
2805}
2806
2807static void handle_configure(void *data UNUSED,
2808 struct wl_shell_surface *shell_surface UNUSED,
2809 uint32_t edges UNUSED, int32_t width UNUSED,
2810 int32_t height UNUSED) {}
2811
2812static void handle_popup_done(void *data UNUSED,
2813 struct wl_shell_surface *shell_surface UNUSED) {}
2814
2815static const struct wl_shell_surface_listener shell_surface_listener = {
2816 handle_ping, handle_configure, handle_popup_done};
2817
2818static void demo_create_window(struct demo *demo) {
2819 demo->window = wl_compositor_create_surface(demo->compositor);
2820 if (!demo->window) {
2821 printf("Can not create wayland_surface from compositor!\n");
2822 fflush(stdout);
2823 exit(1);
2824 }
2825
2826 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2827 if (!demo->shell_surface) {
2828 printf("Can not get shell_surface from wayland_surface!\n");
2829 fflush(stdout);
2830 exit(1);
2831 }
2832 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2833 demo);
2834 wl_shell_surface_set_toplevel(demo->shell_surface);
2835 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2836}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002837#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2838static void demo_run(struct demo *demo) {
2839 if (!demo->prepared)
2840 return;
2841
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002842 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002843 demo->curFrame++;
2844}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002845#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002846#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2847static VkResult demo_create_display_surface(struct demo *demo) {
2848 VkResult U_ASSERT_ONLY err;
2849 uint32_t display_count;
2850 uint32_t mode_count;
2851 uint32_t plane_count;
2852 VkDisplayPropertiesKHR display_props;
2853 VkDisplayKHR display;
2854 VkDisplayModePropertiesKHR mode_props;
2855 VkDisplayPlanePropertiesKHR *plane_props;
2856 VkBool32 found_plane = VK_FALSE;
2857 uint32_t plane_index;
2858 VkExtent2D image_extent;
2859 VkDisplaySurfaceCreateInfoKHR create_info;
2860
2861 // Get the first display
2862 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2863 assert(!err);
2864
2865 if (display_count == 0) {
2866 printf("Cannot find any display!\n");
2867 fflush(stdout);
2868 exit(1);
2869 }
2870
2871 display_count = 1;
2872 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2873 assert(!err || (err == VK_INCOMPLETE));
2874
2875 display = display_props.display;
2876
2877 // Get the first mode of the display
2878 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2879 assert(!err);
2880
2881 if (mode_count == 0) {
2882 printf("Cannot find any mode for the display!\n");
2883 fflush(stdout);
2884 exit(1);
2885 }
2886
2887 mode_count = 1;
2888 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2889 assert(!err || (err == VK_INCOMPLETE));
2890
2891 // Get the list of planes
2892 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2893 assert(!err);
2894
2895 if (plane_count == 0) {
2896 printf("Cannot find any plane!\n");
2897 fflush(stdout);
2898 exit(1);
2899 }
2900
2901 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2902 assert(plane_props);
2903
2904 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2905 assert(!err);
2906
2907 // Find a plane compatible with the display
2908 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2909 uint32_t supported_count;
2910 VkDisplayKHR *supported_displays;
2911
2912 // Disqualify planes that are bound to a different display
2913 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2914 (plane_props[plane_index].currentDisplay != display)) {
2915 continue;
2916 }
2917
2918 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2919 assert(!err);
2920
2921 if (supported_count == 0) {
2922 continue;
2923 }
2924
2925 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2926 assert(supported_displays);
2927
2928 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2929 assert(!err);
2930
2931 for (uint32_t i = 0; i < supported_count; i++) {
2932 if (supported_displays[i] == display) {
2933 found_plane = VK_TRUE;
2934 break;
2935 }
2936 }
2937
2938 free(supported_displays);
2939
2940 if (found_plane) {
2941 break;
2942 }
2943 }
2944
2945 if (!found_plane) {
2946 printf("Cannot find a plane compatible with the display!\n");
2947 fflush(stdout);
2948 exit(1);
2949 }
2950
2951 free(plane_props);
2952
Tony Barbour820b55b2017-03-14 08:55:46 -06002953 VkDisplayPlaneCapabilitiesKHR planeCaps;
2954 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2955 // Find a supported alpha mode
2956 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2957 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
2958 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2959 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2960 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2961 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2962 };
2963 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2964 if (planeCaps.supportedAlpha & alphaModes[i]) {
2965 alphaMode = alphaModes[i];
2966 break;
2967 }
2968 }
Damien Leone600c3052017-01-31 10:26:07 -07002969 image_extent.width = mode_props.parameters.visibleRegion.width;
2970 image_extent.height = mode_props.parameters.visibleRegion.height;
2971
2972 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2973 create_info.pNext = NULL;
2974 create_info.flags = 0;
2975 create_info.displayMode = mode_props.displayMode;
2976 create_info.planeIndex = plane_index;
2977 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2978 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002979 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002980 create_info.globalAlpha = 1.0f;
2981 create_info.imageExtent = image_extent;
2982
2983 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2984}
2985
2986static void demo_run_display(struct demo *demo)
2987{
2988 while (!demo->quit) {
2989 demo_draw(demo);
2990 demo->curFrame++;
2991
2992 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2993 demo->quit = true;
2994 }
2995 }
2996}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002997#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002998
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002999/*
3000 * Return 1 (true) if all layer names specified in check_names
3001 * can be found in given layer properties.
3002 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003003static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003004 uint32_t layer_count,
3005 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003006 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003007 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003008 for (uint32_t j = 0; j < layer_count; j++) {
3009 if (!strcmp(check_names[i], layers[j].layerName)) {
3010 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07003011 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003012 }
3013 }
3014 if (!found) {
3015 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
3016 return 0;
3017 }
3018 }
3019 return 1;
3020}
3021
Karl Schultze4dc75c2016-02-02 15:37:51 -07003022static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003023 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003024 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003025 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06003026 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003027 char **instance_validation_layers = NULL;
3028 demo->enabled_extension_count = 0;
3029 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003030
Karl Schultz7c50ad62016-04-01 12:07:03 -06003031 char *instance_validation_layers_alt1[] = {
3032 "VK_LAYER_LUNARG_standard_validation"
3033 };
3034
3035 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07003036 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
3037 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
3038 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003039 };
3040
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003041 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003042 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003043 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003044
Karl Schultz7c50ad62016-04-01 12:07:03 -06003045 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003046 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003047
Karl Schultz7c50ad62016-04-01 12:07:03 -06003048 instance_validation_layers = instance_validation_layers_alt1;
3049 if (instance_layer_count > 0) {
3050 VkLayerProperties *instance_layers =
3051 malloc(sizeof (VkLayerProperties) * instance_layer_count);
3052 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
3053 instance_layers);
3054 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003055
Karl Schultz7c50ad62016-04-01 12:07:03 -06003056
3057 validation_found = demo_check_layers(
3058 ARRAY_SIZE(instance_validation_layers_alt1),
3059 instance_validation_layers, instance_layer_count,
3060 instance_layers);
3061 if (validation_found) {
3062 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003063 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
3064 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003065 } else {
3066 // use alternative set of validation layers
3067 instance_validation_layers = instance_validation_layers_alt2;
3068 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
3069 validation_found = demo_check_layers(
3070 ARRAY_SIZE(instance_validation_layers_alt2),
3071 instance_validation_layers, instance_layer_count,
3072 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003073 validation_layer_count =
3074 ARRAY_SIZE(instance_validation_layers_alt2);
3075 for (uint32_t i = 0; i < validation_layer_count; i++) {
3076 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06003077 }
3078 }
3079 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003080 }
Dustin Graves7c287352016-01-27 13:48:06 -07003081
Karl Schultz7c50ad62016-04-01 12:07:03 -06003082 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06003083 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06003084 "required validation layer.\n\n"
3085 "Please look at the Getting Started guide for additional "
3086 "information.\n",
3087 "vkCreateInstance Failure");
3088 }
Dustin Graves7c287352016-01-27 13:48:06 -07003089 }
3090
3091 /* Look for instance extensions */
3092 VkBool32 surfaceExtFound = 0;
3093 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003094 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003095
Karl Schultze4dc75c2016-02-02 15:37:51 -07003096 err = vkEnumerateInstanceExtensionProperties(
3097 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003098 assert(!err);
3099
Dustin Graves7c287352016-01-27 13:48:06 -07003100 if (instance_extension_count > 0) {
3101 VkExtensionProperties *instance_extensions =
3102 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3103 err = vkEnumerateInstanceExtensionProperties(
3104 NULL, &instance_extension_count, instance_extensions);
3105 assert(!err);
3106 for (uint32_t i = 0; i < instance_extension_count; i++) {
3107 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
3108 instance_extensions[i].extensionName)) {
3109 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003110 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003111 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003112 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003113#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003114 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
3115 instance_extensions[i].extensionName)) {
3116 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003117 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003118 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
3119 }
Tony Barbour153cb062016-12-07 13:43:36 -07003120#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003121 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
3122 instance_extensions[i].extensionName)) {
3123 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06003124 demo->extension_names[demo->enabled_extension_count++] =
3125 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
3126 }
Tony Barbour153cb062016-12-07 13:43:36 -07003127#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003128 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
3129 instance_extensions[i].extensionName)) {
3130 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003131 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003132 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
3133 }
Tony Barbour153cb062016-12-07 13:43:36 -07003134#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003135 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
3136 instance_extensions[i].extensionName)) {
3137 platformSurfaceExtFound = 1;
3138 demo->extension_names[demo->enabled_extension_count++] =
3139 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
3140 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003141#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003142#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3143 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
3144 instance_extensions[i].extensionName)) {
3145 platformSurfaceExtFound = 1;
3146 demo->extension_names[demo->enabled_extension_count++] =
3147 VK_KHR_DISPLAY_EXTENSION_NAME;
3148 }
Tony Barbour153cb062016-12-07 13:43:36 -07003149#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003150 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
3151 instance_extensions[i].extensionName)) {
3152 platformSurfaceExtFound = 1;
3153 demo->extension_names[demo->enabled_extension_count++] =
3154 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
3155 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003156#elif defined(VK_USE_PLATFORM_IOS_MVK)
3157 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3158 platformSurfaceExtFound = 1;
3159 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3160 }
3161#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3162 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3163 platformSurfaceExtFound = 1;
3164 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3165 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003166#endif
Dustin Graves7c287352016-01-27 13:48:06 -07003167 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
3168 instance_extensions[i].extensionName)) {
3169 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003170 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003171 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
3172 }
3173 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003174 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003175 }
Dustin Graves7c287352016-01-27 13:48:06 -07003176
3177 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003178 }
Dustin Graves7c287352016-01-27 13:48:06 -07003179
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003180 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003181 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3182 "the " VK_KHR_SURFACE_EXTENSION_NAME
3183 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06003184 "Vulkan installable client driver (ICD) installed?\nPlease "
3185 "look at the Getting Started guide for additional "
3186 "information.\n",
3187 "vkCreateInstance Failure");
3188 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003189 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003190#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003191 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3192 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3193 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003194 "Vulkan installable client driver (ICD) installed?\nPlease "
3195 "look at the Getting Started guide for additional "
3196 "information.\n",
3197 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003198#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003199 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3200 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3201 "Vulkan installable client driver (ICD) installed?\nPlease "
3202 "look at the Getting Started guide for additional "
3203 "information.\n",
3204 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003205#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003206 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3207 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3208 "Vulkan installable client driver (ICD) installed?\nPlease "
3209 "look at the Getting Started guide for additional "
3210 "information.\n",
3211 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003212#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003213 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3214 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3215 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003216 "Vulkan installable client driver (ICD) installed?\nPlease "
3217 "look at the Getting Started guide for additional "
3218 "information.\n",
3219 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003220#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3221 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3222 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3223 " extension.\n\nDo you have a compatible "
3224 "Vulkan installable client driver (ICD) installed?\nPlease "
3225 "look at the Getting Started guide for additional "
3226 "information.\n",
3227 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003228#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003229#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3230 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3231 "the " VK_KHR_DISPLAY_EXTENSION_NAME
3232 " extension.\n\nDo you have a compatible "
3233 "Vulkan installable client driver (ICD) installed?\nPlease "
3234 "look at the Getting Started guide for additional "
3235 "information.\n",
3236 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003237#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3238 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3239 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3240 " extension.\n\nDo you have a compatible "
3241 "Vulkan installable client driver (ICD) installed?\nPlease "
3242 "look at the Getting Started guide for additional "
3243 "information.\n",
3244 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003245#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003246 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3247 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3248 " extension.\n\nDo you have a compatible "
3249 "Vulkan installable client driver (ICD) installed?\nPlease "
3250 "look at the Getting Started guide for additional "
3251 "information.\n",
3252 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003253#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003254 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003255 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003256 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003257 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003258 .pApplicationName = APP_SHORT_NAME,
3259 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003260 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003261 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003262 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003263 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003264 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003265 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003266 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003267 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003268 .enabledLayerCount = demo->enabled_layer_count,
3269 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3270 .enabledExtensionCount = demo->enabled_extension_count,
3271 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003272 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003273
3274 /*
3275 * This is info for a temp callback to use during CreateInstance.
3276 * After the instance is created, we use the instance-based
3277 * function to register the final callback.
3278 */
Karl Schultza2615462017-01-20 13:19:20 -07003279 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003280 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003281 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07003282 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
3283 dbgCreateInfoTemp.pNext = NULL;
3284 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
3285 dbgCreateInfoTemp.pUserData = demo;
3286 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06003287 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003288 if (demo->validate_checks_disabled) {
3289 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
3290 val_flags.pNext = NULL;
3291 val_flags.disabledValidationCheckCount = 1;
3292 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3293 val_flags.pDisabledValidationChecks = &disabled_check;
3294 dbgCreateInfoTemp.pNext = (void*)&val_flags;
3295 }
Karl Schultza2615462017-01-20 13:19:20 -07003296 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003297 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003298
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003299 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003300
Chia-I Wu63636062015-10-26 21:10:41 +08003301 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003302 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
3303 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06003304 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06003305 "additional information.\n",
3306 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003307 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003308 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07003309 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003310 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003311 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06003312 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3313 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003314 "the Getting Started guide for additional information.\n",
3315 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003316 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003317
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003318 /* Make initial call to query gpu_count, then second call for gpu info*/
3319 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3320 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003321
3322 if (gpu_count > 0) {
3323 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3324 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3325 assert(!err);
3326 /* For cube demo we just grab the first physical device */
3327 demo->gpu = physical_devices[0];
3328 free(physical_devices);
3329 } else {
3330 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3331 "Do you have a compatible Vulkan installable client driver (ICD) "
3332 "installed?\nPlease look at the Getting Started guide for "
3333 "additional information.\n",
3334 "vkEnumeratePhysicalDevices Failure");
3335 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003336
Dustin Graves7c287352016-01-27 13:48:06 -07003337 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003338 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003339 VkBool32 swapchainExtFound = 0;
3340 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003341 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003342
Karl Schultze4dc75c2016-02-02 15:37:51 -07003343 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3344 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003345 assert(!err);
3346
Dustin Graves7c287352016-01-27 13:48:06 -07003347 if (device_extension_count > 0) {
3348 VkExtensionProperties *device_extensions =
3349 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3350 err = vkEnumerateDeviceExtensionProperties(
3351 demo->gpu, NULL, &device_extension_count, device_extensions);
3352 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003353
Dustin Graves7c287352016-01-27 13:48:06 -07003354 for (uint32_t i = 0; i < device_extension_count; i++) {
3355 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3356 device_extensions[i].extensionName)) {
3357 swapchainExtFound = 1;
3358 demo->extension_names[demo->enabled_extension_count++] =
3359 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3360 }
3361 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003362 }
Dustin Graves7c287352016-01-27 13:48:06 -07003363
Ian Elliott53622a72017-03-28 11:06:33 -06003364 if (demo->VK_KHR_incremental_present_enabled) {
3365 // Even though the user "enabled" the extension via the command
3366 // line, we must make sure that it's enumerated for use with the
3367 // device. Therefore, disable it here, and re-enable it again if
3368 // enumerated.
3369 demo->VK_KHR_incremental_present_enabled = false;
3370 for (uint32_t i = 0; i < device_extension_count; i++) {
3371 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
3372 device_extensions[i].extensionName)) {
3373 demo->extension_names[demo->enabled_extension_count++] =
3374 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
3375 demo->VK_KHR_incremental_present_enabled = true;
3376 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3377 }
3378 assert(demo->enabled_extension_count < 64);
3379 }
3380 if (!demo->VK_KHR_incremental_present_enabled) {
3381 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3382 }
3383 }
3384
Ian Elliottd940ca22017-03-22 08:31:27 -06003385 if (demo->VK_GOOGLE_display_timing_enabled) {
3386 // Even though the user "enabled" the extension via the command
3387 // line, we must make sure that it's enumerated for use with the
3388 // device. Therefore, disable it here, and re-enable it again if
3389 // enumerated.
3390 demo->VK_GOOGLE_display_timing_enabled = false;
3391 for (uint32_t i = 0; i < device_extension_count; i++) {
3392 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
3393 device_extensions[i].extensionName)) {
3394 demo->extension_names[demo->enabled_extension_count++] =
3395 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
3396 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003397 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003398 }
3399 assert(demo->enabled_extension_count < 64);
3400 }
3401 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003402 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003403 }
3404 }
3405
Dustin Graves7c287352016-01-27 13:48:06 -07003406 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003407 }
Dustin Graves7c287352016-01-27 13:48:06 -07003408
Tony Barbourcc69f452015-10-08 13:59:42 -06003409 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003410 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3411 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3412 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003413 "Vulkan installable client driver (ICD) installed?\nPlease "
3414 "look at the Getting Started guide for additional "
3415 "information.\n",
3416 "vkCreateInstance Failure");
3417 }
3418
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003419 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003420 demo->CreateDebugReportCallback =
3421 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3422 demo->inst, "vkCreateDebugReportCallbackEXT");
3423 demo->DestroyDebugReportCallback =
3424 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3425 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003426 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003427 ERR_EXIT(
3428 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3429 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003430 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003431 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003432 ERR_EXIT(
3433 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3434 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003435 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003436 demo->DebugReportMessage =
3437 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3438 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003439 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003440 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003441 "vkGetProcAddr Failure");
3442 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003443
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003444 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003445 PFN_vkDebugReportCallbackEXT callback;
3446 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003447 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003448 dbgCreateInfo.pNext = NULL;
3449 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003450 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003451 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003452 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003453 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3454 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003455 switch (err) {
3456 case VK_SUCCESS:
3457 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003458 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003459 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3460 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003461 break;
3462 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003463 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3464 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003465 break;
3466 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003467 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003468 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003469
3470 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003471 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3472 &demo->queue_family_count, NULL);
3473 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003474
Karl Schultze4dc75c2016-02-02 15:37:51 -07003475 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003476 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3477 vkGetPhysicalDeviceQueueFamilyProperties(
3478 demo->gpu, &demo->queue_family_count, demo->queue_props);
3479
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003480 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003481 // If app has specific feature requirements it should check supported
3482 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003483 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003484 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003485
Tony Barbourda2bad52016-01-22 14:36:40 -07003486 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3487 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3488 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3489 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3490 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3491}
3492
Karl Schultze4dc75c2016-02-02 15:37:51 -07003493static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003494 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003495 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003496 VkDeviceQueueCreateInfo queues[2];
3497 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3498 queues[0].pNext = NULL;
3499 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3500 queues[0].queueCount = 1;
3501 queues[0].pQueuePriorities = queue_priorities;
3502 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003503
3504 VkDeviceCreateInfo device = {
3505 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3506 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003507 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003508 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003509 .enabledLayerCount = 0,
3510 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003511 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003512 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3513 .pEnabledFeatures =
3514 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003515 };
Tony Barbour22e06272016-09-07 16:07:56 -06003516 if (demo->separate_present_queue) {
3517 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3518 queues[1].pNext = NULL;
3519 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3520 queues[1].queueCount = 1;
3521 queues[1].pQueuePriorities = queue_priorities;
3522 queues[1].flags = 0;
3523 device.queueCreateInfoCount = 2;
3524 }
Chia-I Wu63636062015-10-26 21:10:41 +08003525 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003526 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003527}
3528
Karl Schultze4dc75c2016-02-02 15:37:51 -07003529static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003530 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003531
Karl Schultze4dc75c2016-02-02 15:37:51 -07003532// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003533#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003534 VkWin32SurfaceCreateInfoKHR createInfo;
3535 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3536 createInfo.pNext = NULL;
3537 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003538 createInfo.hinstance = demo->connection;
3539 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003540
Karl Schultze4dc75c2016-02-02 15:37:51 -07003541 err =
3542 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003543#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003544 VkWaylandSurfaceCreateInfoKHR createInfo;
3545 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3546 createInfo.pNext = NULL;
3547 createInfo.flags = 0;
3548 createInfo.display = demo->display;
3549 createInfo.surface = demo->window;
3550
3551 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3552 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003553#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003554#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3555 VkAndroidSurfaceCreateInfoKHR createInfo;
3556 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3557 createInfo.pNext = NULL;
3558 createInfo.flags = 0;
3559 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003560
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003561 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003562#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3563 VkXlibSurfaceCreateInfoKHR createInfo;
3564 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3565 createInfo.pNext = NULL;
3566 createInfo.flags = 0;
3567 createInfo.dpy = demo->display;
3568 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003569
Tony Barbour153cb062016-12-07 13:43:36 -07003570 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003571 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003572#elif defined(VK_USE_PLATFORM_XCB_KHR)
3573 VkXcbSurfaceCreateInfoKHR createInfo;
3574 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3575 createInfo.pNext = NULL;
3576 createInfo.flags = 0;
3577 createInfo.connection = demo->connection;
3578 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003579
Tony Barbour153cb062016-12-07 13:43:36 -07003580 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003581#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3582 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003583#elif defined(VK_USE_PLATFORM_IOS_MVK)
3584 VkIOSSurfaceCreateInfoMVK surface;
3585 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3586 surface.pNext = NULL;
3587 surface.flags = 0;
3588 surface.pView = demo->window;
3589
3590 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3591#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3592 VkMacOSSurfaceCreateInfoMVK surface;
3593 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3594 surface.pNext = NULL;
3595 surface.flags = 0;
3596 surface.pView = demo->window;
3597
3598 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003599#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003600 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003601
Tony Barbourcc69f452015-10-08 13:59:42 -06003602 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003603 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003604 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003605 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003606 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003607 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003608 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003609
3610 // Search for a graphics and a present queue in the array of queue
3611 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003612 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3613 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003614 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003615 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3616 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3617 graphicsQueueFamilyIndex = i;
3618 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003619
Tony Barbourab2a0362016-09-06 11:40:12 -06003620 if (supportsPresent[i] == VK_TRUE) {
3621 graphicsQueueFamilyIndex = i;
3622 presentQueueFamilyIndex = i;
3623 break;
3624 }
3625 }
3626 }
3627
3628 if (presentQueueFamilyIndex == UINT32_MAX) {
3629 // If didn't find a queue that supports both graphics and present, then
3630 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003631 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003632 if (supportsPresent[i] == VK_TRUE) {
3633 presentQueueFamilyIndex = i;
3634 break;
3635 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003636 }
3637 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003638
3639 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003640 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3641 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003642 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003643 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003644 }
3645
Tony Barbourab2a0362016-09-06 11:40:12 -06003646 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3647 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003648 demo->separate_present_queue =
3649 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003650 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003651
Tony Barbourda2bad52016-01-22 14:36:40 -07003652 demo_create_device(demo);
3653
3654 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3655 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3656 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3657 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3658 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003659 if (demo->VK_GOOGLE_display_timing_enabled) {
3660 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3661 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3662 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003663
Tony Barboura2a67812016-07-18 13:21:06 -06003664 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3665 &demo->graphics_queue);
3666
Tony Barbour22e06272016-09-07 16:07:56 -06003667 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003668 demo->present_queue = demo->graphics_queue;
3669 } else {
3670 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3671 &demo->present_queue);
3672 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003673
Ian Elliottaaae5352015-07-06 14:27:58 -06003674 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003675 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003676 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003677 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003678 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003679 VkSurfaceFormatKHR *surfFormats =
3680 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003681 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003682 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003683 assert(!err);
3684 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3685 // the surface has no preferred format. Otherwise, at least one
3686 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003687 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003688 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003689 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003690 assert(formatCount >= 1);
3691 demo->format = surfFormats[0].format;
3692 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003693 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003694
David Pinedo2bb7c932015-06-18 17:03:14 -06003695 demo->quit = false;
3696 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003697
Tony Barbource7524e2016-07-28 12:01:45 -06003698 // Create semaphores to synchronize acquiring presentable buffers before
3699 // rendering and waiting for drawing to be complete before presenting
3700 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3701 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3702 .pNext = NULL,
3703 .flags = 0,
3704 };
Tony Barbource7524e2016-07-28 12:01:45 -06003705
Tony Barbour66a56c32016-09-21 13:10:56 -06003706 // Create fences that we can use to throttle if we get too far
3707 // ahead of the image presents
3708 VkFenceCreateInfo fence_ci = {
3709 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3710 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003711 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003712 };
3713 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003714 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3715 assert(!err);
3716
Tony Barbour22e06272016-09-07 16:07:56 -06003717 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003718 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003719 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003720
3721 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3722 &demo->draw_complete_semaphores[i]);
3723 assert(!err);
3724
3725 if (demo->separate_present_queue) {
3726 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3727 &demo->image_ownership_semaphores[i]);
3728 assert(!err);
3729 }
Tony Barbour22e06272016-09-07 16:07:56 -06003730 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003731 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003732
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003733 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003734 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003735}
3736
Tony Barbour153cb062016-12-07 13:43:36 -07003737#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003738static void registry_handle_global(void *data, struct wl_registry *registry,
3739 uint32_t name, const char *interface,
3740 uint32_t version UNUSED) {
3741 struct demo *demo = data;
3742 if (strcmp(interface, "wl_compositor") == 0) {
3743 demo->compositor =
3744 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3745 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3746 * tp xdg_shell */
3747 } else if (strcmp(interface, "wl_shell") == 0) {
3748 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3749 }
3750}
3751
3752static void registry_handle_global_remove(void *data UNUSED,
3753 struct wl_registry *registry UNUSED,
3754 uint32_t name UNUSED) {}
3755
3756static const struct wl_registry_listener registry_listener = {
3757 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003758#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003759#endif
3760
Karl Schultze4dc75c2016-02-02 15:37:51 -07003761static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003762#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003763 const xcb_setup_t *setup;
3764 xcb_screen_iterator_t iter;
3765 int scr;
3766
3767 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003768 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003769 printf("Cannot find a compatible Vulkan installable client driver "
3770 "(ICD).\nExiting ...\n");
3771 fflush(stdout);
3772 exit(1);
3773 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003774
3775 setup = xcb_get_setup(demo->connection);
3776 iter = xcb_setup_roots_iterator(setup);
3777 while (scr-- > 0)
3778 xcb_screen_next(&iter);
3779
3780 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003781#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3782 demo->display = wl_display_connect(NULL);
3783
3784 if (demo->display == NULL) {
3785 printf("Cannot find a compatible Vulkan installable client driver "
3786 "(ICD).\nExiting ...\n");
3787 fflush(stdout);
3788 exit(1);
3789 }
3790
3791 demo->registry = wl_display_get_registry(demo->display);
3792 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3793 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003794#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003795#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003796}
3797
Karl Schultze4dc75c2016-02-02 15:37:51 -07003798static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003799 vec3 eye = {0.0f, 3.0f, 5.0f};
3800 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003801 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003802
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003803 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003804 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003805 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003806
Piers Daniell735ee532015-02-23 16:23:13 -07003807 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003808 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003809 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003810 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003811 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003812 if ((strcmp(argv[i], "--present_mode") == 0) &&
3813 (i < argc - 1)) {
3814 demo->presentMode = atoi(argv[i+1]);
3815 i++;
3816 continue;
3817 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003818 if (strcmp(argv[i], "--break") == 0) {
3819 demo->use_break = true;
3820 continue;
3821 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003822 if (strcmp(argv[i], "--validate") == 0) {
3823 demo->validate = true;
3824 continue;
3825 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003826 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3827 demo->validate = true;
3828 demo->validate_checks_disabled = true;
3829 continue;
3830 }
Tony Barbour2593b182016-04-19 10:57:58 -06003831 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003832 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003833 continue;
3834 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003835 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3836 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3837 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003838 i++;
3839 continue;
3840 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003841 if (strcmp(argv[i], "--suppress_popups") == 0) {
3842 demo->suppress_popups = true;
3843 continue;
3844 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003845 if (strcmp(argv[i], "--display_timing") == 0) {
3846 demo->VK_GOOGLE_display_timing_enabled = true;
3847 continue;
3848 }
Ian Elliott53622a72017-03-28 11:06:33 -06003849 if (strcmp(argv[i], "--incremental_present") == 0) {
3850 demo->VK_KHR_incremental_present_enabled = true;
3851 continue;
3852 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003853
Cody Northropaf28a532016-09-27 10:50:57 -06003854#if defined(ANDROID)
3855 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3856#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003857 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Ian Elliott53622a72017-03-28 11:06:33 -06003858 "[--c <framecount>] [--suppress_popups] [--incremental_present] [--display_timing] [--present_mode <present mode enum>]\n"
Tony Barbour291d57b2016-10-21 14:47:07 -06003859 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3860 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3861 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3862 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3863 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3864 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003865 fflush(stderr);
3866 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003867#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003868 }
3869
Tony Barbour153cb062016-12-07 13:43:36 -07003870 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003871
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003872 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003873
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003874 demo->width = 500;
3875 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003876
Tony Barbour66a56c32016-09-21 13:10:56 -06003877 demo->spin_angle = 4.0f;
3878 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003879 demo->pause = false;
3880
Karl Schultze4dc75c2016-02-02 15:37:51 -07003881 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3882 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003883 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3884 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003885
3886 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003887}
3888
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003889#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003890// Include header required for parsing the command line options.
3891#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003892
Karl Schultze4dc75c2016-02-02 15:37:51 -07003893int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3894 int nCmdShow) {
3895 MSG msg; // message
3896 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003897 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003898 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003899
Jamie Madillb2ff6502017-03-15 16:17:46 -04003900 // Ensure wParam is initialized.
3901 msg.wParam = 0;
3902
Karl Schultze4dc75c2016-02-02 15:37:51 -07003903 // Use the CommandLine functions to get the command line arguments.
3904 // Unfortunately, Microsoft outputs
3905 // this information as wide characters for Unicode, and we simply want the
3906 // Ascii version to be compatible
3907 // with the non-Windows side. So, we have to convert the information to
3908 // Ascii character strings.
3909 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3910 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003911 argc = 0;
3912 }
3913
Karl Schultze4dc75c2016-02-02 15:37:51 -07003914 if (argc > 0) {
3915 argv = (char **)malloc(sizeof(char *) * argc);
3916 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003917 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003918 } else {
3919 for (int iii = 0; iii < argc; iii++) {
3920 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003921 size_t numConverted = 0;
3922
Karl Schultze4dc75c2016-02-02 15:37:51 -07003923 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3924 if (argv[iii] != NULL) {
3925 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3926 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003927 }
3928 }
3929 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003930 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003931 argv = NULL;
3932 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003933
3934 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003935
3936 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003937 if (argc > 0 && argv != NULL) {
3938 for (int iii = 0; iii < argc; iii++) {
3939 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003940 free(argv[iii]);
3941 }
3942 }
3943 free(argv);
3944 }
3945
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003946 demo.connection = hInstance;
3947 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003948 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003949 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003950
3951 demo_prepare(&demo);
3952
Karl Schultze4dc75c2016-02-02 15:37:51 -07003953 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003954
3955 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003956 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003957 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003958 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003959 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003960 done = true; // if found, quit app
3961 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003962 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003963 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003964 DispatchMessage(&msg);
3965 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003966 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003967 }
3968
3969 demo_cleanup(&demo);
3970
Karl Schultze4dc75c2016-02-02 15:37:51 -07003971 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003972}
Bill Hollingsf4352142017-02-14 22:58:56 -05003973
3974#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3975static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003976 const char* argv[] = { "CubeSample" };
3977 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003978
Tony Barbourc65cd752017-03-13 15:29:35 -06003979 demo_init(demo, argc, (char**)argv);
3980 demo->window = view;
3981 demo_init_vk_swapchain(demo);
3982 demo_prepare(demo);
3983 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003984}
3985
3986static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003987 // Wait for work to finish before updating MVP.
3988 vkDeviceWaitIdle(demo->device);
3989 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003990
Tony Barbourc65cd752017-03-13 15:29:35 -06003991 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003992}
3993
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003994#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3995#include <android/log.h>
3996#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003997#include "android_util.h"
3998
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003999static bool initialized = false;
4000static bool active = false;
4001struct demo demo;
4002
4003static int32_t processInput(struct android_app* app, AInputEvent* event) {
4004 return 0;
4005}
4006
4007static void processCommand(struct android_app* app, int32_t cmd) {
4008 switch(cmd) {
4009 case APP_CMD_INIT_WINDOW: {
4010 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06004011 // We're getting a new window. If the app is starting up, we
4012 // need to initialize. If the app has already been
4013 // initialized, that means that we lost our previous window,
4014 // which means that we have a lot of work to do. At a minimum,
4015 // we need to destroy the swapchain and surface associated with
4016 // the old window, and create a new surface and swapchain.
4017 // However, since there are a lot of other objects/state that
4018 // is tied to the swapchain, it's easiest to simply cleanup and
4019 // start over (i.e. use a brute-force approach of re-starting
4020 // the app)
4021 if (demo.prepared) {
4022 demo_cleanup(&demo);
4023 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004024
4025 // Parse Intents into argc, argv
4026 // Use the following key to send arguments, i.e.
4027 // --es args "--validate"
4028 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06004029 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004030 int argc = 0;
4031 char** argv = get_args(app, key, appTag, &argc);
4032
4033 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
4034 for (int i = 0; i < argc; i++)
4035 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
4036
4037 demo_init(&demo, argc, argv);
4038
4039 // Free the argv malloc'd by get_args
4040 for (int i = 0; i < argc; i++)
4041 free(argv[i]);
4042
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004043 demo.window = (void*)app->window;
4044 demo_init_vk_swapchain(&demo);
4045 demo_prepare(&demo);
4046 initialized = true;
4047 }
4048 break;
4049 }
4050 case APP_CMD_GAINED_FOCUS: {
4051 active = true;
4052 break;
4053 }
4054 case APP_CMD_LOST_FOCUS: {
4055 active = false;
4056 break;
4057 }
4058 }
4059}
4060
4061void android_main(struct android_app *app)
4062{
4063 app_dummy();
4064
Cody Northrop8707a6e2016-04-26 19:59:19 -06004065#ifdef ANDROID
4066 int vulkanSupport = InitVulkan();
4067 if (vulkanSupport == 0)
4068 return;
4069#endif
4070
Ian Elliottf05d5d12016-05-17 12:03:35 -06004071 demo.prepared = false;
4072
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004073 app->onAppCmd = processCommand;
4074 app->onInputEvent = processInput;
4075
4076 while(1) {
4077 int events;
4078 struct android_poll_source* source;
4079 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
4080 if (source) {
4081 source->process(app, source);
4082 }
4083
4084 if (app->destroyRequested != 0) {
4085 demo_cleanup(&demo);
4086 return;
4087 }
4088 }
4089 if (initialized && active) {
4090 demo_run(&demo);
4091 }
4092 }
4093
4094}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004095#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004096int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004097 struct demo demo;
4098
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004099 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004100#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004101 demo_create_xcb_window(&demo);
4102#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4103 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004104#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4105 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004106#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004107#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004108
Tony Barbourcc69f452015-10-08 13:59:42 -06004109 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004110
4111 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004112
Tony Barbour153cb062016-12-07 13:43:36 -07004113#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004114 demo_run_xcb(&demo);
4115#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4116 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004117#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4118 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004119#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07004120#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4121 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004122#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004123
4124 demo_cleanup(&demo);
4125
Karl Schultz0218c002016-03-22 17:06:13 -06004126 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004127}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004128#endif