blob: 8985c971bba5dcccb2bd0c6db358a7231cf7cdde [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) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001000 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001001 }
1002
1003 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001004 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001005 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001006
Karl Schultze4dc75c2016-02-02 15:37:51 -07001007 VkImage *swapchainImages =
1008 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001009 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001010 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001011 &demo->swapchainImageCount,
1012 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001013 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001014
Tony Barboura72d9492017-01-11 13:35:09 -07001015 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001016 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001017 assert(demo->swapchain_image_resources);
1018
1019 VkFenceCreateInfo fence_ci = {
1020 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1021 .pNext = NULL,
1022 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1023 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001024
Ian Elliotta0781972015-08-21 15:09:33 -06001025 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001026 VkImageViewCreateInfo color_image_view = {
1027 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028 .pNext = NULL,
1029 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001030 .components =
1031 {
1032 .r = VK_COMPONENT_SWIZZLE_R,
1033 .g = VK_COMPONENT_SWIZZLE_G,
1034 .b = VK_COMPONENT_SWIZZLE_B,
1035 .a = VK_COMPONENT_SWIZZLE_A,
1036 },
1037 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1038 .baseMipLevel = 0,
1039 .levelCount = 1,
1040 .baseArrayLayer = 0,
1041 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001042 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1043 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 };
1045
Tony Barboura72d9492017-01-11 13:35:09 -07001046 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001047
Tony Barboura72d9492017-01-11 13:35:09 -07001048 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001049
Karl Schultze4dc75c2016-02-02 15:37:51 -07001050 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001051 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001053
Tony Barboura72d9492017-01-11 13:35:09 -07001054 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1055 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001057
Mark Young04fd3d82016-01-25 14:43:50 -07001058 if (NULL != presentModes) {
1059 free(presentModes);
1060 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061}
1062
Karl Schultze4dc75c2016-02-02 15:37:51 -07001063static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001064 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001065 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001066 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001067 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001068 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001069 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001070 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001071 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001072 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001073 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001074 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001075 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001076 .flags = 0,
1077 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001078
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001079 VkImageViewCreateInfo view = {
1080 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001081 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001082 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001083 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001084 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1085 .baseMipLevel = 0,
1086 .levelCount = 1,
1087 .baseArrayLayer = 0,
1088 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001089 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001090 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001091 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001092
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001093 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001094 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001095 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001096
1097 demo->depth.format = depth_format;
1098
1099 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001100 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001101 assert(!err);
1102
Karl Schultze4dc75c2016-02-02 15:37:51 -07001103 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001104 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001105
Chia-I Wuf48700b2015-11-10 16:21:09 +08001106 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001107 demo->depth.mem_alloc.pNext = NULL;
1108 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1109 demo->depth.mem_alloc.memoryTypeIndex = 0;
1110
Karl Schultze4dc75c2016-02-02 15:37:51 -07001111 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001112 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001113 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001114 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001115
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001116 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001117 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1118 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001119 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001120
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001121 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001122 err =
1123 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001125
1126 /* create image view */
1127 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001128 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001129 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001130}
1131
Tony Barbour1a86d032015-09-21 15:17:33 -06001132/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001134 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001135
1136#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001137 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001138#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001139
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001140#ifdef __ANDROID__
1141#include <lunarg.ppm.h>
1142 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001143 cPtr = (char*)lunarg_ppm;
1144 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001145 return false;
1146 }
1147 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001148 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001149 if (rgba_data == NULL) {
1150 return true;
1151 }
1152 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001153 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001154 return false;
1155 }
1156 while(strncmp(cPtr++, "\n", 1));
1157
1158 for (int y = 0; y < *height; y++) {
1159 uint8_t *rowPtr = rgba_data;
1160 for (int x = 0; x < *width; x++) {
1161 memcpy(rowPtr, cPtr, 3);
1162 rowPtr[3] = 255; /* Alpha of 1 */
1163 rowPtr += 4;
1164 cPtr += 3;
1165 }
1166 rgba_data += layout->rowPitch;
1167 }
1168
1169 return true;
1170#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001171 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001172 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001173
Tony Barbour1a86d032015-09-21 15:17:33 -06001174 if (!fPtr)
1175 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001176
Tony Barbour1a86d032015-09-21 15:17:33 -06001177 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001178 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1179 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001180 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001181 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001182
Tony Barbour1a86d032015-09-21 15:17:33 -06001183 do {
1184 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001185 if (cPtr == NULL) {
1186 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001187 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001188 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001189 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001190
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001191 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001192 if (rgba_data == NULL) {
1193 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001194 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001195 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001196 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001197 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001198 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1199 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001200 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001201 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001202
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001204 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001206 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001207 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001208 rowPtr[3] = 255; /* Alpha of 1 */
1209 rowPtr += 4;
1210 }
1211 rgba_data += layout->rowPitch;
1212 }
1213 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001214 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001215#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001216}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001217
Karl Schultze4dc75c2016-02-02 15:37:51 -07001218static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001219 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001220 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001221 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001222 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001223 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001224 int32_t tex_width;
1225 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001226 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001227 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001228
Karl Schultze4dc75c2016-02-02 15:37:51 -07001229 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001230 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001231 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001232
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001233 tex_obj->tex_width = tex_width;
1234 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001235
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001236 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001237 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001239 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001241 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001242 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001243 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001244 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001245 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001246 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001247 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001248 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001249 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001250
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001251 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252
Karl Schultze4dc75c2016-02-02 15:37:51 -07001253 err =
1254 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001255 assert(!err);
1256
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001257 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001258
Chia-I Wuf48700b2015-11-10 16:21:09 +08001259 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001260 tex_obj->mem_alloc.pNext = NULL;
1261 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1262 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001263
Karl Schultze4dc75c2016-02-02 15:37:51 -07001264 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1265 required_props,
1266 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001267 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001268
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001269 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001270 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001271 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001272 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001274 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001275 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001276 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001277
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001278 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001279 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001280 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001281 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001282 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001283 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001284 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001285 void *data;
1286
Karl Schultze4dc75c2016-02-02 15:37:51 -07001287 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1288 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001289
Karl Schultze4dc75c2016-02-02 15:37:51 -07001290 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1291 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001292 assert(!err);
1293
1294 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1295 fprintf(stderr, "Error loading texture: %s\n", filename);
1296 }
1297
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001298 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001299 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001300
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001301 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001302}
1303
Karl Schultze4dc75c2016-02-02 15:37:51 -07001304static void demo_destroy_texture_image(struct demo *demo,
1305 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001306 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001307 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1308 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001309}
1310
Karl Schultze4dc75c2016-02-02 15:37:51 -07001311static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001312 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001313 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001314 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001316 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001317
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001318 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001319 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001320
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 if ((props.linearTilingFeatures &
1322 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1323 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001324 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001325 demo_prepare_texture_image(
1326 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1327 VK_IMAGE_USAGE_SAMPLED_BIT,
1328 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1329 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001330 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1331 // shader to run until layout transition completes
1332 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1333 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1334 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1335 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001336 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001337 } else if (props.optimalTilingFeatures &
1338 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001339 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001340
Tony Barbour16156cb2016-10-19 14:15:49 -06001341 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001342 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001343 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001344 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1345 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1346 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001347
Karl Schultze4dc75c2016-02-02 15:37:51 -07001348 demo_prepare_texture_image(
1349 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1350 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1351 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001352
Tony Barbour16156cb2016-10-19 14:15:49 -06001353 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001354 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001355 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001356 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001357 VK_ACCESS_HOST_WRITE_BIT,
1358 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1359 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001360
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001361 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001362 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001363 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001364 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001365 VK_ACCESS_HOST_WRITE_BIT,
1366 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1367 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001368
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001369 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001370 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1371 .srcOffset = {0, 0, 0},
1372 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1373 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001374 .extent = {demo->staging_texture.tex_width,
1375 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001376 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001377 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001378 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001379 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1380 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001381
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001382 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 VK_IMAGE_ASPECT_COLOR_BIT,
1384 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001385 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001386 VK_ACCESS_TRANSFER_WRITE_BIT,
1387 VK_PIPELINE_STAGE_TRANSFER_BIT,
1388 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001389
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001390 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001391 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1392 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001393 }
1394
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001395 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001396 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001398 .magFilter = VK_FILTER_NEAREST,
1399 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001400 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001401 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1402 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1403 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001405 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001406 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001407 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408 .minLod = 0.0f,
1409 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001410 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001411 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001413
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001414 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001415 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001417 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001418 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001419 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001420 .components =
1421 {
1422 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1423 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1424 },
1425 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001426 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001428
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001430 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001431 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432 assert(!err);
1433
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434 /* create image view */
1435 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001436 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001437 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438 assert(!err);
1439 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001440}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441
Tony Barboura72d9492017-01-11 13:35:09 -07001442void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001443 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001444 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001445 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001446 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001447 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001448 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001449 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001450 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001451
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001452 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001453 mat4x4_mul(MVP, VP, demo->model_matrix);
1454 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001455 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001456
Karl Schultza2615462017-01-20 13:19:20 -07001457 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001458 data.position[i][0] = g_vertex_buffer_data[i * 3];
1459 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1460 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001461 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001462 data.attr[i][0] = g_uv_buffer_data[2 * i];
1463 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001464 data.attr[i][2] = 0;
1465 data.attr[i][3] = 0;
1466 }
1467
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001468 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001469 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001470 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001471 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001472
Tony Barboura72d9492017-01-11 13:35:09 -07001473 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1474 err =
1475 vkCreateBuffer(demo->device, &buf_info, NULL,
1476 &demo->swapchain_image_resources[i].uniform_buffer);
1477 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001478
Tony Barboura72d9492017-01-11 13:35:09 -07001479 vkGetBufferMemoryRequirements(demo->device,
1480 demo->swapchain_image_resources[i].uniform_buffer,
1481 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001482
Tony Barboura72d9492017-01-11 13:35:09 -07001483 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1484 mem_alloc.pNext = NULL;
1485 mem_alloc.allocationSize = mem_reqs.size;
1486 mem_alloc.memoryTypeIndex = 0;
1487
1488 pass = memory_type_from_properties(
1489 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001490 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001491 &mem_alloc.memoryTypeIndex);
1492 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001493
Tony Barboura72d9492017-01-11 13:35:09 -07001494 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1495 &demo->swapchain_image_resources[i].uniform_memory);
1496 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001497
Tony Barboura72d9492017-01-11 13:35:09 -07001498 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1499 VK_WHOLE_SIZE, 0, (void **)&pData);
1500 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001501
Tony Barboura72d9492017-01-11 13:35:09 -07001502 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001503
Tony Barboura72d9492017-01-11 13:35:09 -07001504 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001505
Tony Barboura72d9492017-01-11 13:35:09 -07001506 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1507 demo->swapchain_image_resources[i].uniform_memory, 0);
1508 assert(!err);
1509 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001510}
1511
Karl Schultze4dc75c2016-02-02 15:37:51 -07001512static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001513 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001514 [0] =
1515 {
1516 .binding = 0,
1517 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1518 .descriptorCount = 1,
1519 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1520 .pImmutableSamplers = NULL,
1521 },
1522 [1] =
1523 {
1524 .binding = 1,
1525 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1526 .descriptorCount = DEMO_TEXTURE_COUNT,
1527 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1528 .pImmutableSamplers = NULL,
1529 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001530 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001531 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001532 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001533 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001534 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001535 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001536 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001537 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001538
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1540 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001541 assert(!err);
1542
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001543 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001544 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1545 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001546 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001547 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001548 };
1549
Karl Schultze4dc75c2016-02-02 15:37:51 -07001550 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001551 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001553}
1554
Karl Schultze4dc75c2016-02-02 15:37:51 -07001555static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001556 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1557 // because at the start of the renderpass, we don't care about their contents.
1558 // At the start of the subpass, the color attachment's layout will be transitioned
1559 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1560 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1561 // the renderpass, the color attachment's layout will be transitioned to
1562 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1563 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001564 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001565 [0] =
1566 {
1567 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001568 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 .samples = VK_SAMPLE_COUNT_1_BIT,
1570 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1571 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1572 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1573 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001574 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001575 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001576 },
1577 [1] =
1578 {
1579 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001580 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001581 .samples = VK_SAMPLE_COUNT_1_BIT,
1582 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1583 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1584 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1585 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1586 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001587 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001588 .finalLayout =
1589 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1590 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001591 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001592 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001593 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001594 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001595 const VkAttachmentReference depth_reference = {
1596 .attachment = 1,
1597 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1598 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001599 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001600 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1601 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001602 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001603 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001604 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001605 .pColorAttachments = &color_reference,
1606 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001607 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001608 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001609 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001610 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001611 const VkRenderPassCreateInfo rp_info = {
1612 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1613 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001614 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001615 .attachmentCount = 2,
1616 .pAttachments = attachments,
1617 .subpassCount = 1,
1618 .pSubpasses = &subpass,
1619 .dependencyCount = 0,
1620 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001621 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001622 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001623
Chia-I Wu63636062015-10-26 21:10:41 +08001624 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001625 assert(!err);
1626}
1627
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001628//TODO: Merge shader reading
1629#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001630static VkShaderModule
1631demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001632 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001633 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001634 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001635
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001636 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1637 moduleCreateInfo.pNext = NULL;
1638
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001639 moduleCreateInfo.codeSize = size;
1640 moduleCreateInfo.pCode = code;
1641 moduleCreateInfo.flags = 0;
1642 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1643 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001644
1645 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001646}
1647
Karl Schultze4dc75c2016-02-02 15:37:51 -07001648char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001649 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001650 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001651 void *shader_code;
1652
Bill Hollingsf4352142017-02-14 22:58:56 -05001653#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001654 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001655#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001656
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001657 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001658 if (!fp)
1659 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001660
1661 fseek(fp, 0L, SEEK_END);
1662 size = ftell(fp);
1663
1664 fseek(fp, 0L, SEEK_SET);
1665
1666 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001667 retval = fread(shader_code, size, 1, fp);
1668 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001669
1670 *psize = size;
1671
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001672 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001673 return shader_code;
1674}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001675#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001676
Karl Schultze4dc75c2016-02-02 15:37:51 -07001677static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001678#ifdef __ANDROID__
1679 VkShaderModuleCreateInfo sh_info = {};
1680 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1681
1682#include "cube.vert.h"
1683 sh_info.codeSize = sizeof(cube_vert);
1684 sh_info.pCode = cube_vert;
1685 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1686 assert(!err);
1687#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001688 void *vertShaderCode;
1689 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001690
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001691 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001692 if (!vertShaderCode) {
1693 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1694 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001695
Karl Schultze4dc75c2016-02-02 15:37:51 -07001696 demo->vert_shader_module =
1697 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001698
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001699 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001700#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001701
1702 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703}
1704
Karl Schultze4dc75c2016-02-02 15:37:51 -07001705static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001706#ifdef __ANDROID__
1707 VkShaderModuleCreateInfo sh_info = {};
1708 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1709
1710#include "cube.frag.h"
1711 sh_info.codeSize = sizeof(cube_frag);
1712 sh_info.pCode = cube_frag;
1713 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1714 assert(!err);
1715#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001716 void *fragShaderCode;
1717 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001718
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001719 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001720 if (!fragShaderCode) {
1721 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
1722 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001723
Karl Schultze4dc75c2016-02-02 15:37:51 -07001724 demo->frag_shader_module =
1725 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001726
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001727 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001728#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001729
1730 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001731}
1732
Karl Schultze4dc75c2016-02-02 15:37:51 -07001733static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001734 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001735 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001737 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001738 VkPipelineRasterizationStateCreateInfo rs;
1739 VkPipelineColorBlendStateCreateInfo cb;
1740 VkPipelineDepthStencilStateCreateInfo ds;
1741 VkPipelineViewportStateCreateInfo vp;
1742 VkPipelineMultisampleStateCreateInfo ms;
1743 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1744 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001745 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746
Piers Daniellba839d12015-09-29 13:01:09 -06001747 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1748 memset(&dynamicState, 0, sizeof dynamicState);
1749 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1750 dynamicState.pDynamicStates = dynamicStateEnables;
1751
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001753 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001754 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001755
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001756 memset(&vi, 0, sizeof(vi));
1757 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1758
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001759 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001760 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001761 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762
1763 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001764 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1765 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001766 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001767 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001768 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001769 rs.rasterizerDiscardEnable = VK_FALSE;
1770 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001771 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001772
1773 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001774 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1775 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001776 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001777 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001778 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001779 cb.attachmentCount = 1;
1780 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001781
Tony Barbour29645d02015-01-16 14:27:35 -07001782 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001783 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001784 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001785 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1786 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001787 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001788 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1789 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001790
1791 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001792 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001793 ds.depthTestEnable = VK_TRUE;
1794 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001795 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001796 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001797 ds.back.failOp = VK_STENCIL_OP_KEEP;
1798 ds.back.passOp = VK_STENCIL_OP_KEEP;
1799 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001800 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001801 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001802
Tony Barbour29645d02015-01-16 14:27:35 -07001803 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001804 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001805 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001806 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001807
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001808 // Two stages: vs and fs
1809 pipeline.stageCount = 2;
1810 VkPipelineShaderStageCreateInfo shaderStages[2];
1811 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1812
Karl Schultze4dc75c2016-02-02 15:37:51 -07001813 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1814 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001815 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001816 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001817
Karl Schultze4dc75c2016-02-02 15:37:51 -07001818 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1819 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001820 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001821 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001822
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001823 memset(&pipelineCache, 0, sizeof(pipelineCache));
1824 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001825
Karl Schultze4dc75c2016-02-02 15:37:51 -07001826 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1827 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001828 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001829
Karl Schultze4dc75c2016-02-02 15:37:51 -07001830 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001831 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001832 pipeline.pRasterizationState = &rs;
1833 pipeline.pColorBlendState = &cb;
1834 pipeline.pMultisampleState = &ms;
1835 pipeline.pViewportState = &vp;
1836 pipeline.pDepthStencilState = &ds;
1837 pipeline.pStages = shaderStages;
1838 pipeline.renderPass = demo->render_pass;
1839 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001840
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001841 pipeline.renderPass = demo->render_pass;
1842
Karl Schultze4dc75c2016-02-02 15:37:51 -07001843 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1844 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001845 assert(!err);
1846
Chia-I Wu63636062015-10-26 21:10:41 +08001847 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1848 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001849}
1850
Karl Schultze4dc75c2016-02-02 15:37:51 -07001851static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001852 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001853 [0] =
1854 {
1855 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07001856 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001857 },
1858 [1] =
1859 {
1860 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07001861 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001862 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001863 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001864 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001865 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001866 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001867 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08001868 .poolSizeCount = 2,
1869 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001870 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001871 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001872
Karl Schultze4dc75c2016-02-02 15:37:51 -07001873 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1874 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001875 assert(!err);
1876}
1877
Karl Schultze4dc75c2016-02-02 15:37:51 -07001878static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001879 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001880 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001881 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001882
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001883 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001884 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001885 .pNext = NULL,
1886 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001887 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001888 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07001889
1890 VkDescriptorBufferInfo buffer_info;
1891 buffer_info.offset = 0;
1892 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001893
Chia-I Wuae721ba2015-05-25 16:27:55 +08001894 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07001895 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001896 tex_descs[i].sampler = demo->textures[i].sampler;
1897 tex_descs[i].imageView = demo->textures[i].view;
1898 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001899 }
1900
1901 memset(&writes, 0, sizeof(writes));
1902
1903 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001904 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001905 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07001906 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001907
1908 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001909 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001910 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001911 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001912 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001913
Tony Barboura72d9492017-01-11 13:35:09 -07001914 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1915 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
1916 assert(!err);
1917 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
1918 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1919 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1920 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1921 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001922}
1923
Karl Schultze4dc75c2016-02-02 15:37:51 -07001924static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001925 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001926 attachments[1] = demo->depth.view;
1927
Chia-I Wuf973e322015-07-08 13:34:24 +08001928 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001929 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1930 .pNext = NULL,
1931 .renderPass = demo->render_pass,
1932 .attachmentCount = 2,
1933 .pAttachments = attachments,
1934 .width = demo->width,
1935 .height = demo->height,
1936 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001937 };
1938 VkResult U_ASSERT_ONLY err;
1939 uint32_t i;
1940
Tony Barbourd8e504f2015-10-08 14:26:24 -06001941 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07001942 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001943 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001944 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08001945 assert(!err);
1946 }
1947}
1948
Karl Schultze4dc75c2016-02-02 15:37:51 -07001949static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001950 VkResult U_ASSERT_ONLY err;
1951
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001952 const VkCommandPoolCreateInfo cmd_pool_info = {
1953 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001954 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001955 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001956 .flags = 0,
1957 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001958 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1959 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001960 assert(!err);
1961
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001962 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001963 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001965 .commandPool = demo->cmd_pool,
1966 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001967 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001969 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1970 assert(!err);
1971 VkCommandBufferBeginInfo cmd_buf_info = {
1972 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1973 .pNext = NULL,
1974 .flags = 0,
1975 .pInheritanceInfo = NULL,
1976 };
1977 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1978 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001979
1980 demo_prepare_buffers(demo);
1981 demo_prepare_depth(demo);
1982 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07001983 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001984
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001985 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001986 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001988
Ian Elliotta0781972015-08-21 15:09:33 -06001989 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001990 err =
Tony Barboura72d9492017-01-11 13:35:09 -07001991 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001992 assert(!err);
1993 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001994
Tony Barbour22e06272016-09-07 16:07:56 -06001995 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07001996 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06001997 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1998 .pNext = NULL,
1999 .queueFamilyIndex = demo->present_queue_family_index,
2000 .flags = 0,
2001 };
Karl Schultza2615462017-01-20 13:19:20 -07002002 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002003 &demo->present_cmd_pool);
2004 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002005 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002006 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2007 .pNext = NULL,
2008 .commandPool = demo->present_cmd_pool,
2009 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2010 .commandBufferCount = 1,
2011 };
2012 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2013 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002014 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002015 assert(!err);
2016 demo_build_image_ownership_cmd(demo, i);
2017 }
2018 }
2019
Chia-I Wu63ea9262015-03-26 13:14:16 +08002020 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002021 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002022
Chia-I Wuf973e322015-07-08 13:34:24 +08002023 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002024
Ian Elliotta0781972015-08-21 15:09:33 -06002025 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002026 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002027 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002028 }
2029
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002030 /*
2031 * Prepare functions above may generate pipeline commands
2032 * that need to be flushed before beginning the render loop.
2033 */
2034 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002035 if (demo->staging_texture.image) {
2036 demo_destroy_texture_image(demo, &demo->staging_texture);
2037 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002038
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002039 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002040 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041}
2042
Karl Schultze4dc75c2016-02-02 15:37:51 -07002043static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002044 uint32_t i;
2045
2046 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002047 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002048
Tony Barbour66a56c32016-09-21 13:10:56 -06002049 // Wait for fences from present operations
2050 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002051 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002052 vkDestroyFence(demo->device, demo->fences[i], NULL);
2053 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2054 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2055 if (demo->separate_present_queue) {
2056 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2057 }
2058 }
2059
Tony Barbourd8e504f2015-10-08 14:26:24 -06002060 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002061 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002062 }
Chia-I Wu63636062015-10-26 21:10:41 +08002063 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002064
Chia-I Wu63636062015-10-26 21:10:41 +08002065 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2066 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2067 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2068 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2069 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002070
2071 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002072 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2073 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2074 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2075 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002076 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002077 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002078
Chia-I Wu63636062015-10-26 21:10:41 +08002079 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2080 vkDestroyImage(demo->device, demo->depth.image, NULL);
2081 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002082
Ian Elliotta0781972015-08-21 15:09:33 -06002083 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002084 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002085 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002086 &demo->swapchain_image_resources[i].cmd);
2087 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2088 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2089 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002090 }
Tony Barboura72d9492017-01-11 13:35:09 -07002091 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002092 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002093 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002094
Tony Barbour22e06272016-09-07 16:07:56 -06002095 if (demo->separate_present_queue) {
2096 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002097 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002098 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002099 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002100 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002101 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002102 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002103 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002104 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002105
Tony Barbour153cb062016-12-07 13:43:36 -07002106#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002107 XDestroyWindow(demo->display, demo->xlib_window);
2108 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002109#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002110 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002111 xcb_disconnect(demo->connection);
2112 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002113#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2114 wl_shell_surface_destroy(demo->shell_surface);
2115 wl_surface_destroy(demo->window);
2116 wl_shell_destroy(demo->shell);
2117 wl_compositor_destroy(demo->compositor);
2118 wl_registry_destroy(demo->registry);
2119 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002120#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002121#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002122}
2123
Karl Schultze4dc75c2016-02-02 15:37:51 -07002124static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002125 uint32_t i;
2126
Mike Stroyanbb60b382015-10-28 09:46:57 -06002127 // Don't react to resize until after first initialization.
2128 if (!demo->prepared) {
2129 return;
2130 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002131 // In order to properly resize the window, we must re-create the swapchain
2132 // AND redo the command buffers, etc.
2133 //
2134 // First, perform part of the demo_cleanup() function:
2135 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002136 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002137
2138 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002139 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002140 }
Chia-I Wu63636062015-10-26 21:10:41 +08002141 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002142
Chia-I Wu63636062015-10-26 21:10:41 +08002143 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2144 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2145 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2146 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2147 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002148
2149 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002150 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2151 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2152 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2153 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002154 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002155
Chia-I Wu63636062015-10-26 21:10:41 +08002156 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2157 vkDestroyImage(demo->device, demo->depth.image, NULL);
2158 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002159
Ian Elliottcf074d32015-10-16 18:02:43 -06002160 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002161 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002162 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002163 &demo->swapchain_image_resources[i].cmd);
2164 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2165 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2166 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002167 }
Chia-I Wu63636062015-10-26 21:10:41 +08002168 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002169 if (demo->separate_present_queue) {
2170 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2171 }
Tony Barboura72d9492017-01-11 13:35:09 -07002172 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002173
Ian Elliottcf074d32015-10-16 18:02:43 -06002174 // Second, re-perform the demo_prepare() function, which will re-create the
2175 // swapchain:
2176 demo_prepare(demo);
2177}
2178
David Pinedo2bb7c932015-06-18 17:03:14 -06002179// On MS-Windows, make this a global, so it's available to WndProc()
2180struct demo demo;
2181
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002182#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002183static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002184 if (!demo->prepared)
2185 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002186
Ian Elliott639ca472015-04-16 15:23:05 -06002187 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002188 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002189 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002190 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002191 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002192}
Ian Elliott639ca472015-04-16 15:23:05 -06002193
2194// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002195LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2196 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002197 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002198 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002199 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002200 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002201 // The validation callback calls MessageBox which can generate paint
2202 // events - don't make more Vulkan calls if we got here from the
2203 // callback
2204 if (!in_callback) {
2205 demo_run(&demo);
2206 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002207 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002208 case WM_GETMINMAXINFO: // set window's minimum size
2209 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2210 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002211 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002212 // Resize the application to the new window size, except when
2213 // it was minimized. Vulkan doesn't support images or swapchains
2214 // with width=0 and height=0.
2215 if (wParam != SIZE_MINIMIZED) {
2216 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002217 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002218 demo_resize(&demo);
2219 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002220 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002221 default:
2222 break;
2223 }
2224 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2225}
2226
Karl Schultze4dc75c2016-02-02 15:37:51 -07002227static void demo_create_window(struct demo *demo) {
2228 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002229
2230 // Initialize the window class structure:
2231 win_class.cbSize = sizeof(WNDCLASSEX);
2232 win_class.style = CS_HREDRAW | CS_VREDRAW;
2233 win_class.lpfnWndProc = WndProc;
2234 win_class.cbClsExtra = 0;
2235 win_class.cbWndExtra = 0;
2236 win_class.hInstance = demo->connection; // hInstance
2237 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2238 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2239 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2240 win_class.lpszMenuName = NULL;
2241 win_class.lpszClassName = demo->name;
2242 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2243 // Register window class:
2244 if (!RegisterClassEx(&win_class)) {
2245 // It didn't work, so try to give a useful error:
2246 printf("Unexpected error trying to start the application!\n");
2247 fflush(stdout);
2248 exit(1);
2249 }
2250 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002251 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002252 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002253 demo->window = CreateWindowEx(0,
2254 demo->name, // class name
2255 demo->name, // app name
2256 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002257 WS_VISIBLE | WS_SYSMENU,
2258 100, 100, // x/y coords
2259 wr.right - wr.left, // width
2260 wr.bottom - wr.top, // height
2261 NULL, // handle to parent
2262 NULL, // handle to menu
2263 demo->connection, // hInstance
2264 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002265 if (!demo->window) {
2266 // It didn't work, so try to give a useful error:
2267 printf("Cannot create a window in which to draw!\n");
2268 fflush(stdout);
2269 exit(1);
2270 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002271 // Window client area size must be at least 1 pixel high, to prevent crash.
2272 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2273 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002274}
Tony Barbour8295ac42016-08-08 11:04:17 -06002275#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002276static void demo_create_xlib_window(struct demo *demo) {
2277
Tony Barbouree9cd372017-01-31 16:09:58 -07002278 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002279 demo->display = XOpenDisplay(NULL);
2280 long visualMask = VisualScreenMask;
2281 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002282 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002283 vInfoTemplate.screen = DefaultScreen(demo->display);
2284 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2285 &vInfoTemplate, &numberOfVisuals);
2286
2287 Colormap colormap = XCreateColormap(
2288 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2289 visualInfo->visual, AllocNone);
2290
Rene Lindsay11612722016-06-13 17:20:39 -06002291 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002292 windowAttributes.colormap = colormap;
2293 windowAttributes.background_pixel = 0xFFFFFFFF;
2294 windowAttributes.border_pixel = 0;
2295 windowAttributes.event_mask =
2296 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2297
2298 demo->xlib_window = XCreateWindow(
2299 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2300 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2301 visualInfo->visual,
2302 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2303
2304 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2305 XMapWindow(demo->display, demo->xlib_window);
2306 XFlush(demo->display);
2307 demo->xlib_wm_delete_window =
2308 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2309}
2310static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2311 switch(event->type) {
2312 case ClientMessage:
2313 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2314 demo->quit = true;
2315 break;
2316 case KeyPress:
2317 switch (event->xkey.keycode) {
2318 case 0x9: // Escape
2319 demo->quit = true;
2320 break;
2321 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002322 demo->spin_angle -= demo->spin_increment;
2323 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002324 case 0x72: // right arrow key
2325 demo->spin_angle += demo->spin_increment;
2326 break;
2327 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002328 demo->pause = !demo->pause;
2329 break;
2330 }
2331 break;
2332 case ConfigureNotify:
2333 if ((demo->width != event->xconfigure.width) ||
2334 (demo->height != event->xconfigure.height)) {
2335 demo->width = event->xconfigure.width;
2336 demo->height = event->xconfigure.height;
2337 demo_resize(demo);
2338 }
2339 break;
2340 default:
2341 break;
2342 }
2343
2344}
2345
2346static void demo_run_xlib(struct demo *demo) {
2347
2348 while (!demo->quit) {
2349 XEvent event;
2350
2351 if (demo->pause) {
2352 XNextEvent(demo->display, &event);
2353 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002354 }
2355 while (XPending(demo->display) > 0) {
2356 XNextEvent(demo->display, &event);
2357 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002358 }
2359
Tony Barbour2593b182016-04-19 10:57:58 -06002360 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002361 demo->curFrame++;
2362 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2363 demo->quit = true;
2364 }
2365}
Tony Barbour153cb062016-12-07 13:43:36 -07002366#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002367static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002368 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002369 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002370 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002371 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002372 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002373 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002374 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002375 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2376 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002377 demo->quit = true;
2378 }
2379 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002380 case XCB_KEY_RELEASE: {
2381 const xcb_key_release_event_t *key =
2382 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002383
Karl Schultze4dc75c2016-02-02 15:37:51 -07002384 switch (key->detail) {
2385 case 0x9: // Escape
2386 demo->quit = true;
2387 break;
2388 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002389 demo->spin_angle -= demo->spin_increment;
2390 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002391 case 0x72: // right arrow key
2392 demo->spin_angle += demo->spin_increment;
2393 break;
2394 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002395 demo->pause = !demo->pause;
2396 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002397 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002398 } break;
2399 case XCB_CONFIGURE_NOTIFY: {
2400 const xcb_configure_notify_event_t *cfg =
2401 (const xcb_configure_notify_event_t *)event;
2402 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002403 demo->width = cfg->width;
2404 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002405 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002406 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002407 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002408 default:
2409 break;
2410 }
2411}
2412
Tony Barbour2593b182016-04-19 10:57:58 -06002413static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002414 xcb_flush(demo->connection);
2415
2416 while (!demo->quit) {
2417 xcb_generic_event_t *event;
2418
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002419 if (demo->pause) {
2420 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002421 }
2422 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002423 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002424 }
2425 while (event) {
2426 demo_handle_xcb_event(demo, event);
2427 free(event);
2428 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002429 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002430
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002431 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002432 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002433 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002434 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002435 }
2436}
2437
Tony Barbour2593b182016-04-19 10:57:58 -06002438static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002439 uint32_t value_mask, value_list[32];
2440
Tony Barbour2593b182016-04-19 10:57:58 -06002441 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002442
2443 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2444 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002445 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002446 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002447
Tony Barbour2593b182016-04-19 10:57:58 -06002448 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002449 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2450 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2451 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002452
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002453 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002454 xcb_intern_atom_cookie_t cookie =
2455 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2456 xcb_intern_atom_reply_t *reply =
2457 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002458
Karl Schultze4dc75c2016-02-02 15:37:51 -07002459 xcb_intern_atom_cookie_t cookie2 =
2460 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2461 demo->atom_wm_delete_window =
2462 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002463
Tony Barbour2593b182016-04-19 10:57:58 -06002464 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002465 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002466 &(*demo->atom_wm_delete_window).atom);
2467 free(reply);
2468
Tony Barbour2593b182016-04-19 10:57:58 -06002469 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002470
Karl Schultze4dc75c2016-02-02 15:37:51 -07002471 // Force the x/y coordinates to 100,100 results are identical in consecutive
2472 // runs
2473 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002474 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002475 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002476}
Tony Barbour6b131662016-08-25 13:14:45 -06002477// VK_USE_PLATFORM_XCB_KHR
2478#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002479static void demo_run(struct demo *demo) {
2480 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002481 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002482 demo->curFrame++;
2483 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2484 demo->quit = true;
2485 }
2486}
2487
2488static void handle_ping(void *data UNUSED,
2489 struct wl_shell_surface *shell_surface,
2490 uint32_t serial) {
2491 wl_shell_surface_pong(shell_surface, serial);
2492}
2493
2494static void handle_configure(void *data UNUSED,
2495 struct wl_shell_surface *shell_surface UNUSED,
2496 uint32_t edges UNUSED, int32_t width UNUSED,
2497 int32_t height UNUSED) {}
2498
2499static void handle_popup_done(void *data UNUSED,
2500 struct wl_shell_surface *shell_surface UNUSED) {}
2501
2502static const struct wl_shell_surface_listener shell_surface_listener = {
2503 handle_ping, handle_configure, handle_popup_done};
2504
2505static void demo_create_window(struct demo *demo) {
2506 demo->window = wl_compositor_create_surface(demo->compositor);
2507 if (!demo->window) {
2508 printf("Can not create wayland_surface from compositor!\n");
2509 fflush(stdout);
2510 exit(1);
2511 }
2512
2513 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2514 if (!demo->shell_surface) {
2515 printf("Can not get shell_surface from wayland_surface!\n");
2516 fflush(stdout);
2517 exit(1);
2518 }
2519 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2520 demo);
2521 wl_shell_surface_set_toplevel(demo->shell_surface);
2522 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2523}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002524#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2525static void demo_run(struct demo *demo) {
2526 if (!demo->prepared)
2527 return;
2528
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002529 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002530 demo->curFrame++;
2531}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002532#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002533#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2534static VkResult demo_create_display_surface(struct demo *demo) {
2535 VkResult U_ASSERT_ONLY err;
2536 uint32_t display_count;
2537 uint32_t mode_count;
2538 uint32_t plane_count;
2539 VkDisplayPropertiesKHR display_props;
2540 VkDisplayKHR display;
2541 VkDisplayModePropertiesKHR mode_props;
2542 VkDisplayPlanePropertiesKHR *plane_props;
2543 VkBool32 found_plane = VK_FALSE;
2544 uint32_t plane_index;
2545 VkExtent2D image_extent;
2546 VkDisplaySurfaceCreateInfoKHR create_info;
2547
2548 // Get the first display
2549 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2550 assert(!err);
2551
2552 if (display_count == 0) {
2553 printf("Cannot find any display!\n");
2554 fflush(stdout);
2555 exit(1);
2556 }
2557
2558 display_count = 1;
2559 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2560 assert(!err || (err == VK_INCOMPLETE));
2561
2562 display = display_props.display;
2563
2564 // Get the first mode of the display
2565 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2566 assert(!err);
2567
2568 if (mode_count == 0) {
2569 printf("Cannot find any mode for the display!\n");
2570 fflush(stdout);
2571 exit(1);
2572 }
2573
2574 mode_count = 1;
2575 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2576 assert(!err || (err == VK_INCOMPLETE));
2577
2578 // Get the list of planes
2579 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2580 assert(!err);
2581
2582 if (plane_count == 0) {
2583 printf("Cannot find any plane!\n");
2584 fflush(stdout);
2585 exit(1);
2586 }
2587
2588 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2589 assert(plane_props);
2590
2591 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2592 assert(!err);
2593
2594 // Find a plane compatible with the display
2595 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2596 uint32_t supported_count;
2597 VkDisplayKHR *supported_displays;
2598
2599 // Disqualify planes that are bound to a different display
2600 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2601 (plane_props[plane_index].currentDisplay != display)) {
2602 continue;
2603 }
2604
2605 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2606 assert(!err);
2607
2608 if (supported_count == 0) {
2609 continue;
2610 }
2611
2612 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2613 assert(supported_displays);
2614
2615 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2616 assert(!err);
2617
2618 for (uint32_t i = 0; i < supported_count; i++) {
2619 if (supported_displays[i] == display) {
2620 found_plane = VK_TRUE;
2621 break;
2622 }
2623 }
2624
2625 free(supported_displays);
2626
2627 if (found_plane) {
2628 break;
2629 }
2630 }
2631
2632 if (!found_plane) {
2633 printf("Cannot find a plane compatible with the display!\n");
2634 fflush(stdout);
2635 exit(1);
2636 }
2637
2638 free(plane_props);
2639
Tony Barbour820b55b2017-03-14 08:55:46 -06002640 VkDisplayPlaneCapabilitiesKHR planeCaps;
2641 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2642 // Find a supported alpha mode
2643 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2644 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
2645 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2646 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2647 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2648 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2649 };
2650 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2651 if (planeCaps.supportedAlpha & alphaModes[i]) {
2652 alphaMode = alphaModes[i];
2653 break;
2654 }
2655 }
Damien Leone600c3052017-01-31 10:26:07 -07002656 image_extent.width = mode_props.parameters.visibleRegion.width;
2657 image_extent.height = mode_props.parameters.visibleRegion.height;
2658
2659 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2660 create_info.pNext = NULL;
2661 create_info.flags = 0;
2662 create_info.displayMode = mode_props.displayMode;
2663 create_info.planeIndex = plane_index;
2664 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2665 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002666 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002667 create_info.globalAlpha = 1.0f;
2668 create_info.imageExtent = image_extent;
2669
2670 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2671}
2672
2673static void demo_run_display(struct demo *demo)
2674{
2675 while (!demo->quit) {
2676 demo_draw(demo);
2677 demo->curFrame++;
2678
2679 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2680 demo->quit = true;
2681 }
2682 }
2683}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002684#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002685
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002686/*
2687 * Return 1 (true) if all layer names specified in check_names
2688 * can be found in given layer properties.
2689 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002690static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002691 uint32_t layer_count,
2692 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002693 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002694 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002695 for (uint32_t j = 0; j < layer_count; j++) {
2696 if (!strcmp(check_names[i], layers[j].layerName)) {
2697 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002698 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002699 }
2700 }
2701 if (!found) {
2702 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2703 return 0;
2704 }
2705 }
2706 return 1;
2707}
2708
Karl Schultze4dc75c2016-02-02 15:37:51 -07002709static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002710 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002711 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002712 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002713 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002714 char **instance_validation_layers = NULL;
2715 demo->enabled_extension_count = 0;
2716 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002717
Karl Schultz7c50ad62016-04-01 12:07:03 -06002718 char *instance_validation_layers_alt1[] = {
2719 "VK_LAYER_LUNARG_standard_validation"
2720 };
2721
2722 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07002723 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2724 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2725 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002726 };
2727
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002728 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002729 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002730 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002731
Karl Schultz7c50ad62016-04-01 12:07:03 -06002732 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002733 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002734
Karl Schultz7c50ad62016-04-01 12:07:03 -06002735 instance_validation_layers = instance_validation_layers_alt1;
2736 if (instance_layer_count > 0) {
2737 VkLayerProperties *instance_layers =
2738 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2739 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2740 instance_layers);
2741 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002742
Karl Schultz7c50ad62016-04-01 12:07:03 -06002743
2744 validation_found = demo_check_layers(
2745 ARRAY_SIZE(instance_validation_layers_alt1),
2746 instance_validation_layers, instance_layer_count,
2747 instance_layers);
2748 if (validation_found) {
2749 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002750 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2751 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002752 } else {
2753 // use alternative set of validation layers
2754 instance_validation_layers = instance_validation_layers_alt2;
2755 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2756 validation_found = demo_check_layers(
2757 ARRAY_SIZE(instance_validation_layers_alt2),
2758 instance_validation_layers, instance_layer_count,
2759 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002760 validation_layer_count =
2761 ARRAY_SIZE(instance_validation_layers_alt2);
2762 for (uint32_t i = 0; i < validation_layer_count; i++) {
2763 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002764 }
2765 }
2766 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002767 }
Dustin Graves7c287352016-01-27 13:48:06 -07002768
Karl Schultz7c50ad62016-04-01 12:07:03 -06002769 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002770 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002771 "required validation layer.\n\n"
2772 "Please look at the Getting Started guide for additional "
2773 "information.\n",
2774 "vkCreateInstance Failure");
2775 }
Dustin Graves7c287352016-01-27 13:48:06 -07002776 }
2777
2778 /* Look for instance extensions */
2779 VkBool32 surfaceExtFound = 0;
2780 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002781 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002782
Karl Schultze4dc75c2016-02-02 15:37:51 -07002783 err = vkEnumerateInstanceExtensionProperties(
2784 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002785 assert(!err);
2786
Dustin Graves7c287352016-01-27 13:48:06 -07002787 if (instance_extension_count > 0) {
2788 VkExtensionProperties *instance_extensions =
2789 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2790 err = vkEnumerateInstanceExtensionProperties(
2791 NULL, &instance_extension_count, instance_extensions);
2792 assert(!err);
2793 for (uint32_t i = 0; i < instance_extension_count; i++) {
2794 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2795 instance_extensions[i].extensionName)) {
2796 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002797 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002798 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002799 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002800#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002801 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2802 instance_extensions[i].extensionName)) {
2803 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002804 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002805 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2806 }
Tony Barbour153cb062016-12-07 13:43:36 -07002807#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002808 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2809 instance_extensions[i].extensionName)) {
2810 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06002811 demo->extension_names[demo->enabled_extension_count++] =
2812 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2813 }
Tony Barbour153cb062016-12-07 13:43:36 -07002814#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002815 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2816 instance_extensions[i].extensionName)) {
2817 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002818 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002819 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2820 }
Tony Barbour153cb062016-12-07 13:43:36 -07002821#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002822 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2823 instance_extensions[i].extensionName)) {
2824 platformSurfaceExtFound = 1;
2825 demo->extension_names[demo->enabled_extension_count++] =
2826 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2827 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002828#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002829#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2830 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
2831 instance_extensions[i].extensionName)) {
2832 platformSurfaceExtFound = 1;
2833 demo->extension_names[demo->enabled_extension_count++] =
2834 VK_KHR_DISPLAY_EXTENSION_NAME;
2835 }
Tony Barbour153cb062016-12-07 13:43:36 -07002836#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002837 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2838 instance_extensions[i].extensionName)) {
2839 platformSurfaceExtFound = 1;
2840 demo->extension_names[demo->enabled_extension_count++] =
2841 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2842 }
Bill Hollingsf4352142017-02-14 22:58:56 -05002843#elif defined(VK_USE_PLATFORM_IOS_MVK)
2844 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2845 platformSurfaceExtFound = 1;
2846 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
2847 }
2848#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2849 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2850 platformSurfaceExtFound = 1;
2851 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
2852 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002853#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002854 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2855 instance_extensions[i].extensionName)) {
2856 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002857 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002858 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2859 }
2860 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002861 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002862 }
Dustin Graves7c287352016-01-27 13:48:06 -07002863
2864 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002865 }
Dustin Graves7c287352016-01-27 13:48:06 -07002866
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002867 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002868 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2869 "the " VK_KHR_SURFACE_EXTENSION_NAME
2870 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002871 "Vulkan installable client driver (ICD) installed?\nPlease "
2872 "look at the Getting Started guide for additional "
2873 "information.\n",
2874 "vkCreateInstance Failure");
2875 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002876 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002877#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002878 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2879 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2880 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002881 "Vulkan installable client driver (ICD) installed?\nPlease "
2882 "look at the Getting Started guide for additional "
2883 "information.\n",
2884 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002885#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002886 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2887 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2888 "Vulkan installable client driver (ICD) installed?\nPlease "
2889 "look at the Getting Started guide for additional "
2890 "information.\n",
2891 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002892#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002893 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2894 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2895 "Vulkan installable client driver (ICD) installed?\nPlease "
2896 "look at the Getting Started guide for additional "
2897 "information.\n",
2898 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002899#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002900 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2901 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2902 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002903 "Vulkan installable client driver (ICD) installed?\nPlease "
2904 "look at the Getting Started guide for additional "
2905 "information.\n",
2906 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002907#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2908 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2909 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2910 " extension.\n\nDo you have a compatible "
2911 "Vulkan installable client driver (ICD) installed?\nPlease "
2912 "look at the Getting Started guide for additional "
2913 "information.\n",
2914 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002915#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002916#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2917 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2918 "the " VK_KHR_DISPLAY_EXTENSION_NAME
2919 " extension.\n\nDo you have a compatible "
2920 "Vulkan installable client driver (ICD) installed?\nPlease "
2921 "look at the Getting Started guide for additional "
2922 "information.\n",
2923 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002924#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2925 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2926 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2927 " extension.\n\nDo you have a compatible "
2928 "Vulkan installable client driver (ICD) installed?\nPlease "
2929 "look at the Getting Started guide for additional "
2930 "information.\n",
2931 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002932#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002933 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2934 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2935 " extension.\n\nDo you have a compatible "
2936 "Vulkan installable client driver (ICD) installed?\nPlease "
2937 "look at the Getting Started guide for additional "
2938 "information.\n",
2939 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002940#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002941 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002942 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002943 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002944 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002945 .pApplicationName = APP_SHORT_NAME,
2946 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002947 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002948 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002949 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002950 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002951 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002952 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002953 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002954 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002955 .enabledLayerCount = demo->enabled_layer_count,
2956 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2957 .enabledExtensionCount = demo->enabled_extension_count,
2958 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002959 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002960
2961 /*
2962 * This is info for a temp callback to use during CreateInstance.
2963 * After the instance is created, we use the instance-based
2964 * function to register the final callback.
2965 */
Karl Schultza2615462017-01-20 13:19:20 -07002966 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002967 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002968 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07002969 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2970 dbgCreateInfoTemp.pNext = NULL;
2971 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
2972 dbgCreateInfoTemp.pUserData = demo;
2973 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06002974 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002975 if (demo->validate_checks_disabled) {
2976 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
2977 val_flags.pNext = NULL;
2978 val_flags.disabledValidationCheckCount = 1;
2979 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
2980 val_flags.pDisabledValidationChecks = &disabled_check;
2981 dbgCreateInfoTemp.pNext = (void*)&val_flags;
2982 }
Karl Schultza2615462017-01-20 13:19:20 -07002983 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002984 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002985
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002986 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002987
Chia-I Wu63636062015-10-26 21:10:41 +08002988 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002989 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2990 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002991 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002992 "additional information.\n",
2993 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002994 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002995 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002996 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002997 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002998 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002999 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3000 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003001 "the Getting Started guide for additional information.\n",
3002 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003003 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003004
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003005 /* Make initial call to query gpu_count, then second call for gpu info*/
3006 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3007 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003008
3009 if (gpu_count > 0) {
3010 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3011 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3012 assert(!err);
3013 /* For cube demo we just grab the first physical device */
3014 demo->gpu = physical_devices[0];
3015 free(physical_devices);
3016 } else {
3017 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3018 "Do you have a compatible Vulkan installable client driver (ICD) "
3019 "installed?\nPlease look at the Getting Started guide for "
3020 "additional information.\n",
3021 "vkEnumeratePhysicalDevices Failure");
3022 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003023
Dustin Graves7c287352016-01-27 13:48:06 -07003024 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003025 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003026 VkBool32 swapchainExtFound = 0;
3027 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003028 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003029
Karl Schultze4dc75c2016-02-02 15:37:51 -07003030 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3031 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003032 assert(!err);
3033
Dustin Graves7c287352016-01-27 13:48:06 -07003034 if (device_extension_count > 0) {
3035 VkExtensionProperties *device_extensions =
3036 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3037 err = vkEnumerateDeviceExtensionProperties(
3038 demo->gpu, NULL, &device_extension_count, device_extensions);
3039 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003040
Dustin Graves7c287352016-01-27 13:48:06 -07003041 for (uint32_t i = 0; i < device_extension_count; i++) {
3042 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3043 device_extensions[i].extensionName)) {
3044 swapchainExtFound = 1;
3045 demo->extension_names[demo->enabled_extension_count++] =
3046 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3047 }
3048 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003049 }
Dustin Graves7c287352016-01-27 13:48:06 -07003050
3051 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003052 }
Dustin Graves7c287352016-01-27 13:48:06 -07003053
Tony Barbourcc69f452015-10-08 13:59:42 -06003054 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003055 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3056 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3057 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003058 "Vulkan installable client driver (ICD) installed?\nPlease "
3059 "look at the Getting Started guide for additional "
3060 "information.\n",
3061 "vkCreateInstance Failure");
3062 }
3063
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003064 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003065 demo->CreateDebugReportCallback =
3066 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3067 demo->inst, "vkCreateDebugReportCallbackEXT");
3068 demo->DestroyDebugReportCallback =
3069 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3070 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003071 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003072 ERR_EXIT(
3073 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3074 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003075 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003076 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003077 ERR_EXIT(
3078 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3079 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003080 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003081 demo->DebugReportMessage =
3082 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3083 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003084 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003085 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003086 "vkGetProcAddr Failure");
3087 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003088
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003089 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003090 PFN_vkDebugReportCallbackEXT callback;
3091 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003092 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003093 dbgCreateInfo.pNext = NULL;
3094 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003095 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003096 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003097 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003098 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3099 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003100 switch (err) {
3101 case VK_SUCCESS:
3102 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003103 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003104 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3105 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003106 break;
3107 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003108 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3109 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003110 break;
3111 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003112 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003113 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003114
3115 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003116 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3117 &demo->queue_family_count, NULL);
3118 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003119
Karl Schultze4dc75c2016-02-02 15:37:51 -07003120 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003121 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3122 vkGetPhysicalDeviceQueueFamilyProperties(
3123 demo->gpu, &demo->queue_family_count, demo->queue_props);
3124
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003125 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003126 // If app has specific feature requirements it should check supported
3127 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003128 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003129 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003130
Tony Barbourda2bad52016-01-22 14:36:40 -07003131 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3132 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3133 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3134 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3135 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3136}
3137
Karl Schultze4dc75c2016-02-02 15:37:51 -07003138static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003139 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003140 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003141 VkDeviceQueueCreateInfo queues[2];
3142 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3143 queues[0].pNext = NULL;
3144 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3145 queues[0].queueCount = 1;
3146 queues[0].pQueuePriorities = queue_priorities;
3147 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003148
3149 VkDeviceCreateInfo device = {
3150 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3151 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003152 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003153 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003154 .enabledLayerCount = 0,
3155 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003156 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003157 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3158 .pEnabledFeatures =
3159 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003160 };
Tony Barbour22e06272016-09-07 16:07:56 -06003161 if (demo->separate_present_queue) {
3162 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3163 queues[1].pNext = NULL;
3164 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3165 queues[1].queueCount = 1;
3166 queues[1].pQueuePriorities = queue_priorities;
3167 queues[1].flags = 0;
3168 device.queueCreateInfoCount = 2;
3169 }
Chia-I Wu63636062015-10-26 21:10:41 +08003170 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003171 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003172}
3173
Karl Schultze4dc75c2016-02-02 15:37:51 -07003174static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003175 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003176
Karl Schultze4dc75c2016-02-02 15:37:51 -07003177// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003178#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003179 VkWin32SurfaceCreateInfoKHR createInfo;
3180 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3181 createInfo.pNext = NULL;
3182 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003183 createInfo.hinstance = demo->connection;
3184 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003185
Karl Schultze4dc75c2016-02-02 15:37:51 -07003186 err =
3187 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003188#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003189 VkWaylandSurfaceCreateInfoKHR createInfo;
3190 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3191 createInfo.pNext = NULL;
3192 createInfo.flags = 0;
3193 createInfo.display = demo->display;
3194 createInfo.surface = demo->window;
3195
3196 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3197 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003198#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003199#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3200 VkAndroidSurfaceCreateInfoKHR createInfo;
3201 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3202 createInfo.pNext = NULL;
3203 createInfo.flags = 0;
3204 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003205
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003206 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003207#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3208 VkXlibSurfaceCreateInfoKHR createInfo;
3209 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3210 createInfo.pNext = NULL;
3211 createInfo.flags = 0;
3212 createInfo.dpy = demo->display;
3213 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003214
Tony Barbour153cb062016-12-07 13:43:36 -07003215 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003216 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003217#elif defined(VK_USE_PLATFORM_XCB_KHR)
3218 VkXcbSurfaceCreateInfoKHR createInfo;
3219 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3220 createInfo.pNext = NULL;
3221 createInfo.flags = 0;
3222 createInfo.connection = demo->connection;
3223 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003224
Tony Barbour153cb062016-12-07 13:43:36 -07003225 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003226#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3227 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003228#elif defined(VK_USE_PLATFORM_IOS_MVK)
3229 VkIOSSurfaceCreateInfoMVK surface;
3230 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3231 surface.pNext = NULL;
3232 surface.flags = 0;
3233 surface.pView = demo->window;
3234
3235 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3236#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3237 VkMacOSSurfaceCreateInfoMVK surface;
3238 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3239 surface.pNext = NULL;
3240 surface.flags = 0;
3241 surface.pView = demo->window;
3242
3243 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003244#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003245 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003246
Tony Barbourcc69f452015-10-08 13:59:42 -06003247 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003248 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003249 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003250 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003251 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003252 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003253 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003254
3255 // Search for a graphics and a present queue in the array of queue
3256 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003257 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3258 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003259 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003260 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3261 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3262 graphicsQueueFamilyIndex = i;
3263 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003264
Tony Barbourab2a0362016-09-06 11:40:12 -06003265 if (supportsPresent[i] == VK_TRUE) {
3266 graphicsQueueFamilyIndex = i;
3267 presentQueueFamilyIndex = i;
3268 break;
3269 }
3270 }
3271 }
3272
3273 if (presentQueueFamilyIndex == UINT32_MAX) {
3274 // If didn't find a queue that supports both graphics and present, then
3275 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003276 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003277 if (supportsPresent[i] == VK_TRUE) {
3278 presentQueueFamilyIndex = i;
3279 break;
3280 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003281 }
3282 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003283
3284 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003285 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3286 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003287 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003288 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003289 }
3290
Tony Barbourab2a0362016-09-06 11:40:12 -06003291 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3292 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003293 demo->separate_present_queue =
3294 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003295 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003296
Tony Barbourda2bad52016-01-22 14:36:40 -07003297 demo_create_device(demo);
3298
3299 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3300 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3301 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3302 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3303 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3304
Tony Barboura2a67812016-07-18 13:21:06 -06003305 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3306 &demo->graphics_queue);
3307
Tony Barbour22e06272016-09-07 16:07:56 -06003308 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003309 demo->present_queue = demo->graphics_queue;
3310 } else {
3311 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3312 &demo->present_queue);
3313 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003314
Ian Elliottaaae5352015-07-06 14:27:58 -06003315 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003316 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003317 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003318 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003319 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003320 VkSurfaceFormatKHR *surfFormats =
3321 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003322 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003323 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003324 assert(!err);
3325 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3326 // the surface has no preferred format. Otherwise, at least one
3327 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003328 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003329 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003330 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003331 assert(formatCount >= 1);
3332 demo->format = surfFormats[0].format;
3333 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003334 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003335
David Pinedo2bb7c932015-06-18 17:03:14 -06003336 demo->quit = false;
3337 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003338
Tony Barbource7524e2016-07-28 12:01:45 -06003339 // Create semaphores to synchronize acquiring presentable buffers before
3340 // rendering and waiting for drawing to be complete before presenting
3341 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3342 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3343 .pNext = NULL,
3344 .flags = 0,
3345 };
Tony Barbource7524e2016-07-28 12:01:45 -06003346
Tony Barbour66a56c32016-09-21 13:10:56 -06003347 // Create fences that we can use to throttle if we get too far
3348 // ahead of the image presents
3349 VkFenceCreateInfo fence_ci = {
3350 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3351 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003352 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003353 };
3354 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003355 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3356 assert(!err);
3357
Tony Barbour22e06272016-09-07 16:07:56 -06003358 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003359 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003360 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003361
3362 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3363 &demo->draw_complete_semaphores[i]);
3364 assert(!err);
3365
3366 if (demo->separate_present_queue) {
3367 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3368 &demo->image_ownership_semaphores[i]);
3369 assert(!err);
3370 }
Tony Barbour22e06272016-09-07 16:07:56 -06003371 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003372 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003373
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003374 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003375 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003376}
3377
Tony Barbour153cb062016-12-07 13:43:36 -07003378#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003379static void registry_handle_global(void *data, struct wl_registry *registry,
3380 uint32_t name, const char *interface,
3381 uint32_t version UNUSED) {
3382 struct demo *demo = data;
3383 if (strcmp(interface, "wl_compositor") == 0) {
3384 demo->compositor =
3385 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3386 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3387 * tp xdg_shell */
3388 } else if (strcmp(interface, "wl_shell") == 0) {
3389 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3390 }
3391}
3392
3393static void registry_handle_global_remove(void *data UNUSED,
3394 struct wl_registry *registry UNUSED,
3395 uint32_t name UNUSED) {}
3396
3397static const struct wl_registry_listener registry_listener = {
3398 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003399#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003400#endif
3401
Karl Schultze4dc75c2016-02-02 15:37:51 -07003402static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003403#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003404 const xcb_setup_t *setup;
3405 xcb_screen_iterator_t iter;
3406 int scr;
3407
3408 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003409 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003410 printf("Cannot find a compatible Vulkan installable client driver "
3411 "(ICD).\nExiting ...\n");
3412 fflush(stdout);
3413 exit(1);
3414 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003415
3416 setup = xcb_get_setup(demo->connection);
3417 iter = xcb_setup_roots_iterator(setup);
3418 while (scr-- > 0)
3419 xcb_screen_next(&iter);
3420
3421 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003422#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3423 demo->display = wl_display_connect(NULL);
3424
3425 if (demo->display == NULL) {
3426 printf("Cannot find a compatible Vulkan installable client driver "
3427 "(ICD).\nExiting ...\n");
3428 fflush(stdout);
3429 exit(1);
3430 }
3431
3432 demo->registry = wl_display_get_registry(demo->display);
3433 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3434 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003435#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003436#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003437}
3438
Karl Schultze4dc75c2016-02-02 15:37:51 -07003439static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003440 vec3 eye = {0.0f, 3.0f, 5.0f};
3441 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003442 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003443
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003444 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003445 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003446 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003447
Piers Daniell735ee532015-02-23 16:23:13 -07003448 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003449 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003450 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003451 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003452 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003453 if ((strcmp(argv[i], "--present_mode") == 0) &&
3454 (i < argc - 1)) {
3455 demo->presentMode = atoi(argv[i+1]);
3456 i++;
3457 continue;
3458 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003459 if (strcmp(argv[i], "--break") == 0) {
3460 demo->use_break = true;
3461 continue;
3462 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003463 if (strcmp(argv[i], "--validate") == 0) {
3464 demo->validate = true;
3465 continue;
3466 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003467 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3468 demo->validate = true;
3469 demo->validate_checks_disabled = true;
3470 continue;
3471 }
Tony Barbour2593b182016-04-19 10:57:58 -06003472 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003473 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003474 continue;
3475 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003476 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3477 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3478 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003479 i++;
3480 continue;
3481 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003482 if (strcmp(argv[i], "--suppress_popups") == 0) {
3483 demo->suppress_popups = true;
3484 continue;
3485 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003486
Cody Northropaf28a532016-09-27 10:50:57 -06003487#if defined(ANDROID)
3488 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3489#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003490 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Tony Barbour291d57b2016-10-21 14:47:07 -06003491 "[--c <framecount>] [--suppress_popups] [--present_mode <present mode enum>]\n"
3492 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3493 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3494 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3495 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3496 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3497 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003498 fflush(stderr);
3499 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003500#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003501 }
3502
Tony Barbour153cb062016-12-07 13:43:36 -07003503 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003504
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003505 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003506
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003507 demo->width = 500;
3508 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003509
Tony Barbour66a56c32016-09-21 13:10:56 -06003510 demo->spin_angle = 4.0f;
3511 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003512 demo->pause = false;
3513
Karl Schultze4dc75c2016-02-02 15:37:51 -07003514 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3515 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003516 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3517 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003518
3519 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003520}
3521
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003522#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003523// Include header required for parsing the command line options.
3524#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003525
Karl Schultze4dc75c2016-02-02 15:37:51 -07003526int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3527 int nCmdShow) {
3528 MSG msg; // message
3529 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003530 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003531 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003532
Karl Schultze4dc75c2016-02-02 15:37:51 -07003533 // Use the CommandLine functions to get the command line arguments.
3534 // Unfortunately, Microsoft outputs
3535 // this information as wide characters for Unicode, and we simply want the
3536 // Ascii version to be compatible
3537 // with the non-Windows side. So, we have to convert the information to
3538 // Ascii character strings.
3539 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3540 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003541 argc = 0;
3542 }
3543
Karl Schultze4dc75c2016-02-02 15:37:51 -07003544 if (argc > 0) {
3545 argv = (char **)malloc(sizeof(char *) * argc);
3546 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003547 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003548 } else {
3549 for (int iii = 0; iii < argc; iii++) {
3550 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003551 size_t numConverted = 0;
3552
Karl Schultze4dc75c2016-02-02 15:37:51 -07003553 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3554 if (argv[iii] != NULL) {
3555 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3556 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003557 }
3558 }
3559 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003560 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003561 argv = NULL;
3562 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003563
3564 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003565
3566 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003567 if (argc > 0 && argv != NULL) {
3568 for (int iii = 0; iii < argc; iii++) {
3569 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003570 free(argv[iii]);
3571 }
3572 }
3573 free(argv);
3574 }
3575
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003576 demo.connection = hInstance;
3577 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003578 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003579 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003580
3581 demo_prepare(&demo);
3582
Karl Schultze4dc75c2016-02-02 15:37:51 -07003583 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003584
3585 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003586 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003587 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003588 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003589 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003590 done = true; // if found, quit app
3591 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003592 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003593 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003594 DispatchMessage(&msg);
3595 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003596 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003597 }
3598
3599 demo_cleanup(&demo);
3600
Karl Schultze4dc75c2016-02-02 15:37:51 -07003601 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003602}
Bill Hollingsf4352142017-02-14 22:58:56 -05003603
3604#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3605static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003606 const char* argv[] = { "CubeSample" };
3607 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003608
Tony Barbourc65cd752017-03-13 15:29:35 -06003609 demo_init(demo, argc, (char**)argv);
3610 demo->window = view;
3611 demo_init_vk_swapchain(demo);
3612 demo_prepare(demo);
3613 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003614}
3615
3616static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003617 // Wait for work to finish before updating MVP.
3618 vkDeviceWaitIdle(demo->device);
3619 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003620
Tony Barbourc65cd752017-03-13 15:29:35 -06003621 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003622}
3623
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003624#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3625#include <android/log.h>
3626#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003627#include "android_util.h"
3628
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003629static bool initialized = false;
3630static bool active = false;
3631struct demo demo;
3632
3633static int32_t processInput(struct android_app* app, AInputEvent* event) {
3634 return 0;
3635}
3636
3637static void processCommand(struct android_app* app, int32_t cmd) {
3638 switch(cmd) {
3639 case APP_CMD_INIT_WINDOW: {
3640 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003641 // We're getting a new window. If the app is starting up, we
3642 // need to initialize. If the app has already been
3643 // initialized, that means that we lost our previous window,
3644 // which means that we have a lot of work to do. At a minimum,
3645 // we need to destroy the swapchain and surface associated with
3646 // the old window, and create a new surface and swapchain.
3647 // However, since there are a lot of other objects/state that
3648 // is tied to the swapchain, it's easiest to simply cleanup and
3649 // start over (i.e. use a brute-force approach of re-starting
3650 // the app)
3651 if (demo.prepared) {
3652 demo_cleanup(&demo);
3653 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003654
3655 // Parse Intents into argc, argv
3656 // Use the following key to send arguments, i.e.
3657 // --es args "--validate"
3658 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003659 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003660 int argc = 0;
3661 char** argv = get_args(app, key, appTag, &argc);
3662
3663 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3664 for (int i = 0; i < argc; i++)
3665 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3666
3667 demo_init(&demo, argc, argv);
3668
3669 // Free the argv malloc'd by get_args
3670 for (int i = 0; i < argc; i++)
3671 free(argv[i]);
3672
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003673 demo.window = (void*)app->window;
3674 demo_init_vk_swapchain(&demo);
3675 demo_prepare(&demo);
3676 initialized = true;
3677 }
3678 break;
3679 }
3680 case APP_CMD_GAINED_FOCUS: {
3681 active = true;
3682 break;
3683 }
3684 case APP_CMD_LOST_FOCUS: {
3685 active = false;
3686 break;
3687 }
3688 }
3689}
3690
3691void android_main(struct android_app *app)
3692{
3693 app_dummy();
3694
Cody Northrop8707a6e2016-04-26 19:59:19 -06003695#ifdef ANDROID
3696 int vulkanSupport = InitVulkan();
3697 if (vulkanSupport == 0)
3698 return;
3699#endif
3700
Ian Elliottf05d5d12016-05-17 12:03:35 -06003701 demo.prepared = false;
3702
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003703 app->onAppCmd = processCommand;
3704 app->onInputEvent = processInput;
3705
3706 while(1) {
3707 int events;
3708 struct android_poll_source* source;
3709 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3710 if (source) {
3711 source->process(app, source);
3712 }
3713
3714 if (app->destroyRequested != 0) {
3715 demo_cleanup(&demo);
3716 return;
3717 }
3718 }
3719 if (initialized && active) {
3720 demo_run(&demo);
3721 }
3722 }
3723
3724}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003725#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003726int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003727 struct demo demo;
3728
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003729 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003730#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003731 demo_create_xcb_window(&demo);
3732#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3733 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003734#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3735 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003736#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003737#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003738
Tony Barbourcc69f452015-10-08 13:59:42 -06003739 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003740
3741 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003742
Tony Barbour153cb062016-12-07 13:43:36 -07003743#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003744 demo_run_xcb(&demo);
3745#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3746 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003747#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3748 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003749#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003750#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3751 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003752#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003753
3754 demo_cleanup(&demo);
3755
Karl Schultz0218c002016-03-22 17:06:13 -06003756 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003757}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003758#endif