blob: 3e2733ee287f2ead9115e4443ff1828b89693f48 [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
2* 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: Jon Ashburn <jon@lunarg.com>
22* Author: Gwan-gyeong Mun <elongbug@gmail.com>
Tony Barbour66a56c32016-09-21 13:10:56 -060023* Author: Tony Barbour <tony@LunarG.com>
Bill Hollingsf4352142017-02-14 22:58:56 -050024* Author: Bill Hollings <bill.hollings@brenwill.com>
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090025*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070026
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060027#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdbool.h>
32#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070033#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090034#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060035#include <X11/Xutil.h>
36#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060037
Ian Elliott639ca472015-04-16 15:23:05 -060038#ifdef _WIN32
39#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060040#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070041#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060042
Tony Barbourefd0c5a2016-12-07 14:45:12 -070043#if defined(VK_USE_PLATFORM_MIR_KHR)
44#warning "Cube does not have code for Mir at this time"
45#endif
46
Cody Northrop8707a6e2016-04-26 19:59:19 -060047#ifdef ANDROID
48#include "vulkan_wrapper.h"
49#else
David Pinedoc5193c12015-11-06 12:54:48 -070050#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060051#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070052
David Pinedo541526f2015-11-24 09:00:24 -070053#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060054#include "linmath.h"
55
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060056#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060057#define APP_SHORT_NAME "cube"
58#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060059
Tony Barbour66a56c32016-09-21 13:10:56 -060060// Allow a maximum of two outstanding presentation operations.
61#define FRAME_LAG 2
62
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060063#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
64
Tony Barbourfdc2d352015-04-22 09:02:32 -060065#if defined(NDEBUG) && defined(__GNUC__)
66#define U_ASSERT_ONLY __attribute__((unused))
67#else
68#define U_ASSERT_ONLY
69#endif
70
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090071#if defined(__GNUC__)
72#define UNUSED __attribute__((unused))
73#else
74#define UNUSED
75#endif
76
Ian Elliott07264132015-04-28 11:35:02 -060077#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060078bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070079#define ERR_EXIT(err_msg, err_class) \
80 do { \
81 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
82 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070083 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060084
Michael Lentinec6cde4b2016-04-18 13:20:45 -050085#elif defined __ANDROID__
86#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070087#define ERR_EXIT(err_msg, err_class) \
88 do { \
89 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
90 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -050091 } while (0)
92#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070093#define ERR_EXIT(err_msg, err_class) \
94 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -080095 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070096 fflush(stdout); \
97 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070098 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050099#endif
Ian Elliott07264132015-04-28 11:35:02 -0600100
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700101#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
102 { \
103 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
104 if (demo->fp##entrypoint == NULL) { \
105 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
106 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700107 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600108
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600109static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
110
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700111#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
112 { \
113 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
114 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
115 if (demo->fp##entrypoint == NULL) { \
116 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
117 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700118 }
Ian Elliott673898b2015-06-22 15:07:49 -0600119
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600120/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700121 * structure to track all objects related to a texture.
122 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600123struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600124 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700125
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600126 VkImage image;
127 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600128
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800129 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500130 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600131 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700132 int32_t tex_width, tex_height;
133};
134
Karl Schultze4dc75c2016-02-02 15:37:51 -0700135static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600136
Karl Schultz0218c002016-03-22 17:06:13 -0600137static int validation_error = 0;
138
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600139struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600140 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700141 float mvp[4][4];
142 float position[12 * 3][4];
143 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600144};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600145
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146//--------------------------------------------------------------------------------------
147// Mesh and VertexFormat Data
148//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700149// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600150static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800151 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 -1.0f,-1.0f, 1.0f,
153 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800154 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155 -1.0f, 1.0f,-1.0f,
156 -1.0f,-1.0f,-1.0f,
157
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 1.0f, 1.0f,-1.0f,
160 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800161 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600163 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800168 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600170 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 -1.0f, 1.0f, 1.0f,
174 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800175 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600177 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f, 1.0f, 1.0f,
181 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800182 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183 1.0f,-1.0f,-1.0f,
184 1.0f, 1.0f,-1.0f,
185
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600188 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800189 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190 1.0f,-1.0f, 1.0f,
191 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600192};
193
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600194static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700195 0.0f, 1.0f, // -X side
196 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 0.0f, 0.0f,
200 0.0f, 1.0f,
201
Rene Lindsay76867e82016-07-29 10:49:11 -0700202 1.0f, 1.0f, // -Z side
203 0.0f, 0.0f,
204 0.0f, 1.0f,
205 1.0f, 1.0f,
206 1.0f, 0.0f,
207 0.0f, 0.0f,
208
209 1.0f, 0.0f, // -Y side
210 1.0f, 1.0f,
211 0.0f, 1.0f,
212 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600213 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800214 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600215
Rene Lindsay76867e82016-07-29 10:49:11 -0700216 1.0f, 0.0f, // +Y side
217 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700220 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600221 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222
Rene Lindsay76867e82016-07-29 10:49:11 -0700223 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800224 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700225 0.0f, 1.0f,
226 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700229
230 0.0f, 0.0f, // +Z side
231 0.0f, 1.0f,
232 1.0f, 0.0f,
233 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700235 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600236};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700237// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600238
Karl Schultze4dc75c2016-02-02 15:37:51 -0700239void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600240 int i;
241
242 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700243 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600244 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
245 }
246 printf("\n");
247 fflush(stdout);
248}
249
Karl Schultze4dc75c2016-02-02 15:37:51 -0700250void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600251 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700252 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600253 printf("\n");
254 fflush(stdout);
255}
256
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700257VKAPI_ATTR VkBool32 VKAPI_CALL BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
258 size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg,
259 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700260#ifndef WIN32
261 raise(SIGTRAP);
262#else
263 DebugBreak();
264#endif
265
266 return false;
267}
268
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600269typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600270 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800271 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600272 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600273 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700274 VkBuffer uniform_buffer;
275 VkDeviceMemory uniform_memory;
276 VkFramebuffer framebuffer;
277 VkDescriptorSet descriptor_set;
278 VkFence fence;
279} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600280
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600281struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500282#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600283#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700284 HINSTANCE connection; // hInstance - Windows Instance
285 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
286 HWND window; // hWnd - window handle
287 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700288#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700289 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600290 Window xlib_window;
291 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700292#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700293 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600294 xcb_connection_t *connection;
295 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600296 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800297 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900298#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
299 struct wl_display *display;
300 struct wl_registry *registry;
301 struct wl_compositor *compositor;
302 struct wl_surface *window;
303 struct wl_shell *shell;
304 struct wl_shell_surface *shell_surface;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700305#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500306#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700307 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500308#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
309 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500310#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700311 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600312 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700313 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600314 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600315
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600316 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600317 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600319 VkQueue graphics_queue;
320 VkQueue present_queue;
321 uint32_t graphics_queue_family_index;
322 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600323 VkSemaphore image_acquired_semaphores[FRAME_LAG];
324 VkSemaphore draw_complete_semaphores[FRAME_LAG];
325 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600326 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600327 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600328 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Tony Barbourda2bad52016-01-22 14:36:40 -0700330 uint32_t enabled_extension_count;
331 uint32_t enabled_layer_count;
332 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600333 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700334
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600336 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600337 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700339 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
340 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
341 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
342 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600343 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
344 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
345 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
346 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
347 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600348 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600349 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700350 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600351 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600352 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600353 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800355 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600356 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357
358 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600361 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800362 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500363 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600364 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365 } depth;
366
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600367 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600368 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700370 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500371 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600372 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600373 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800374 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600377 mat4x4 projection_matrix;
378 mat4x4 view_matrix;
379 mat4x4 model_matrix;
380
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600381 float spin_angle;
382 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600383 bool pause;
384
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600385 VkShaderModule vert_shader_module;
386 VkShaderModule frag_shader_module;
387
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800389
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600391 int32_t curFrame;
392 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600393 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600394 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600395 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600396 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700397 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
398 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
399 VkDebugReportCallbackEXT msg_callback;
400 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600401
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600402 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600403 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404};
405
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700406VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
407 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600408 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600409 char *message = (char *)malloc(strlen(pMsg) + 100);
410
411 assert(message);
412
Cody Northropaf28a532016-09-27 10:50:57 -0600413 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
414 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600415 validation_error = 1;
416 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600417 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
418 validation_error = 1;
419 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600420 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600421 validation_error = 1;
422 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
423 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
424 validation_error = 1;
425 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
426 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600427 validation_error = 1;
428 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600429 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600430 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600431 }
432
433#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600434
Tony Barbour602ca4f2016-09-12 14:02:43 -0600435 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600436 struct demo *demo = (struct demo*) pUserData;
437 if (!demo->suppress_popups)
438 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600439 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600440
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600441#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600442
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600443 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600444 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600445 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600446 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600447 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600448 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600449 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600450 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600451 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600452 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600453 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600454 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600455 }
Cody Northropaf28a532016-09-27 10:50:57 -0600456
lenny-lunargfbe22392016-06-06 11:07:53 -0600457#else
Cody Northropaf28a532016-09-27 10:50:57 -0600458
lenny-lunargfbe22392016-06-06 11:07:53 -0600459 printf("%s\n", message);
460 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600461
lenny-lunargfbe22392016-06-06 11:07:53 -0600462#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600463
lenny-lunargfbe22392016-06-06 11:07:53 -0600464 free(message);
465
Cody Northropaf28a532016-09-27 10:50:57 -0600466 //clang-format on
467
lenny-lunargfbe22392016-06-06 11:07:53 -0600468 /*
469 * false indicates that layer should not bail-out of an
470 * API call that had validation failures. This may mean that the
471 * app dies inside the driver due to invalid parameter(s).
472 * That's what would happen without validation layers, so we'll
473 * keep that behavior here.
474 */
475 return false;
476}
477
Ian Elliottcf074d32015-10-16 18:02:43 -0600478// Forward declaration:
479static void demo_resize(struct demo *demo);
480
Karl Schultze4dc75c2016-02-02 15:37:51 -0700481static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
482 VkFlags requirements_mask,
483 uint32_t *typeIndex) {
484 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600485 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700486 if ((typeBits & 1) == 1) {
487 // Type is available, does it match user properties?
488 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
489 requirements_mask) == requirements_mask) {
490 *typeIndex = i;
491 return true;
492 }
493 }
494 typeBits >>= 1;
495 }
496 // No memory types matched, return failure
497 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600498}
499
Karl Schultze4dc75c2016-02-02 15:37:51 -0700500static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600501 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502
Tony Barbource7524e2016-07-28 12:01:45 -0600503 // This function could get called twice if the texture uses a staging buffer
504 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600505 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600506 return;
507
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600508 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509 assert(!err);
510
Tony Barbource7524e2016-07-28 12:01:45 -0600511 VkFence fence;
512 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
513 .pNext = NULL,
514 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700515 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
516 assert(!err);
517
Karl Schultze4dc75c2016-02-02 15:37:51 -0700518 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700519 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
520 .pNext = NULL,
521 .waitSemaphoreCount = 0,
522 .pWaitSemaphores = NULL,
523 .pWaitDstStageMask = NULL,
524 .commandBufferCount = 1,
525 .pCommandBuffers = cmd_bufs,
526 .signalSemaphoreCount = 0,
527 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528
Tony Barbource7524e2016-07-28 12:01:45 -0600529 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530 assert(!err);
531
Tony Barbource7524e2016-07-28 12:01:45 -0600532 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 assert(!err);
534
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600535 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600536 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600538}
539
Karl Schultze4dc75c2016-02-02 15:37:51 -0700540static void demo_set_image_layout(struct demo *demo, VkImage image,
541 VkImageAspectFlags aspectMask,
542 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700543 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600544 VkAccessFlagBits srcAccessMask,
545 VkPipelineStageFlags src_stages,
546 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600547 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600548
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600549 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600550 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700552 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800553 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600554 .oldLayout = old_image_layout,
555 .newLayout = new_image_layout,
556 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700557 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600558
Tony Barboureb5077b2016-10-19 15:23:36 -0600559 switch (new_image_layout) {
560 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600561 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600562 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
563 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600564
Tony Barboureb5077b2016-10-19 15:23:36 -0600565 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700566 image_memory_barrier.dstAccessMask =
567 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600568 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700569
Tony Barboureb5077b2016-10-19 15:23:36 -0600570 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700571 image_memory_barrier.dstAccessMask =
572 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600573 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700574
Tony Barboureb5077b2016-10-19 15:23:36 -0600575 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700576 image_memory_barrier.dstAccessMask =
577 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600578 break;
579
580 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
581 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
582 break;
583
584 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
585 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
586 break;
587
588 default:
589 image_memory_barrier.dstAccessMask = 0;
590 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600591 }
592
Tony Barbourf165b5d2016-10-03 16:01:41 -0600593
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600594 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600595
Karl Schultze4dc75c2016-02-02 15:37:51 -0700596 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
597 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600598}
599
Karl Schultze4dc75c2016-02-02 15:37:51 -0700600static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700601 const VkCommandBufferBeginInfo cmd_buf_info = {
602 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
603 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600604 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700605 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700606 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800607 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
609 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800610 };
611 const VkRenderPassBeginInfo rp_begin = {
612 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
613 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800614 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700615 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800616 .renderArea.offset.x = 0,
617 .renderArea.offset.y = 0,
618 .renderArea.extent.width = demo->width,
619 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600620 .clearValueCount = 2,
621 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700622 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800623 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600625 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800627 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700628 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
629 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700630 demo->pipeline_layout, 0, 1,
631 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
632 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600633 VkViewport viewport;
634 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700635 viewport.height = (float)demo->height;
636 viewport.width = (float)demo->width;
637 viewport.minDepth = (float)0.0f;
638 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700639 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600640
641 VkRect2D scissor;
642 memset(&scissor, 0, sizeof(scissor));
643 scissor.extent.width = demo->width;
644 scissor.extent.height = demo->height;
645 scissor.offset.x = 0;
646 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700647 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600648 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600649 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600650 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800651 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Tony Barbour22e06272016-09-07 16:07:56 -0600653 if (demo->separate_present_queue) {
654 // We have to transfer ownership from the graphics queue family to the
655 // present queue family to be able to present. Note that we don't have
656 // to transfer from present queue family back to graphics queue family at
657 // the start of the next frame because we don't care about the image's
658 // contents at that point.
659 VkImageMemoryBarrier image_ownership_barrier = {
660 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
661 .pNext = NULL,
662 .srcAccessMask = 0,
663 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
664 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
665 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
666 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
667 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700668 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600669 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
670
671 vkCmdPipelineBarrier(cmd_buf,
672 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600673 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600674 0, NULL, 0, NULL, 1, &image_ownership_barrier);
675 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600676 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677 assert(!err);
678}
679
Tony Barbour22e06272016-09-07 16:07:56 -0600680void demo_build_image_ownership_cmd(struct demo *demo, int i) {
681 VkResult U_ASSERT_ONLY err;
682
683 const VkCommandBufferBeginInfo cmd_buf_info = {
684 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
685 .pNext = NULL,
686 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
687 .pInheritanceInfo = NULL,
688 };
Tony Barboura72d9492017-01-11 13:35:09 -0700689 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600690 &cmd_buf_info);
691 assert(!err);
692
693 VkImageMemoryBarrier image_ownership_barrier = {
694 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
695 .pNext = NULL,
696 .srcAccessMask = 0,
697 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
698 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
699 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
700 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
701 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700702 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600703 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
704
Tony Barboura72d9492017-01-11 13:35:09 -0700705 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600706 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
707 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
708 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700709 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600710 assert(!err);
711}
712
Karl Schultze4dc75c2016-02-02 15:37:51 -0700713void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600714 mat4x4 MVP, Model, VP;
715 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600716 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600717 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600718
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600719 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700721 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600722 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700723 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
724 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600725 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600726
Tony Barboura72d9492017-01-11 13:35:09 -0700727 vkWaitForFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence,
728 VK_TRUE, UINT64_MAX);
729
730 err = vkMapMemory(demo->device,
731 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
732 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733 assert(!err);
734
Karl Schultze4dc75c2016-02-02 15:37:51 -0700735 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600736
Tony Barboura72d9492017-01-11 13:35:09 -0700737 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738}
739
Karl Schultze4dc75c2016-02-02 15:37:51 -0700740static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600741 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600742
szdarkhackd322ff02016-10-08 09:51:22 +0300743 // Ensure no more than FRAME_LAG presentations are outstanding
744 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
745 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600746
Ian Elliottaaae5352015-07-06 14:27:58 -0600747 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700748 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -0700749 demo->image_acquired_semaphores[demo->frame_index],
750 demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600751 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600752
Tony Barboura72d9492017-01-11 13:35:09 -0700753 demo_update_data_buffer(demo);
754
Ian Elliottcf074d32015-10-16 18:02:43 -0600755 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
756 // demo->swapchain is out of date (e.g. the window was resized) and
757 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600758 demo->frame_index += 1;
759 demo->frame_index %= FRAME_LAG;
760
Ian Elliottcf074d32015-10-16 18:02:43 -0600761 demo_resize(demo);
762 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600763 return;
764 } else if (err == VK_SUBOPTIMAL_KHR) {
765 // demo->swapchain is not as optimal as it could be, but the platform's
766 // presentation engine will still present the image correctly.
767 } else {
768 assert(!err);
769 }
Tony Barbource7524e2016-07-28 12:01:45 -0600770 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600771 // that the image won't be rendered to until the presentation
772 // engine has fully released ownership to the application, and it is
773 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -0600774 VkPipelineStageFlags pipe_stage_flags;
775 VkSubmitInfo submit_info;
776 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
777 submit_info.pNext = NULL;
778 submit_info.pWaitDstStageMask = &pipe_stage_flags;
779 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
780 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600781 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600782 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -0700783 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600784 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600785 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -0700786 vkResetFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence);
787 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
788 demo->swapchain_image_resources[demo->current_buffer].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600789 assert(!err);
790
Tony Barbour22e06272016-09-07 16:07:56 -0600791 if (demo->separate_present_queue) {
792 // If we are using separate queues, change image ownership to the
793 // present queue before presenting, waiting for the draw complete
794 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -0700795 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600796 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
797 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600798 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600799 submit_info.commandBufferCount = 1;
800 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -0700801 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600802 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600803 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600804 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
805 assert(!err);
806 }
807
808 // If we are using separate queues we have to wait for image ownership,
809 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600810 VkPresentInfoKHR present = {
811 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600812 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600813 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600814 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600815 ? &demo->image_ownership_semaphores[demo->frame_index]
816 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600817 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700818 .pSwapchains = &demo->swapchain,
819 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600820 };
821
Tony Barboura2a67812016-07-18 13:21:06 -0600822 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600823 demo->frame_index += 1;
824 demo->frame_index %= FRAME_LAG;
825
Ian Elliottcf074d32015-10-16 18:02:43 -0600826 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
827 // demo->swapchain is out of date (e.g. the window was resized) and
828 // must be recreated:
829 demo_resize(demo);
830 } else if (err == VK_SUBOPTIMAL_KHR) {
831 // demo->swapchain is not as optimal as it could be, but the platform's
832 // presentation engine will still present the image correctly.
833 } else {
834 assert(!err);
835 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836}
837
Karl Schultze4dc75c2016-02-02 15:37:51 -0700838static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600839 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600840 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600841
Ian Elliottb08e0d92015-11-20 11:55:46 -0700842 // Check the surface capabilities and formats
843 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700844 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
845 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600846 assert(!err);
847
Ian Elliott8528eff2015-08-07 15:56:59 -0600848 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700849 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
850 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600851 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600852 VkPresentModeKHR *presentModes =
853 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600854 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700855 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
856 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600857 assert(!err);
858
Ian Elliotta0781972015-08-21 15:09:33 -0600859 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600860 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
861 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
862 // If the surface size is undefined, the size is set to the size
863 // of the images requested, which must fit within the minimum and
864 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600865 swapchainExtent.width = demo->width;
866 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600867
868 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
869 swapchainExtent.width = surfCapabilities.minImageExtent.width;
870 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
871 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
872 }
873
874 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
875 swapchainExtent.height = surfCapabilities.minImageExtent.height;
876 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
877 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
878 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700879 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700881 swapchainExtent = surfCapabilities.currentExtent;
882 demo->width = surfCapabilities.currentExtent.width;
883 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600884 }
885
Tony Barbour66a56c32016-09-21 13:10:56 -0600886 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600887 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600888 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -0600889
Ian Elliottb18fdde2016-10-03 12:11:12 -0600890 // There are times when you may wish to use another present mode. The
891 // following code shows how to select them, and the comments provide some
892 // reasons you may wish to use them.
893 //
894 // It should be noted that Vulkan 1.0 doesn't provide a method for
895 // synchronizing rendering with the presentation engine's display. There
896 // is a method provided for throttling rendering with the display, but
897 // there are some presentation engines for which this method will not work.
898 // If an application doesn't throttle its rendering, and if it renders much
899 // faster than the refresh rate of the display, this can waste power on
900 // mobile devices. That is because power is being spent rendering images
901 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -0600902
Ian Elliottb18fdde2016-10-03 12:11:12 -0600903 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
904 // tearing, or have some way of synchronizing their rendering with the
905 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600906 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
907 // generally render a new presentable image every refresh cycle, but are
908 // occasionally early. In this case, the application wants the new image
909 // to be displayed instead of the previously-queued-for-presentation image
910 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600911 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
912 // render a new presentable image every refresh cycle, but are occasionally
913 // late. In this case (perhaps because of stuttering/latency concerns),
914 // the application wants the late image to be immediately displayed, even
915 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -0600916
917 if (demo->presentMode != swapchainPresentMode) {
918
919 for (size_t i = 0; i < presentModeCount; ++i) {
920 if (presentModes[i] == demo->presentMode) {
921 swapchainPresentMode = demo->presentMode;
922 break;
923 }
Ian Elliottb18fdde2016-10-03 12:11:12 -0600924 }
925 }
Tony Barbour291d57b2016-10-21 14:47:07 -0600926 if (swapchainPresentMode != demo->presentMode) {
927 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
928 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600929
Tony Barbour84376fe2017-01-06 15:54:22 -0700930 // Determine the number of VkImages to use in the swap chain.
931 // Application desires to acquire 3 images at a time for triple
932 // buffering
933 uint32_t desiredNumOfSwapchainImages = 3;
934 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
935 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
936 }
Ian Elliottcf7f7482016-08-19 03:46:58 -0600937 // If maxImageCount is 0, we can ask for as many images as we want;
938 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700939 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600940 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600941 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600942 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600943 }
944
Tony Barbour9fd6d352015-09-16 15:01:32 -0600945 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700946 if (surfCapabilities.supportedTransforms &
947 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700948 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600949 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700950 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600951 }
952
Tony Barbour820b55b2017-03-14 08:55:46 -0600953 // Find a supported composite alpha mode - one of these is guaranteed to be set
954 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
955 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
956 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
957 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
958 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
959 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
960 };
961 for (uint32_t i = 0; i < sizeof(compositeAlphaFlags); i++) {
962 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
963 compositeAlpha = compositeAlphaFlags[i];
964 break;
965 }
966 }
967
Tony Barboura2a67812016-07-18 13:21:06 -0600968 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600969 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800970 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700971 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600972 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800973 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600974 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700975 .imageExtent =
976 {
977 .width = swapchainExtent.width, .height = swapchainExtent.height,
978 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700979 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600980 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -0600981 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700982 .imageArrayLayers = 1,
983 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
984 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600985 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600986 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600987 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600988 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600989 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600990 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600991 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700992 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800993 assert(!err);
994
Ian Elliottcf074d32015-10-16 18:02:43 -0600995 // If we just re-created an existing swapchain, we should destroy the old
996 // swapchain at this point.
997 // Note: destroying the swapchain also cleans up all its associated
998 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800999 if (oldSwapchain != VK_NULL_HANDLE) {
Tony Barboura90a4f92017-03-23 13:25:36 -06001000 // AMD driver times out waiting on fences used in AcquireNextImage on
1001 // a swapchain that is subsequently destroyed before the wait.
1002 vkWaitForFences(demo->device, FRAME_LAG, demo->fences, VK_TRUE, UINT64_MAX);
Ian Elliottb08e0d92015-11-20 11:55:46 -07001003 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001004 }
1005
1006 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001007 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001008 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001009
Karl Schultze4dc75c2016-02-02 15:37:51 -07001010 VkImage *swapchainImages =
1011 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001012 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001013 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001014 &demo->swapchainImageCount,
1015 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001016 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001017
Tony Barboura72d9492017-01-11 13:35:09 -07001018 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001019 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001020 assert(demo->swapchain_image_resources);
1021
1022 VkFenceCreateInfo fence_ci = {
1023 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1024 .pNext = NULL,
1025 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1026 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001027
Ian Elliotta0781972015-08-21 15:09:33 -06001028 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001029 VkImageViewCreateInfo color_image_view = {
1030 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001031 .pNext = NULL,
1032 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001033 .components =
1034 {
1035 .r = VK_COMPONENT_SWIZZLE_R,
1036 .g = VK_COMPONENT_SWIZZLE_G,
1037 .b = VK_COMPONENT_SWIZZLE_B,
1038 .a = VK_COMPONENT_SWIZZLE_A,
1039 },
1040 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1041 .baseMipLevel = 0,
1042 .levelCount = 1,
1043 .baseArrayLayer = 0,
1044 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001045 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1046 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001047 };
1048
Tony Barboura72d9492017-01-11 13:35:09 -07001049 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001050
Tony Barboura72d9492017-01-11 13:35:09 -07001051 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052
Karl Schultze4dc75c2016-02-02 15:37:51 -07001053 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001054 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001056
Tony Barboura72d9492017-01-11 13:35:09 -07001057 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1058 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001059 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001060
Mark Young04fd3d82016-01-25 14:43:50 -07001061 if (NULL != presentModes) {
1062 free(presentModes);
1063 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001064}
1065
Karl Schultze4dc75c2016-02-02 15:37:51 -07001066static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001067 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001068 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001069 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001071 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001073 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001074 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001075 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001076 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001077 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001078 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001079 .flags = 0,
1080 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001081
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001082 VkImageViewCreateInfo view = {
1083 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001085 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001086 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001087 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1088 .baseMipLevel = 0,
1089 .levelCount = 1,
1090 .baseArrayLayer = 0,
1091 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001092 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001093 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001094 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001095
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001096 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001097 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001098 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001099
1100 demo->depth.format = depth_format;
1101
1102 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001103 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001104 assert(!err);
1105
Karl Schultze4dc75c2016-02-02 15:37:51 -07001106 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001107 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001108
Chia-I Wuf48700b2015-11-10 16:21:09 +08001109 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001110 demo->depth.mem_alloc.pNext = NULL;
1111 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1112 demo->depth.mem_alloc.memoryTypeIndex = 0;
1113
Karl Schultze4dc75c2016-02-02 15:37:51 -07001114 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001115 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001116 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001117 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001118
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001119 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001120 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1121 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001122 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001123
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001125 err =
1126 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001127 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001128
1129 /* create image view */
1130 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001131 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001133}
1134
Tony Barbour1a86d032015-09-21 15:17:33 -06001135/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001136bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001137 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001138
1139#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001140 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001141#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001142
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001143#ifdef __ANDROID__
1144#include <lunarg.ppm.h>
1145 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001146 cPtr = (char*)lunarg_ppm;
1147 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001148 return false;
1149 }
1150 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001151 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001152 if (rgba_data == NULL) {
1153 return true;
1154 }
1155 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001156 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001157 return false;
1158 }
1159 while(strncmp(cPtr++, "\n", 1));
1160
1161 for (int y = 0; y < *height; y++) {
1162 uint8_t *rowPtr = rgba_data;
1163 for (int x = 0; x < *width; x++) {
1164 memcpy(rowPtr, cPtr, 3);
1165 rowPtr[3] = 255; /* Alpha of 1 */
1166 rowPtr += 4;
1167 cPtr += 3;
1168 }
1169 rgba_data += layout->rowPitch;
1170 }
1171
1172 return true;
1173#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001175 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001176
Tony Barbour1a86d032015-09-21 15:17:33 -06001177 if (!fPtr)
1178 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001179
Tony Barbour1a86d032015-09-21 15:17:33 -06001180 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001181 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1182 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001183 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001184 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001185
Tony Barbour1a86d032015-09-21 15:17:33 -06001186 do {
1187 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001188 if (cPtr == NULL) {
1189 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001190 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001191 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001192 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001193
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001194 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001195 if (rgba_data == NULL) {
1196 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001197 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001198 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001199 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001200 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001201 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1202 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001203 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001204 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001205
Karl Schultze4dc75c2016-02-02 15:37:51 -07001206 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001207 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001208 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001209 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001210 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001211 rowPtr[3] = 255; /* Alpha of 1 */
1212 rowPtr += 4;
1213 }
1214 rgba_data += layout->rowPitch;
1215 }
1216 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001217 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001218#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001219}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001220
Karl Schultze4dc75c2016-02-02 15:37:51 -07001221static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001222 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001223 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001224 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001225 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001226 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001227 int32_t tex_width;
1228 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001229 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001230 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231
Karl Schultze4dc75c2016-02-02 15:37:51 -07001232 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001233 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001234 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001235
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001236 tex_obj->tex_width = tex_width;
1237 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001239 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001240 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001241 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001242 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001243 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001244 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001245 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001246 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001247 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001248 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001249 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001250 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001251 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001253
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001254 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001255
Karl Schultze4dc75c2016-02-02 15:37:51 -07001256 err =
1257 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001258 assert(!err);
1259
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001260 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001261
Chia-I Wuf48700b2015-11-10 16:21:09 +08001262 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001263 tex_obj->mem_alloc.pNext = NULL;
1264 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1265 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266
Karl Schultze4dc75c2016-02-02 15:37:51 -07001267 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1268 required_props,
1269 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001270 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001271
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001272 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001273 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001274 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001275 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001276
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001277 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001278 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001279 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001280
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001281 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001282 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001283 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001284 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001285 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001286 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001287 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001288 void *data;
1289
Karl Schultze4dc75c2016-02-02 15:37:51 -07001290 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1291 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001292
Karl Schultze4dc75c2016-02-02 15:37:51 -07001293 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1294 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001295 assert(!err);
1296
1297 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1298 fprintf(stderr, "Error loading texture: %s\n", filename);
1299 }
1300
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001301 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001302 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001303
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001304 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001305}
1306
Karl Schultze4dc75c2016-02-02 15:37:51 -07001307static void demo_destroy_texture_image(struct demo *demo,
1308 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001309 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001310 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1311 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001312}
1313
Karl Schultze4dc75c2016-02-02 15:37:51 -07001314static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001315 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001316 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001317 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001318
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001319 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001320
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001321 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001322 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001323
Karl Schultze4dc75c2016-02-02 15:37:51 -07001324 if ((props.linearTilingFeatures &
1325 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1326 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001327 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001328 demo_prepare_texture_image(
1329 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1330 VK_IMAGE_USAGE_SAMPLED_BIT,
1331 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1332 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001333 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1334 // shader to run until layout transition completes
1335 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1336 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1337 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1338 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001339 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340 } else if (props.optimalTilingFeatures &
1341 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001342 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001343
Tony Barbour16156cb2016-10-19 14:15:49 -06001344 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001345 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001346 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001347 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1348 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1349 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001350
Karl Schultze4dc75c2016-02-02 15:37:51 -07001351 demo_prepare_texture_image(
1352 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1353 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1354 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001355
Tony Barbour16156cb2016-10-19 14:15:49 -06001356 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001357 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001358 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001359 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001360 VK_ACCESS_HOST_WRITE_BIT,
1361 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1362 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001363
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001364 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001365 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001366 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001367 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001368 VK_ACCESS_HOST_WRITE_BIT,
1369 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1370 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001371
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001372 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001373 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1374 .srcOffset = {0, 0, 0},
1375 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1376 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001377 .extent = {demo->staging_texture.tex_width,
1378 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001379 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001380 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001381 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001382 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1383 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001384
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001385 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001386 VK_IMAGE_ASPECT_COLOR_BIT,
1387 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001388 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001389 VK_ACCESS_TRANSFER_WRITE_BIT,
1390 VK_PIPELINE_STAGE_TRANSFER_BIT,
1391 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001392
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001393 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001394 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1395 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001396 }
1397
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001398 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001399 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001401 .magFilter = VK_FILTER_NEAREST,
1402 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001403 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001404 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1405 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1406 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001408 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001409 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001410 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411 .minLod = 0.0f,
1412 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001413 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001414 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001416
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001417 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001418 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001420 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001421 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001422 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001423 .components =
1424 {
1425 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1426 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1427 },
1428 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001429 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001431
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001433 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001434 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 assert(!err);
1436
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437 /* create image view */
1438 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001439 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001440 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441 assert(!err);
1442 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444
Tony Barboura72d9492017-01-11 13:35:09 -07001445void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001446 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001447 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001448 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001449 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001450 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001451 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001452 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001453 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001454
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001455 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001456 mat4x4_mul(MVP, VP, demo->model_matrix);
1457 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001458 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001459
Karl Schultza2615462017-01-20 13:19:20 -07001460 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001461 data.position[i][0] = g_vertex_buffer_data[i * 3];
1462 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1463 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001464 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001465 data.attr[i][0] = g_uv_buffer_data[2 * i];
1466 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001467 data.attr[i][2] = 0;
1468 data.attr[i][3] = 0;
1469 }
1470
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001471 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001472 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001473 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001474 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001475
Tony Barboura72d9492017-01-11 13:35:09 -07001476 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1477 err =
1478 vkCreateBuffer(demo->device, &buf_info, NULL,
1479 &demo->swapchain_image_resources[i].uniform_buffer);
1480 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001481
Tony Barboura72d9492017-01-11 13:35:09 -07001482 vkGetBufferMemoryRequirements(demo->device,
1483 demo->swapchain_image_resources[i].uniform_buffer,
1484 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001485
Tony Barboura72d9492017-01-11 13:35:09 -07001486 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1487 mem_alloc.pNext = NULL;
1488 mem_alloc.allocationSize = mem_reqs.size;
1489 mem_alloc.memoryTypeIndex = 0;
1490
1491 pass = memory_type_from_properties(
1492 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001493 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001494 &mem_alloc.memoryTypeIndex);
1495 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001496
Tony Barboura72d9492017-01-11 13:35:09 -07001497 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1498 &demo->swapchain_image_resources[i].uniform_memory);
1499 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001500
Tony Barboura72d9492017-01-11 13:35:09 -07001501 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1502 VK_WHOLE_SIZE, 0, (void **)&pData);
1503 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001504
Tony Barboura72d9492017-01-11 13:35:09 -07001505 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001506
Tony Barboura72d9492017-01-11 13:35:09 -07001507 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001508
Tony Barboura72d9492017-01-11 13:35:09 -07001509 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1510 demo->swapchain_image_resources[i].uniform_memory, 0);
1511 assert(!err);
1512 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001513}
1514
Karl Schultze4dc75c2016-02-02 15:37:51 -07001515static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001516 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001517 [0] =
1518 {
1519 .binding = 0,
1520 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1521 .descriptorCount = 1,
1522 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1523 .pImmutableSamplers = NULL,
1524 },
1525 [1] =
1526 {
1527 .binding = 1,
1528 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1529 .descriptorCount = DEMO_TEXTURE_COUNT,
1530 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1531 .pImmutableSamplers = NULL,
1532 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001533 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001534 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001535 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001536 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001537 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001538 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001539 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001540 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001541
Karl Schultze4dc75c2016-02-02 15:37:51 -07001542 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1543 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001544 assert(!err);
1545
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001546 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001547 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1548 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001549 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001550 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001551 };
1552
Karl Schultze4dc75c2016-02-02 15:37:51 -07001553 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001554 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001555 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001556}
1557
Karl Schultze4dc75c2016-02-02 15:37:51 -07001558static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001559 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1560 // because at the start of the renderpass, we don't care about their contents.
1561 // At the start of the subpass, the color attachment's layout will be transitioned
1562 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1563 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1564 // the renderpass, the color attachment's layout will be transitioned to
1565 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1566 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001567 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001568 [0] =
1569 {
1570 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001571 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001572 .samples = VK_SAMPLE_COUNT_1_BIT,
1573 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1574 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1575 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1576 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001577 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001578 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001579 },
1580 [1] =
1581 {
1582 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001583 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001584 .samples = VK_SAMPLE_COUNT_1_BIT,
1585 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1586 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1587 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1588 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1589 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001590 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001591 .finalLayout =
1592 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1593 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001594 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001595 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001596 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001597 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001598 const VkAttachmentReference depth_reference = {
1599 .attachment = 1,
1600 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1601 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001602 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001603 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1604 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001605 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001606 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001607 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001608 .pColorAttachments = &color_reference,
1609 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001610 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001611 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001612 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001613 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001614 const VkRenderPassCreateInfo rp_info = {
1615 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1616 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001617 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001618 .attachmentCount = 2,
1619 .pAttachments = attachments,
1620 .subpassCount = 1,
1621 .pSubpasses = &subpass,
1622 .dependencyCount = 0,
1623 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001624 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001625 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001626
Chia-I Wu63636062015-10-26 21:10:41 +08001627 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001628 assert(!err);
1629}
1630
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001631//TODO: Merge shader reading
1632#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001633static VkShaderModule
1634demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001635 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001636 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001637 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001639 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1640 moduleCreateInfo.pNext = NULL;
1641
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001642 moduleCreateInfo.codeSize = size;
1643 moduleCreateInfo.pCode = code;
1644 moduleCreateInfo.flags = 0;
1645 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1646 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001647
1648 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001649}
1650
Karl Schultze4dc75c2016-02-02 15:37:51 -07001651char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001652 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001653 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001654 void *shader_code;
1655
Bill Hollingsf4352142017-02-14 22:58:56 -05001656#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001657 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001658#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001659
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001660 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661 if (!fp)
1662 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001663
1664 fseek(fp, 0L, SEEK_END);
1665 size = ftell(fp);
1666
1667 fseek(fp, 0L, SEEK_SET);
1668
1669 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001670 retval = fread(shader_code, size, 1, fp);
1671 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001672
1673 *psize = size;
1674
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001675 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001676 return shader_code;
1677}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001678#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001679
Karl Schultze4dc75c2016-02-02 15:37:51 -07001680static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001681#ifdef __ANDROID__
1682 VkShaderModuleCreateInfo sh_info = {};
1683 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1684
1685#include "cube.vert.h"
1686 sh_info.codeSize = sizeof(cube_vert);
1687 sh_info.pCode = cube_vert;
1688 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1689 assert(!err);
1690#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001691 void *vertShaderCode;
1692 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001693
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001694 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001695 if (!vertShaderCode) {
1696 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1697 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001698
Karl Schultze4dc75c2016-02-02 15:37:51 -07001699 demo->vert_shader_module =
1700 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001701
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001702 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001703#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001704
1705 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001706}
1707
Karl Schultze4dc75c2016-02-02 15:37:51 -07001708static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001709#ifdef __ANDROID__
1710 VkShaderModuleCreateInfo sh_info = {};
1711 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1712
1713#include "cube.frag.h"
1714 sh_info.codeSize = sizeof(cube_frag);
1715 sh_info.pCode = cube_frag;
1716 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1717 assert(!err);
1718#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001719 void *fragShaderCode;
1720 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001721
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001722 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001723 if (!fragShaderCode) {
1724 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
1725 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001726
Karl Schultze4dc75c2016-02-02 15:37:51 -07001727 demo->frag_shader_module =
1728 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001729
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001730 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001731#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001732
1733 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734}
1735
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001737 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001738 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001739 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001740 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001741 VkPipelineRasterizationStateCreateInfo rs;
1742 VkPipelineColorBlendStateCreateInfo cb;
1743 VkPipelineDepthStencilStateCreateInfo ds;
1744 VkPipelineViewportStateCreateInfo vp;
1745 VkPipelineMultisampleStateCreateInfo ms;
1746 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1747 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001748 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749
Piers Daniellba839d12015-09-29 13:01:09 -06001750 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1751 memset(&dynamicState, 0, sizeof dynamicState);
1752 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1753 dynamicState.pDynamicStates = dynamicStateEnables;
1754
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001755 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001756 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001757 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001759 memset(&vi, 0, sizeof(vi));
1760 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1761
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001763 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001764 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765
1766 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001767 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1768 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001769 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001770 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001771 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001772 rs.rasterizerDiscardEnable = VK_FALSE;
1773 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001774 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001775
1776 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001777 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1778 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001779 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001780 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001781 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001782 cb.attachmentCount = 1;
1783 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784
Tony Barbour29645d02015-01-16 14:27:35 -07001785 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001786 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001787 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001788 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1789 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001790 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001791 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1792 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001793
1794 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001795 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001796 ds.depthTestEnable = VK_TRUE;
1797 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001798 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001799 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001800 ds.back.failOp = VK_STENCIL_OP_KEEP;
1801 ds.back.passOp = VK_STENCIL_OP_KEEP;
1802 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001803 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001804 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001805
Tony Barbour29645d02015-01-16 14:27:35 -07001806 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001807 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001808 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001809 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001811 // Two stages: vs and fs
1812 pipeline.stageCount = 2;
1813 VkPipelineShaderStageCreateInfo shaderStages[2];
1814 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1815
Karl Schultze4dc75c2016-02-02 15:37:51 -07001816 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1817 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001818 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001819 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001820
Karl Schultze4dc75c2016-02-02 15:37:51 -07001821 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1822 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001823 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001824 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001825
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001826 memset(&pipelineCache, 0, sizeof(pipelineCache));
1827 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001828
Karl Schultze4dc75c2016-02-02 15:37:51 -07001829 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1830 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001831 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001832
Karl Schultze4dc75c2016-02-02 15:37:51 -07001833 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001834 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001835 pipeline.pRasterizationState = &rs;
1836 pipeline.pColorBlendState = &cb;
1837 pipeline.pMultisampleState = &ms;
1838 pipeline.pViewportState = &vp;
1839 pipeline.pDepthStencilState = &ds;
1840 pipeline.pStages = shaderStages;
1841 pipeline.renderPass = demo->render_pass;
1842 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001843
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001844 pipeline.renderPass = demo->render_pass;
1845
Karl Schultze4dc75c2016-02-02 15:37:51 -07001846 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1847 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848 assert(!err);
1849
Chia-I Wu63636062015-10-26 21:10:41 +08001850 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1851 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852}
1853
Karl Schultze4dc75c2016-02-02 15:37:51 -07001854static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001855 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001856 [0] =
1857 {
1858 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07001859 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001860 },
1861 [1] =
1862 {
1863 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07001864 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001865 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001866 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001867 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001868 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001869 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001870 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08001871 .poolSizeCount = 2,
1872 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001873 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001874 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001875
Karl Schultze4dc75c2016-02-02 15:37:51 -07001876 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1877 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001878 assert(!err);
1879}
1880
Karl Schultze4dc75c2016-02-02 15:37:51 -07001881static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001882 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001883 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001884 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001885
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001886 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001887 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001888 .pNext = NULL,
1889 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001890 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001891 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07001892
1893 VkDescriptorBufferInfo buffer_info;
1894 buffer_info.offset = 0;
1895 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001896
Chia-I Wuae721ba2015-05-25 16:27:55 +08001897 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07001898 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001899 tex_descs[i].sampler = demo->textures[i].sampler;
1900 tex_descs[i].imageView = demo->textures[i].view;
1901 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001902 }
1903
1904 memset(&writes, 0, sizeof(writes));
1905
1906 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001907 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001908 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07001909 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001910
1911 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001912 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001913 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001914 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001915 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001916
Tony Barboura72d9492017-01-11 13:35:09 -07001917 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1918 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
1919 assert(!err);
1920 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
1921 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1922 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1923 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1924 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001925}
1926
Karl Schultze4dc75c2016-02-02 15:37:51 -07001927static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001928 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001929 attachments[1] = demo->depth.view;
1930
Chia-I Wuf973e322015-07-08 13:34:24 +08001931 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001932 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1933 .pNext = NULL,
1934 .renderPass = demo->render_pass,
1935 .attachmentCount = 2,
1936 .pAttachments = attachments,
1937 .width = demo->width,
1938 .height = demo->height,
1939 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001940 };
1941 VkResult U_ASSERT_ONLY err;
1942 uint32_t i;
1943
Tony Barbourd8e504f2015-10-08 14:26:24 -06001944 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07001945 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001946 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001947 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08001948 assert(!err);
1949 }
1950}
1951
Karl Schultze4dc75c2016-02-02 15:37:51 -07001952static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001953 VkResult U_ASSERT_ONLY err;
1954
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001955 const VkCommandPoolCreateInfo cmd_pool_info = {
1956 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001957 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001958 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001959 .flags = 0,
1960 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001961 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1962 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001963 assert(!err);
1964
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001965 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001966 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001967 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001968 .commandPool = demo->cmd_pool,
1969 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001970 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001972 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1973 assert(!err);
1974 VkCommandBufferBeginInfo cmd_buf_info = {
1975 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1976 .pNext = NULL,
1977 .flags = 0,
1978 .pInheritanceInfo = NULL,
1979 };
1980 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1981 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982
1983 demo_prepare_buffers(demo);
1984 demo_prepare_depth(demo);
1985 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07001986 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001988 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001989 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001990 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001991
Ian Elliotta0781972015-08-21 15:09:33 -06001992 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001993 err =
Tony Barboura72d9492017-01-11 13:35:09 -07001994 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001995 assert(!err);
1996 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001997
Tony Barbour22e06272016-09-07 16:07:56 -06001998 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07001999 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002000 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2001 .pNext = NULL,
2002 .queueFamilyIndex = demo->present_queue_family_index,
2003 .flags = 0,
2004 };
Karl Schultza2615462017-01-20 13:19:20 -07002005 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002006 &demo->present_cmd_pool);
2007 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002008 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002009 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2010 .pNext = NULL,
2011 .commandPool = demo->present_cmd_pool,
2012 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2013 .commandBufferCount = 1,
2014 };
2015 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2016 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002017 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002018 assert(!err);
2019 demo_build_image_ownership_cmd(demo, i);
2020 }
2021 }
2022
Chia-I Wu63ea9262015-03-26 13:14:16 +08002023 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002024 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002025
Chia-I Wuf973e322015-07-08 13:34:24 +08002026 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002027
Ian Elliotta0781972015-08-21 15:09:33 -06002028 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002029 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002030 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002031 }
2032
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002033 /*
2034 * Prepare functions above may generate pipeline commands
2035 * that need to be flushed before beginning the render loop.
2036 */
2037 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002038 if (demo->staging_texture.image) {
2039 demo_destroy_texture_image(demo, &demo->staging_texture);
2040 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002041
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002042 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002043 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044}
2045
Karl Schultze4dc75c2016-02-02 15:37:51 -07002046static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002047 uint32_t i;
2048
2049 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002050 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002051
Tony Barbour66a56c32016-09-21 13:10:56 -06002052 // Wait for fences from present operations
2053 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002054 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002055 vkDestroyFence(demo->device, demo->fences[i], NULL);
2056 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2057 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2058 if (demo->separate_present_queue) {
2059 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2060 }
2061 }
2062
Tony Barbourd8e504f2015-10-08 14:26:24 -06002063 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002064 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002065 }
Chia-I Wu63636062015-10-26 21:10:41 +08002066 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002067
Chia-I Wu63636062015-10-26 21:10:41 +08002068 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2069 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2070 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2071 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2072 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002073
2074 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002075 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2076 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2077 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2078 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002079 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002080 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002081
Chia-I Wu63636062015-10-26 21:10:41 +08002082 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2083 vkDestroyImage(demo->device, demo->depth.image, NULL);
2084 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002085
Ian Elliotta0781972015-08-21 15:09:33 -06002086 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002087 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002088 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002089 &demo->swapchain_image_resources[i].cmd);
2090 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2091 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2092 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002093 }
Tony Barboura72d9492017-01-11 13:35:09 -07002094 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002095 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002096 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002097
Tony Barbour22e06272016-09-07 16:07:56 -06002098 if (demo->separate_present_queue) {
2099 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002100 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002101 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002102 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002103 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002104 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002105 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002106 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002107 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002108
Tony Barbour153cb062016-12-07 13:43:36 -07002109#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002110 XDestroyWindow(demo->display, demo->xlib_window);
2111 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002112#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002113 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002114 xcb_disconnect(demo->connection);
2115 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002116#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2117 wl_shell_surface_destroy(demo->shell_surface);
2118 wl_surface_destroy(demo->window);
2119 wl_shell_destroy(demo->shell);
2120 wl_compositor_destroy(demo->compositor);
2121 wl_registry_destroy(demo->registry);
2122 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002123#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002124#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002125}
2126
Karl Schultze4dc75c2016-02-02 15:37:51 -07002127static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002128 uint32_t i;
2129
Mike Stroyanbb60b382015-10-28 09:46:57 -06002130 // Don't react to resize until after first initialization.
2131 if (!demo->prepared) {
2132 return;
2133 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002134 // In order to properly resize the window, we must re-create the swapchain
2135 // AND redo the command buffers, etc.
2136 //
2137 // First, perform part of the demo_cleanup() function:
2138 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002139 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002140
2141 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002142 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002143 }
Chia-I Wu63636062015-10-26 21:10:41 +08002144 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002145
Chia-I Wu63636062015-10-26 21:10:41 +08002146 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2147 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2148 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2149 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2150 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002151
2152 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002153 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2154 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2155 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2156 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002157 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002158
Chia-I Wu63636062015-10-26 21:10:41 +08002159 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2160 vkDestroyImage(demo->device, demo->depth.image, NULL);
2161 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002162
Ian Elliottcf074d32015-10-16 18:02:43 -06002163 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002164 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002165 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002166 &demo->swapchain_image_resources[i].cmd);
2167 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2168 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2169 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002170 }
Chia-I Wu63636062015-10-26 21:10:41 +08002171 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002172 if (demo->separate_present_queue) {
2173 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2174 }
Tony Barboura72d9492017-01-11 13:35:09 -07002175 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002176
Ian Elliottcf074d32015-10-16 18:02:43 -06002177 // Second, re-perform the demo_prepare() function, which will re-create the
2178 // swapchain:
2179 demo_prepare(demo);
2180}
2181
David Pinedo2bb7c932015-06-18 17:03:14 -06002182// On MS-Windows, make this a global, so it's available to WndProc()
2183struct demo demo;
2184
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002185#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002187 if (!demo->prepared)
2188 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002189
Ian Elliott639ca472015-04-16 15:23:05 -06002190 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002191 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002192 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002193 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002194 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002195}
Ian Elliott639ca472015-04-16 15:23:05 -06002196
2197// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002198LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2199 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002200 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002201 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002202 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002203 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002204 // The validation callback calls MessageBox which can generate paint
2205 // events - don't make more Vulkan calls if we got here from the
2206 // callback
2207 if (!in_callback) {
2208 demo_run(&demo);
2209 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002210 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002211 case WM_GETMINMAXINFO: // set window's minimum size
2212 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2213 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002214 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002215 // Resize the application to the new window size, except when
2216 // it was minimized. Vulkan doesn't support images or swapchains
2217 // with width=0 and height=0.
2218 if (wParam != SIZE_MINIMIZED) {
2219 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002220 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002221 demo_resize(&demo);
2222 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002223 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002224 default:
2225 break;
2226 }
2227 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2228}
2229
Karl Schultze4dc75c2016-02-02 15:37:51 -07002230static void demo_create_window(struct demo *demo) {
2231 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002232
2233 // Initialize the window class structure:
2234 win_class.cbSize = sizeof(WNDCLASSEX);
2235 win_class.style = CS_HREDRAW | CS_VREDRAW;
2236 win_class.lpfnWndProc = WndProc;
2237 win_class.cbClsExtra = 0;
2238 win_class.cbWndExtra = 0;
2239 win_class.hInstance = demo->connection; // hInstance
2240 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2241 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2242 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2243 win_class.lpszMenuName = NULL;
2244 win_class.lpszClassName = demo->name;
2245 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2246 // Register window class:
2247 if (!RegisterClassEx(&win_class)) {
2248 // It didn't work, so try to give a useful error:
2249 printf("Unexpected error trying to start the application!\n");
2250 fflush(stdout);
2251 exit(1);
2252 }
2253 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002254 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002255 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002256 demo->window = CreateWindowEx(0,
2257 demo->name, // class name
2258 demo->name, // app name
2259 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002260 WS_VISIBLE | WS_SYSMENU,
2261 100, 100, // x/y coords
2262 wr.right - wr.left, // width
2263 wr.bottom - wr.top, // height
2264 NULL, // handle to parent
2265 NULL, // handle to menu
2266 demo->connection, // hInstance
2267 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002268 if (!demo->window) {
2269 // It didn't work, so try to give a useful error:
2270 printf("Cannot create a window in which to draw!\n");
2271 fflush(stdout);
2272 exit(1);
2273 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002274 // Window client area size must be at least 1 pixel high, to prevent crash.
2275 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2276 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002277}
Tony Barbour8295ac42016-08-08 11:04:17 -06002278#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002279static void demo_create_xlib_window(struct demo *demo) {
2280
Tony Barbouree9cd372017-01-31 16:09:58 -07002281 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002282 demo->display = XOpenDisplay(NULL);
2283 long visualMask = VisualScreenMask;
2284 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002285 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002286 vInfoTemplate.screen = DefaultScreen(demo->display);
2287 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2288 &vInfoTemplate, &numberOfVisuals);
2289
2290 Colormap colormap = XCreateColormap(
2291 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2292 visualInfo->visual, AllocNone);
2293
Rene Lindsay11612722016-06-13 17:20:39 -06002294 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002295 windowAttributes.colormap = colormap;
2296 windowAttributes.background_pixel = 0xFFFFFFFF;
2297 windowAttributes.border_pixel = 0;
2298 windowAttributes.event_mask =
2299 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2300
2301 demo->xlib_window = XCreateWindow(
2302 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2303 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2304 visualInfo->visual,
2305 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2306
2307 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2308 XMapWindow(demo->display, demo->xlib_window);
2309 XFlush(demo->display);
2310 demo->xlib_wm_delete_window =
2311 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2312}
2313static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2314 switch(event->type) {
2315 case ClientMessage:
2316 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2317 demo->quit = true;
2318 break;
2319 case KeyPress:
2320 switch (event->xkey.keycode) {
2321 case 0x9: // Escape
2322 demo->quit = true;
2323 break;
2324 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002325 demo->spin_angle -= demo->spin_increment;
2326 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002327 case 0x72: // right arrow key
2328 demo->spin_angle += demo->spin_increment;
2329 break;
2330 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002331 demo->pause = !demo->pause;
2332 break;
2333 }
2334 break;
2335 case ConfigureNotify:
2336 if ((demo->width != event->xconfigure.width) ||
2337 (demo->height != event->xconfigure.height)) {
2338 demo->width = event->xconfigure.width;
2339 demo->height = event->xconfigure.height;
2340 demo_resize(demo);
2341 }
2342 break;
2343 default:
2344 break;
2345 }
2346
2347}
2348
2349static void demo_run_xlib(struct demo *demo) {
2350
2351 while (!demo->quit) {
2352 XEvent event;
2353
2354 if (demo->pause) {
2355 XNextEvent(demo->display, &event);
2356 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002357 }
2358 while (XPending(demo->display) > 0) {
2359 XNextEvent(demo->display, &event);
2360 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002361 }
2362
Tony Barbour2593b182016-04-19 10:57:58 -06002363 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002364 demo->curFrame++;
2365 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2366 demo->quit = true;
2367 }
2368}
Tony Barbour153cb062016-12-07 13:43:36 -07002369#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002370static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002371 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002372 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002373 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002374 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002375 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002376 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002377 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002378 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2379 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002380 demo->quit = true;
2381 }
2382 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002383 case XCB_KEY_RELEASE: {
2384 const xcb_key_release_event_t *key =
2385 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002386
Karl Schultze4dc75c2016-02-02 15:37:51 -07002387 switch (key->detail) {
2388 case 0x9: // Escape
2389 demo->quit = true;
2390 break;
2391 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002392 demo->spin_angle -= demo->spin_increment;
2393 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002394 case 0x72: // right arrow key
2395 demo->spin_angle += demo->spin_increment;
2396 break;
2397 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002398 demo->pause = !demo->pause;
2399 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002400 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002401 } break;
2402 case XCB_CONFIGURE_NOTIFY: {
2403 const xcb_configure_notify_event_t *cfg =
2404 (const xcb_configure_notify_event_t *)event;
2405 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002406 demo->width = cfg->width;
2407 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002408 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002409 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002410 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002411 default:
2412 break;
2413 }
2414}
2415
Tony Barbour2593b182016-04-19 10:57:58 -06002416static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002417 xcb_flush(demo->connection);
2418
2419 while (!demo->quit) {
2420 xcb_generic_event_t *event;
2421
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002422 if (demo->pause) {
2423 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002424 }
2425 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002426 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002427 }
2428 while (event) {
2429 demo_handle_xcb_event(demo, event);
2430 free(event);
2431 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002432 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002433
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002434 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002435 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002436 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002437 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002438 }
2439}
2440
Tony Barbour2593b182016-04-19 10:57:58 -06002441static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002442 uint32_t value_mask, value_list[32];
2443
Tony Barbour2593b182016-04-19 10:57:58 -06002444 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002445
2446 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2447 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002448 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002449 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002450
Tony Barbour2593b182016-04-19 10:57:58 -06002451 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002452 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2453 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2454 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002455
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002456 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002457 xcb_intern_atom_cookie_t cookie =
2458 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2459 xcb_intern_atom_reply_t *reply =
2460 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002461
Karl Schultze4dc75c2016-02-02 15:37:51 -07002462 xcb_intern_atom_cookie_t cookie2 =
2463 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2464 demo->atom_wm_delete_window =
2465 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002466
Tony Barbour2593b182016-04-19 10:57:58 -06002467 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002468 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002469 &(*demo->atom_wm_delete_window).atom);
2470 free(reply);
2471
Tony Barbour2593b182016-04-19 10:57:58 -06002472 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002473
Karl Schultze4dc75c2016-02-02 15:37:51 -07002474 // Force the x/y coordinates to 100,100 results are identical in consecutive
2475 // runs
2476 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002477 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002478 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002479}
Tony Barbour6b131662016-08-25 13:14:45 -06002480// VK_USE_PLATFORM_XCB_KHR
2481#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002482static void demo_run(struct demo *demo) {
2483 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002484 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002485 demo->curFrame++;
2486 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2487 demo->quit = true;
2488 }
2489}
2490
2491static void handle_ping(void *data UNUSED,
2492 struct wl_shell_surface *shell_surface,
2493 uint32_t serial) {
2494 wl_shell_surface_pong(shell_surface, serial);
2495}
2496
2497static void handle_configure(void *data UNUSED,
2498 struct wl_shell_surface *shell_surface UNUSED,
2499 uint32_t edges UNUSED, int32_t width UNUSED,
2500 int32_t height UNUSED) {}
2501
2502static void handle_popup_done(void *data UNUSED,
2503 struct wl_shell_surface *shell_surface UNUSED) {}
2504
2505static const struct wl_shell_surface_listener shell_surface_listener = {
2506 handle_ping, handle_configure, handle_popup_done};
2507
2508static void demo_create_window(struct demo *demo) {
2509 demo->window = wl_compositor_create_surface(demo->compositor);
2510 if (!demo->window) {
2511 printf("Can not create wayland_surface from compositor!\n");
2512 fflush(stdout);
2513 exit(1);
2514 }
2515
2516 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2517 if (!demo->shell_surface) {
2518 printf("Can not get shell_surface from wayland_surface!\n");
2519 fflush(stdout);
2520 exit(1);
2521 }
2522 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2523 demo);
2524 wl_shell_surface_set_toplevel(demo->shell_surface);
2525 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2526}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002527#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2528static void demo_run(struct demo *demo) {
2529 if (!demo->prepared)
2530 return;
2531
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002532 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002533 demo->curFrame++;
2534}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002535#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002536#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2537static VkResult demo_create_display_surface(struct demo *demo) {
2538 VkResult U_ASSERT_ONLY err;
2539 uint32_t display_count;
2540 uint32_t mode_count;
2541 uint32_t plane_count;
2542 VkDisplayPropertiesKHR display_props;
2543 VkDisplayKHR display;
2544 VkDisplayModePropertiesKHR mode_props;
2545 VkDisplayPlanePropertiesKHR *plane_props;
2546 VkBool32 found_plane = VK_FALSE;
2547 uint32_t plane_index;
2548 VkExtent2D image_extent;
2549 VkDisplaySurfaceCreateInfoKHR create_info;
2550
2551 // Get the first display
2552 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2553 assert(!err);
2554
2555 if (display_count == 0) {
2556 printf("Cannot find any display!\n");
2557 fflush(stdout);
2558 exit(1);
2559 }
2560
2561 display_count = 1;
2562 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2563 assert(!err || (err == VK_INCOMPLETE));
2564
2565 display = display_props.display;
2566
2567 // Get the first mode of the display
2568 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2569 assert(!err);
2570
2571 if (mode_count == 0) {
2572 printf("Cannot find any mode for the display!\n");
2573 fflush(stdout);
2574 exit(1);
2575 }
2576
2577 mode_count = 1;
2578 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2579 assert(!err || (err == VK_INCOMPLETE));
2580
2581 // Get the list of planes
2582 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2583 assert(!err);
2584
2585 if (plane_count == 0) {
2586 printf("Cannot find any plane!\n");
2587 fflush(stdout);
2588 exit(1);
2589 }
2590
2591 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2592 assert(plane_props);
2593
2594 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2595 assert(!err);
2596
2597 // Find a plane compatible with the display
2598 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2599 uint32_t supported_count;
2600 VkDisplayKHR *supported_displays;
2601
2602 // Disqualify planes that are bound to a different display
2603 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2604 (plane_props[plane_index].currentDisplay != display)) {
2605 continue;
2606 }
2607
2608 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2609 assert(!err);
2610
2611 if (supported_count == 0) {
2612 continue;
2613 }
2614
2615 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2616 assert(supported_displays);
2617
2618 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2619 assert(!err);
2620
2621 for (uint32_t i = 0; i < supported_count; i++) {
2622 if (supported_displays[i] == display) {
2623 found_plane = VK_TRUE;
2624 break;
2625 }
2626 }
2627
2628 free(supported_displays);
2629
2630 if (found_plane) {
2631 break;
2632 }
2633 }
2634
2635 if (!found_plane) {
2636 printf("Cannot find a plane compatible with the display!\n");
2637 fflush(stdout);
2638 exit(1);
2639 }
2640
2641 free(plane_props);
2642
Tony Barbour820b55b2017-03-14 08:55:46 -06002643 VkDisplayPlaneCapabilitiesKHR planeCaps;
2644 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2645 // Find a supported alpha mode
2646 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2647 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
2648 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2649 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2650 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2651 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2652 };
2653 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2654 if (planeCaps.supportedAlpha & alphaModes[i]) {
2655 alphaMode = alphaModes[i];
2656 break;
2657 }
2658 }
Damien Leone600c3052017-01-31 10:26:07 -07002659 image_extent.width = mode_props.parameters.visibleRegion.width;
2660 image_extent.height = mode_props.parameters.visibleRegion.height;
2661
2662 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2663 create_info.pNext = NULL;
2664 create_info.flags = 0;
2665 create_info.displayMode = mode_props.displayMode;
2666 create_info.planeIndex = plane_index;
2667 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2668 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002669 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002670 create_info.globalAlpha = 1.0f;
2671 create_info.imageExtent = image_extent;
2672
2673 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2674}
2675
2676static void demo_run_display(struct demo *demo)
2677{
2678 while (!demo->quit) {
2679 demo_draw(demo);
2680 demo->curFrame++;
2681
2682 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2683 demo->quit = true;
2684 }
2685 }
2686}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002687#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002688
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002689/*
2690 * Return 1 (true) if all layer names specified in check_names
2691 * can be found in given layer properties.
2692 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002693static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002694 uint32_t layer_count,
2695 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002696 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002697 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002698 for (uint32_t j = 0; j < layer_count; j++) {
2699 if (!strcmp(check_names[i], layers[j].layerName)) {
2700 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002701 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002702 }
2703 }
2704 if (!found) {
2705 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2706 return 0;
2707 }
2708 }
2709 return 1;
2710}
2711
Karl Schultze4dc75c2016-02-02 15:37:51 -07002712static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002713 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002714 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002715 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002716 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002717 char **instance_validation_layers = NULL;
2718 demo->enabled_extension_count = 0;
2719 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002720
Karl Schultz7c50ad62016-04-01 12:07:03 -06002721 char *instance_validation_layers_alt1[] = {
2722 "VK_LAYER_LUNARG_standard_validation"
2723 };
2724
2725 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07002726 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2727 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2728 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002729 };
2730
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002731 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002732 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002733 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002734
Karl Schultz7c50ad62016-04-01 12:07:03 -06002735 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002736 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002737
Karl Schultz7c50ad62016-04-01 12:07:03 -06002738 instance_validation_layers = instance_validation_layers_alt1;
2739 if (instance_layer_count > 0) {
2740 VkLayerProperties *instance_layers =
2741 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2742 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2743 instance_layers);
2744 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002745
Karl Schultz7c50ad62016-04-01 12:07:03 -06002746
2747 validation_found = demo_check_layers(
2748 ARRAY_SIZE(instance_validation_layers_alt1),
2749 instance_validation_layers, instance_layer_count,
2750 instance_layers);
2751 if (validation_found) {
2752 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002753 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2754 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002755 } else {
2756 // use alternative set of validation layers
2757 instance_validation_layers = instance_validation_layers_alt2;
2758 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2759 validation_found = demo_check_layers(
2760 ARRAY_SIZE(instance_validation_layers_alt2),
2761 instance_validation_layers, instance_layer_count,
2762 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002763 validation_layer_count =
2764 ARRAY_SIZE(instance_validation_layers_alt2);
2765 for (uint32_t i = 0; i < validation_layer_count; i++) {
2766 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002767 }
2768 }
2769 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002770 }
Dustin Graves7c287352016-01-27 13:48:06 -07002771
Karl Schultz7c50ad62016-04-01 12:07:03 -06002772 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002773 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002774 "required validation layer.\n\n"
2775 "Please look at the Getting Started guide for additional "
2776 "information.\n",
2777 "vkCreateInstance Failure");
2778 }
Dustin Graves7c287352016-01-27 13:48:06 -07002779 }
2780
2781 /* Look for instance extensions */
2782 VkBool32 surfaceExtFound = 0;
2783 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002784 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002785
Karl Schultze4dc75c2016-02-02 15:37:51 -07002786 err = vkEnumerateInstanceExtensionProperties(
2787 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002788 assert(!err);
2789
Dustin Graves7c287352016-01-27 13:48:06 -07002790 if (instance_extension_count > 0) {
2791 VkExtensionProperties *instance_extensions =
2792 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2793 err = vkEnumerateInstanceExtensionProperties(
2794 NULL, &instance_extension_count, instance_extensions);
2795 assert(!err);
2796 for (uint32_t i = 0; i < instance_extension_count; i++) {
2797 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2798 instance_extensions[i].extensionName)) {
2799 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002800 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002801 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002802 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002803#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002804 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2805 instance_extensions[i].extensionName)) {
2806 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002807 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002808 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2809 }
Tony Barbour153cb062016-12-07 13:43:36 -07002810#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002811 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2812 instance_extensions[i].extensionName)) {
2813 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06002814 demo->extension_names[demo->enabled_extension_count++] =
2815 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2816 }
Tony Barbour153cb062016-12-07 13:43:36 -07002817#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002818 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2819 instance_extensions[i].extensionName)) {
2820 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002821 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002822 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2823 }
Tony Barbour153cb062016-12-07 13:43:36 -07002824#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002825 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2826 instance_extensions[i].extensionName)) {
2827 platformSurfaceExtFound = 1;
2828 demo->extension_names[demo->enabled_extension_count++] =
2829 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2830 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002831#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002832#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2833 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
2834 instance_extensions[i].extensionName)) {
2835 platformSurfaceExtFound = 1;
2836 demo->extension_names[demo->enabled_extension_count++] =
2837 VK_KHR_DISPLAY_EXTENSION_NAME;
2838 }
Tony Barbour153cb062016-12-07 13:43:36 -07002839#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002840 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2841 instance_extensions[i].extensionName)) {
2842 platformSurfaceExtFound = 1;
2843 demo->extension_names[demo->enabled_extension_count++] =
2844 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2845 }
Bill Hollingsf4352142017-02-14 22:58:56 -05002846#elif defined(VK_USE_PLATFORM_IOS_MVK)
2847 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2848 platformSurfaceExtFound = 1;
2849 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
2850 }
2851#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2852 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2853 platformSurfaceExtFound = 1;
2854 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
2855 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002856#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002857 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2858 instance_extensions[i].extensionName)) {
2859 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002860 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002861 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2862 }
2863 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002864 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002865 }
Dustin Graves7c287352016-01-27 13:48:06 -07002866
2867 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002868 }
Dustin Graves7c287352016-01-27 13:48:06 -07002869
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002870 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002871 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2872 "the " VK_KHR_SURFACE_EXTENSION_NAME
2873 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002874 "Vulkan installable client driver (ICD) installed?\nPlease "
2875 "look at the Getting Started guide for additional "
2876 "information.\n",
2877 "vkCreateInstance Failure");
2878 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002879 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002880#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002881 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2882 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2883 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002884 "Vulkan installable client driver (ICD) installed?\nPlease "
2885 "look at the Getting Started guide for additional "
2886 "information.\n",
2887 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002888#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002889 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2890 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2891 "Vulkan installable client driver (ICD) installed?\nPlease "
2892 "look at the Getting Started guide for additional "
2893 "information.\n",
2894 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002895#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002896 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2897 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2898 "Vulkan installable client driver (ICD) installed?\nPlease "
2899 "look at the Getting Started guide for additional "
2900 "information.\n",
2901 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002902#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002903 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2904 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2905 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002906 "Vulkan installable client driver (ICD) installed?\nPlease "
2907 "look at the Getting Started guide for additional "
2908 "information.\n",
2909 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002910#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2911 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2912 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2913 " extension.\n\nDo you have a compatible "
2914 "Vulkan installable client driver (ICD) installed?\nPlease "
2915 "look at the Getting Started guide for additional "
2916 "information.\n",
2917 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002918#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002919#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2920 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2921 "the " VK_KHR_DISPLAY_EXTENSION_NAME
2922 " extension.\n\nDo you have a compatible "
2923 "Vulkan installable client driver (ICD) installed?\nPlease "
2924 "look at the Getting Started guide for additional "
2925 "information.\n",
2926 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002927#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2928 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2929 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2930 " extension.\n\nDo you have a compatible "
2931 "Vulkan installable client driver (ICD) installed?\nPlease "
2932 "look at the Getting Started guide for additional "
2933 "information.\n",
2934 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002935#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002936 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2937 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2938 " extension.\n\nDo you have a compatible "
2939 "Vulkan installable client driver (ICD) installed?\nPlease "
2940 "look at the Getting Started guide for additional "
2941 "information.\n",
2942 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002943#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002944 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002945 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002946 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002947 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002948 .pApplicationName = APP_SHORT_NAME,
2949 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002950 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002951 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002952 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002953 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002954 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002955 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002956 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002957 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002958 .enabledLayerCount = demo->enabled_layer_count,
2959 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2960 .enabledExtensionCount = demo->enabled_extension_count,
2961 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002962 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002963
2964 /*
2965 * This is info for a temp callback to use during CreateInstance.
2966 * After the instance is created, we use the instance-based
2967 * function to register the final callback.
2968 */
Karl Schultza2615462017-01-20 13:19:20 -07002969 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002970 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002971 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07002972 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2973 dbgCreateInfoTemp.pNext = NULL;
2974 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
2975 dbgCreateInfoTemp.pUserData = demo;
2976 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06002977 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002978 if (demo->validate_checks_disabled) {
2979 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
2980 val_flags.pNext = NULL;
2981 val_flags.disabledValidationCheckCount = 1;
2982 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
2983 val_flags.pDisabledValidationChecks = &disabled_check;
2984 dbgCreateInfoTemp.pNext = (void*)&val_flags;
2985 }
Karl Schultza2615462017-01-20 13:19:20 -07002986 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002987 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002988
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002989 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002990
Chia-I Wu63636062015-10-26 21:10:41 +08002991 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002992 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2993 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002994 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002995 "additional information.\n",
2996 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002997 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002998 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002999 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003000 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003001 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06003002 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3003 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003004 "the Getting Started guide for additional information.\n",
3005 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003006 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003007
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003008 /* Make initial call to query gpu_count, then second call for gpu info*/
3009 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3010 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003011
3012 if (gpu_count > 0) {
3013 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3014 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3015 assert(!err);
3016 /* For cube demo we just grab the first physical device */
3017 demo->gpu = physical_devices[0];
3018 free(physical_devices);
3019 } else {
3020 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3021 "Do you have a compatible Vulkan installable client driver (ICD) "
3022 "installed?\nPlease look at the Getting Started guide for "
3023 "additional information.\n",
3024 "vkEnumeratePhysicalDevices Failure");
3025 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003026
Dustin Graves7c287352016-01-27 13:48:06 -07003027 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003028 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003029 VkBool32 swapchainExtFound = 0;
3030 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003031 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003032
Karl Schultze4dc75c2016-02-02 15:37:51 -07003033 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3034 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003035 assert(!err);
3036
Dustin Graves7c287352016-01-27 13:48:06 -07003037 if (device_extension_count > 0) {
3038 VkExtensionProperties *device_extensions =
3039 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3040 err = vkEnumerateDeviceExtensionProperties(
3041 demo->gpu, NULL, &device_extension_count, device_extensions);
3042 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003043
Dustin Graves7c287352016-01-27 13:48:06 -07003044 for (uint32_t i = 0; i < device_extension_count; i++) {
3045 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3046 device_extensions[i].extensionName)) {
3047 swapchainExtFound = 1;
3048 demo->extension_names[demo->enabled_extension_count++] =
3049 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3050 }
3051 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003052 }
Dustin Graves7c287352016-01-27 13:48:06 -07003053
3054 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003055 }
Dustin Graves7c287352016-01-27 13:48:06 -07003056
Tony Barbourcc69f452015-10-08 13:59:42 -06003057 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003058 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3059 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3060 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003061 "Vulkan installable client driver (ICD) installed?\nPlease "
3062 "look at the Getting Started guide for additional "
3063 "information.\n",
3064 "vkCreateInstance Failure");
3065 }
3066
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003067 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003068 demo->CreateDebugReportCallback =
3069 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3070 demo->inst, "vkCreateDebugReportCallbackEXT");
3071 demo->DestroyDebugReportCallback =
3072 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3073 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003074 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003075 ERR_EXIT(
3076 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3077 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003078 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003079 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003080 ERR_EXIT(
3081 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3082 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003083 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003084 demo->DebugReportMessage =
3085 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3086 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003087 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003088 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003089 "vkGetProcAddr Failure");
3090 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003091
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003092 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003093 PFN_vkDebugReportCallbackEXT callback;
3094 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003095 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003096 dbgCreateInfo.pNext = NULL;
3097 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003098 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003099 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003100 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003101 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3102 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003103 switch (err) {
3104 case VK_SUCCESS:
3105 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003106 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003107 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3108 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003109 break;
3110 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003111 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3112 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003113 break;
3114 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003115 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003116 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003117
3118 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003119 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3120 &demo->queue_family_count, NULL);
3121 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003122
Karl Schultze4dc75c2016-02-02 15:37:51 -07003123 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003124 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3125 vkGetPhysicalDeviceQueueFamilyProperties(
3126 demo->gpu, &demo->queue_family_count, demo->queue_props);
3127
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003128 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003129 // If app has specific feature requirements it should check supported
3130 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003131 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003132 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003133
Tony Barbourda2bad52016-01-22 14:36:40 -07003134 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3135 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3136 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3137 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3138 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3139}
3140
Karl Schultze4dc75c2016-02-02 15:37:51 -07003141static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003142 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003143 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003144 VkDeviceQueueCreateInfo queues[2];
3145 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3146 queues[0].pNext = NULL;
3147 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3148 queues[0].queueCount = 1;
3149 queues[0].pQueuePriorities = queue_priorities;
3150 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003151
3152 VkDeviceCreateInfo device = {
3153 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3154 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003155 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003156 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003157 .enabledLayerCount = 0,
3158 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003159 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003160 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3161 .pEnabledFeatures =
3162 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003163 };
Tony Barbour22e06272016-09-07 16:07:56 -06003164 if (demo->separate_present_queue) {
3165 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3166 queues[1].pNext = NULL;
3167 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3168 queues[1].queueCount = 1;
3169 queues[1].pQueuePriorities = queue_priorities;
3170 queues[1].flags = 0;
3171 device.queueCreateInfoCount = 2;
3172 }
Chia-I Wu63636062015-10-26 21:10:41 +08003173 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003174 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003175}
3176
Karl Schultze4dc75c2016-02-02 15:37:51 -07003177static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003178 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003179
Karl Schultze4dc75c2016-02-02 15:37:51 -07003180// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003181#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003182 VkWin32SurfaceCreateInfoKHR createInfo;
3183 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3184 createInfo.pNext = NULL;
3185 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003186 createInfo.hinstance = demo->connection;
3187 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003188
Karl Schultze4dc75c2016-02-02 15:37:51 -07003189 err =
3190 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003191#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003192 VkWaylandSurfaceCreateInfoKHR createInfo;
3193 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3194 createInfo.pNext = NULL;
3195 createInfo.flags = 0;
3196 createInfo.display = demo->display;
3197 createInfo.surface = demo->window;
3198
3199 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3200 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003201#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003202#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3203 VkAndroidSurfaceCreateInfoKHR createInfo;
3204 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3205 createInfo.pNext = NULL;
3206 createInfo.flags = 0;
3207 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003208
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003209 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003210#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3211 VkXlibSurfaceCreateInfoKHR createInfo;
3212 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3213 createInfo.pNext = NULL;
3214 createInfo.flags = 0;
3215 createInfo.dpy = demo->display;
3216 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003217
Tony Barbour153cb062016-12-07 13:43:36 -07003218 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003219 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003220#elif defined(VK_USE_PLATFORM_XCB_KHR)
3221 VkXcbSurfaceCreateInfoKHR createInfo;
3222 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3223 createInfo.pNext = NULL;
3224 createInfo.flags = 0;
3225 createInfo.connection = demo->connection;
3226 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003227
Tony Barbour153cb062016-12-07 13:43:36 -07003228 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003229#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3230 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003231#elif defined(VK_USE_PLATFORM_IOS_MVK)
3232 VkIOSSurfaceCreateInfoMVK surface;
3233 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3234 surface.pNext = NULL;
3235 surface.flags = 0;
3236 surface.pView = demo->window;
3237
3238 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3239#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3240 VkMacOSSurfaceCreateInfoMVK surface;
3241 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3242 surface.pNext = NULL;
3243 surface.flags = 0;
3244 surface.pView = demo->window;
3245
3246 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003247#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003248 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003249
Tony Barbourcc69f452015-10-08 13:59:42 -06003250 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003251 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003252 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003253 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003254 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003255 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003256 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003257
3258 // Search for a graphics and a present queue in the array of queue
3259 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003260 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3261 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003262 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003263 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3264 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3265 graphicsQueueFamilyIndex = i;
3266 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003267
Tony Barbourab2a0362016-09-06 11:40:12 -06003268 if (supportsPresent[i] == VK_TRUE) {
3269 graphicsQueueFamilyIndex = i;
3270 presentQueueFamilyIndex = i;
3271 break;
3272 }
3273 }
3274 }
3275
3276 if (presentQueueFamilyIndex == UINT32_MAX) {
3277 // If didn't find a queue that supports both graphics and present, then
3278 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003279 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003280 if (supportsPresent[i] == VK_TRUE) {
3281 presentQueueFamilyIndex = i;
3282 break;
3283 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003284 }
3285 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003286
3287 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003288 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3289 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003290 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003291 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003292 }
3293
Tony Barbourab2a0362016-09-06 11:40:12 -06003294 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3295 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003296 demo->separate_present_queue =
3297 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003298 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003299
Tony Barbourda2bad52016-01-22 14:36:40 -07003300 demo_create_device(demo);
3301
3302 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3303 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3304 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3305 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3306 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3307
Tony Barboura2a67812016-07-18 13:21:06 -06003308 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3309 &demo->graphics_queue);
3310
Tony Barbour22e06272016-09-07 16:07:56 -06003311 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003312 demo->present_queue = demo->graphics_queue;
3313 } else {
3314 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3315 &demo->present_queue);
3316 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003317
Ian Elliottaaae5352015-07-06 14:27:58 -06003318 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003319 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003320 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003321 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003322 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003323 VkSurfaceFormatKHR *surfFormats =
3324 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003325 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003326 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003327 assert(!err);
3328 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3329 // the surface has no preferred format. Otherwise, at least one
3330 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003331 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003332 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003333 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003334 assert(formatCount >= 1);
3335 demo->format = surfFormats[0].format;
3336 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003337 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003338
David Pinedo2bb7c932015-06-18 17:03:14 -06003339 demo->quit = false;
3340 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003341
Tony Barbource7524e2016-07-28 12:01:45 -06003342 // Create semaphores to synchronize acquiring presentable buffers before
3343 // rendering and waiting for drawing to be complete before presenting
3344 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3345 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3346 .pNext = NULL,
3347 .flags = 0,
3348 };
Tony Barbource7524e2016-07-28 12:01:45 -06003349
Tony Barbour66a56c32016-09-21 13:10:56 -06003350 // Create fences that we can use to throttle if we get too far
3351 // ahead of the image presents
3352 VkFenceCreateInfo fence_ci = {
3353 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3354 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003355 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003356 };
3357 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003358 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3359 assert(!err);
3360
Tony Barbour22e06272016-09-07 16:07:56 -06003361 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003362 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003363 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003364
3365 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3366 &demo->draw_complete_semaphores[i]);
3367 assert(!err);
3368
3369 if (demo->separate_present_queue) {
3370 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3371 &demo->image_ownership_semaphores[i]);
3372 assert(!err);
3373 }
Tony Barbour22e06272016-09-07 16:07:56 -06003374 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003375 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003376
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003377 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003378 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003379}
3380
Tony Barbour153cb062016-12-07 13:43:36 -07003381#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003382static void registry_handle_global(void *data, struct wl_registry *registry,
3383 uint32_t name, const char *interface,
3384 uint32_t version UNUSED) {
3385 struct demo *demo = data;
3386 if (strcmp(interface, "wl_compositor") == 0) {
3387 demo->compositor =
3388 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3389 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3390 * tp xdg_shell */
3391 } else if (strcmp(interface, "wl_shell") == 0) {
3392 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3393 }
3394}
3395
3396static void registry_handle_global_remove(void *data UNUSED,
3397 struct wl_registry *registry UNUSED,
3398 uint32_t name UNUSED) {}
3399
3400static const struct wl_registry_listener registry_listener = {
3401 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003402#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003403#endif
3404
Karl Schultze4dc75c2016-02-02 15:37:51 -07003405static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003406#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003407 const xcb_setup_t *setup;
3408 xcb_screen_iterator_t iter;
3409 int scr;
3410
3411 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003412 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003413 printf("Cannot find a compatible Vulkan installable client driver "
3414 "(ICD).\nExiting ...\n");
3415 fflush(stdout);
3416 exit(1);
3417 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003418
3419 setup = xcb_get_setup(demo->connection);
3420 iter = xcb_setup_roots_iterator(setup);
3421 while (scr-- > 0)
3422 xcb_screen_next(&iter);
3423
3424 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003425#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3426 demo->display = wl_display_connect(NULL);
3427
3428 if (demo->display == NULL) {
3429 printf("Cannot find a compatible Vulkan installable client driver "
3430 "(ICD).\nExiting ...\n");
3431 fflush(stdout);
3432 exit(1);
3433 }
3434
3435 demo->registry = wl_display_get_registry(demo->display);
3436 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3437 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003438#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003439#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003440}
3441
Karl Schultze4dc75c2016-02-02 15:37:51 -07003442static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003443 vec3 eye = {0.0f, 3.0f, 5.0f};
3444 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003445 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003446
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003447 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003448 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003449 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003450
Piers Daniell735ee532015-02-23 16:23:13 -07003451 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003452 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003453 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003454 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003455 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003456 if ((strcmp(argv[i], "--present_mode") == 0) &&
3457 (i < argc - 1)) {
3458 demo->presentMode = atoi(argv[i+1]);
3459 i++;
3460 continue;
3461 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003462 if (strcmp(argv[i], "--break") == 0) {
3463 demo->use_break = true;
3464 continue;
3465 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003466 if (strcmp(argv[i], "--validate") == 0) {
3467 demo->validate = true;
3468 continue;
3469 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003470 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3471 demo->validate = true;
3472 demo->validate_checks_disabled = true;
3473 continue;
3474 }
Tony Barbour2593b182016-04-19 10:57:58 -06003475 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003476 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003477 continue;
3478 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003479 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3480 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3481 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003482 i++;
3483 continue;
3484 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003485 if (strcmp(argv[i], "--suppress_popups") == 0) {
3486 demo->suppress_popups = true;
3487 continue;
3488 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003489
Cody Northropaf28a532016-09-27 10:50:57 -06003490#if defined(ANDROID)
3491 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3492#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003493 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Tony Barbour291d57b2016-10-21 14:47:07 -06003494 "[--c <framecount>] [--suppress_popups] [--present_mode <present mode enum>]\n"
3495 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3496 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3497 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3498 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3499 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3500 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003501 fflush(stderr);
3502 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003503#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003504 }
3505
Tony Barbour153cb062016-12-07 13:43:36 -07003506 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003507
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003508 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003509
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003510 demo->width = 500;
3511 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003512
Tony Barbour66a56c32016-09-21 13:10:56 -06003513 demo->spin_angle = 4.0f;
3514 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003515 demo->pause = false;
3516
Karl Schultze4dc75c2016-02-02 15:37:51 -07003517 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3518 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003519 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3520 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003521
3522 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003523}
3524
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003525#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003526// Include header required for parsing the command line options.
3527#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003528
Karl Schultze4dc75c2016-02-02 15:37:51 -07003529int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3530 int nCmdShow) {
3531 MSG msg; // message
3532 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003533 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003534 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003535
Jamie Madillb2ff6502017-03-15 16:17:46 -04003536 // Ensure wParam is initialized.
3537 msg.wParam = 0;
3538
Karl Schultze4dc75c2016-02-02 15:37:51 -07003539 // Use the CommandLine functions to get the command line arguments.
3540 // Unfortunately, Microsoft outputs
3541 // this information as wide characters for Unicode, and we simply want the
3542 // Ascii version to be compatible
3543 // with the non-Windows side. So, we have to convert the information to
3544 // Ascii character strings.
3545 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3546 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003547 argc = 0;
3548 }
3549
Karl Schultze4dc75c2016-02-02 15:37:51 -07003550 if (argc > 0) {
3551 argv = (char **)malloc(sizeof(char *) * argc);
3552 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003553 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003554 } else {
3555 for (int iii = 0; iii < argc; iii++) {
3556 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003557 size_t numConverted = 0;
3558
Karl Schultze4dc75c2016-02-02 15:37:51 -07003559 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3560 if (argv[iii] != NULL) {
3561 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3562 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003563 }
3564 }
3565 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003566 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003567 argv = NULL;
3568 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003569
3570 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003571
3572 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003573 if (argc > 0 && argv != NULL) {
3574 for (int iii = 0; iii < argc; iii++) {
3575 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003576 free(argv[iii]);
3577 }
3578 }
3579 free(argv);
3580 }
3581
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003582 demo.connection = hInstance;
3583 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003584 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003585 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003586
3587 demo_prepare(&demo);
3588
Karl Schultze4dc75c2016-02-02 15:37:51 -07003589 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003590
3591 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003592 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003593 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003594 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003595 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003596 done = true; // if found, quit app
3597 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003598 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003599 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003600 DispatchMessage(&msg);
3601 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003602 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003603 }
3604
3605 demo_cleanup(&demo);
3606
Karl Schultze4dc75c2016-02-02 15:37:51 -07003607 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003608}
Bill Hollingsf4352142017-02-14 22:58:56 -05003609
3610#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3611static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003612 const char* argv[] = { "CubeSample" };
3613 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003614
Tony Barbourc65cd752017-03-13 15:29:35 -06003615 demo_init(demo, argc, (char**)argv);
3616 demo->window = view;
3617 demo_init_vk_swapchain(demo);
3618 demo_prepare(demo);
3619 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003620}
3621
3622static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003623 // Wait for work to finish before updating MVP.
3624 vkDeviceWaitIdle(demo->device);
3625 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003626
Tony Barbourc65cd752017-03-13 15:29:35 -06003627 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003628}
3629
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003630#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3631#include <android/log.h>
3632#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003633#include "android_util.h"
3634
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003635static bool initialized = false;
3636static bool active = false;
3637struct demo demo;
3638
3639static int32_t processInput(struct android_app* app, AInputEvent* event) {
3640 return 0;
3641}
3642
3643static void processCommand(struct android_app* app, int32_t cmd) {
3644 switch(cmd) {
3645 case APP_CMD_INIT_WINDOW: {
3646 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003647 // We're getting a new window. If the app is starting up, we
3648 // need to initialize. If the app has already been
3649 // initialized, that means that we lost our previous window,
3650 // which means that we have a lot of work to do. At a minimum,
3651 // we need to destroy the swapchain and surface associated with
3652 // the old window, and create a new surface and swapchain.
3653 // However, since there are a lot of other objects/state that
3654 // is tied to the swapchain, it's easiest to simply cleanup and
3655 // start over (i.e. use a brute-force approach of re-starting
3656 // the app)
3657 if (demo.prepared) {
3658 demo_cleanup(&demo);
3659 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003660
3661 // Parse Intents into argc, argv
3662 // Use the following key to send arguments, i.e.
3663 // --es args "--validate"
3664 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003665 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003666 int argc = 0;
3667 char** argv = get_args(app, key, appTag, &argc);
3668
3669 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3670 for (int i = 0; i < argc; i++)
3671 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3672
3673 demo_init(&demo, argc, argv);
3674
3675 // Free the argv malloc'd by get_args
3676 for (int i = 0; i < argc; i++)
3677 free(argv[i]);
3678
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003679 demo.window = (void*)app->window;
3680 demo_init_vk_swapchain(&demo);
3681 demo_prepare(&demo);
3682 initialized = true;
3683 }
3684 break;
3685 }
3686 case APP_CMD_GAINED_FOCUS: {
3687 active = true;
3688 break;
3689 }
3690 case APP_CMD_LOST_FOCUS: {
3691 active = false;
3692 break;
3693 }
3694 }
3695}
3696
3697void android_main(struct android_app *app)
3698{
3699 app_dummy();
3700
Cody Northrop8707a6e2016-04-26 19:59:19 -06003701#ifdef ANDROID
3702 int vulkanSupport = InitVulkan();
3703 if (vulkanSupport == 0)
3704 return;
3705#endif
3706
Ian Elliottf05d5d12016-05-17 12:03:35 -06003707 demo.prepared = false;
3708
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003709 app->onAppCmd = processCommand;
3710 app->onInputEvent = processInput;
3711
3712 while(1) {
3713 int events;
3714 struct android_poll_source* source;
3715 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3716 if (source) {
3717 source->process(app, source);
3718 }
3719
3720 if (app->destroyRequested != 0) {
3721 demo_cleanup(&demo);
3722 return;
3723 }
3724 }
3725 if (initialized && active) {
3726 demo_run(&demo);
3727 }
3728 }
3729
3730}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003731#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003732int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003733 struct demo demo;
3734
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003735 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003736#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003737 demo_create_xcb_window(&demo);
3738#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3739 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003740#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3741 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003742#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003743#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003744
Tony Barbourcc69f452015-10-08 13:59:42 -06003745 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003746
3747 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003748
Tony Barbour153cb062016-12-07 13:43:36 -07003749#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003750 demo_run_xcb(&demo);
3751#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3752 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003753#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3754 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003755#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003756#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3757 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003758#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003759
3760 demo_cleanup(&demo);
3761
Karl Schultz0218c002016-03-22 17:06:13 -06003762 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003763}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003764#endif