blob: a429af7eb1c63c60892cdae46b68a8d53674565a [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
Ian Elliottd940ca22017-03-22 08:31:27 -06002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Ian Elliott <ianelliott@google.com>
22 * Author: Jon Ashburn <jon@lunarg.com>
23 * Author: Gwan-gyeong Mun <elongbug@gmail.com>
24 * Author: Tony Barbour <tony@LunarG.com>
25 * Author: Bill Hollings <bill.hollings@brenwill.com>
26 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070027
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
Ian Elliottd940ca22017-03-22 08:31:27 -060030#include <stdarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060031#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070035#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090036#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060037#include <X11/Xutil.h>
38#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060039
Ian Elliott639ca472015-04-16 15:23:05 -060040#ifdef _WIN32
41#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060042#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070043#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060044
Tony Barbourefd0c5a2016-12-07 14:45:12 -070045#if defined(VK_USE_PLATFORM_MIR_KHR)
46#warning "Cube does not have code for Mir at this time"
47#endif
48
Cody Northrop8707a6e2016-04-26 19:59:19 -060049#ifdef ANDROID
50#include "vulkan_wrapper.h"
51#else
David Pinedoc5193c12015-11-06 12:54:48 -070052#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060053#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070054
David Pinedo541526f2015-11-24 09:00:24 -070055#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060056#include "linmath.h"
57
Ian Elliottd940ca22017-03-22 08:31:27 -060058#include "gettime.h"
59#include "inttypes.h"
60#define MILLION 1000000L
61#define BILLION 1000000000L
62
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060063#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060064#define APP_SHORT_NAME "cube"
65#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060066
Tony Barbour66a56c32016-09-21 13:10:56 -060067// Allow a maximum of two outstanding presentation operations.
68#define FRAME_LAG 2
69
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060070#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
71
Tony Barbourfdc2d352015-04-22 09:02:32 -060072#if defined(NDEBUG) && defined(__GNUC__)
73#define U_ASSERT_ONLY __attribute__((unused))
74#else
75#define U_ASSERT_ONLY
76#endif
77
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090078#if defined(__GNUC__)
79#define UNUSED __attribute__((unused))
80#else
81#define UNUSED
82#endif
83
Ian Elliott07264132015-04-28 11:35:02 -060084#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060085bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070086#define ERR_EXIT(err_msg, err_class) \
87 do { \
88 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
89 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070090 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060091void DbgMsg(char *fmt, ...) {
92 va_list va;
93 va_start(va, fmt);
94 printf(fmt, va);
95 fflush(stdout);
96 va_end(va);
97}
Ian Elliott07264132015-04-28 11:35:02 -060098
Michael Lentinec6cde4b2016-04-18 13:20:45 -050099#elif defined __ANDROID__
100#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700101#define ERR_EXIT(err_msg, err_class) \
102 do { \
103 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
104 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500105 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600106#ifdef VARARGS_WORKS_ON_ANDROID
107void DbgMsg(const char *fmt, ...) {
108 va_list va;
109 va_start(va, fmt);
110 __android_log_print(ANDROID_LOG_INFO, "Cube", fmt, va);
111 va_end(va);
112}
113#else // VARARGS_WORKS_ON_ANDROID
114#define DbgMsg(fmt, ...) \
115 do { \
116 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", fmt, ##__VA_ARGS__)); \
117 } while (0)
118#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500119#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700120#define ERR_EXIT(err_msg, err_class) \
121 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800122 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700123 fflush(stdout); \
124 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700125 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600126void DbgMsg(char *fmt, ...) {
127 va_list va;
128 va_start(va, fmt);
129 printf(fmt, va);
130 fflush(stdout);
131 va_end(va);
132}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500133#endif
Ian Elliott07264132015-04-28 11:35:02 -0600134
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700135#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
136 { \
137 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
138 if (demo->fp##entrypoint == NULL) { \
139 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
140 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700141 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600142
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600143static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
144
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700145#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
146 { \
147 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
148 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
149 if (demo->fp##entrypoint == NULL) { \
150 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
151 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700152 }
Ian Elliott673898b2015-06-22 15:07:49 -0600153
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600154/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700155 * structure to track all objects related to a texture.
156 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600157struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600158 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700159
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600160 VkImage image;
161 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600162
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800163 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500164 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600165 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700166 int32_t tex_width, tex_height;
167};
168
Karl Schultze4dc75c2016-02-02 15:37:51 -0700169static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170
Karl Schultz0218c002016-03-22 17:06:13 -0600171static int validation_error = 0;
172
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600173struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600174 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700175 float mvp[4][4];
176 float position[12 * 3][4];
177 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600178};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600179
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600180//--------------------------------------------------------------------------------------
181// Mesh and VertexFormat Data
182//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700183// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600184static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600186 -1.0f,-1.0f, 1.0f,
187 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f, 1.0f,-1.0f,
190 -1.0f,-1.0f,-1.0f,
191
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600193 1.0f, 1.0f,-1.0f,
194 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600197 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800199 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600200 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600201 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600204 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600205
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600207 -1.0f, 1.0f, 1.0f,
208 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800209 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600211 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214 1.0f, 1.0f, 1.0f,
215 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f,-1.0f,-1.0f,
218 1.0f, 1.0f,-1.0f,
219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600222 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f,-1.0f, 1.0f,
225 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600226};
227
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600228static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f, // -X side
230 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800231 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 0.0f, 0.0f,
234 0.0f, 1.0f,
235
Rene Lindsay76867e82016-07-29 10:49:11 -0700236 1.0f, 1.0f, // -Z side
237 0.0f, 0.0f,
238 0.0f, 1.0f,
239 1.0f, 1.0f,
240 1.0f, 0.0f,
241 0.0f, 0.0f,
242
243 1.0f, 0.0f, // -Y side
244 1.0f, 1.0f,
245 0.0f, 1.0f,
246 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600247 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800248 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600249
Rene Lindsay76867e82016-07-29 10:49:11 -0700250 1.0f, 0.0f, // +Y side
251 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800252 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600253 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700254 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600255 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600256
Rene Lindsay76867e82016-07-29 10:49:11 -0700257 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800258 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700259 0.0f, 1.0f,
260 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600261 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800262 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700263
264 0.0f, 0.0f, // +Z side
265 0.0f, 1.0f,
266 1.0f, 0.0f,
267 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600268 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700269 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600270};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700271// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600272
Karl Schultze4dc75c2016-02-02 15:37:51 -0700273void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600274 int i;
275
276 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700277 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600278 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
279 }
280 printf("\n");
281 fflush(stdout);
282}
283
Karl Schultze4dc75c2016-02-02 15:37:51 -0700284void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600285 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700286 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600287 printf("\n");
288 fflush(stdout);
289}
290
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700291VKAPI_ATTR VkBool32 VKAPI_CALL BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
292 size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg,
293 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700294#ifndef WIN32
295 raise(SIGTRAP);
296#else
297 DebugBreak();
298#endif
299
300 return false;
301}
302
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600303typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600304 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800305 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600306 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600307 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700308 VkBuffer uniform_buffer;
309 VkDeviceMemory uniform_memory;
310 VkFramebuffer framebuffer;
311 VkDescriptorSet descriptor_set;
312 VkFence fence;
313} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600314
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600315struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500316#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600317#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700318 HINSTANCE connection; // hInstance - Windows Instance
319 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
320 HWND window; // hWnd - window handle
321 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700322#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700323 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600324 Window xlib_window;
325 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700326#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700327 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328 xcb_connection_t *connection;
329 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600330 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800331 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900332#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
333 struct wl_display *display;
334 struct wl_registry *registry;
335 struct wl_compositor *compositor;
336 struct wl_surface *window;
337 struct wl_shell *shell;
338 struct wl_shell_surface *shell_surface;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700339#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500340#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700341 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500342#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
343 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500344#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700345 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600346 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700347 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600348 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
Ian Elliottd940ca22017-03-22 08:31:27 -0600350 bool VK_GOOGLE_display_timing_enabled;
351 bool syncd_with_actual_presents;
352 uint64_t refresh_duration;
353 uint64_t refresh_duration_multiplier;
354 uint64_t target_IPD; // image present duration (inverse of frame rate)
355 uint64_t prev_desired_present_time;
356 uint32_t next_present_id;
357 uint32_t last_early_id; // 0 if no early images
358 uint32_t last_late_id; // 0 if no late images
359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600361 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600363 VkQueue graphics_queue;
364 VkQueue present_queue;
365 uint32_t graphics_queue_family_index;
366 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600367 VkSemaphore image_acquired_semaphores[FRAME_LAG];
368 VkSemaphore draw_complete_semaphores[FRAME_LAG];
369 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600370 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600371 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600372 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Tony Barbourda2bad52016-01-22 14:36:40 -0700374 uint32_t enabled_extension_count;
375 uint32_t enabled_layer_count;
376 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600377 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700378
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600381 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600382
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700383 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
384 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
385 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
386 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600387 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
388 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
389 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
390 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
391 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600392 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
393 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600394 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600395 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700396 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600397 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600398 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600399 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800401 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600402 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403
404 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600406
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800408 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500409 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600410 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600411 } depth;
412
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600414 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600415
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700416 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500417 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600418 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600419 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800420 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600421 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600422
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600423 mat4x4 projection_matrix;
424 mat4x4 view_matrix;
425 mat4x4 model_matrix;
426
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600427 float spin_angle;
428 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600429 bool pause;
430
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600431 VkShaderModule vert_shader_module;
432 VkShaderModule frag_shader_module;
433
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600434 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800435
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600436 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600437 int32_t curFrame;
438 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600439 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600440 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600441 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600442 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700443 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
444 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
445 VkDebugReportCallbackEXT msg_callback;
446 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600447
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600448 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600449 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450};
451
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700452VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
453 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600454 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600455 char *message = (char *)malloc(strlen(pMsg) + 100);
456
457 assert(message);
458
Cody Northropaf28a532016-09-27 10:50:57 -0600459 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
460 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600461 validation_error = 1;
462 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600463 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
464 validation_error = 1;
465 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600466 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600467 validation_error = 1;
468 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
469 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
470 validation_error = 1;
471 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
472 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600473 validation_error = 1;
474 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600475 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600476 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600477 }
478
479#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600480
Tony Barbour602ca4f2016-09-12 14:02:43 -0600481 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600482 struct demo *demo = (struct demo*) pUserData;
483 if (!demo->suppress_popups)
484 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600485 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600486
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600487#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600488
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600489 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600490 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600491 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600492 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600493 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600494 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600495 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600496 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600497 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600498 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600499 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600500 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600501 }
Cody Northropaf28a532016-09-27 10:50:57 -0600502
lenny-lunargfbe22392016-06-06 11:07:53 -0600503#else
Cody Northropaf28a532016-09-27 10:50:57 -0600504
lenny-lunargfbe22392016-06-06 11:07:53 -0600505 printf("%s\n", message);
506 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600507
lenny-lunargfbe22392016-06-06 11:07:53 -0600508#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600509
lenny-lunargfbe22392016-06-06 11:07:53 -0600510 free(message);
511
Cody Northropaf28a532016-09-27 10:50:57 -0600512 //clang-format on
513
lenny-lunargfbe22392016-06-06 11:07:53 -0600514 /*
515 * false indicates that layer should not bail-out of an
516 * API call that had validation failures. This may mean that the
517 * app dies inside the driver due to invalid parameter(s).
518 * That's what would happen without validation layers, so we'll
519 * keep that behavior here.
520 */
521 return false;
522}
523
Ian Elliottd940ca22017-03-22 08:31:27 -0600524bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
525 // The desired time was the earliest time that the present should have
526 // occured. In almost every case, the actual time should be later than the
527 // desired time. We should only consider the actual time "late" if it is
528 // after "desired + rdur".
529 if (actual <= desired) {
530 // The actual time was before or equal to the desired time. This will
531 // probably never happen, but in case it does, return false since the
532 // present was obviously NOT late.
533 return false;
534 }
535 uint64_t deadline = actual + rdur;
536 if (actual > deadline) {
537 return true;
538 } else {
539 return false;
540 }
541}
542bool CanPresentEarlier(uint64_t earliest,
543 uint64_t actual,
544 uint64_t margin,
545 uint64_t rdur) {
546 if (earliest < actual) {
547 // Consider whether this present could have occured earlier. Make sure
548 // that earliest time was at least 2msec earlier than actual time, and
549 // that the margin was at least 2msec:
550 uint64_t diff = actual - earliest;
551 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
552 // This present could have occured earlier because both: 1) the
553 // earliest time was at least 2 msec before actual time, and 2) the
554 // margin was at least 2msec.
555 return true;
556 }
557 }
558 return false;
559}
560
Ian Elliottcf074d32015-10-16 18:02:43 -0600561// Forward declaration:
562static void demo_resize(struct demo *demo);
563
Karl Schultze4dc75c2016-02-02 15:37:51 -0700564static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
565 VkFlags requirements_mask,
566 uint32_t *typeIndex) {
567 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600568 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700569 if ((typeBits & 1) == 1) {
570 // Type is available, does it match user properties?
571 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
572 requirements_mask) == requirements_mask) {
573 *typeIndex = i;
574 return true;
575 }
576 }
577 typeBits >>= 1;
578 }
579 // No memory types matched, return failure
580 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600581}
582
Karl Schultze4dc75c2016-02-02 15:37:51 -0700583static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600584 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585
Tony Barbource7524e2016-07-28 12:01:45 -0600586 // This function could get called twice if the texture uses a staging buffer
587 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600588 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600589 return;
590
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600591 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600592 assert(!err);
593
Tony Barbource7524e2016-07-28 12:01:45 -0600594 VkFence fence;
595 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
596 .pNext = NULL,
597 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700598 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
599 assert(!err);
600
Karl Schultze4dc75c2016-02-02 15:37:51 -0700601 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700602 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
603 .pNext = NULL,
604 .waitSemaphoreCount = 0,
605 .pWaitSemaphores = NULL,
606 .pWaitDstStageMask = NULL,
607 .commandBufferCount = 1,
608 .pCommandBuffers = cmd_bufs,
609 .signalSemaphoreCount = 0,
610 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600611
Tony Barbource7524e2016-07-28 12:01:45 -0600612 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600613 assert(!err);
614
Tony Barbource7524e2016-07-28 12:01:45 -0600615 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600616 assert(!err);
617
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600618 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600619 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600620 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600621}
622
Karl Schultze4dc75c2016-02-02 15:37:51 -0700623static void demo_set_image_layout(struct demo *demo, VkImage image,
624 VkImageAspectFlags aspectMask,
625 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700626 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600627 VkAccessFlagBits srcAccessMask,
628 VkPipelineStageFlags src_stages,
629 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600630 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600631
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600632 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600633 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600634 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700635 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800636 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600637 .oldLayout = old_image_layout,
638 .newLayout = new_image_layout,
639 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700640 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641
Tony Barboureb5077b2016-10-19 15:23:36 -0600642 switch (new_image_layout) {
643 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600644 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600645 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
646 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600647
Tony Barboureb5077b2016-10-19 15:23:36 -0600648 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700649 image_memory_barrier.dstAccessMask =
650 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600651 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700652
Tony Barboureb5077b2016-10-19 15:23:36 -0600653 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700654 image_memory_barrier.dstAccessMask =
655 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600656 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700657
Tony Barboureb5077b2016-10-19 15:23:36 -0600658 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700659 image_memory_barrier.dstAccessMask =
660 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600661 break;
662
663 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
664 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
665 break;
666
667 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
668 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
669 break;
670
671 default:
672 image_memory_barrier.dstAccessMask = 0;
673 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600674 }
675
Tony Barbourf165b5d2016-10-03 16:01:41 -0600676
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600677 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600678
Karl Schultze4dc75c2016-02-02 15:37:51 -0700679 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
680 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600681}
682
Karl Schultze4dc75c2016-02-02 15:37:51 -0700683static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700684 const VkCommandBufferBeginInfo cmd_buf_info = {
685 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
686 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600687 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700688 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700689 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800690 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
692 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800693 };
694 const VkRenderPassBeginInfo rp_begin = {
695 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
696 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800697 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700698 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800699 .renderArea.offset.x = 0,
700 .renderArea.offset.y = 0,
701 .renderArea.extent.width = demo->width,
702 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600703 .clearValueCount = 2,
704 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700705 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800706 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600707
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600708 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600709 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800710 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700711 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
712 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700713 demo->pipeline_layout, 0, 1,
714 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
715 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600716 VkViewport viewport;
717 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700718 viewport.height = (float)demo->height;
719 viewport.width = (float)demo->width;
720 viewport.minDepth = (float)0.0f;
721 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700722 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600723
724 VkRect2D scissor;
725 memset(&scissor, 0, sizeof(scissor));
726 scissor.extent.width = demo->width;
727 scissor.extent.height = demo->height;
728 scissor.offset.x = 0;
729 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700730 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600731 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600732 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600733 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800734 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600735
Tony Barbour22e06272016-09-07 16:07:56 -0600736 if (demo->separate_present_queue) {
737 // We have to transfer ownership from the graphics queue family to the
738 // present queue family to be able to present. Note that we don't have
739 // to transfer from present queue family back to graphics queue family at
740 // the start of the next frame because we don't care about the image's
741 // contents at that point.
742 VkImageMemoryBarrier image_ownership_barrier = {
743 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
744 .pNext = NULL,
745 .srcAccessMask = 0,
746 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
747 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
748 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
749 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
750 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700751 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600752 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
753
754 vkCmdPipelineBarrier(cmd_buf,
755 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600756 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600757 0, NULL, 0, NULL, 1, &image_ownership_barrier);
758 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600759 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600760 assert(!err);
761}
762
Tony Barbour22e06272016-09-07 16:07:56 -0600763void demo_build_image_ownership_cmd(struct demo *demo, int i) {
764 VkResult U_ASSERT_ONLY err;
765
766 const VkCommandBufferBeginInfo cmd_buf_info = {
767 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
768 .pNext = NULL,
769 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
770 .pInheritanceInfo = NULL,
771 };
Tony Barboura72d9492017-01-11 13:35:09 -0700772 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600773 &cmd_buf_info);
774 assert(!err);
775
776 VkImageMemoryBarrier image_ownership_barrier = {
777 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
778 .pNext = NULL,
779 .srcAccessMask = 0,
780 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
781 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
782 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
783 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
784 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700785 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600786 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
787
Tony Barboura72d9492017-01-11 13:35:09 -0700788 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600789 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
790 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
791 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700792 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600793 assert(!err);
794}
795
Karl Schultze4dc75c2016-02-02 15:37:51 -0700796void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600797 mat4x4 MVP, Model, VP;
798 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600799 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600800 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600802 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600803
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700804 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600805 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700806 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
807 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600808 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600809
Tony Barboura72d9492017-01-11 13:35:09 -0700810 vkWaitForFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence,
811 VK_TRUE, UINT64_MAX);
812
813 err = vkMapMemory(demo->device,
814 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
815 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600816 assert(!err);
817
Karl Schultze4dc75c2016-02-02 15:37:51 -0700818 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819
Tony Barboura72d9492017-01-11 13:35:09 -0700820 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600821}
822
Ian Elliottd940ca22017-03-22 08:31:27 -0600823void DemoUpdateTargetIPD(struct demo *demo) {
824 // Look at what happened to previous presents, and make appropriate
825 // adjustments in timing:
826 VkResult U_ASSERT_ONLY err;
827 VkPastPresentationTimingGOOGLE* past = NULL;
828 uint32_t count = 0;
829 DbgMsg("%s(): about to call vkGetPastPresentationTimingGOOGLE\n", __FUNCTION__);
830
831 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
832 demo->swapchain,
833 &count,
834 NULL);
835 assert(!err);
836 DbgMsg("%s(): vkGetPastPresentationTimingGOOGLE returned count of %d\n",
837 __FUNCTION__, count);
838 if (count) {
839 past = (VkPastPresentationTimingGOOGLE*) malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
840 assert(past);
841 DbgMsg("%s(): about to call vkGetPastPresentationTimingGOOGLE again\n", __FUNCTION__);
842 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
843 demo->swapchain,
844 &count,
845 past);
846 assert(!err);
847
848 bool early = false;
849 bool late = false;
850 bool calibrate_next = false;
851 static VkPastPresentationTimingGOOGLE prior = {0xffffffff, 0, 0, 0, 0};
852 for (uint32_t i = 0 ; i < count ; i++) {
853 DbgMsg("%s():\n", __FUNCTION__);
854 DbgMsg("%s():\t presentID, desiredPresentTime, actualPresentTime, earliestPresentTime, presentMargin\n", __FUNCTION__);
855 if (prior.presentID != 0xffffffff) {
856 DbgMsg("%s(): prior: %8d, %17"PRId64", %16"PRId64", %18"PRId64", %12"PRId64"\n", __FUNCTION__,
857 prior.presentID,
858 prior.desiredPresentTime,
859 prior.actualPresentTime,
860 prior.earliestPresentTime,
861 prior.presentMargin);
862 }
863 DbgMsg("%s(): cur: %8d, %17"PRId64", %16"PRId64", %18"PRId64", %12"PRId64"\n", __FUNCTION__,
864 past[i].presentID,
865 past[i].desiredPresentTime,
866 past[i].actualPresentTime,
867 past[i].earliestPresentTime,
868 past[i].presentMargin);
869 if (prior.presentID != 0xffffffff) {
870 int64_t diff_desiredPresentTime = past[i].desiredPresentTime - prior.desiredPresentTime;
871 int64_t diff_actualPresentTime = past[i].actualPresentTime - prior.actualPresentTime;
872 int64_t diff_earliestPresentTime = past[i].earliestPresentTime - prior.earliestPresentTime;
873 int64_t diff_presentMargin = past[i].presentMargin - prior.presentMargin;
874 DbgMsg("%s():\t ====================================================================================\n", __FUNCTION__);
875 DbgMsg("%s(): diff:\t %17"PRId64", %16"PRId64", %18"PRId64", %12"PRId64"\n", __FUNCTION__,
876 diff_desiredPresentTime,
877 diff_actualPresentTime,
878 diff_earliestPresentTime,
879 diff_presentMargin);
880 }
881 DbgMsg("%s():\n", __FUNCTION__);
882
883
884 DbgMsg("%s(): actualPresentTime= %16"PRId64", actualPresentTime = %16"PRId64", PRIORactualPresentTime= %16"PRId64"\n",
885 __FUNCTION__, past[i].actualPresentTime, past[i].actualPresentTime, prior.actualPresentTime);
886 DbgMsg("%s():desiredPresentTime= %16"PRId64", earliestPresentTime = %16"PRId64", earliestPresentTime = %16"PRId64"\n",
887 __FUNCTION__, past[i].desiredPresentTime, past[i].earliestPresentTime, past[i].earliestPresentTime);
888 int64_t diffDesireVsActual = past[i].actualPresentTime - past[i].desiredPresentTime;
889 uint64_t diffEarlyVsActual = past[i].actualPresentTime - past[i].earliestPresentTime;
890 uint64_t diffEarlyVsPriorActual = past[i].earliestPresentTime - prior.actualPresentTime;
891 DbgMsg("%s():\t\t\t ================================================================================================\n", __FUNCTION__);
892 DbgMsg("%s(): Amt late (-early) = %12"PRId64", Amt could have been early = %10"PRId64", Sanity check earliest = %16"PRId64"\n",
893 __FUNCTION__, diffDesireVsActual, diffEarlyVsActual, diffEarlyVsPriorActual);
894 DbgMsg("%s(): demo->target_IPD = %"PRId64"\n", __FUNCTION__, demo->target_IPD);
895 DbgMsg("%s(): demo->next_present_id: %d\n", __FUNCTION__, demo->next_present_id);
896 prior.presentID = past[i].presentID;
897 prior.desiredPresentTime = past[i].desiredPresentTime;
898 prior.actualPresentTime = past[i].actualPresentTime;
899 prior.earliestPresentTime = past[i].earliestPresentTime;
900 prior.presentMargin = past[i].presentMargin;
901 if (!demo->syncd_with_actual_presents) {
902 // This is the first time that we've received an
903 // actualPresentTime for this swapchain. In order to not
904 // perceive these early frames as "late", we need to sync-up
905 // our future desiredPresentTime's with the
906 // actualPresentTime(s) that we're receiving now.
907 calibrate_next = true;
908 DbgMsg("%s():\n", __FUNCTION__);
909 DbgMsg("%s(): FIRST TIME GETTING BACK ACTUAL TIME(S)\n", __FUNCTION__);
910 DbgMsg("%s(): Number of TimingInfo's we received was %d\n", __FUNCTION__, count);
911 if (count > 1) {
912 DbgMsg("%s(): past[%d].actualPresentTime = %"PRId64"\n", __FUNCTION__, (count-1), past[count-1].actualPresentTime);
913 DbgMsg("%s(): Last presentID is: %d\n", __FUNCTION__, past[count-1].presentID);
914 }
915 DbgMsg("%s(): demo->prev_desired_present_time= %"PRId64"\n", __FUNCTION__, demo->prev_desired_present_time);
916
917 DbgMsg("%s():\n", __FUNCTION__);
918 DbgMsg("%s():\n", __FUNCTION__);
919 // So that we don't suspect any pending presents as late,
920 // record them all as suspected-late presents:
921 demo->last_late_id = demo->next_present_id - 1;
922 demo->last_early_id = 0;
923 DbgMsg("%s(): Skip past presentID: %d\n", __FUNCTION__, demo->last_late_id);
924
925 demo->syncd_with_actual_presents = true;
926 break;
927 } else if (CanPresentEarlier(past[i].earliestPresentTime,
928 past[i].actualPresentTime,
929 past[i].presentMargin,
930 demo->refresh_duration)) {
931 // This image could have been presented earlier. We don't want
932 // to decrease the target_IPD until we've seen early presents
933 // for at least two seconds.
934 if (demo->last_early_id == past[i].presentID) {
935 // We've now seen two seconds worth of early presents.
936 // Flag it as such, and reset the counter:
937 early = true;
938 demo->last_early_id = 0;
939 } else if (demo->last_early_id == 0) {
940 // This is the first early present we've seen.
941 // Calculate the presentID for two seconds from now.
942 uint64_t lastEarlyTime =
943 past[i].actualPresentTime + (2 * BILLION);
944 uint32_t howManyPresents =
945 (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
946 demo->last_early_id = past[i].presentID + howManyPresents;
947 } else {
948 // We are in the midst of a set of early images,
949 // and so we won't do anything.
950 }
951 late = false;
952 demo->last_late_id = 0;
953 } else if (ActualTimeLate(past[i].desiredPresentTime,
954 past[i].actualPresentTime,
955 demo->refresh_duration)) {
956 // This image was presented after its desired time. Since
957 // there's a delay between calling vkQueuePresentKHR and when
958 // we get the timing data, several presents may have been late.
959 // Thus, we need to threat all of the outstanding presents as
960 // being likely late, so that we only increase the target_IPD
961 // once for all of those presents.
962 if ((demo->last_late_id == 0) ||
963 (demo->last_late_id < past[i].presentID)) {
964 late = true;
965 // Record the last suspected-late present:
966 demo->last_late_id = demo->next_present_id - 1;
967 } else {
968 // We are in the midst of a set of likely-late images,
969 // and so we won't do anything.
970 }
971 early = false;
972 demo->last_early_id = 0;
973 } else {
974 // Since this image was not presented early or late, reset
975 // any sets of early or late presentIDs:
976 early = false;
977 late = false;
978 calibrate_next = true;
979 demo->last_early_id = 0;
980 demo->last_late_id = 0;
981 }
982 }
983
984 if (early) {
985 // Since we've seen at least two-seconds worth of presnts that
986 // could have occured earlier than desired, let's decrease the
987 // target_IPD (i.e. increase the frame rate):
988 //
989 // TODO(ianelliott): Try to calculate a better target_IPD based
990 // on the most recently-seen present (this is overly-simplistic).
991 demo->refresh_duration_multiplier--;
992 if (demo->refresh_duration_multiplier == 0) {
993 // This should never happen, but in case it does, don't
994 // try to go faster.
995 demo->refresh_duration_multiplier = 1;
996 }
997 demo->target_IPD =
998 demo->refresh_duration * demo->refresh_duration_multiplier;
999 DbgMsg("\t INCREASING FRAME RATE! NEW VALUE OF target_IPD = %"PRId64"\n", demo->target_IPD);
1000 }
1001 if (late) {
1002 // Since we found a new instance of a late present, we want to
1003 // increase the target_IPD (i.e. decrease the frame rate):
1004 //
1005 // TODO(ianelliott): Try to calculate a better target_IPD based
1006 // on the most recently-seen present (this is overly-simplistic).
1007 demo->refresh_duration_multiplier++;
1008 demo->target_IPD =
1009 demo->refresh_duration * demo->refresh_duration_multiplier;
1010 DbgMsg("\t DECREASING FRAME RATE! NEW VALUE OF target_IPD = %"PRId64"\n", demo->target_IPD);
1011 }
1012
1013 if (calibrate_next) {
1014 int64_t multiple = demo->next_present_id - past[count-1].presentID;
1015 demo->prev_desired_present_time =
1016 (past[count-1].actualPresentTime +
1017 (multiple * demo->target_IPD));
1018 DbgMsg("%s():\n", __FUNCTION__);
1019 DbgMsg("%s(): CALIBRATING!!!\n", __FUNCTION__);
1020 DbgMsg("%s(): multiple = %"PRId64"\n", __FUNCTION__, multiple);
1021 DbgMsg("%s(): demo->prev_desired_present_time= %"PRId64"\n", __FUNCTION__, demo->prev_desired_present_time);
1022 int64_t next_time = demo->prev_desired_present_time + demo->target_IPD;
1023 DbgMsg("%s(): past[%d].desiredPresentTime = %"PRId64"\n", __FUNCTION__, demo->next_present_id, next_time);
1024 }
1025 }
1026 DbgMsg("\t\t\t NEW VALUE OF target_IPD = %"PRId64"\n", demo->target_IPD);
1027}
1028
Karl Schultze4dc75c2016-02-02 15:37:51 -07001029static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001030 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001031
szdarkhackd322ff02016-10-08 09:51:22 +03001032 // Ensure no more than FRAME_LAG presentations are outstanding
1033 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1034 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001035
Ian Elliottaaae5352015-07-06 14:27:58 -06001036 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001037 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -07001038 demo->image_acquired_semaphores[demo->frame_index],
1039 demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -06001040 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001041
Tony Barboura72d9492017-01-11 13:35:09 -07001042 demo_update_data_buffer(demo);
1043
Ian Elliottcf074d32015-10-16 18:02:43 -06001044 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1045 // demo->swapchain is out of date (e.g. the window was resized) and
1046 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -06001047 demo->frame_index += 1;
1048 demo->frame_index %= FRAME_LAG;
1049
Ian Elliottcf074d32015-10-16 18:02:43 -06001050 demo_resize(demo);
1051 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06001052 return;
1053 } else if (err == VK_SUBOPTIMAL_KHR) {
1054 // demo->swapchain is not as optimal as it could be, but the platform's
1055 // presentation engine will still present the image correctly.
1056 } else {
1057 assert(!err);
1058 }
Ian Elliottd940ca22017-03-22 08:31:27 -06001059 if (demo->VK_GOOGLE_display_timing_enabled) {
1060 // Look at what happened to previous presents, and make appropriate
1061 // adjustments in timing:
1062 DemoUpdateTargetIPD(demo);
1063
1064 // Note: a real application would position its geometry to that it's in
1065 // the correct locatoin for when the next image is presented. It might
1066 // also wait, so that there's less latency between any input and when
1067 // the next image is rendered/presented. This demo program is so
1068 // simple that it doesn't do either of those.
1069 }
1070
Tony Barbource7524e2016-07-28 12:01:45 -06001071 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001072 // that the image won't be rendered to until the presentation
1073 // engine has fully released ownership to the application, and it is
1074 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001075 VkPipelineStageFlags pipe_stage_flags;
1076 VkSubmitInfo submit_info;
1077 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1078 submit_info.pNext = NULL;
1079 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1080 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1081 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001082 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001083 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001084 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001085 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001086 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -07001087 vkResetFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence);
1088 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
1089 demo->swapchain_image_resources[demo->current_buffer].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001090 assert(!err);
1091
Tony Barbour22e06272016-09-07 16:07:56 -06001092 if (demo->separate_present_queue) {
1093 // If we are using separate queues, change image ownership to the
1094 // present queue before presenting, waiting for the draw complete
1095 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001096 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001097 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1098 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001099 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001100 submit_info.commandBufferCount = 1;
1101 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -07001102 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001103 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001104 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001105 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1106 assert(!err);
1107 }
1108
1109 // If we are using separate queues we have to wait for image ownership,
1110 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001111 VkPresentInfoKHR present = {
1112 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001113 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001114 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06001115 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -06001116 ? &demo->image_ownership_semaphores[demo->frame_index]
1117 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001118 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001119 .pSwapchains = &demo->swapchain,
1120 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001121 };
1122
Ian Elliottd940ca22017-03-22 08:31:27 -06001123 if (demo->VK_GOOGLE_display_timing_enabled) {
1124 VkPresentTimeGOOGLE ptime;
1125 if (demo->prev_desired_present_time == 0) {
1126 // This must be the first present for this swapchain.
1127 //
1128 // We don't know where we are relative to the presentation engine's
1129 // display's refresh cycle. We also don't know how long rendering
1130 // takes. Let's make a grossly-simplified assumption that the
1131 // desiredPresentTime should be half way between now and
1132 // now+target_IPD. We will adjust over time.
1133 uint64_t curtime = getTimeInNanoseconds();
1134 if (curtime == 0) {
1135 // Since we didn't find out the current time, don't give a
1136 // desiredPresentTime:
1137 ptime.desiredPresentTime = 0;
1138 } else {
1139 DbgMsg("Current time (e.g. CLOCK_MONOTONIC) in nanoseconds: %"PRId64"\n",
1140 curtime);
1141 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1142 }
1143 } else {
1144 ptime.desiredPresentTime = (demo->prev_desired_present_time +
1145 demo->target_IPD);
1146 }
1147 ptime.presentID = demo->next_present_id++;
1148 demo->prev_desired_present_time = ptime.desiredPresentTime;
1149 DbgMsg("presentID = %d, desiredPresentTime = %"PRId64"\n",
1150 ptime.presentID,
1151 ptime.desiredPresentTime);
1152
1153 VkPresentTimesInfoGOOGLE present_time = {
1154 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1155 .pNext = present.pNext,
1156 .swapchainCount = present.swapchainCount,
1157 .pTimes = &ptime,
1158 };
1159 if (demo->VK_GOOGLE_display_timing_enabled) {
1160 present.pNext = &present_time;
1161 DbgMsg("present.pNext = %p, present_time = %p, present_time.pNext = %p\n",
1162 present.pNext,
1163 &present_time,
1164 present_time.pNext);
1165 }
1166 }
1167
Tony Barboura2a67812016-07-18 13:21:06 -06001168 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001169 demo->frame_index += 1;
1170 demo->frame_index %= FRAME_LAG;
1171
Ian Elliottcf074d32015-10-16 18:02:43 -06001172 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1173 // demo->swapchain is out of date (e.g. the window was resized) and
1174 // must be recreated:
1175 demo_resize(demo);
1176 } else if (err == VK_SUBOPTIMAL_KHR) {
1177 // demo->swapchain is not as optimal as it could be, but the platform's
1178 // presentation engine will still present the image correctly.
1179 } else {
1180 assert(!err);
1181 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001182}
1183
Karl Schultze4dc75c2016-02-02 15:37:51 -07001184static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001185 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001186 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001187
Ian Elliottb08e0d92015-11-20 11:55:46 -07001188 // Check the surface capabilities and formats
1189 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001190 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
1191 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001192 assert(!err);
1193
Ian Elliott8528eff2015-08-07 15:56:59 -06001194 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001195 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1196 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001197 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06001198 VkPresentModeKHR *presentModes =
1199 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001200 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001201 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1202 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001203 assert(!err);
1204
Ian Elliotta0781972015-08-21 15:09:33 -06001205 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001206 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1207 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1208 // If the surface size is undefined, the size is set to the size
1209 // of the images requested, which must fit within the minimum and
1210 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001211 swapchainExtent.width = demo->width;
1212 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001213
1214 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1215 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1216 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1217 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1218 }
1219
1220 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1221 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1222 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1223 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1224 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001225 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001226 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001227 swapchainExtent = surfCapabilities.currentExtent;
1228 demo->width = surfCapabilities.currentExtent.width;
1229 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001230 }
1231
Tony Barbour66a56c32016-09-21 13:10:56 -06001232 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001233 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001234 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001235
Ian Elliottb18fdde2016-10-03 12:11:12 -06001236 // There are times when you may wish to use another present mode. The
1237 // following code shows how to select them, and the comments provide some
1238 // reasons you may wish to use them.
1239 //
1240 // It should be noted that Vulkan 1.0 doesn't provide a method for
1241 // synchronizing rendering with the presentation engine's display. There
1242 // is a method provided for throttling rendering with the display, but
1243 // there are some presentation engines for which this method will not work.
1244 // If an application doesn't throttle its rendering, and if it renders much
1245 // faster than the refresh rate of the display, this can waste power on
1246 // mobile devices. That is because power is being spent rendering images
1247 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001248
Ian Elliottb18fdde2016-10-03 12:11:12 -06001249 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1250 // tearing, or have some way of synchronizing their rendering with the
1251 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001252 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1253 // generally render a new presentable image every refresh cycle, but are
1254 // occasionally early. In this case, the application wants the new image
1255 // to be displayed instead of the previously-queued-for-presentation image
1256 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001257 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1258 // render a new presentable image every refresh cycle, but are occasionally
1259 // late. In this case (perhaps because of stuttering/latency concerns),
1260 // the application wants the late image to be immediately displayed, even
1261 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001262
1263 if (demo->presentMode != swapchainPresentMode) {
1264
1265 for (size_t i = 0; i < presentModeCount; ++i) {
1266 if (presentModes[i] == demo->presentMode) {
1267 swapchainPresentMode = demo->presentMode;
1268 break;
1269 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001270 }
1271 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001272 if (swapchainPresentMode != demo->presentMode) {
1273 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1274 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001275
Tony Barbour84376fe2017-01-06 15:54:22 -07001276 // Determine the number of VkImages to use in the swap chain.
1277 // Application desires to acquire 3 images at a time for triple
1278 // buffering
1279 uint32_t desiredNumOfSwapchainImages = 3;
1280 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1281 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1282 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001283 // If maxImageCount is 0, we can ask for as many images as we want;
1284 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -07001285 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -06001286 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001287 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001288 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001289 }
1290
Tony Barbour9fd6d352015-09-16 15:01:32 -06001291 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001292 if (surfCapabilities.supportedTransforms &
1293 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001294 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001295 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001296 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001297 }
1298
Tony Barbour820b55b2017-03-14 08:55:46 -06001299 // Find a supported composite alpha mode - one of these is guaranteed to be set
1300 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1301 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1302 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1303 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1304 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1305 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1306 };
1307 for (uint32_t i = 0; i < sizeof(compositeAlphaFlags); i++) {
1308 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1309 compositeAlpha = compositeAlphaFlags[i];
1310 break;
1311 }
1312 }
1313
Tony Barboura2a67812016-07-18 13:21:06 -06001314 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001315 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001316 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001317 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001318 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001319 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001320 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 .imageExtent =
1322 {
1323 .width = swapchainExtent.width, .height = swapchainExtent.height,
1324 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001325 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001326 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001327 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001328 .imageArrayLayers = 1,
1329 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1330 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001331 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001332 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001333 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001334 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001336 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -06001337 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001338 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001339 assert(!err);
1340
Ian Elliottcf074d32015-10-16 18:02:43 -06001341 // If we just re-created an existing swapchain, we should destroy the old
1342 // swapchain at this point.
1343 // Note: destroying the swapchain also cleans up all its associated
1344 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001345 if (oldSwapchain != VK_NULL_HANDLE) {
Tony Barboura90a4f92017-03-23 13:25:36 -06001346 // AMD driver times out waiting on fences used in AcquireNextImage on
1347 // a swapchain that is subsequently destroyed before the wait.
1348 vkWaitForFences(demo->device, FRAME_LAG, demo->fences, VK_TRUE, UINT64_MAX);
Ian Elliottb08e0d92015-11-20 11:55:46 -07001349 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001350 }
1351
1352 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001353 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001354 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001355
Karl Schultze4dc75c2016-02-02 15:37:51 -07001356 VkImage *swapchainImages =
1357 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001358 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001359 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001360 &demo->swapchainImageCount,
1361 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001362 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001363
Tony Barboura72d9492017-01-11 13:35:09 -07001364 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001365 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001366 assert(demo->swapchain_image_resources);
1367
1368 VkFenceCreateInfo fence_ci = {
1369 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1370 .pNext = NULL,
1371 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1372 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001373
Ian Elliotta0781972015-08-21 15:09:33 -06001374 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001375 VkImageViewCreateInfo color_image_view = {
1376 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001377 .pNext = NULL,
1378 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001379 .components =
1380 {
1381 .r = VK_COMPONENT_SWIZZLE_R,
1382 .g = VK_COMPONENT_SWIZZLE_G,
1383 .b = VK_COMPONENT_SWIZZLE_B,
1384 .a = VK_COMPONENT_SWIZZLE_A,
1385 },
1386 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1387 .baseMipLevel = 0,
1388 .levelCount = 1,
1389 .baseArrayLayer = 0,
1390 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001391 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1392 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393 };
1394
Tony Barboura72d9492017-01-11 13:35:09 -07001395 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396
Tony Barboura72d9492017-01-11 13:35:09 -07001397 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398
Karl Schultze4dc75c2016-02-02 15:37:51 -07001399 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001400 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001401 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001402
Tony Barboura72d9492017-01-11 13:35:09 -07001403 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1404 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001405 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001406
Ian Elliottd940ca22017-03-22 08:31:27 -06001407 if (demo->VK_GOOGLE_display_timing_enabled) {
1408 VkRefreshCycleDurationGOOGLE rc_dur;
1409 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device,
1410 demo->swapchain,
1411 &rc_dur);
1412 assert(!err);
1413 demo->refresh_duration = rc_dur.refreshDuration;
1414
1415 demo->syncd_with_actual_presents = false;
1416 // Initially target 1X the refresh duration:
1417 demo->target_IPD = demo->refresh_duration;
1418 demo->refresh_duration_multiplier = 1;
1419 demo->prev_desired_present_time = 0;
1420 demo->next_present_id = 1;
1421
1422 DbgMsg("refresh_duration = %"PRId64"\n", demo->refresh_duration);
1423 DbgMsg("\t\t\tINITIAL VALUE OF target_IPD = %"PRId64"\n", demo->target_IPD);
1424 }
1425
Mark Young04fd3d82016-01-25 14:43:50 -07001426 if (NULL != presentModes) {
1427 free(presentModes);
1428 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429}
1430
Karl Schultze4dc75c2016-02-02 15:37:51 -07001431static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001432 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001433 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001436 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001438 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001440 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001441 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001442 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001443 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444 .flags = 0,
1445 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001446
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001447 VkImageViewCreateInfo view = {
1448 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001449 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001450 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001451 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001452 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1453 .baseMipLevel = 0,
1454 .levelCount = 1,
1455 .baseArrayLayer = 0,
1456 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001458 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001460
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001461 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001462 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001463 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001464
1465 demo->depth.format = depth_format;
1466
1467 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001469 assert(!err);
1470
Karl Schultze4dc75c2016-02-02 15:37:51 -07001471 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001472 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001473
Chia-I Wuf48700b2015-11-10 16:21:09 +08001474 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001475 demo->depth.mem_alloc.pNext = NULL;
1476 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1477 demo->depth.mem_alloc.memoryTypeIndex = 0;
1478
Karl Schultze4dc75c2016-02-02 15:37:51 -07001479 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001480 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001481 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001482 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001483
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001484 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001485 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1486 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001487 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001489 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001490 err =
1491 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001492 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001493
1494 /* create image view */
1495 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001496 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001497 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001498}
1499
Tony Barbour1a86d032015-09-21 15:17:33 -06001500/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001501bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001502 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001503
1504#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001505 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001506#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001507
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001508#ifdef __ANDROID__
1509#include <lunarg.ppm.h>
1510 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001511 cPtr = (char*)lunarg_ppm;
1512 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001513 return false;
1514 }
1515 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001516 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001517 if (rgba_data == NULL) {
1518 return true;
1519 }
1520 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001521 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001522 return false;
1523 }
1524 while(strncmp(cPtr++, "\n", 1));
1525
1526 for (int y = 0; y < *height; y++) {
1527 uint8_t *rowPtr = rgba_data;
1528 for (int x = 0; x < *width; x++) {
1529 memcpy(rowPtr, cPtr, 3);
1530 rowPtr[3] = 255; /* Alpha of 1 */
1531 rowPtr += 4;
1532 cPtr += 3;
1533 }
1534 rgba_data += layout->rowPitch;
1535 }
1536
1537 return true;
1538#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001540 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001541
Tony Barbour1a86d032015-09-21 15:17:33 -06001542 if (!fPtr)
1543 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001544
Tony Barbour1a86d032015-09-21 15:17:33 -06001545 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001546 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1547 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001548 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001549 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001550
Tony Barbour1a86d032015-09-21 15:17:33 -06001551 do {
1552 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001553 if (cPtr == NULL) {
1554 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001555 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001556 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001557 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001558
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001559 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001560 if (rgba_data == NULL) {
1561 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001562 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001563 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001564 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001565 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001566 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1567 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001568 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001569 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001570
Karl Schultze4dc75c2016-02-02 15:37:51 -07001571 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001572 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001573 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001574 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001575 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001576 rowPtr[3] = 255; /* Alpha of 1 */
1577 rowPtr += 4;
1578 }
1579 rgba_data += layout->rowPitch;
1580 }
1581 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001582 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001583#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001584}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001585
Karl Schultze4dc75c2016-02-02 15:37:51 -07001586static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001587 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001588 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001589 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001590 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001591 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001592 int32_t tex_width;
1593 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001594 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001595 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001596
Karl Schultze4dc75c2016-02-02 15:37:51 -07001597 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001598 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001599 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001600
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001601 tex_obj->tex_width = tex_width;
1602 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001603
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001604 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001605 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001606 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001607 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001608 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001609 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001610 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001611 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001612 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001613 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001614 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001615 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001616 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001617 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001618
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001619 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001620
Karl Schultze4dc75c2016-02-02 15:37:51 -07001621 err =
1622 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001623 assert(!err);
1624
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001625 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001626
Chia-I Wuf48700b2015-11-10 16:21:09 +08001627 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001628 tex_obj->mem_alloc.pNext = NULL;
1629 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1630 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001631
Karl Schultze4dc75c2016-02-02 15:37:51 -07001632 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1633 required_props,
1634 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001635 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001636
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001637 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001638 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001639 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001640 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001641
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001642 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001643 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001644 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001645
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001646 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001647 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001648 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001649 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001650 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001651 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001652 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001653 void *data;
1654
Karl Schultze4dc75c2016-02-02 15:37:51 -07001655 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1656 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001657
Karl Schultze4dc75c2016-02-02 15:37:51 -07001658 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1659 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001660 assert(!err);
1661
1662 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1663 fprintf(stderr, "Error loading texture: %s\n", filename);
1664 }
1665
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001666 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001667 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001668
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001669 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001670}
1671
Karl Schultze4dc75c2016-02-02 15:37:51 -07001672static void demo_destroy_texture_image(struct demo *demo,
1673 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001674 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001675 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1676 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001677}
1678
Karl Schultze4dc75c2016-02-02 15:37:51 -07001679static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001680 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001681 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001682 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001683
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001684 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001685
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001687 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001688
Karl Schultze4dc75c2016-02-02 15:37:51 -07001689 if ((props.linearTilingFeatures &
1690 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1691 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001692 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001693 demo_prepare_texture_image(
1694 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1695 VK_IMAGE_USAGE_SAMPLED_BIT,
1696 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1697 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001698 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1699 // shader to run until layout transition completes
1700 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1701 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1702 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1703 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001704 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001705 } else if (props.optimalTilingFeatures &
1706 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001707 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001708
Tony Barbour16156cb2016-10-19 14:15:49 -06001709 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001710 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001711 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001712 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1713 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1714 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001715
Karl Schultze4dc75c2016-02-02 15:37:51 -07001716 demo_prepare_texture_image(
1717 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1718 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1719 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001720
Tony Barbour16156cb2016-10-19 14:15:49 -06001721 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001722 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001723 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001724 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001725 VK_ACCESS_HOST_WRITE_BIT,
1726 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1727 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001728
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001729 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001730 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001731 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001732 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001733 VK_ACCESS_HOST_WRITE_BIT,
1734 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1735 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001736
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001737 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001738 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1739 .srcOffset = {0, 0, 0},
1740 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1741 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001742 .extent = {demo->staging_texture.tex_width,
1743 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001744 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001745 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001746 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001747 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1748 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001749
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001750 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001751 VK_IMAGE_ASPECT_COLOR_BIT,
1752 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001753 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001754 VK_ACCESS_TRANSFER_WRITE_BIT,
1755 VK_PIPELINE_STAGE_TRANSFER_BIT,
1756 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001757
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001758 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001759 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1760 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001761 }
1762
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001763 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001764 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001766 .magFilter = VK_FILTER_NEAREST,
1767 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001768 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001769 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1770 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1771 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001772 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001773 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001774 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001775 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001776 .minLod = 0.0f,
1777 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001778 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001779 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001780 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001781
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001782 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001783 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001785 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001786 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001787 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001788 .components =
1789 {
1790 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1791 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1792 },
1793 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001794 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001795 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001796
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001797 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001798 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001799 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001800 assert(!err);
1801
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001802 /* create image view */
1803 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001804 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001805 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001806 assert(!err);
1807 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001808}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001809
Tony Barboura72d9492017-01-11 13:35:09 -07001810void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001811 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001812 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001813 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001814 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001815 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001816 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001817 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001818 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001819
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001820 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001821 mat4x4_mul(MVP, VP, demo->model_matrix);
1822 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001823 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001824
Karl Schultza2615462017-01-20 13:19:20 -07001825 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001826 data.position[i][0] = g_vertex_buffer_data[i * 3];
1827 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1828 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001829 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001830 data.attr[i][0] = g_uv_buffer_data[2 * i];
1831 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001832 data.attr[i][2] = 0;
1833 data.attr[i][3] = 0;
1834 }
1835
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001836 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001837 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001838 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001839 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001840
Tony Barboura72d9492017-01-11 13:35:09 -07001841 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1842 err =
1843 vkCreateBuffer(demo->device, &buf_info, NULL,
1844 &demo->swapchain_image_resources[i].uniform_buffer);
1845 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001846
Tony Barboura72d9492017-01-11 13:35:09 -07001847 vkGetBufferMemoryRequirements(demo->device,
1848 demo->swapchain_image_resources[i].uniform_buffer,
1849 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001850
Tony Barboura72d9492017-01-11 13:35:09 -07001851 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1852 mem_alloc.pNext = NULL;
1853 mem_alloc.allocationSize = mem_reqs.size;
1854 mem_alloc.memoryTypeIndex = 0;
1855
1856 pass = memory_type_from_properties(
1857 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001858 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001859 &mem_alloc.memoryTypeIndex);
1860 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001861
Tony Barboura72d9492017-01-11 13:35:09 -07001862 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1863 &demo->swapchain_image_resources[i].uniform_memory);
1864 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001865
Tony Barboura72d9492017-01-11 13:35:09 -07001866 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1867 VK_WHOLE_SIZE, 0, (void **)&pData);
1868 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001869
Tony Barboura72d9492017-01-11 13:35:09 -07001870 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001871
Tony Barboura72d9492017-01-11 13:35:09 -07001872 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001873
Tony Barboura72d9492017-01-11 13:35:09 -07001874 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1875 demo->swapchain_image_resources[i].uniform_memory, 0);
1876 assert(!err);
1877 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001878}
1879
Karl Schultze4dc75c2016-02-02 15:37:51 -07001880static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001881 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001882 [0] =
1883 {
1884 .binding = 0,
1885 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1886 .descriptorCount = 1,
1887 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1888 .pImmutableSamplers = NULL,
1889 },
1890 [1] =
1891 {
1892 .binding = 1,
1893 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1894 .descriptorCount = DEMO_TEXTURE_COUNT,
1895 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1896 .pImmutableSamplers = NULL,
1897 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001898 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001899 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001900 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001901 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001902 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001903 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001904 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001905 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001906
Karl Schultze4dc75c2016-02-02 15:37:51 -07001907 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1908 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001909 assert(!err);
1910
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001911 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001912 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1913 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001914 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001915 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001916 };
1917
Karl Schultze4dc75c2016-02-02 15:37:51 -07001918 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001919 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001920 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001921}
1922
Karl Schultze4dc75c2016-02-02 15:37:51 -07001923static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001924 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1925 // because at the start of the renderpass, we don't care about their contents.
1926 // At the start of the subpass, the color attachment's layout will be transitioned
1927 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1928 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1929 // the renderpass, the color attachment's layout will be transitioned to
1930 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1931 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001932 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001933 [0] =
1934 {
1935 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001936 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001937 .samples = VK_SAMPLE_COUNT_1_BIT,
1938 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1939 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1940 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1941 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001942 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001943 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001944 },
1945 [1] =
1946 {
1947 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001948 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001949 .samples = VK_SAMPLE_COUNT_1_BIT,
1950 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1951 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1952 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1953 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1954 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001955 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001956 .finalLayout =
1957 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1958 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001959 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001960 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001961 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001962 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001963 const VkAttachmentReference depth_reference = {
1964 .attachment = 1,
1965 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1966 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001967 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001968 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1969 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001970 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001971 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001972 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001973 .pColorAttachments = &color_reference,
1974 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001975 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001976 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001977 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001978 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001979 const VkRenderPassCreateInfo rp_info = {
1980 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1981 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001982 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001983 .attachmentCount = 2,
1984 .pAttachments = attachments,
1985 .subpassCount = 1,
1986 .pSubpasses = &subpass,
1987 .dependencyCount = 0,
1988 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001989 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001990 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001991
Chia-I Wu63636062015-10-26 21:10:41 +08001992 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001993 assert(!err);
1994}
1995
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001996//TODO: Merge shader reading
1997#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001998static VkShaderModule
1999demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08002000 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06002001 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002002 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002003
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06002004 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
2005 moduleCreateInfo.pNext = NULL;
2006
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002007 moduleCreateInfo.codeSize = size;
2008 moduleCreateInfo.pCode = code;
2009 moduleCreateInfo.flags = 0;
2010 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
2011 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08002012
2013 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002014}
2015
Karl Schultze4dc75c2016-02-02 15:37:51 -07002016char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002017 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06002018 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002019 void *shader_code;
2020
Bill Hollingsf4352142017-02-14 22:58:56 -05002021#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06002022 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05002023#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06002024
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002025 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07002026 if (!fp)
2027 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002028
2029 fseek(fp, 0L, SEEK_END);
2030 size = ftell(fp);
2031
2032 fseek(fp, 0L, SEEK_SET);
2033
2034 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06002035 retval = fread(shader_code, size, 1, fp);
2036 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002037
2038 *psize = size;
2039
Mike Stroyan9e9792a2015-10-28 11:15:46 -06002040 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002041 return shader_code;
2042}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002043#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002044
Karl Schultze4dc75c2016-02-02 15:37:51 -07002045static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002046#ifdef __ANDROID__
2047 VkShaderModuleCreateInfo sh_info = {};
2048 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
2049
2050#include "cube.vert.h"
2051 sh_info.codeSize = sizeof(cube_vert);
2052 sh_info.pCode = cube_vert;
2053 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
2054 assert(!err);
2055#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002056 void *vertShaderCode;
2057 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002058
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002059 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08002060 if (!vertShaderCode) {
2061 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
2062 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002063
Karl Schultze4dc75c2016-02-02 15:37:51 -07002064 demo->vert_shader_module =
2065 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002066
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002067 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002068#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002069
2070 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002071}
2072
Karl Schultze4dc75c2016-02-02 15:37:51 -07002073static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002074#ifdef __ANDROID__
2075 VkShaderModuleCreateInfo sh_info = {};
2076 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
2077
2078#include "cube.frag.h"
2079 sh_info.codeSize = sizeof(cube_frag);
2080 sh_info.pCode = cube_frag;
2081 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
2082 assert(!err);
2083#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002084 void *fragShaderCode;
2085 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002086
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002087 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08002088 if (!fragShaderCode) {
2089 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
2090 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002091
Karl Schultze4dc75c2016-02-02 15:37:51 -07002092 demo->frag_shader_module =
2093 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002094
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002095 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002096#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002097
2098 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002099}
2100
Karl Schultze4dc75c2016-02-02 15:37:51 -07002101static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002102 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002103 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002104 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002105 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002106 VkPipelineRasterizationStateCreateInfo rs;
2107 VkPipelineColorBlendStateCreateInfo cb;
2108 VkPipelineDepthStencilStateCreateInfo ds;
2109 VkPipelineViewportStateCreateInfo vp;
2110 VkPipelineMultisampleStateCreateInfo ms;
2111 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
2112 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06002113 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002114
Piers Daniellba839d12015-09-29 13:01:09 -06002115 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2116 memset(&dynamicState, 0, sizeof dynamicState);
2117 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2118 dynamicState.pDynamicStates = dynamicStateEnables;
2119
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002120 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002121 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002122 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002123
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002124 memset(&vi, 0, sizeof(vi));
2125 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2126
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002127 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002128 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002129 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002130
2131 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002132 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2133 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002134 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002135 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002136 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002137 rs.rasterizerDiscardEnable = VK_FALSE;
2138 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002139 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002140
2141 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002142 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2143 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002144 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002145 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002146 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002147 cb.attachmentCount = 1;
2148 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002149
Tony Barbour29645d02015-01-16 14:27:35 -07002150 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002151 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002152 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002153 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2154 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002155 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002156 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2157 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002158
2159 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002160 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002161 ds.depthTestEnable = VK_TRUE;
2162 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002163 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002164 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002165 ds.back.failOp = VK_STENCIL_OP_KEEP;
2166 ds.back.passOp = VK_STENCIL_OP_KEEP;
2167 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002168 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002169 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002170
Tony Barbour29645d02015-01-16 14:27:35 -07002171 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002172 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002173 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002174 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002175
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002176 // Two stages: vs and fs
2177 pipeline.stageCount = 2;
2178 VkPipelineShaderStageCreateInfo shaderStages[2];
2179 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2180
Karl Schultze4dc75c2016-02-02 15:37:51 -07002181 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2182 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002183 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002184 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002185
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2187 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002188 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002189 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002190
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002191 memset(&pipelineCache, 0, sizeof(pipelineCache));
2192 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002193
Karl Schultze4dc75c2016-02-02 15:37:51 -07002194 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
2195 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002196 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002197
Karl Schultze4dc75c2016-02-02 15:37:51 -07002198 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002199 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002200 pipeline.pRasterizationState = &rs;
2201 pipeline.pColorBlendState = &cb;
2202 pipeline.pMultisampleState = &ms;
2203 pipeline.pViewportState = &vp;
2204 pipeline.pDepthStencilState = &ds;
2205 pipeline.pStages = shaderStages;
2206 pipeline.renderPass = demo->render_pass;
2207 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002208
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002209 pipeline.renderPass = demo->render_pass;
2210
Karl Schultze4dc75c2016-02-02 15:37:51 -07002211 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
2212 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002213 assert(!err);
2214
Chia-I Wu63636062015-10-26 21:10:41 +08002215 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2216 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002217}
2218
Karl Schultze4dc75c2016-02-02 15:37:51 -07002219static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002220 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002221 [0] =
2222 {
2223 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07002224 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002225 },
2226 [1] =
2227 {
2228 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07002229 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002230 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002231 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002232 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002233 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002234 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002235 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002236 .poolSizeCount = 2,
2237 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002238 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002239 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002240
Karl Schultze4dc75c2016-02-02 15:37:51 -07002241 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
2242 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002243 assert(!err);
2244}
2245
Karl Schultze4dc75c2016-02-02 15:37:51 -07002246static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002247 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002248 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002249 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002250
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002251 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002252 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06002253 .pNext = NULL,
2254 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002255 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002256 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002257
2258 VkDescriptorBufferInfo buffer_info;
2259 buffer_info.offset = 0;
2260 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002261
Chia-I Wuae721ba2015-05-25 16:27:55 +08002262 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002263 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002264 tex_descs[i].sampler = demo->textures[i].sampler;
2265 tex_descs[i].imageView = demo->textures[i].view;
2266 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002267 }
2268
2269 memset(&writes, 0, sizeof(writes));
2270
2271 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002272 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002273 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002274 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002275
2276 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002277 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002278 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002279 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002280 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002281
Tony Barboura72d9492017-01-11 13:35:09 -07002282 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2283 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2284 assert(!err);
2285 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2286 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2287 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2288 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2289 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002290}
2291
Karl Schultze4dc75c2016-02-02 15:37:51 -07002292static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002293 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002294 attachments[1] = demo->depth.view;
2295
Chia-I Wuf973e322015-07-08 13:34:24 +08002296 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002297 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2298 .pNext = NULL,
2299 .renderPass = demo->render_pass,
2300 .attachmentCount = 2,
2301 .pAttachments = attachments,
2302 .width = demo->width,
2303 .height = demo->height,
2304 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002305 };
2306 VkResult U_ASSERT_ONLY err;
2307 uint32_t i;
2308
Tony Barbourd8e504f2015-10-08 14:26:24 -06002309 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002310 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002311 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002312 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002313 assert(!err);
2314 }
2315}
2316
Karl Schultze4dc75c2016-02-02 15:37:51 -07002317static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002318 VkResult U_ASSERT_ONLY err;
2319
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002320 const VkCommandPoolCreateInfo cmd_pool_info = {
2321 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06002322 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002323 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06002324 .flags = 0,
2325 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07002326 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
2327 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06002328 assert(!err);
2329
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002330 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002331 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002332 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002333 .commandPool = demo->cmd_pool,
2334 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002335 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002336 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002337 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2338 assert(!err);
2339 VkCommandBufferBeginInfo cmd_buf_info = {
2340 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2341 .pNext = NULL,
2342 .flags = 0,
2343 .pInheritanceInfo = NULL,
2344 };
2345 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2346 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002347
2348 demo_prepare_buffers(demo);
2349 demo_prepare_depth(demo);
2350 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002351 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002352
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002353 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002354 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002355 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002356
Ian Elliotta0781972015-08-21 15:09:33 -06002357 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002358 err =
Tony Barboura72d9492017-01-11 13:35:09 -07002359 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002360 assert(!err);
2361 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002362
Tony Barbour22e06272016-09-07 16:07:56 -06002363 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002364 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002365 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2366 .pNext = NULL,
2367 .queueFamilyIndex = demo->present_queue_family_index,
2368 .flags = 0,
2369 };
Karl Schultza2615462017-01-20 13:19:20 -07002370 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002371 &demo->present_cmd_pool);
2372 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002373 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002374 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2375 .pNext = NULL,
2376 .commandPool = demo->present_cmd_pool,
2377 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2378 .commandBufferCount = 1,
2379 };
2380 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2381 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002382 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002383 assert(!err);
2384 demo_build_image_ownership_cmd(demo, i);
2385 }
2386 }
2387
Chia-I Wu63ea9262015-03-26 13:14:16 +08002388 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002389 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002390
Chia-I Wuf973e322015-07-08 13:34:24 +08002391 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002392
Ian Elliotta0781972015-08-21 15:09:33 -06002393 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002394 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002395 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002396 }
2397
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002398 /*
2399 * Prepare functions above may generate pipeline commands
2400 * that need to be flushed before beginning the render loop.
2401 */
2402 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002403 if (demo->staging_texture.image) {
2404 demo_destroy_texture_image(demo, &demo->staging_texture);
2405 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002406
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002407 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002408 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002409}
2410
Karl Schultze4dc75c2016-02-02 15:37:51 -07002411static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002412 uint32_t i;
2413
2414 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002415 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002416
Tony Barbour66a56c32016-09-21 13:10:56 -06002417 // Wait for fences from present operations
2418 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002419 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002420 vkDestroyFence(demo->device, demo->fences[i], NULL);
2421 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2422 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2423 if (demo->separate_present_queue) {
2424 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2425 }
2426 }
2427
Tony Barbourd8e504f2015-10-08 14:26:24 -06002428 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002429 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002430 }
Chia-I Wu63636062015-10-26 21:10:41 +08002431 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002432
Chia-I Wu63636062015-10-26 21:10:41 +08002433 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2434 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2435 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2436 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2437 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002438
2439 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002440 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2441 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2442 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2443 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002444 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002445 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002446
Chia-I Wu63636062015-10-26 21:10:41 +08002447 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2448 vkDestroyImage(demo->device, demo->depth.image, NULL);
2449 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002450
Ian Elliotta0781972015-08-21 15:09:33 -06002451 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002452 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002453 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002454 &demo->swapchain_image_resources[i].cmd);
2455 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2456 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2457 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002458 }
Tony Barboura72d9492017-01-11 13:35:09 -07002459 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002460 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002461 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002462
Tony Barbour22e06272016-09-07 16:07:56 -06002463 if (demo->separate_present_queue) {
2464 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002465 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002466 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002467 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002468 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002469 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002470 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002471 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002472 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002473
Tony Barbour153cb062016-12-07 13:43:36 -07002474#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002475 XDestroyWindow(demo->display, demo->xlib_window);
2476 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002477#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002478 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002479 xcb_disconnect(demo->connection);
2480 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002481#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2482 wl_shell_surface_destroy(demo->shell_surface);
2483 wl_surface_destroy(demo->window);
2484 wl_shell_destroy(demo->shell);
2485 wl_compositor_destroy(demo->compositor);
2486 wl_registry_destroy(demo->registry);
2487 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002488#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002489#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002490}
2491
Karl Schultze4dc75c2016-02-02 15:37:51 -07002492static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002493 uint32_t i;
2494
Mike Stroyanbb60b382015-10-28 09:46:57 -06002495 // Don't react to resize until after first initialization.
2496 if (!demo->prepared) {
2497 return;
2498 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002499 // In order to properly resize the window, we must re-create the swapchain
2500 // AND redo the command buffers, etc.
2501 //
2502 // First, perform part of the demo_cleanup() function:
2503 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002504 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002505
2506 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002507 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002508 }
Chia-I Wu63636062015-10-26 21:10:41 +08002509 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002510
Chia-I Wu63636062015-10-26 21:10:41 +08002511 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2512 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2513 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2514 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2515 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002516
2517 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002518 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2519 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2520 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2521 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002522 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002523
Chia-I Wu63636062015-10-26 21:10:41 +08002524 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2525 vkDestroyImage(demo->device, demo->depth.image, NULL);
2526 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002527
Ian Elliottcf074d32015-10-16 18:02:43 -06002528 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002529 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002530 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002531 &demo->swapchain_image_resources[i].cmd);
2532 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2533 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2534 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002535 }
Chia-I Wu63636062015-10-26 21:10:41 +08002536 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002537 if (demo->separate_present_queue) {
2538 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2539 }
Tony Barboura72d9492017-01-11 13:35:09 -07002540 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002541
Ian Elliottcf074d32015-10-16 18:02:43 -06002542 // Second, re-perform the demo_prepare() function, which will re-create the
2543 // swapchain:
2544 demo_prepare(demo);
2545}
2546
David Pinedo2bb7c932015-06-18 17:03:14 -06002547// On MS-Windows, make this a global, so it's available to WndProc()
2548struct demo demo;
2549
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002550#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002551static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002552 if (!demo->prepared)
2553 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002554
Ian Elliott639ca472015-04-16 15:23:05 -06002555 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002556 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002557 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002558 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002559 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002560}
Ian Elliott639ca472015-04-16 15:23:05 -06002561
2562// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002563LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2564 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002565 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002566 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002567 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002568 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002569 // The validation callback calls MessageBox which can generate paint
2570 // events - don't make more Vulkan calls if we got here from the
2571 // callback
2572 if (!in_callback) {
2573 demo_run(&demo);
2574 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002575 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002576 case WM_GETMINMAXINFO: // set window's minimum size
2577 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2578 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002579 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002580 // Resize the application to the new window size, except when
2581 // it was minimized. Vulkan doesn't support images or swapchains
2582 // with width=0 and height=0.
2583 if (wParam != SIZE_MINIMIZED) {
2584 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002585 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002586 demo_resize(&demo);
2587 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002588 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002589 default:
2590 break;
2591 }
2592 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2593}
2594
Karl Schultze4dc75c2016-02-02 15:37:51 -07002595static void demo_create_window(struct demo *demo) {
2596 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002597
2598 // Initialize the window class structure:
2599 win_class.cbSize = sizeof(WNDCLASSEX);
2600 win_class.style = CS_HREDRAW | CS_VREDRAW;
2601 win_class.lpfnWndProc = WndProc;
2602 win_class.cbClsExtra = 0;
2603 win_class.cbWndExtra = 0;
2604 win_class.hInstance = demo->connection; // hInstance
2605 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2606 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2607 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2608 win_class.lpszMenuName = NULL;
2609 win_class.lpszClassName = demo->name;
2610 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2611 // Register window class:
2612 if (!RegisterClassEx(&win_class)) {
2613 // It didn't work, so try to give a useful error:
2614 printf("Unexpected error trying to start the application!\n");
2615 fflush(stdout);
2616 exit(1);
2617 }
2618 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002619 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002620 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002621 demo->window = CreateWindowEx(0,
2622 demo->name, // class name
2623 demo->name, // app name
2624 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002625 WS_VISIBLE | WS_SYSMENU,
2626 100, 100, // x/y coords
2627 wr.right - wr.left, // width
2628 wr.bottom - wr.top, // height
2629 NULL, // handle to parent
2630 NULL, // handle to menu
2631 demo->connection, // hInstance
2632 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002633 if (!demo->window) {
2634 // It didn't work, so try to give a useful error:
2635 printf("Cannot create a window in which to draw!\n");
2636 fflush(stdout);
2637 exit(1);
2638 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002639 // Window client area size must be at least 1 pixel high, to prevent crash.
2640 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2641 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002642}
Tony Barbour8295ac42016-08-08 11:04:17 -06002643#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002644static void demo_create_xlib_window(struct demo *demo) {
2645
Tony Barbouree9cd372017-01-31 16:09:58 -07002646 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002647 demo->display = XOpenDisplay(NULL);
2648 long visualMask = VisualScreenMask;
2649 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002650 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002651 vInfoTemplate.screen = DefaultScreen(demo->display);
2652 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2653 &vInfoTemplate, &numberOfVisuals);
2654
2655 Colormap colormap = XCreateColormap(
2656 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2657 visualInfo->visual, AllocNone);
2658
Rene Lindsay11612722016-06-13 17:20:39 -06002659 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002660 windowAttributes.colormap = colormap;
2661 windowAttributes.background_pixel = 0xFFFFFFFF;
2662 windowAttributes.border_pixel = 0;
2663 windowAttributes.event_mask =
2664 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2665
2666 demo->xlib_window = XCreateWindow(
2667 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2668 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2669 visualInfo->visual,
2670 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2671
2672 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2673 XMapWindow(demo->display, demo->xlib_window);
2674 XFlush(demo->display);
2675 demo->xlib_wm_delete_window =
2676 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2677}
2678static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2679 switch(event->type) {
2680 case ClientMessage:
2681 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2682 demo->quit = true;
2683 break;
2684 case KeyPress:
2685 switch (event->xkey.keycode) {
2686 case 0x9: // Escape
2687 demo->quit = true;
2688 break;
2689 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002690 demo->spin_angle -= demo->spin_increment;
2691 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002692 case 0x72: // right arrow key
2693 demo->spin_angle += demo->spin_increment;
2694 break;
2695 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002696 demo->pause = !demo->pause;
2697 break;
2698 }
2699 break;
2700 case ConfigureNotify:
2701 if ((demo->width != event->xconfigure.width) ||
2702 (demo->height != event->xconfigure.height)) {
2703 demo->width = event->xconfigure.width;
2704 demo->height = event->xconfigure.height;
2705 demo_resize(demo);
2706 }
2707 break;
2708 default:
2709 break;
2710 }
2711
2712}
2713
2714static void demo_run_xlib(struct demo *demo) {
2715
2716 while (!demo->quit) {
2717 XEvent event;
2718
2719 if (demo->pause) {
2720 XNextEvent(demo->display, &event);
2721 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002722 }
2723 while (XPending(demo->display) > 0) {
2724 XNextEvent(demo->display, &event);
2725 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002726 }
2727
Tony Barbour2593b182016-04-19 10:57:58 -06002728 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002729 demo->curFrame++;
2730 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2731 demo->quit = true;
2732 }
2733}
Tony Barbour153cb062016-12-07 13:43:36 -07002734#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002735static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002736 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002737 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002738 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002739 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002740 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002741 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002742 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002743 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2744 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002745 demo->quit = true;
2746 }
2747 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002748 case XCB_KEY_RELEASE: {
2749 const xcb_key_release_event_t *key =
2750 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002751
Karl Schultze4dc75c2016-02-02 15:37:51 -07002752 switch (key->detail) {
2753 case 0x9: // Escape
2754 demo->quit = true;
2755 break;
2756 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002757 demo->spin_angle -= demo->spin_increment;
2758 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002759 case 0x72: // right arrow key
2760 demo->spin_angle += demo->spin_increment;
2761 break;
2762 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002763 demo->pause = !demo->pause;
2764 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002765 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002766 } break;
2767 case XCB_CONFIGURE_NOTIFY: {
2768 const xcb_configure_notify_event_t *cfg =
2769 (const xcb_configure_notify_event_t *)event;
2770 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002771 demo->width = cfg->width;
2772 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002773 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002774 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002775 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002776 default:
2777 break;
2778 }
2779}
2780
Tony Barbour2593b182016-04-19 10:57:58 -06002781static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002782 xcb_flush(demo->connection);
2783
2784 while (!demo->quit) {
2785 xcb_generic_event_t *event;
2786
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002787 if (demo->pause) {
2788 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002789 }
2790 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002791 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002792 }
2793 while (event) {
2794 demo_handle_xcb_event(demo, event);
2795 free(event);
2796 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002797 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002798
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002799 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002800 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002801 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002802 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002803 }
2804}
2805
Tony Barbour2593b182016-04-19 10:57:58 -06002806static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002807 uint32_t value_mask, value_list[32];
2808
Tony Barbour2593b182016-04-19 10:57:58 -06002809 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002810
2811 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2812 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002813 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002814 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002815
Tony Barbour2593b182016-04-19 10:57:58 -06002816 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002817 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2818 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2819 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002820
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002821 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002822 xcb_intern_atom_cookie_t cookie =
2823 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2824 xcb_intern_atom_reply_t *reply =
2825 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002826
Karl Schultze4dc75c2016-02-02 15:37:51 -07002827 xcb_intern_atom_cookie_t cookie2 =
2828 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2829 demo->atom_wm_delete_window =
2830 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002831
Tony Barbour2593b182016-04-19 10:57:58 -06002832 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002833 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002834 &(*demo->atom_wm_delete_window).atom);
2835 free(reply);
2836
Tony Barbour2593b182016-04-19 10:57:58 -06002837 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002838
Karl Schultze4dc75c2016-02-02 15:37:51 -07002839 // Force the x/y coordinates to 100,100 results are identical in consecutive
2840 // runs
2841 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002842 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002843 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002844}
Tony Barbour6b131662016-08-25 13:14:45 -06002845// VK_USE_PLATFORM_XCB_KHR
2846#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002847static void demo_run(struct demo *demo) {
2848 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002849 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002850 demo->curFrame++;
2851 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2852 demo->quit = true;
2853 }
2854}
2855
2856static void handle_ping(void *data UNUSED,
2857 struct wl_shell_surface *shell_surface,
2858 uint32_t serial) {
2859 wl_shell_surface_pong(shell_surface, serial);
2860}
2861
2862static void handle_configure(void *data UNUSED,
2863 struct wl_shell_surface *shell_surface UNUSED,
2864 uint32_t edges UNUSED, int32_t width UNUSED,
2865 int32_t height UNUSED) {}
2866
2867static void handle_popup_done(void *data UNUSED,
2868 struct wl_shell_surface *shell_surface UNUSED) {}
2869
2870static const struct wl_shell_surface_listener shell_surface_listener = {
2871 handle_ping, handle_configure, handle_popup_done};
2872
2873static void demo_create_window(struct demo *demo) {
2874 demo->window = wl_compositor_create_surface(demo->compositor);
2875 if (!demo->window) {
2876 printf("Can not create wayland_surface from compositor!\n");
2877 fflush(stdout);
2878 exit(1);
2879 }
2880
2881 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2882 if (!demo->shell_surface) {
2883 printf("Can not get shell_surface from wayland_surface!\n");
2884 fflush(stdout);
2885 exit(1);
2886 }
2887 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2888 demo);
2889 wl_shell_surface_set_toplevel(demo->shell_surface);
2890 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2891}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002892#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2893static void demo_run(struct demo *demo) {
2894 if (!demo->prepared)
2895 return;
2896
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002897 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002898 demo->curFrame++;
2899}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002900#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002901#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2902static VkResult demo_create_display_surface(struct demo *demo) {
2903 VkResult U_ASSERT_ONLY err;
2904 uint32_t display_count;
2905 uint32_t mode_count;
2906 uint32_t plane_count;
2907 VkDisplayPropertiesKHR display_props;
2908 VkDisplayKHR display;
2909 VkDisplayModePropertiesKHR mode_props;
2910 VkDisplayPlanePropertiesKHR *plane_props;
2911 VkBool32 found_plane = VK_FALSE;
2912 uint32_t plane_index;
2913 VkExtent2D image_extent;
2914 VkDisplaySurfaceCreateInfoKHR create_info;
2915
2916 // Get the first display
2917 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2918 assert(!err);
2919
2920 if (display_count == 0) {
2921 printf("Cannot find any display!\n");
2922 fflush(stdout);
2923 exit(1);
2924 }
2925
2926 display_count = 1;
2927 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2928 assert(!err || (err == VK_INCOMPLETE));
2929
2930 display = display_props.display;
2931
2932 // Get the first mode of the display
2933 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2934 assert(!err);
2935
2936 if (mode_count == 0) {
2937 printf("Cannot find any mode for the display!\n");
2938 fflush(stdout);
2939 exit(1);
2940 }
2941
2942 mode_count = 1;
2943 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2944 assert(!err || (err == VK_INCOMPLETE));
2945
2946 // Get the list of planes
2947 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2948 assert(!err);
2949
2950 if (plane_count == 0) {
2951 printf("Cannot find any plane!\n");
2952 fflush(stdout);
2953 exit(1);
2954 }
2955
2956 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2957 assert(plane_props);
2958
2959 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2960 assert(!err);
2961
2962 // Find a plane compatible with the display
2963 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2964 uint32_t supported_count;
2965 VkDisplayKHR *supported_displays;
2966
2967 // Disqualify planes that are bound to a different display
2968 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2969 (plane_props[plane_index].currentDisplay != display)) {
2970 continue;
2971 }
2972
2973 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2974 assert(!err);
2975
2976 if (supported_count == 0) {
2977 continue;
2978 }
2979
2980 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2981 assert(supported_displays);
2982
2983 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2984 assert(!err);
2985
2986 for (uint32_t i = 0; i < supported_count; i++) {
2987 if (supported_displays[i] == display) {
2988 found_plane = VK_TRUE;
2989 break;
2990 }
2991 }
2992
2993 free(supported_displays);
2994
2995 if (found_plane) {
2996 break;
2997 }
2998 }
2999
3000 if (!found_plane) {
3001 printf("Cannot find a plane compatible with the display!\n");
3002 fflush(stdout);
3003 exit(1);
3004 }
3005
3006 free(plane_props);
3007
Tony Barbour820b55b2017-03-14 08:55:46 -06003008 VkDisplayPlaneCapabilitiesKHR planeCaps;
3009 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
3010 // Find a supported alpha mode
3011 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
3012 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
3013 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
3014 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
3015 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
3016 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
3017 };
3018 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
3019 if (planeCaps.supportedAlpha & alphaModes[i]) {
3020 alphaMode = alphaModes[i];
3021 break;
3022 }
3023 }
Damien Leone600c3052017-01-31 10:26:07 -07003024 image_extent.width = mode_props.parameters.visibleRegion.width;
3025 image_extent.height = mode_props.parameters.visibleRegion.height;
3026
3027 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
3028 create_info.pNext = NULL;
3029 create_info.flags = 0;
3030 create_info.displayMode = mode_props.displayMode;
3031 create_info.planeIndex = plane_index;
3032 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
3033 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06003034 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07003035 create_info.globalAlpha = 1.0f;
3036 create_info.imageExtent = image_extent;
3037
3038 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
3039}
3040
3041static void demo_run_display(struct demo *demo)
3042{
3043 while (!demo->quit) {
3044 demo_draw(demo);
3045 demo->curFrame++;
3046
3047 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
3048 demo->quit = true;
3049 }
3050 }
3051}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003052#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003054/*
3055 * Return 1 (true) if all layer names specified in check_names
3056 * can be found in given layer properties.
3057 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003058static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003059 uint32_t layer_count,
3060 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003061 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003062 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003063 for (uint32_t j = 0; j < layer_count; j++) {
3064 if (!strcmp(check_names[i], layers[j].layerName)) {
3065 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07003066 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003067 }
3068 }
3069 if (!found) {
3070 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
3071 return 0;
3072 }
3073 }
3074 return 1;
3075}
3076
Karl Schultze4dc75c2016-02-02 15:37:51 -07003077static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003078 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003079 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003080 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06003081 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003082 char **instance_validation_layers = NULL;
3083 demo->enabled_extension_count = 0;
3084 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003085
Karl Schultz7c50ad62016-04-01 12:07:03 -06003086 char *instance_validation_layers_alt1[] = {
3087 "VK_LAYER_LUNARG_standard_validation"
3088 };
3089
3090 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07003091 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
3092 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
3093 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003094 };
3095
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003096 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003097 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003098 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003099
Karl Schultz7c50ad62016-04-01 12:07:03 -06003100 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003101 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003102
Karl Schultz7c50ad62016-04-01 12:07:03 -06003103 instance_validation_layers = instance_validation_layers_alt1;
3104 if (instance_layer_count > 0) {
3105 VkLayerProperties *instance_layers =
3106 malloc(sizeof (VkLayerProperties) * instance_layer_count);
3107 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
3108 instance_layers);
3109 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003110
Karl Schultz7c50ad62016-04-01 12:07:03 -06003111
3112 validation_found = demo_check_layers(
3113 ARRAY_SIZE(instance_validation_layers_alt1),
3114 instance_validation_layers, instance_layer_count,
3115 instance_layers);
3116 if (validation_found) {
3117 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003118 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
3119 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003120 } else {
3121 // use alternative set of validation layers
3122 instance_validation_layers = instance_validation_layers_alt2;
3123 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
3124 validation_found = demo_check_layers(
3125 ARRAY_SIZE(instance_validation_layers_alt2),
3126 instance_validation_layers, instance_layer_count,
3127 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003128 validation_layer_count =
3129 ARRAY_SIZE(instance_validation_layers_alt2);
3130 for (uint32_t i = 0; i < validation_layer_count; i++) {
3131 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06003132 }
3133 }
3134 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003135 }
Dustin Graves7c287352016-01-27 13:48:06 -07003136
Karl Schultz7c50ad62016-04-01 12:07:03 -06003137 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06003138 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06003139 "required validation layer.\n\n"
3140 "Please look at the Getting Started guide for additional "
3141 "information.\n",
3142 "vkCreateInstance Failure");
3143 }
Dustin Graves7c287352016-01-27 13:48:06 -07003144 }
3145
3146 /* Look for instance extensions */
3147 VkBool32 surfaceExtFound = 0;
3148 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003149 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003150
Karl Schultze4dc75c2016-02-02 15:37:51 -07003151 err = vkEnumerateInstanceExtensionProperties(
3152 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003153 assert(!err);
3154
Dustin Graves7c287352016-01-27 13:48:06 -07003155 if (instance_extension_count > 0) {
3156 VkExtensionProperties *instance_extensions =
3157 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3158 err = vkEnumerateInstanceExtensionProperties(
3159 NULL, &instance_extension_count, instance_extensions);
3160 assert(!err);
3161 for (uint32_t i = 0; i < instance_extension_count; i++) {
3162 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
3163 instance_extensions[i].extensionName)) {
3164 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003165 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003166 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003167 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003168#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003169 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
3170 instance_extensions[i].extensionName)) {
3171 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003172 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003173 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
3174 }
Tony Barbour153cb062016-12-07 13:43:36 -07003175#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003176 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
3177 instance_extensions[i].extensionName)) {
3178 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06003179 demo->extension_names[demo->enabled_extension_count++] =
3180 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
3181 }
Tony Barbour153cb062016-12-07 13:43:36 -07003182#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003183 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
3184 instance_extensions[i].extensionName)) {
3185 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003186 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003187 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
3188 }
Tony Barbour153cb062016-12-07 13:43:36 -07003189#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003190 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
3191 instance_extensions[i].extensionName)) {
3192 platformSurfaceExtFound = 1;
3193 demo->extension_names[demo->enabled_extension_count++] =
3194 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
3195 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003196#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003197#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3198 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
3199 instance_extensions[i].extensionName)) {
3200 platformSurfaceExtFound = 1;
3201 demo->extension_names[demo->enabled_extension_count++] =
3202 VK_KHR_DISPLAY_EXTENSION_NAME;
3203 }
Tony Barbour153cb062016-12-07 13:43:36 -07003204#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003205 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
3206 instance_extensions[i].extensionName)) {
3207 platformSurfaceExtFound = 1;
3208 demo->extension_names[demo->enabled_extension_count++] =
3209 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
3210 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003211#elif defined(VK_USE_PLATFORM_IOS_MVK)
3212 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3213 platformSurfaceExtFound = 1;
3214 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3215 }
3216#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3217 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3218 platformSurfaceExtFound = 1;
3219 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3220 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003221#endif
Dustin Graves7c287352016-01-27 13:48:06 -07003222 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
3223 instance_extensions[i].extensionName)) {
3224 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003225 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003226 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
3227 }
3228 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003229 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003230 }
Dustin Graves7c287352016-01-27 13:48:06 -07003231
3232 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003233 }
Dustin Graves7c287352016-01-27 13:48:06 -07003234
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003235 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003236 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3237 "the " VK_KHR_SURFACE_EXTENSION_NAME
3238 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06003239 "Vulkan installable client driver (ICD) installed?\nPlease "
3240 "look at the Getting Started guide for additional "
3241 "information.\n",
3242 "vkCreateInstance Failure");
3243 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003244 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003245#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003246 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3247 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3248 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003249 "Vulkan installable client driver (ICD) installed?\nPlease "
3250 "look at the Getting Started guide for additional "
3251 "information.\n",
3252 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003253#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003254 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3255 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3256 "Vulkan installable client driver (ICD) installed?\nPlease "
3257 "look at the Getting Started guide for additional "
3258 "information.\n",
3259 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003260#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003261 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3262 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3263 "Vulkan installable client driver (ICD) installed?\nPlease "
3264 "look at the Getting Started guide for additional "
3265 "information.\n",
3266 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003267#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003268 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3269 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3270 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003271 "Vulkan installable client driver (ICD) installed?\nPlease "
3272 "look at the Getting Started guide for additional "
3273 "information.\n",
3274 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003275#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3276 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3277 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3278 " extension.\n\nDo you have a compatible "
3279 "Vulkan installable client driver (ICD) installed?\nPlease "
3280 "look at the Getting Started guide for additional "
3281 "information.\n",
3282 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003283#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003284#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3285 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3286 "the " VK_KHR_DISPLAY_EXTENSION_NAME
3287 " extension.\n\nDo you have a compatible "
3288 "Vulkan installable client driver (ICD) installed?\nPlease "
3289 "look at the Getting Started guide for additional "
3290 "information.\n",
3291 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003292#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3293 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3294 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3295 " extension.\n\nDo you have a compatible "
3296 "Vulkan installable client driver (ICD) installed?\nPlease "
3297 "look at the Getting Started guide for additional "
3298 "information.\n",
3299 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003300#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003301 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3302 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3303 " extension.\n\nDo you have a compatible "
3304 "Vulkan installable client driver (ICD) installed?\nPlease "
3305 "look at the Getting Started guide for additional "
3306 "information.\n",
3307 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003308#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003309 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003310 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003311 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003312 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003313 .pApplicationName = APP_SHORT_NAME,
3314 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003315 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003316 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003317 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003318 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003319 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003320 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003321 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003322 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003323 .enabledLayerCount = demo->enabled_layer_count,
3324 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3325 .enabledExtensionCount = demo->enabled_extension_count,
3326 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003327 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003328
3329 /*
3330 * This is info for a temp callback to use during CreateInstance.
3331 * After the instance is created, we use the instance-based
3332 * function to register the final callback.
3333 */
Karl Schultza2615462017-01-20 13:19:20 -07003334 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003335 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003336 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07003337 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
3338 dbgCreateInfoTemp.pNext = NULL;
3339 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
3340 dbgCreateInfoTemp.pUserData = demo;
3341 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06003342 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003343 if (demo->validate_checks_disabled) {
3344 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
3345 val_flags.pNext = NULL;
3346 val_flags.disabledValidationCheckCount = 1;
3347 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3348 val_flags.pDisabledValidationChecks = &disabled_check;
3349 dbgCreateInfoTemp.pNext = (void*)&val_flags;
3350 }
Karl Schultza2615462017-01-20 13:19:20 -07003351 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003352 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003353
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003354 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003355
Chia-I Wu63636062015-10-26 21:10:41 +08003356 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003357 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
3358 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06003359 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06003360 "additional information.\n",
3361 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003362 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003363 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07003364 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003365 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003366 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06003367 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3368 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003369 "the Getting Started guide for additional information.\n",
3370 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003371 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003372
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003373 /* Make initial call to query gpu_count, then second call for gpu info*/
3374 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3375 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003376
3377 if (gpu_count > 0) {
3378 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3379 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3380 assert(!err);
3381 /* For cube demo we just grab the first physical device */
3382 demo->gpu = physical_devices[0];
3383 free(physical_devices);
3384 } else {
3385 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3386 "Do you have a compatible Vulkan installable client driver (ICD) "
3387 "installed?\nPlease look at the Getting Started guide for "
3388 "additional information.\n",
3389 "vkEnumeratePhysicalDevices Failure");
3390 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003391
Dustin Graves7c287352016-01-27 13:48:06 -07003392 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003393 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003394 VkBool32 swapchainExtFound = 0;
3395 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003396 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003397
Karl Schultze4dc75c2016-02-02 15:37:51 -07003398 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3399 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003400 assert(!err);
3401
Dustin Graves7c287352016-01-27 13:48:06 -07003402 if (device_extension_count > 0) {
3403 VkExtensionProperties *device_extensions =
3404 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3405 err = vkEnumerateDeviceExtensionProperties(
3406 demo->gpu, NULL, &device_extension_count, device_extensions);
3407 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003408
Dustin Graves7c287352016-01-27 13:48:06 -07003409 for (uint32_t i = 0; i < device_extension_count; i++) {
3410 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3411 device_extensions[i].extensionName)) {
3412 swapchainExtFound = 1;
3413 demo->extension_names[demo->enabled_extension_count++] =
3414 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3415 }
3416 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003417 }
Dustin Graves7c287352016-01-27 13:48:06 -07003418
Ian Elliottd940ca22017-03-22 08:31:27 -06003419 if (demo->VK_GOOGLE_display_timing_enabled) {
3420 // Even though the user "enabled" the extension via the command
3421 // line, we must make sure that it's enumerated for use with the
3422 // device. Therefore, disable it here, and re-enable it again if
3423 // enumerated.
3424 demo->VK_GOOGLE_display_timing_enabled = false;
3425 for (uint32_t i = 0; i < device_extension_count; i++) {
3426 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
3427 device_extensions[i].extensionName)) {
3428 demo->extension_names[demo->enabled_extension_count++] =
3429 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
3430 demo->VK_GOOGLE_display_timing_enabled = true;
3431 DbgMsg("VK_GOOGLE_display_timing enabled\n");
3432 }
3433 assert(demo->enabled_extension_count < 64);
3434 }
3435 if (!demo->VK_GOOGLE_display_timing_enabled) {
3436 DbgMsg("VK_GOOGLE_display_timing NOT ENABLED\n");
3437 }
3438 }
3439
Dustin Graves7c287352016-01-27 13:48:06 -07003440 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003441 }
Dustin Graves7c287352016-01-27 13:48:06 -07003442
Tony Barbourcc69f452015-10-08 13:59:42 -06003443 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003444 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3445 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3446 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003447 "Vulkan installable client driver (ICD) installed?\nPlease "
3448 "look at the Getting Started guide for additional "
3449 "information.\n",
3450 "vkCreateInstance Failure");
3451 }
3452
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003453 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003454 demo->CreateDebugReportCallback =
3455 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3456 demo->inst, "vkCreateDebugReportCallbackEXT");
3457 demo->DestroyDebugReportCallback =
3458 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3459 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003460 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003461 ERR_EXIT(
3462 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3463 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003464 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003465 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003466 ERR_EXIT(
3467 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3468 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003469 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003470 demo->DebugReportMessage =
3471 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3472 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003473 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003474 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003475 "vkGetProcAddr Failure");
3476 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003477
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003478 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003479 PFN_vkDebugReportCallbackEXT callback;
3480 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003481 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003482 dbgCreateInfo.pNext = NULL;
3483 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003484 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003485 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003486 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003487 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3488 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003489 switch (err) {
3490 case VK_SUCCESS:
3491 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003492 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003493 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3494 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003495 break;
3496 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003497 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3498 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003499 break;
3500 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003501 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003502 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003503
3504 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003505 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3506 &demo->queue_family_count, NULL);
3507 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003508
Karl Schultze4dc75c2016-02-02 15:37:51 -07003509 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003510 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3511 vkGetPhysicalDeviceQueueFamilyProperties(
3512 demo->gpu, &demo->queue_family_count, demo->queue_props);
3513
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003514 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003515 // If app has specific feature requirements it should check supported
3516 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003517 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003518 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003519
Tony Barbourda2bad52016-01-22 14:36:40 -07003520 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3521 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3522 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3523 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3524 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3525}
3526
Karl Schultze4dc75c2016-02-02 15:37:51 -07003527static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003528 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003529 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003530 VkDeviceQueueCreateInfo queues[2];
3531 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3532 queues[0].pNext = NULL;
3533 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3534 queues[0].queueCount = 1;
3535 queues[0].pQueuePriorities = queue_priorities;
3536 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003537
3538 VkDeviceCreateInfo device = {
3539 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3540 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003541 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003542 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003543 .enabledLayerCount = 0,
3544 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003545 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003546 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3547 .pEnabledFeatures =
3548 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003549 };
Tony Barbour22e06272016-09-07 16:07:56 -06003550 if (demo->separate_present_queue) {
3551 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3552 queues[1].pNext = NULL;
3553 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3554 queues[1].queueCount = 1;
3555 queues[1].pQueuePriorities = queue_priorities;
3556 queues[1].flags = 0;
3557 device.queueCreateInfoCount = 2;
3558 }
Chia-I Wu63636062015-10-26 21:10:41 +08003559 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003560 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003561}
3562
Karl Schultze4dc75c2016-02-02 15:37:51 -07003563static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003564 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003565
Karl Schultze4dc75c2016-02-02 15:37:51 -07003566// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003567#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003568 VkWin32SurfaceCreateInfoKHR createInfo;
3569 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3570 createInfo.pNext = NULL;
3571 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003572 createInfo.hinstance = demo->connection;
3573 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003574
Karl Schultze4dc75c2016-02-02 15:37:51 -07003575 err =
3576 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003577#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003578 VkWaylandSurfaceCreateInfoKHR createInfo;
3579 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3580 createInfo.pNext = NULL;
3581 createInfo.flags = 0;
3582 createInfo.display = demo->display;
3583 createInfo.surface = demo->window;
3584
3585 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3586 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003587#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003588#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3589 VkAndroidSurfaceCreateInfoKHR createInfo;
3590 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3591 createInfo.pNext = NULL;
3592 createInfo.flags = 0;
3593 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003594
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003595 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003596#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3597 VkXlibSurfaceCreateInfoKHR createInfo;
3598 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3599 createInfo.pNext = NULL;
3600 createInfo.flags = 0;
3601 createInfo.dpy = demo->display;
3602 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003603
Tony Barbour153cb062016-12-07 13:43:36 -07003604 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003605 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003606#elif defined(VK_USE_PLATFORM_XCB_KHR)
3607 VkXcbSurfaceCreateInfoKHR createInfo;
3608 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3609 createInfo.pNext = NULL;
3610 createInfo.flags = 0;
3611 createInfo.connection = demo->connection;
3612 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003613
Tony Barbour153cb062016-12-07 13:43:36 -07003614 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003615#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3616 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003617#elif defined(VK_USE_PLATFORM_IOS_MVK)
3618 VkIOSSurfaceCreateInfoMVK surface;
3619 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3620 surface.pNext = NULL;
3621 surface.flags = 0;
3622 surface.pView = demo->window;
3623
3624 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3625#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3626 VkMacOSSurfaceCreateInfoMVK surface;
3627 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3628 surface.pNext = NULL;
3629 surface.flags = 0;
3630 surface.pView = demo->window;
3631
3632 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003633#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003634 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003635
Tony Barbourcc69f452015-10-08 13:59:42 -06003636 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003637 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003638 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003639 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003640 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003641 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003642 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003643
3644 // Search for a graphics and a present queue in the array of queue
3645 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003646 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3647 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003648 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003649 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3650 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3651 graphicsQueueFamilyIndex = i;
3652 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003653
Tony Barbourab2a0362016-09-06 11:40:12 -06003654 if (supportsPresent[i] == VK_TRUE) {
3655 graphicsQueueFamilyIndex = i;
3656 presentQueueFamilyIndex = i;
3657 break;
3658 }
3659 }
3660 }
3661
3662 if (presentQueueFamilyIndex == UINT32_MAX) {
3663 // If didn't find a queue that supports both graphics and present, then
3664 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003665 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003666 if (supportsPresent[i] == VK_TRUE) {
3667 presentQueueFamilyIndex = i;
3668 break;
3669 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003670 }
3671 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003672
3673 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003674 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3675 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003676 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003677 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003678 }
3679
Tony Barbourab2a0362016-09-06 11:40:12 -06003680 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3681 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003682 demo->separate_present_queue =
3683 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003684 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003685
Tony Barbourda2bad52016-01-22 14:36:40 -07003686 demo_create_device(demo);
3687
3688 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3689 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3690 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3691 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3692 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003693 if (demo->VK_GOOGLE_display_timing_enabled) {
3694 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3695 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3696 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003697
Tony Barboura2a67812016-07-18 13:21:06 -06003698 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3699 &demo->graphics_queue);
3700
Tony Barbour22e06272016-09-07 16:07:56 -06003701 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003702 demo->present_queue = demo->graphics_queue;
3703 } else {
3704 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3705 &demo->present_queue);
3706 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003707
Ian Elliottaaae5352015-07-06 14:27:58 -06003708 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003709 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003710 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003711 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003712 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003713 VkSurfaceFormatKHR *surfFormats =
3714 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003715 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003716 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003717 assert(!err);
3718 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3719 // the surface has no preferred format. Otherwise, at least one
3720 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003721 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003722 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003723 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003724 assert(formatCount >= 1);
3725 demo->format = surfFormats[0].format;
3726 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003727 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003728
David Pinedo2bb7c932015-06-18 17:03:14 -06003729 demo->quit = false;
3730 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003731
Tony Barbource7524e2016-07-28 12:01:45 -06003732 // Create semaphores to synchronize acquiring presentable buffers before
3733 // rendering and waiting for drawing to be complete before presenting
3734 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3735 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3736 .pNext = NULL,
3737 .flags = 0,
3738 };
Tony Barbource7524e2016-07-28 12:01:45 -06003739
Tony Barbour66a56c32016-09-21 13:10:56 -06003740 // Create fences that we can use to throttle if we get too far
3741 // ahead of the image presents
3742 VkFenceCreateInfo fence_ci = {
3743 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3744 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003745 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003746 };
3747 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003748 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3749 assert(!err);
3750
Tony Barbour22e06272016-09-07 16:07:56 -06003751 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003752 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003753 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003754
3755 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3756 &demo->draw_complete_semaphores[i]);
3757 assert(!err);
3758
3759 if (demo->separate_present_queue) {
3760 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3761 &demo->image_ownership_semaphores[i]);
3762 assert(!err);
3763 }
Tony Barbour22e06272016-09-07 16:07:56 -06003764 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003765 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003766
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003767 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003768 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003769}
3770
Tony Barbour153cb062016-12-07 13:43:36 -07003771#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003772static void registry_handle_global(void *data, struct wl_registry *registry,
3773 uint32_t name, const char *interface,
3774 uint32_t version UNUSED) {
3775 struct demo *demo = data;
3776 if (strcmp(interface, "wl_compositor") == 0) {
3777 demo->compositor =
3778 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3779 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3780 * tp xdg_shell */
3781 } else if (strcmp(interface, "wl_shell") == 0) {
3782 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3783 }
3784}
3785
3786static void registry_handle_global_remove(void *data UNUSED,
3787 struct wl_registry *registry UNUSED,
3788 uint32_t name UNUSED) {}
3789
3790static const struct wl_registry_listener registry_listener = {
3791 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003792#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003793#endif
3794
Karl Schultze4dc75c2016-02-02 15:37:51 -07003795static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003796#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003797 const xcb_setup_t *setup;
3798 xcb_screen_iterator_t iter;
3799 int scr;
3800
3801 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003802 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003803 printf("Cannot find a compatible Vulkan installable client driver "
3804 "(ICD).\nExiting ...\n");
3805 fflush(stdout);
3806 exit(1);
3807 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003808
3809 setup = xcb_get_setup(demo->connection);
3810 iter = xcb_setup_roots_iterator(setup);
3811 while (scr-- > 0)
3812 xcb_screen_next(&iter);
3813
3814 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003815#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3816 demo->display = wl_display_connect(NULL);
3817
3818 if (demo->display == NULL) {
3819 printf("Cannot find a compatible Vulkan installable client driver "
3820 "(ICD).\nExiting ...\n");
3821 fflush(stdout);
3822 exit(1);
3823 }
3824
3825 demo->registry = wl_display_get_registry(demo->display);
3826 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3827 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003828#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003829#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003830}
3831
Karl Schultze4dc75c2016-02-02 15:37:51 -07003832static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003833 vec3 eye = {0.0f, 3.0f, 5.0f};
3834 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003835 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003836
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003837 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003838 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003839 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003840
Piers Daniell735ee532015-02-23 16:23:13 -07003841 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003842 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003843 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003844 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003845 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003846 if ((strcmp(argv[i], "--present_mode") == 0) &&
3847 (i < argc - 1)) {
3848 demo->presentMode = atoi(argv[i+1]);
3849 i++;
3850 continue;
3851 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003852 if (strcmp(argv[i], "--break") == 0) {
3853 demo->use_break = true;
3854 continue;
3855 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003856 if (strcmp(argv[i], "--validate") == 0) {
3857 demo->validate = true;
3858 continue;
3859 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003860 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3861 demo->validate = true;
3862 demo->validate_checks_disabled = true;
3863 continue;
3864 }
Tony Barbour2593b182016-04-19 10:57:58 -06003865 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003866 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003867 continue;
3868 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003869 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3870 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3871 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003872 i++;
3873 continue;
3874 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003875 if (strcmp(argv[i], "--suppress_popups") == 0) {
3876 demo->suppress_popups = true;
3877 continue;
3878 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003879 if (strcmp(argv[i], "--display_timing") == 0) {
3880 demo->VK_GOOGLE_display_timing_enabled = true;
3881 continue;
3882 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003883
Cody Northropaf28a532016-09-27 10:50:57 -06003884#if defined(ANDROID)
3885 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3886#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003887 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Ian Elliottd940ca22017-03-22 08:31:27 -06003888 "[--c <framecount>] [--suppress_popups] [--display_timing] [--present_mode <present mode enum>]\n"
Tony Barbour291d57b2016-10-21 14:47:07 -06003889 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3890 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3891 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3892 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3893 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3894 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003895 fflush(stderr);
3896 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003897#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003898 }
3899
Tony Barbour153cb062016-12-07 13:43:36 -07003900 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003901
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003902 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003903
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003904 demo->width = 500;
3905 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003906
Tony Barbour66a56c32016-09-21 13:10:56 -06003907 demo->spin_angle = 4.0f;
3908 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003909 demo->pause = false;
3910
Karl Schultze4dc75c2016-02-02 15:37:51 -07003911 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3912 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003913 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3914 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003915
3916 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003917}
3918
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003919#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003920// Include header required for parsing the command line options.
3921#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003922
Karl Schultze4dc75c2016-02-02 15:37:51 -07003923int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3924 int nCmdShow) {
3925 MSG msg; // message
3926 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003927 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003928 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003929
Jamie Madillb2ff6502017-03-15 16:17:46 -04003930 // Ensure wParam is initialized.
3931 msg.wParam = 0;
3932
Karl Schultze4dc75c2016-02-02 15:37:51 -07003933 // Use the CommandLine functions to get the command line arguments.
3934 // Unfortunately, Microsoft outputs
3935 // this information as wide characters for Unicode, and we simply want the
3936 // Ascii version to be compatible
3937 // with the non-Windows side. So, we have to convert the information to
3938 // Ascii character strings.
3939 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3940 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003941 argc = 0;
3942 }
3943
Karl Schultze4dc75c2016-02-02 15:37:51 -07003944 if (argc > 0) {
3945 argv = (char **)malloc(sizeof(char *) * argc);
3946 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003947 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003948 } else {
3949 for (int iii = 0; iii < argc; iii++) {
3950 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003951 size_t numConverted = 0;
3952
Karl Schultze4dc75c2016-02-02 15:37:51 -07003953 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3954 if (argv[iii] != NULL) {
3955 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3956 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003957 }
3958 }
3959 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003960 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003961 argv = NULL;
3962 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003963
3964 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003965
3966 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003967 if (argc > 0 && argv != NULL) {
3968 for (int iii = 0; iii < argc; iii++) {
3969 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003970 free(argv[iii]);
3971 }
3972 }
3973 free(argv);
3974 }
3975
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003976 demo.connection = hInstance;
3977 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003978 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003979 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003980
3981 demo_prepare(&demo);
3982
Karl Schultze4dc75c2016-02-02 15:37:51 -07003983 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003984
3985 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003986 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003987 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003988 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003989 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003990 done = true; // if found, quit app
3991 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003992 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003993 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003994 DispatchMessage(&msg);
3995 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003996 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003997 }
3998
3999 demo_cleanup(&demo);
4000
Karl Schultze4dc75c2016-02-02 15:37:51 -07004001 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06004002}
Bill Hollingsf4352142017-02-14 22:58:56 -05004003
4004#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
4005static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06004006 const char* argv[] = { "CubeSample" };
4007 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05004008
Tony Barbourc65cd752017-03-13 15:29:35 -06004009 demo_init(demo, argc, (char**)argv);
4010 demo->window = view;
4011 demo_init_vk_swapchain(demo);
4012 demo_prepare(demo);
4013 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05004014}
4015
4016static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06004017 // Wait for work to finish before updating MVP.
4018 vkDeviceWaitIdle(demo->device);
4019 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05004020
Tony Barbourc65cd752017-03-13 15:29:35 -06004021 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05004022}
4023
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004024#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
4025#include <android/log.h>
4026#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004027#include "android_util.h"
4028
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004029static bool initialized = false;
4030static bool active = false;
4031struct demo demo;
4032
4033static int32_t processInput(struct android_app* app, AInputEvent* event) {
4034 return 0;
4035}
4036
4037static void processCommand(struct android_app* app, int32_t cmd) {
4038 switch(cmd) {
4039 case APP_CMD_INIT_WINDOW: {
4040 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06004041 // We're getting a new window. If the app is starting up, we
4042 // need to initialize. If the app has already been
4043 // initialized, that means that we lost our previous window,
4044 // which means that we have a lot of work to do. At a minimum,
4045 // we need to destroy the swapchain and surface associated with
4046 // the old window, and create a new surface and swapchain.
4047 // However, since there are a lot of other objects/state that
4048 // is tied to the swapchain, it's easiest to simply cleanup and
4049 // start over (i.e. use a brute-force approach of re-starting
4050 // the app)
4051 if (demo.prepared) {
4052 demo_cleanup(&demo);
4053 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004054
4055 // Parse Intents into argc, argv
4056 // Use the following key to send arguments, i.e.
4057 // --es args "--validate"
4058 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06004059 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004060 int argc = 0;
4061 char** argv = get_args(app, key, appTag, &argc);
4062
4063 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
4064 for (int i = 0; i < argc; i++)
4065 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
4066
4067 demo_init(&demo, argc, argv);
4068
4069 // Free the argv malloc'd by get_args
4070 for (int i = 0; i < argc; i++)
4071 free(argv[i]);
4072
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004073 demo.window = (void*)app->window;
4074 demo_init_vk_swapchain(&demo);
4075 demo_prepare(&demo);
4076 initialized = true;
4077 }
4078 break;
4079 }
4080 case APP_CMD_GAINED_FOCUS: {
4081 active = true;
4082 break;
4083 }
4084 case APP_CMD_LOST_FOCUS: {
4085 active = false;
4086 break;
4087 }
4088 }
4089}
4090
4091void android_main(struct android_app *app)
4092{
4093 app_dummy();
4094
Cody Northrop8707a6e2016-04-26 19:59:19 -06004095#ifdef ANDROID
4096 int vulkanSupport = InitVulkan();
4097 if (vulkanSupport == 0)
4098 return;
4099#endif
4100
Ian Elliottf05d5d12016-05-17 12:03:35 -06004101 demo.prepared = false;
4102
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004103 app->onAppCmd = processCommand;
4104 app->onInputEvent = processInput;
4105
4106 while(1) {
4107 int events;
4108 struct android_poll_source* source;
4109 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
4110 if (source) {
4111 source->process(app, source);
4112 }
4113
4114 if (app->destroyRequested != 0) {
4115 demo_cleanup(&demo);
4116 return;
4117 }
4118 }
4119 if (initialized && active) {
4120 demo_run(&demo);
4121 }
4122 }
4123
4124}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004125#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004126int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004127 struct demo demo;
4128
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004129 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004130#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004131 demo_create_xcb_window(&demo);
4132#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4133 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004134#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4135 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004136#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004137#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004138
Tony Barbourcc69f452015-10-08 13:59:42 -06004139 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004140
4141 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004142
Tony Barbour153cb062016-12-07 13:43:36 -07004143#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004144 demo_run_xcb(&demo);
4145#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4146 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004147#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4148 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004149#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07004150#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4151 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004152#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004153
4154 demo_cleanup(&demo);
4155
Karl Schultz0218c002016-03-22 17:06:13 -06004156 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004157}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004158#endif