blob: 90d1cb84c81028f85243a4da85f6d8abe8d11d12 [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>
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090024*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070025
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060026#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
31#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070032#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090033#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060034#include <X11/Xutil.h>
35#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036
Ian Elliott639ca472015-04-16 15:23:05 -060037#ifdef _WIN32
38#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060039#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060040#endif // _WIN32
41
Cody Northrop8707a6e2016-04-26 19:59:19 -060042#ifdef ANDROID
43#include "vulkan_wrapper.h"
44#else
David Pinedoc5193c12015-11-06 12:54:48 -070045#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060046#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070047
David Pinedo541526f2015-11-24 09:00:24 -070048#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060049#include "linmath.h"
50
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060051#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054
Tony Barbour66a56c32016-09-21 13:10:56 -060055// Allow a maximum of two outstanding presentation operations.
56#define FRAME_LAG 2
57
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060058#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
59
Tony Barbourfdc2d352015-04-22 09:02:32 -060060#if defined(NDEBUG) && defined(__GNUC__)
61#define U_ASSERT_ONLY __attribute__((unused))
62#else
63#define U_ASSERT_ONLY
64#endif
65
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090066#if defined(__GNUC__)
67#define UNUSED __attribute__((unused))
68#else
69#define UNUSED
70#endif
71
Ian Elliott07264132015-04-28 11:35:02 -060072#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060073bool in_callback = false;
Karl Schultze4dc75c2016-02-02 15:37:51 -070074#define ERR_EXIT(err_msg, err_class) \
75 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060076 if (!demo->suppress_popups) \
77 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070078 exit(1); \
79 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060080
Michael Lentinec6cde4b2016-04-18 13:20:45 -050081#elif defined __ANDROID__
82#include <android/log.h>
83#define ERR_EXIT(err_msg, err_class) \
84 do { \
85 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
86 exit(1); \
87 } while (0)
88#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070089#define ERR_EXIT(err_msg, err_class) \
90 do { \
91 printf(err_msg); \
92 fflush(stdout); \
93 exit(1); \
94 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050095#endif
Ian Elliott07264132015-04-28 11:35:02 -060096
Karl Schultze4dc75c2016-02-02 15:37:51 -070097#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
98 { \
99 demo->fp##entrypoint = \
100 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
101 if (demo->fp##entrypoint == NULL) { \
102 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
103 "vkGetInstanceProcAddr Failure"); \
104 } \
105 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600106
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600107static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
108
Karl Schultze4dc75c2016-02-02 15:37:51 -0700109#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
110 { \
111 if (!g_gdpa) \
112 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
113 demo->inst, "vkGetDeviceProcAddr"); \
114 demo->fp##entrypoint = \
115 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
116 if (demo->fp##entrypoint == NULL) { \
117 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
118 "vkGetDeviceProcAddr Failure"); \
119 } \
120 }
Ian Elliott673898b2015-06-22 15:07:49 -0600121
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600122/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700123 * structure to track all objects related to a texture.
124 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600125struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600126 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700127
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600128 VkImage image;
129 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600130
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800131 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500132 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600133 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700134 int32_t tex_width, tex_height;
135};
136
Karl Schultze4dc75c2016-02-02 15:37:51 -0700137static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600138
Karl Schultz0218c002016-03-22 17:06:13 -0600139static int validation_error = 0;
140
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600141struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700143 float mvp[4][4];
144 float position[12 * 3][4];
145 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146};
147
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600148struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700150 float mvp[4][4];
151 float position[12 * 3][4];
152 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600153};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600154
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600155//--------------------------------------------------------------------------------------
156// Mesh and VertexFormat Data
157//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700158// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600159static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 -1.0f,-1.0f, 1.0f,
162 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 -1.0f, 1.0f,-1.0f,
165 -1.0f,-1.0f,-1.0f,
166
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f, 1.0f,-1.0f,
169 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600176 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600179 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 -1.0f, 1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 1.0f, 1.0f, 1.0f,
190 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 1.0f,-1.0f,-1.0f,
193 1.0f, 1.0f,-1.0f,
194
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600197 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 1.0f,-1.0f, 1.0f,
200 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600201};
202
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600203static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700204 0.0f, 1.0f, // -X side
205 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800207 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208 0.0f, 0.0f,
209 0.0f, 1.0f,
210
Rene Lindsay76867e82016-07-29 10:49:11 -0700211 1.0f, 1.0f, // -Z side
212 0.0f, 0.0f,
213 0.0f, 1.0f,
214 1.0f, 1.0f,
215 1.0f, 0.0f,
216 0.0f, 0.0f,
217
218 1.0f, 0.0f, // -Y side
219 1.0f, 1.0f,
220 0.0f, 1.0f,
221 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600222 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224
Rene Lindsay76867e82016-07-29 10:49:11 -0700225 1.0f, 0.0f, // +Y side
226 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231
Rene Lindsay76867e82016-07-29 10:49:11 -0700232 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700234 0.0f, 1.0f,
235 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700238
239 0.0f, 0.0f, // +Z side
240 0.0f, 1.0f,
241 1.0f, 0.0f,
242 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600243 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700244 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600245};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700246// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249 int i;
250
251 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700252 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600253 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
254 }
255 printf("\n");
256 fflush(stdout);
257}
258
Karl Schultze4dc75c2016-02-02 15:37:51 -0700259void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600260 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600262 printf("\n");
263 fflush(stdout);
264}
265
Karl Schultze4dc75c2016-02-02 15:37:51 -0700266VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600267BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
268 uint64_t srcObject, size_t location, int32_t msgCode,
269 const char *pLayerPrefix, const char *pMsg,
270 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700271#ifndef WIN32
272 raise(SIGTRAP);
273#else
274 DebugBreak();
275#endif
276
277 return false;
278}
279
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600280typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600281 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800282 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600283 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600284 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600285} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600286
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600287struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500288#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600289#define APP_NAME_STR_LEN 80
290 HINSTANCE connection; // hInstance - Windows Instance
291 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700292 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600293 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700294#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600295 Display* display;
296 Window xlib_window;
297 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700298#elif defined(VK_USE_PLATFORM_XCB_KHR)
299 Display* display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600300 xcb_connection_t *connection;
301 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600302 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800303 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900304#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
305 struct wl_display *display;
306 struct wl_registry *registry;
307 struct wl_compositor *compositor;
308 struct wl_surface *window;
309 struct wl_shell *shell;
310 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500311#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
312 ANativeWindow* window;
313#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700314 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600315 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700316 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600317 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600320 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600322 VkQueue graphics_queue;
323 VkQueue present_queue;
324 uint32_t graphics_queue_family_index;
325 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600326 VkSemaphore image_acquired_semaphores[FRAME_LAG];
327 VkSemaphore draw_complete_semaphores[FRAME_LAG];
328 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600329 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600330 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600331 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332
Tony Barbourda2bad52016-01-22 14:36:40 -0700333 uint32_t enabled_extension_count;
334 uint32_t enabled_layer_count;
335 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600336 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700337
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600339 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600340 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341
Karl Schultze4dc75c2016-02-02 15:37:51 -0700342 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
343 fpGetPhysicalDeviceSurfaceSupportKHR;
344 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
345 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
346 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
347 fpGetPhysicalDeviceSurfaceFormatsKHR;
348 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
349 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600350 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
351 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
352 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
353 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
354 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600355 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600356 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600357 SwapchainBuffers *buffers;
Tony Barbour291d57b2016-10-21 14:47:07 -0600358 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600359 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600360 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800362 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600363 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
365 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600368 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800369 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500370 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600371 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372 } depth;
373
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600374 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600375 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376
377 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800379 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500380 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600381 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600382 } uniform_data;
383
Karl Schultze4dc75c2016-02-02 15:37:51 -0700384 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500385 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600387 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800388 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600389 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600391 mat4x4 projection_matrix;
392 mat4x4 view_matrix;
393 mat4x4 model_matrix;
394
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600395 float spin_angle;
396 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600397 bool pause;
398
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600399 VkShaderModule vert_shader_module;
400 VkShaderModule frag_shader_module;
401
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600402 VkDescriptorPool desc_pool;
403 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800404
Tony Barbourd8e504f2015-10-08 14:26:24 -0600405 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800406
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600408 int32_t curFrame;
409 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600410 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600411 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600412 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700413 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
414 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
415 VkDebugReportCallbackEXT msg_callback;
416 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600417
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600418 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600419 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600420};
421
lenny-lunargfbe22392016-06-06 11:07:53 -0600422VKAPI_ATTR VkBool32 VKAPI_CALL
423dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
424 uint64_t srcObject, size_t location, int32_t msgCode,
425 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600426
427 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600428 char *message = (char *)malloc(strlen(pMsg) + 100);
429
430 assert(message);
431
Cody Northropaf28a532016-09-27 10:50:57 -0600432 // We know we're submitting queues without fences, ignore this
433 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer"))
434 return false;
435
436 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
437 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600438 validation_error = 1;
439 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600440 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
441 validation_error = 1;
442 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600443 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600444 validation_error = 1;
445 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
446 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
447 validation_error = 1;
448 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
449 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600450 validation_error = 1;
451 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600452 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600453 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600454 }
455
456#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600457
Tony Barbour602ca4f2016-09-12 14:02:43 -0600458 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600459 struct demo *demo = (struct demo*) pUserData;
460 if (!demo->suppress_popups)
461 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600462 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600463
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600464#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600465
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600466 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600467 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600468 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600469 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600470 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600471 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600472 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600473 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600474 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600475 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600476 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600477 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600478 }
Cody Northropaf28a532016-09-27 10:50:57 -0600479
lenny-lunargfbe22392016-06-06 11:07:53 -0600480#else
Cody Northropaf28a532016-09-27 10:50:57 -0600481
lenny-lunargfbe22392016-06-06 11:07:53 -0600482 printf("%s\n", message);
483 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600484
lenny-lunargfbe22392016-06-06 11:07:53 -0600485#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600486
lenny-lunargfbe22392016-06-06 11:07:53 -0600487 free(message);
488
Cody Northropaf28a532016-09-27 10:50:57 -0600489 //clang-format on
490
lenny-lunargfbe22392016-06-06 11:07:53 -0600491 /*
492 * false indicates that layer should not bail-out of an
493 * API call that had validation failures. This may mean that the
494 * app dies inside the driver due to invalid parameter(s).
495 * That's what would happen without validation layers, so we'll
496 * keep that behavior here.
497 */
498 return false;
499}
500
Ian Elliottcf074d32015-10-16 18:02:43 -0600501// Forward declaration:
502static void demo_resize(struct demo *demo);
503
Karl Schultze4dc75c2016-02-02 15:37:51 -0700504static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
505 VkFlags requirements_mask,
506 uint32_t *typeIndex) {
507 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600508 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700509 if ((typeBits & 1) == 1) {
510 // Type is available, does it match user properties?
511 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
512 requirements_mask) == requirements_mask) {
513 *typeIndex = i;
514 return true;
515 }
516 }
517 typeBits >>= 1;
518 }
519 // No memory types matched, return failure
520 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600521}
522
Karl Schultze4dc75c2016-02-02 15:37:51 -0700523static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600524 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525
Tony Barbource7524e2016-07-28 12:01:45 -0600526 // This function could get called twice if the texture uses a staging buffer
527 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600528 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 return;
530
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600531 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532 assert(!err);
533
Tony Barbource7524e2016-07-28 12:01:45 -0600534 VkFence fence;
535 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
536 .pNext = NULL,
537 .flags = 0};
538 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700539 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700540 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
541 .pNext = NULL,
542 .waitSemaphoreCount = 0,
543 .pWaitSemaphores = NULL,
544 .pWaitDstStageMask = NULL,
545 .commandBufferCount = 1,
546 .pCommandBuffers = cmd_bufs,
547 .signalSemaphoreCount = 0,
548 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549
Tony Barbource7524e2016-07-28 12:01:45 -0600550 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551 assert(!err);
552
Tony Barbource7524e2016-07-28 12:01:45 -0600553 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600554 assert(!err);
555
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600556 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600557 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600558 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559}
560
Karl Schultze4dc75c2016-02-02 15:37:51 -0700561static void demo_set_image_layout(struct demo *demo, VkImage image,
562 VkImageAspectFlags aspectMask,
563 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700564 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600565 VkAccessFlagBits srcAccessMask,
566 VkPipelineStageFlags src_stages,
567 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600568 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600569
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600570 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600571 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600572 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700573 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800574 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600575 .oldLayout = old_image_layout,
576 .newLayout = new_image_layout,
577 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700578 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600579
Tony Barboureb5077b2016-10-19 15:23:36 -0600580 switch (new_image_layout) {
581 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600583 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
584 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585
Tony Barboureb5077b2016-10-19 15:23:36 -0600586 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700587 image_memory_barrier.dstAccessMask =
588 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600589 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700590
Tony Barboureb5077b2016-10-19 15:23:36 -0600591 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700592 image_memory_barrier.dstAccessMask =
593 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600594 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700595
Tony Barboureb5077b2016-10-19 15:23:36 -0600596 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700597 image_memory_barrier.dstAccessMask =
598 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600599 break;
600
601 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
602 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
603 break;
604
605 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
606 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
607 break;
608
609 default:
610 image_memory_barrier.dstAccessMask = 0;
611 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600612 }
613
Tony Barbourf165b5d2016-10-03 16:01:41 -0600614
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600615 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600616
Karl Schultze4dc75c2016-02-02 15:37:51 -0700617 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
618 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600619}
620
Karl Schultze4dc75c2016-02-02 15:37:51 -0700621static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700622 const VkCommandBufferBeginInfo cmd_buf_info = {
623 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
624 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600625 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700626 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700627 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800628 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700629 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
630 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800631 };
632 const VkRenderPassBeginInfo rp_begin = {
633 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
634 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800635 .renderPass = demo->render_pass,
636 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800637 .renderArea.offset.x = 0,
638 .renderArea.offset.y = 0,
639 .renderArea.extent.width = demo->width,
640 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600641 .clearValueCount = 2,
642 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700643 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800644 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600646 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600647 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800648 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700649 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
650 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
651 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
652 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600653 VkViewport viewport;
654 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700655 viewport.height = (float)demo->height;
656 viewport.width = (float)demo->width;
657 viewport.minDepth = (float)0.0f;
658 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700659 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600660
661 VkRect2D scissor;
662 memset(&scissor, 0, sizeof(scissor));
663 scissor.extent.width = demo->width;
664 scissor.extent.height = demo->height;
665 scissor.offset.x = 0;
666 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700667 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600668 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600669 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600670 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800671 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672
Tony Barbour22e06272016-09-07 16:07:56 -0600673 if (demo->separate_present_queue) {
674 // We have to transfer ownership from the graphics queue family to the
675 // present queue family to be able to present. Note that we don't have
676 // to transfer from present queue family back to graphics queue family at
677 // the start of the next frame because we don't care about the image's
678 // contents at that point.
679 VkImageMemoryBarrier image_ownership_barrier = {
680 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
681 .pNext = NULL,
682 .srcAccessMask = 0,
683 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
684 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
685 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
686 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
687 .dstQueueFamilyIndex = demo->present_queue_family_index,
688 .image = demo->buffers[demo->current_buffer].image,
689 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
690
691 vkCmdPipelineBarrier(cmd_buf,
692 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600693 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600694 0, NULL, 0, NULL, 1, &image_ownership_barrier);
695 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600696 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697 assert(!err);
698}
699
Tony Barbour22e06272016-09-07 16:07:56 -0600700void demo_build_image_ownership_cmd(struct demo *demo, int i) {
701 VkResult U_ASSERT_ONLY err;
702
703 const VkCommandBufferBeginInfo cmd_buf_info = {
704 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
705 .pNext = NULL,
706 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
707 .pInheritanceInfo = NULL,
708 };
709 err = vkBeginCommandBuffer(demo->buffers[i].graphics_to_present_cmd,
710 &cmd_buf_info);
711 assert(!err);
712
713 VkImageMemoryBarrier image_ownership_barrier = {
714 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
715 .pNext = NULL,
716 .srcAccessMask = 0,
717 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
718 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
719 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
720 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
721 .dstQueueFamilyIndex = demo->present_queue_family_index,
722 .image = demo->buffers[i].image,
723 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
724
725 vkCmdPipelineBarrier(demo->buffers[i].graphics_to_present_cmd,
726 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
727 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
728 NULL, 0, NULL, 1, &image_ownership_barrier);
729 err = vkEndCommandBuffer(demo->buffers[i].graphics_to_present_cmd);
730 assert(!err);
731}
732
Karl Schultze4dc75c2016-02-02 15:37:51 -0700733void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600734 mat4x4 MVP, Model, VP;
735 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600736 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600737 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600739 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600740
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600741 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600742 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700743 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
744 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600745 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600746
Karl Schultze4dc75c2016-02-02 15:37:51 -0700747 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
748 demo->uniform_data.mem_alloc.allocationSize, 0,
749 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600750 assert(!err);
751
Karl Schultze4dc75c2016-02-02 15:37:51 -0700752 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600754 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600755}
756
Karl Schultze4dc75c2016-02-02 15:37:51 -0700757static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600758 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600759
szdarkhackd322ff02016-10-08 09:51:22 +0300760 // Ensure no more than FRAME_LAG presentations are outstanding
761 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
762 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600763
Ian Elliottaaae5352015-07-06 14:27:58 -0600764 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700765 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour66a56c32016-09-21 13:10:56 -0600766 demo->image_acquired_semaphores[demo->frame_index], demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600767 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600768
Ian Elliottcf074d32015-10-16 18:02:43 -0600769 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
770 // demo->swapchain is out of date (e.g. the window was resized) and
771 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600772 demo->frame_index += 1;
773 demo->frame_index %= FRAME_LAG;
774
Ian Elliottcf074d32015-10-16 18:02:43 -0600775 demo_resize(demo);
776 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600777 return;
778 } else if (err == VK_SUBOPTIMAL_KHR) {
779 // demo->swapchain is not as optimal as it could be, but the platform's
780 // presentation engine will still present the image correctly.
781 } else {
782 assert(!err);
783 }
Tony Barbource7524e2016-07-28 12:01:45 -0600784 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600785 // that the image won't be rendered to until the presentation
786 // engine has fully released ownership to the application, and it is
787 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600788 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600789 VkPipelineStageFlags pipe_stage_flags;
790 VkSubmitInfo submit_info;
791 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
792 submit_info.pNext = NULL;
793 submit_info.pWaitDstStageMask = &pipe_stage_flags;
794 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
795 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600796 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600797 submit_info.commandBufferCount = 1;
798 submit_info.pCommandBuffers = &demo->buffers[demo->current_buffer].cmd;
799 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600800 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura2a67812016-07-18 13:21:06 -0600801 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600802 assert(!err);
803
Tony Barbour22e06272016-09-07 16:07:56 -0600804 if (demo->separate_present_queue) {
805 // If we are using separate queues, change image ownership to the
806 // present queue before presenting, waiting for the draw complete
807 // semaphore and signalling the ownership released semaphore when finished
808 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
809 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600810 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600811 submit_info.commandBufferCount = 1;
812 submit_info.pCommandBuffers =
813 &demo->buffers[demo->current_buffer].graphics_to_present_cmd;
814 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600815 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600816 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
817 assert(!err);
818 }
819
820 // If we are using separate queues we have to wait for image ownership,
821 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600822 VkPresentInfoKHR present = {
823 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600824 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600825 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600826 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600827 ? &demo->image_ownership_semaphores[demo->frame_index]
828 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600829 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700830 .pSwapchains = &demo->swapchain,
831 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600832 };
833
Tony Barboura2a67812016-07-18 13:21:06 -0600834 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600835 demo->frame_index += 1;
836 demo->frame_index %= FRAME_LAG;
837
Ian Elliottcf074d32015-10-16 18:02:43 -0600838 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
839 // demo->swapchain is out of date (e.g. the window was resized) and
840 // must be recreated:
841 demo_resize(demo);
842 } else if (err == VK_SUBOPTIMAL_KHR) {
843 // demo->swapchain is not as optimal as it could be, but the platform's
844 // presentation engine will still present the image correctly.
845 } else {
846 assert(!err);
847 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600848}
849
Karl Schultze4dc75c2016-02-02 15:37:51 -0700850static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600851 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600852 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600853
Ian Elliottb08e0d92015-11-20 11:55:46 -0700854 // Check the surface capabilities and formats
855 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700856 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
857 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600858 assert(!err);
859
Ian Elliott8528eff2015-08-07 15:56:59 -0600860 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700861 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
862 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600863 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600864 VkPresentModeKHR *presentModes =
865 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600866 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700867 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
868 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600869 assert(!err);
870
Ian Elliotta0781972015-08-21 15:09:33 -0600871 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600872 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
873 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
874 // If the surface size is undefined, the size is set to the size
875 // of the images requested, which must fit within the minimum and
876 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600877 swapchainExtent.width = demo->width;
878 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600879
880 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
881 swapchainExtent.width = surfCapabilities.minImageExtent.width;
882 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
883 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
884 }
885
886 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
887 swapchainExtent.height = surfCapabilities.minImageExtent.height;
888 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
889 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
890 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700891 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600892 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700893 swapchainExtent = surfCapabilities.currentExtent;
894 demo->width = surfCapabilities.currentExtent.width;
895 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600896 }
897
Tony Barbour66a56c32016-09-21 13:10:56 -0600898 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600899 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600900 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -0600901
Ian Elliottb18fdde2016-10-03 12:11:12 -0600902 // There are times when you may wish to use another present mode. The
903 // following code shows how to select them, and the comments provide some
904 // reasons you may wish to use them.
905 //
906 // It should be noted that Vulkan 1.0 doesn't provide a method for
907 // synchronizing rendering with the presentation engine's display. There
908 // is a method provided for throttling rendering with the display, but
909 // there are some presentation engines for which this method will not work.
910 // If an application doesn't throttle its rendering, and if it renders much
911 // faster than the refresh rate of the display, this can waste power on
912 // mobile devices. That is because power is being spent rendering images
913 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -0600914
Ian Elliottb18fdde2016-10-03 12:11:12 -0600915 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
916 // tearing, or have some way of synchronizing their rendering with the
917 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600918 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
919 // generally render a new presentable image every refresh cycle, but are
920 // occasionally early. In this case, the application wants the new image
921 // to be displayed instead of the previously-queued-for-presentation image
922 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600923 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
924 // render a new presentable image every refresh cycle, but are occasionally
925 // late. In this case (perhaps because of stuttering/latency concerns),
926 // the application wants the late image to be immediately displayed, even
927 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -0600928
929 if (demo->presentMode != swapchainPresentMode) {
930
931 for (size_t i = 0; i < presentModeCount; ++i) {
932 if (presentModes[i] == demo->presentMode) {
933 swapchainPresentMode = demo->presentMode;
934 break;
935 }
Ian Elliottb18fdde2016-10-03 12:11:12 -0600936 }
937 }
Tony Barbour291d57b2016-10-21 14:47:07 -0600938 if (swapchainPresentMode != demo->presentMode) {
939 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
940 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600941
Ian Elliottcf7f7482016-08-19 03:46:58 -0600942 // Determine the number of VkImage's to use in the swap chain.
943 // Application desires to only acquire 1 image at a time (which is
944 // "surfCapabilities.minImageCount").
945 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
946 // If maxImageCount is 0, we can ask for as many images as we want;
947 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700948 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600949 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600950 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600951 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600952 }
953
Tony Barbour9fd6d352015-09-16 15:01:32 -0600954 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700955 if (surfCapabilities.supportedTransforms &
956 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700957 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600958 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700959 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600960 }
961
Tony Barboura2a67812016-07-18 13:21:06 -0600962 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600963 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800964 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700965 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600966 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800967 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600968 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700969 .imageExtent =
970 {
971 .width = swapchainExtent.width, .height = swapchainExtent.height,
972 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700973 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600974 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700975 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700976 .imageArrayLayers = 1,
977 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
978 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600979 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600980 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600981 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600982 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600984 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600985 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700986 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800987 assert(!err);
988
Ian Elliottcf074d32015-10-16 18:02:43 -0600989 // If we just re-created an existing swapchain, we should destroy the old
990 // swapchain at this point.
991 // Note: destroying the swapchain also cleans up all its associated
992 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800993 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700994 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600995 }
996
997 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600998 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600999 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001000
Karl Schultze4dc75c2016-02-02 15:37:51 -07001001 VkImage *swapchainImages =
1002 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001003 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001004 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001005 &demo->swapchainImageCount,
1006 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001007 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001008
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
1010 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -06001011 assert(demo->buffers);
1012
Ian Elliotta0781972015-08-21 15:09:33 -06001013 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001014 VkImageViewCreateInfo color_image_view = {
1015 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016 .pNext = NULL,
1017 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001018 .components =
1019 {
1020 .r = VK_COMPONENT_SWIZZLE_R,
1021 .g = VK_COMPONENT_SWIZZLE_G,
1022 .b = VK_COMPONENT_SWIZZLE_B,
1023 .a = VK_COMPONENT_SWIZZLE_A,
1024 },
1025 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1026 .baseMipLevel = 0,
1027 .levelCount = 1,
1028 .baseArrayLayer = 0,
1029 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001030 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1031 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001032 };
1033
Ian Elliotta0781972015-08-21 15:09:33 -06001034 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001036 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001037
Karl Schultze4dc75c2016-02-02 15:37:51 -07001038 err = vkCreateImageView(demo->device, &color_image_view, NULL,
1039 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001040 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001041
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001042 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001043
Mark Young04fd3d82016-01-25 14:43:50 -07001044 if (NULL != presentModes) {
1045 free(presentModes);
1046 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001047}
1048
Karl Schultze4dc75c2016-02-02 15:37:51 -07001049static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001050 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001051 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001052 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001053 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001054 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001056 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001058 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001059 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001060 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001061 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001062 .flags = 0,
1063 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001064
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001065 VkImageViewCreateInfo view = {
1066 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001067 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001068 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001069 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001070 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1071 .baseMipLevel = 0,
1072 .levelCount = 1,
1073 .baseArrayLayer = 0,
1074 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001076 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001077 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001078
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001079 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001080 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001081 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001082
1083 demo->depth.format = depth_format;
1084
1085 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001086 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001087 assert(!err);
1088
Karl Schultze4dc75c2016-02-02 15:37:51 -07001089 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001090 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001091
Chia-I Wuf48700b2015-11-10 16:21:09 +08001092 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001093 demo->depth.mem_alloc.pNext = NULL;
1094 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1095 demo->depth.mem_alloc.memoryTypeIndex = 0;
1096
Karl Schultze4dc75c2016-02-02 15:37:51 -07001097 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1098 0, /* No requirements */
1099 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001100 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001101
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001103 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1104 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001105 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001106
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001107 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001108 err =
1109 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001110 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111
1112 /* create image view */
1113 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001114 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001115 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001116}
1117
Tony Barbour1a86d032015-09-21 15:17:33 -06001118/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001120 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001121#ifdef __ANDROID__
1122#include <lunarg.ppm.h>
1123 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001124 cPtr = (char*)lunarg_ppm;
1125 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001126 return false;
1127 }
1128 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001129 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001130 if (rgba_data == NULL) {
1131 return true;
1132 }
1133 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001134 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001135 return false;
1136 }
1137 while(strncmp(cPtr++, "\n", 1));
1138
1139 for (int y = 0; y < *height; y++) {
1140 uint8_t *rowPtr = rgba_data;
1141 for (int x = 0; x < *width; x++) {
1142 memcpy(rowPtr, cPtr, 3);
1143 rowPtr[3] = 255; /* Alpha of 1 */
1144 rowPtr += 4;
1145 cPtr += 3;
1146 }
1147 rgba_data += layout->rowPitch;
1148 }
1149
1150 return true;
1151#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001152 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001153 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001154
Tony Barbour1a86d032015-09-21 15:17:33 -06001155 if (!fPtr)
1156 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001157
Tony Barbour1a86d032015-09-21 15:17:33 -06001158 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001159 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1160 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001161 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001162 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001163
Tony Barbour1a86d032015-09-21 15:17:33 -06001164 do {
1165 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001166 if (cPtr == NULL) {
1167 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001168 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001169 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001170 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001171
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001172 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001173 if (rgba_data == NULL) {
1174 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001175 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001176 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001177 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001178 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001179 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1180 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001181 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001182 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001183
Karl Schultze4dc75c2016-02-02 15:37:51 -07001184 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001185 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001186 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001187 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001188 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001189 rowPtr[3] = 255; /* Alpha of 1 */
1190 rowPtr += 4;
1191 }
1192 rgba_data += layout->rowPitch;
1193 }
1194 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001195 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001196#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001197}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001198
Karl Schultze4dc75c2016-02-02 15:37:51 -07001199static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001200 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001201 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001202 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001204 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001205 int32_t tex_width;
1206 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001207 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001208 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001209
Karl Schultze4dc75c2016-02-02 15:37:51 -07001210 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001211 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001212 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001213
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001214 tex_obj->tex_width = tex_width;
1215 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001217 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001218 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001220 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001221 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001222 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001223 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001224 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001225 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001227 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001228 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001229 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001230 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001232 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001233
Karl Schultze4dc75c2016-02-02 15:37:51 -07001234 err =
1235 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001236 assert(!err);
1237
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001238 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001239
Chia-I Wuf48700b2015-11-10 16:21:09 +08001240 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001241 tex_obj->mem_alloc.pNext = NULL;
1242 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1243 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001244
Karl Schultze4dc75c2016-02-02 15:37:51 -07001245 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1246 required_props,
1247 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001248 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001249
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001250 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001251 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001252 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001253 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001254
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001255 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001256 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001257 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001258
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001259 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001260 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001261 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001262 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001263 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001264 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001265 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266 void *data;
1267
Karl Schultze4dc75c2016-02-02 15:37:51 -07001268 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1269 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001270
Karl Schultze4dc75c2016-02-02 15:37:51 -07001271 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1272 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273 assert(!err);
1274
1275 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1276 fprintf(stderr, "Error loading texture: %s\n", filename);
1277 }
1278
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001279 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001280 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001281
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001282 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001283}
1284
Karl Schultze4dc75c2016-02-02 15:37:51 -07001285static void demo_destroy_texture_image(struct demo *demo,
1286 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001287 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001288 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1289 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001290}
1291
Karl Schultze4dc75c2016-02-02 15:37:51 -07001292static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001293 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001294 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001295 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001296
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001297 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001298
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001299 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001300 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001301
Karl Schultze4dc75c2016-02-02 15:37:51 -07001302 if ((props.linearTilingFeatures &
1303 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1304 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001305 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001306 demo_prepare_texture_image(
1307 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1308 VK_IMAGE_USAGE_SAMPLED_BIT,
1309 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1310 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001311 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1312 // shader to run until layout transition completes
1313 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1314 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1315 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1316 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001317 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001318 } else if (props.optimalTilingFeatures &
1319 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001320 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001321
Tony Barbour16156cb2016-10-19 14:15:49 -06001322 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001323 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001324 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001325 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1326 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1327 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001328
Karl Schultze4dc75c2016-02-02 15:37:51 -07001329 demo_prepare_texture_image(
1330 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1331 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1332 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001333
Tony Barbour16156cb2016-10-19 14:15:49 -06001334 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001335 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001336 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001337 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001338 VK_ACCESS_HOST_WRITE_BIT,
1339 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1340 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001341
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001342 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001343 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001344 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001345 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001346 VK_ACCESS_HOST_WRITE_BIT,
1347 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1348 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001349
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001350 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001351 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1352 .srcOffset = {0, 0, 0},
1353 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1354 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001355 .extent = {demo->staging_texture.tex_width,
1356 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001357 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001358 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001359 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001360 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1361 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001362
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001363 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001364 VK_IMAGE_ASPECT_COLOR_BIT,
1365 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001366 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001367 VK_ACCESS_TRANSFER_WRITE_BIT,
1368 VK_PIPELINE_STAGE_TRANSFER_BIT,
1369 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001370
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001371 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001372 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1373 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001374 }
1375
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001376 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001377 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001379 .magFilter = VK_FILTER_NEAREST,
1380 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001381 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001382 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1383 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1384 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001385 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001386 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001387 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001388 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389 .minLod = 0.0f,
1390 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001391 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001392 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001394
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001395 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001396 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001398 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001399 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001400 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001401 .components =
1402 {
1403 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1404 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1405 },
1406 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001407 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001409
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001411 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001412 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413 assert(!err);
1414
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 /* create image view */
1416 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001417 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001418 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 assert(!err);
1420 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422
Karl Schultze4dc75c2016-02-02 15:37:51 -07001423void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001424 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001425 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001426 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001427 int i;
1428 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001429 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001430 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001431 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001432
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001433 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001434 mat4x4_mul(MVP, VP, demo->model_matrix);
1435 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001436 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001437
Karl Schultze4dc75c2016-02-02 15:37:51 -07001438 for (i = 0; i < 12 * 3; i++) {
1439 data.position[i][0] = g_vertex_buffer_data[i * 3];
1440 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1441 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001442 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001443 data.attr[i][0] = g_uv_buffer_data[2 * i];
1444 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001445 data.attr[i][2] = 0;
1446 data.attr[i][3] = 0;
1447 }
1448
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001449 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001450 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001451 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001452 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001453 err =
1454 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001455 assert(!err);
1456
Karl Schultze4dc75c2016-02-02 15:37:51 -07001457 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1458 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001459
Chia-I Wuf48700b2015-11-10 16:21:09 +08001460 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001461 demo->uniform_data.mem_alloc.pNext = NULL;
1462 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1463 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1464
Karl Schultze4dc75c2016-02-02 15:37:51 -07001465 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001466 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1467 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001469 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001470
Karl Schultze4dc75c2016-02-02 15:37:51 -07001471 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1472 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001473 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001474
Karl Schultze4dc75c2016-02-02 15:37:51 -07001475 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1476 demo->uniform_data.mem_alloc.allocationSize, 0,
1477 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001478 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001479
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001480 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001481
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001482 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001483
Karl Schultze4dc75c2016-02-02 15:37:51 -07001484 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1485 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001486 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001487
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001488 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1489 demo->uniform_data.buffer_info.offset = 0;
1490 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001491}
1492
Karl Schultze4dc75c2016-02-02 15:37:51 -07001493static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001494 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001495 [0] =
1496 {
1497 .binding = 0,
1498 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1499 .descriptorCount = 1,
1500 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1501 .pImmutableSamplers = NULL,
1502 },
1503 [1] =
1504 {
1505 .binding = 1,
1506 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1507 .descriptorCount = DEMO_TEXTURE_COUNT,
1508 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1509 .pImmutableSamplers = NULL,
1510 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001511 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001512 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001513 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001514 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001515 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001516 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001517 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001518 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1521 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001522 assert(!err);
1523
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001524 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001525 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1526 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001527 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001528 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001529 };
1530
Karl Schultze4dc75c2016-02-02 15:37:51 -07001531 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001532 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001533 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001534}
1535
Karl Schultze4dc75c2016-02-02 15:37:51 -07001536static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001537 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1538 // because at the start of the renderpass, we don't care about their contents.
1539 // At the start of the subpass, the color attachment's layout will be transitioned
1540 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1541 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1542 // the renderpass, the color attachment's layout will be transitioned to
1543 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1544 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001545 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001546 [0] =
1547 {
1548 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001549 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001550 .samples = VK_SAMPLE_COUNT_1_BIT,
1551 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1552 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1553 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1554 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001555 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001556 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001557 },
1558 [1] =
1559 {
1560 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001561 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001562 .samples = VK_SAMPLE_COUNT_1_BIT,
1563 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1564 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1565 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1566 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1567 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001568 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 .finalLayout =
1570 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1571 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001572 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001573 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001574 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001575 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001576 const VkAttachmentReference depth_reference = {
1577 .attachment = 1,
1578 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1579 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001580 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001581 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1582 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001583 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001584 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001585 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001586 .pColorAttachments = &color_reference,
1587 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001588 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001589 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001590 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001591 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001592 const VkRenderPassCreateInfo rp_info = {
1593 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1594 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001595 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001596 .attachmentCount = 2,
1597 .pAttachments = attachments,
1598 .subpassCount = 1,
1599 .pSubpasses = &subpass,
1600 .dependencyCount = 0,
1601 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001602 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001603 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001604
Chia-I Wu63636062015-10-26 21:10:41 +08001605 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001606 assert(!err);
1607}
1608
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001609//TODO: Merge shader reading
1610#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001611static VkShaderModule
1612demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001613 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001614 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001615 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001616
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001617 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1618 moduleCreateInfo.pNext = NULL;
1619
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001620 moduleCreateInfo.codeSize = size;
1621 moduleCreateInfo.pCode = code;
1622 moduleCreateInfo.flags = 0;
1623 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1624 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001625
1626 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627}
1628
Karl Schultze4dc75c2016-02-02 15:37:51 -07001629char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001630 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001631 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001632 void *shader_code;
1633
1634 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001635 if (!fp)
1636 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001637
1638 fseek(fp, 0L, SEEK_END);
1639 size = ftell(fp);
1640
1641 fseek(fp, 0L, SEEK_SET);
1642
1643 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001644 retval = fread(shader_code, size, 1, fp);
1645 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001646
1647 *psize = size;
1648
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001649 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001650 return shader_code;
1651}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001652#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001653
Karl Schultze4dc75c2016-02-02 15:37:51 -07001654static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001655#ifdef __ANDROID__
1656 VkShaderModuleCreateInfo sh_info = {};
1657 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1658
1659#include "cube.vert.h"
1660 sh_info.codeSize = sizeof(cube_vert);
1661 sh_info.pCode = cube_vert;
1662 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1663 assert(!err);
1664#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001665 void *vertShaderCode;
1666 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001667
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001668 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1669
Karl Schultze4dc75c2016-02-02 15:37:51 -07001670 demo->vert_shader_module =
1671 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001672
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001673 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001674#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001675
1676 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001677}
1678
Karl Schultze4dc75c2016-02-02 15:37:51 -07001679static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001680#ifdef __ANDROID__
1681 VkShaderModuleCreateInfo sh_info = {};
1682 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1683
1684#include "cube.frag.h"
1685 sh_info.codeSize = sizeof(cube_frag);
1686 sh_info.pCode = cube_frag;
1687 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1688 assert(!err);
1689#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001690 void *fragShaderCode;
1691 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001692
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001693 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001694
Karl Schultze4dc75c2016-02-02 15:37:51 -07001695 demo->frag_shader_module =
1696 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001697
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001698 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001699#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001700
1701 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001702}
1703
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001705 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001706 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001707 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001708 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001709 VkPipelineRasterizationStateCreateInfo rs;
1710 VkPipelineColorBlendStateCreateInfo cb;
1711 VkPipelineDepthStencilStateCreateInfo ds;
1712 VkPipelineViewportStateCreateInfo vp;
1713 VkPipelineMultisampleStateCreateInfo ms;
1714 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1715 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001716 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717
Piers Daniellba839d12015-09-29 13:01:09 -06001718 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1719 memset(&dynamicState, 0, sizeof dynamicState);
1720 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1721 dynamicState.pDynamicStates = dynamicStateEnables;
1722
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001724 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001725 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001727 memset(&vi, 0, sizeof(vi));
1728 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1729
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001731 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001732 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733
1734 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001735 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1736 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001737 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001738 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001739 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001740 rs.rasterizerDiscardEnable = VK_FALSE;
1741 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001742 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743
1744 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001745 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1746 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001747 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001748 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001749 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001750 cb.attachmentCount = 1;
1751 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752
Tony Barbour29645d02015-01-16 14:27:35 -07001753 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001754 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001755 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001756 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1757 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001758 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001759 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1760 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001761
1762 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001763 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001764 ds.depthTestEnable = VK_TRUE;
1765 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001766 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001767 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001768 ds.back.failOp = VK_STENCIL_OP_KEEP;
1769 ds.back.passOp = VK_STENCIL_OP_KEEP;
1770 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001771 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001772 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001773
Tony Barbour29645d02015-01-16 14:27:35 -07001774 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001775 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001776 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001777 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001778
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001779 // Two stages: vs and fs
1780 pipeline.stageCount = 2;
1781 VkPipelineShaderStageCreateInfo shaderStages[2];
1782 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1783
Karl Schultze4dc75c2016-02-02 15:37:51 -07001784 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1785 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001786 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001787 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001788
Karl Schultze4dc75c2016-02-02 15:37:51 -07001789 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1790 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001791 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001792 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001793
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001794 memset(&pipelineCache, 0, sizeof(pipelineCache));
1795 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796
Karl Schultze4dc75c2016-02-02 15:37:51 -07001797 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1798 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001799 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001800
Karl Schultze4dc75c2016-02-02 15:37:51 -07001801 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001802 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001803 pipeline.pRasterizationState = &rs;
1804 pipeline.pColorBlendState = &cb;
1805 pipeline.pMultisampleState = &ms;
1806 pipeline.pViewportState = &vp;
1807 pipeline.pDepthStencilState = &ds;
1808 pipeline.pStages = shaderStages;
1809 pipeline.renderPass = demo->render_pass;
1810 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001811
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001812 pipeline.renderPass = demo->render_pass;
1813
Karl Schultze4dc75c2016-02-02 15:37:51 -07001814 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1815 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001816 assert(!err);
1817
Chia-I Wu63636062015-10-26 21:10:41 +08001818 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1819 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820}
1821
Karl Schultze4dc75c2016-02-02 15:37:51 -07001822static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001823 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001824 [0] =
1825 {
1826 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1827 .descriptorCount = 1,
1828 },
1829 [1] =
1830 {
1831 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1832 .descriptorCount = DEMO_TEXTURE_COUNT,
1833 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001834 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001835 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001836 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001837 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001838 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001839 .poolSizeCount = 2,
1840 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001841 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001842 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001843
Karl Schultze4dc75c2016-02-02 15:37:51 -07001844 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1845 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001846 assert(!err);
1847}
1848
Karl Schultze4dc75c2016-02-02 15:37:51 -07001849static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001850 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001851 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001852 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001853 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001854
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001855 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001856 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001857 .pNext = NULL,
1858 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001859 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001860 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001861 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001862 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001863
Chia-I Wuae721ba2015-05-25 16:27:55 +08001864 memset(&tex_descs, 0, sizeof(tex_descs));
1865 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001866 tex_descs[i].sampler = demo->textures[i].sampler;
1867 tex_descs[i].imageView = demo->textures[i].view;
1868 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001869 }
1870
1871 memset(&writes, 0, sizeof(writes));
1872
1873 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001874 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001875 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001876 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001877 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001878
1879 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001880 writes[1].dstSet = demo->desc_set;
1881 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001882 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001883 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001884 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001885
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001886 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001887}
1888
Karl Schultze4dc75c2016-02-02 15:37:51 -07001889static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001890 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001891 attachments[1] = demo->depth.view;
1892
Chia-I Wuf973e322015-07-08 13:34:24 +08001893 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001894 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1895 .pNext = NULL,
1896 .renderPass = demo->render_pass,
1897 .attachmentCount = 2,
1898 .pAttachments = attachments,
1899 .width = demo->width,
1900 .height = demo->height,
1901 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001902 };
1903 VkResult U_ASSERT_ONLY err;
1904 uint32_t i;
1905
Karl Schultze4dc75c2016-02-02 15:37:51 -07001906 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1907 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001908 assert(demo->framebuffers);
1909
1910 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001911 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001912 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1913 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001914 assert(!err);
1915 }
1916}
1917
Karl Schultze4dc75c2016-02-02 15:37:51 -07001918static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001919 VkResult U_ASSERT_ONLY err;
1920
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001921 const VkCommandPoolCreateInfo cmd_pool_info = {
1922 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001923 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001924 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001925 .flags = 0,
1926 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001927 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1928 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001929 assert(!err);
1930
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001931 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001932 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001933 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001934 .commandPool = demo->cmd_pool,
1935 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001936 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001937 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001938 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1939 assert(!err);
1940 VkCommandBufferBeginInfo cmd_buf_info = {
1941 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1942 .pNext = NULL,
1943 .flags = 0,
1944 .pInheritanceInfo = NULL,
1945 };
1946 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1947 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948
1949 demo_prepare_buffers(demo);
1950 demo_prepare_depth(demo);
1951 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001952 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001954 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001955 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957
Ian Elliotta0781972015-08-21 15:09:33 -06001958 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001959 err =
1960 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001961 assert(!err);
1962 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001963
Tony Barbour22e06272016-09-07 16:07:56 -06001964 if (demo->separate_present_queue) {
1965 const VkCommandPoolCreateInfo cmd_pool_info = {
1966 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1967 .pNext = NULL,
1968 .queueFamilyIndex = demo->present_queue_family_index,
1969 .flags = 0,
1970 };
1971 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1972 &demo->present_cmd_pool);
1973 assert(!err);
1974 const VkCommandBufferAllocateInfo cmd = {
1975 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1976 .pNext = NULL,
1977 .commandPool = demo->present_cmd_pool,
1978 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1979 .commandBufferCount = 1,
1980 };
1981 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1982 err = vkAllocateCommandBuffers(
1983 demo->device, &cmd, &demo->buffers[i].graphics_to_present_cmd);
1984 assert(!err);
1985 demo_build_image_ownership_cmd(demo, i);
1986 }
1987 }
1988
Chia-I Wu63ea9262015-03-26 13:14:16 +08001989 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001990 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001991
Chia-I Wuf973e322015-07-08 13:34:24 +08001992 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001993
Ian Elliotta0781972015-08-21 15:09:33 -06001994 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001995 demo->current_buffer = i;
1996 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1997 }
1998
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001999 /*
2000 * Prepare functions above may generate pipeline commands
2001 * that need to be flushed before beginning the render loop.
2002 */
2003 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002004 if (demo->staging_texture.image) {
2005 demo_destroy_texture_image(demo, &demo->staging_texture);
2006 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002007
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002008 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002009 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010}
2011
Karl Schultze4dc75c2016-02-02 15:37:51 -07002012static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002013 uint32_t i;
2014
2015 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002016 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002017
Tony Barbour66a56c32016-09-21 13:10:56 -06002018 // Wait for fences from present operations
2019 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002020 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002021 vkDestroyFence(demo->device, demo->fences[i], NULL);
2022 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2023 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2024 if (demo->separate_present_queue) {
2025 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2026 }
2027 }
2028
Tony Barbourd8e504f2015-10-08 14:26:24 -06002029 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002030 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002031 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06002032 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002033 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002034
Chia-I Wu63636062015-10-26 21:10:41 +08002035 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2036 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2037 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2038 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2039 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002040
2041 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002042 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2043 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2044 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2045 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002046 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002047 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002048
Chia-I Wu63636062015-10-26 21:10:41 +08002049 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2050 vkDestroyImage(demo->device, demo->depth.image, NULL);
2051 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002052
Chia-I Wu63636062015-10-26 21:10:41 +08002053 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2054 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002055
Ian Elliotta0781972015-08-21 15:09:33 -06002056 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002057 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002058 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2059 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06002060 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002061 free(demo->buffers);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002062 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002063 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002064
Tony Barbour22e06272016-09-07 16:07:56 -06002065 if (demo->separate_present_queue) {
2066 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002067 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002068 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002069 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002070 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002071 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002072 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002073 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002074 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002075
Tony Barbour153cb062016-12-07 13:43:36 -07002076#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002077 XDestroyWindow(demo->display, demo->xlib_window);
2078 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002079#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002080 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002081 xcb_disconnect(demo->connection);
2082 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002083#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2084 wl_shell_surface_destroy(demo->shell_surface);
2085 wl_surface_destroy(demo->window);
2086 wl_shell_destroy(demo->shell);
2087 wl_compositor_destroy(demo->compositor);
2088 wl_registry_destroy(demo->registry);
2089 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002090#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002091}
2092
Karl Schultze4dc75c2016-02-02 15:37:51 -07002093static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002094 uint32_t i;
2095
Mike Stroyanbb60b382015-10-28 09:46:57 -06002096 // Don't react to resize until after first initialization.
2097 if (!demo->prepared) {
2098 return;
2099 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002100 // In order to properly resize the window, we must re-create the swapchain
2101 // AND redo the command buffers, etc.
2102 //
2103 // First, perform part of the demo_cleanup() function:
2104 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002105 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002106
2107 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002108 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002109 }
2110 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002111 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002112
Chia-I Wu63636062015-10-26 21:10:41 +08002113 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2114 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2115 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2116 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2117 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002118
2119 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002120 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2121 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2122 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2123 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002124 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002125
Chia-I Wu63636062015-10-26 21:10:41 +08002126 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2127 vkDestroyImage(demo->device, demo->depth.image, NULL);
2128 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002129
Chia-I Wu63636062015-10-26 21:10:41 +08002130 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2131 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002132
2133 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002134 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002135 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2136 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002137 }
Chia-I Wu63636062015-10-26 21:10:41 +08002138 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002139 if (demo->separate_present_queue) {
2140 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2141 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002142 free(demo->buffers);
2143
Ian Elliottcf074d32015-10-16 18:02:43 -06002144 // Second, re-perform the demo_prepare() function, which will re-create the
2145 // swapchain:
2146 demo_prepare(demo);
2147}
2148
David Pinedo2bb7c932015-06-18 17:03:14 -06002149// On MS-Windows, make this a global, so it's available to WndProc()
2150struct demo demo;
2151
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002152#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002153static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002154 if (!demo->prepared)
2155 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002156
Ian Elliott639ca472015-04-16 15:23:05 -06002157 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002158 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002159 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002160 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002161 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002162 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002163}
Ian Elliott639ca472015-04-16 15:23:05 -06002164
2165// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002166LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2167 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002168 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002169 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002170 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002171 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002172 // The validation callback calls MessageBox which can generate paint
2173 // events - don't make more Vulkan calls if we got here from the
2174 // callback
2175 if (!in_callback) {
2176 demo_run(&demo);
2177 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002178 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002179 case WM_GETMINMAXINFO: // set window's minimum size
2180 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2181 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002182 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002183 // Resize the application to the new window size, except when
2184 // it was minimized. Vulkan doesn't support images or swapchains
2185 // with width=0 and height=0.
2186 if (wParam != SIZE_MINIMIZED) {
2187 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002188 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002189 demo_resize(&demo);
2190 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002191 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002192 default:
2193 break;
2194 }
2195 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2196}
2197
Karl Schultze4dc75c2016-02-02 15:37:51 -07002198static void demo_create_window(struct demo *demo) {
2199 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002200
2201 // Initialize the window class structure:
2202 win_class.cbSize = sizeof(WNDCLASSEX);
2203 win_class.style = CS_HREDRAW | CS_VREDRAW;
2204 win_class.lpfnWndProc = WndProc;
2205 win_class.cbClsExtra = 0;
2206 win_class.cbWndExtra = 0;
2207 win_class.hInstance = demo->connection; // hInstance
2208 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2209 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2210 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2211 win_class.lpszMenuName = NULL;
2212 win_class.lpszClassName = demo->name;
2213 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2214 // Register window class:
2215 if (!RegisterClassEx(&win_class)) {
2216 // It didn't work, so try to give a useful error:
2217 printf("Unexpected error trying to start the application!\n");
2218 fflush(stdout);
2219 exit(1);
2220 }
2221 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002222 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002223 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002224 demo->window = CreateWindowEx(0,
2225 demo->name, // class name
2226 demo->name, // app name
2227 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002228 WS_VISIBLE | WS_SYSMENU,
2229 100, 100, // x/y coords
2230 wr.right - wr.left, // width
2231 wr.bottom - wr.top, // height
2232 NULL, // handle to parent
2233 NULL, // handle to menu
2234 demo->connection, // hInstance
2235 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002236 if (!demo->window) {
2237 // It didn't work, so try to give a useful error:
2238 printf("Cannot create a window in which to draw!\n");
2239 fflush(stdout);
2240 exit(1);
2241 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002242 // Window client area size must be at least 1 pixel high, to prevent crash.
2243 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2244 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002245}
Tony Barbour8295ac42016-08-08 11:04:17 -06002246#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002247static void demo_create_xlib_window(struct demo *demo) {
2248
2249 demo->display = XOpenDisplay(NULL);
2250 long visualMask = VisualScreenMask;
2251 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002252 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002253 vInfoTemplate.screen = DefaultScreen(demo->display);
2254 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2255 &vInfoTemplate, &numberOfVisuals);
2256
2257 Colormap colormap = XCreateColormap(
2258 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2259 visualInfo->visual, AllocNone);
2260
Rene Lindsay11612722016-06-13 17:20:39 -06002261 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002262 windowAttributes.colormap = colormap;
2263 windowAttributes.background_pixel = 0xFFFFFFFF;
2264 windowAttributes.border_pixel = 0;
2265 windowAttributes.event_mask =
2266 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2267
2268 demo->xlib_window = XCreateWindow(
2269 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2270 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2271 visualInfo->visual,
2272 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2273
2274 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2275 XMapWindow(demo->display, demo->xlib_window);
2276 XFlush(demo->display);
2277 demo->xlib_wm_delete_window =
2278 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2279}
2280static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2281 switch(event->type) {
2282 case ClientMessage:
2283 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2284 demo->quit = true;
2285 break;
2286 case KeyPress:
2287 switch (event->xkey.keycode) {
2288 case 0x9: // Escape
2289 demo->quit = true;
2290 break;
2291 case 0x71: // left arrow key
2292 demo->spin_angle += demo->spin_increment;
2293 break;
2294 case 0x72: // right arrow key
2295 demo->spin_angle -= demo->spin_increment;
2296 break;
2297 case 0x41:
2298 demo->pause = !demo->pause;
2299 break;
2300 }
2301 break;
2302 case ConfigureNotify:
2303 if ((demo->width != event->xconfigure.width) ||
2304 (demo->height != event->xconfigure.height)) {
2305 demo->width = event->xconfigure.width;
2306 demo->height = event->xconfigure.height;
2307 demo_resize(demo);
2308 }
2309 break;
2310 default:
2311 break;
2312 }
2313
2314}
2315
2316static void demo_run_xlib(struct demo *demo) {
2317
2318 while (!demo->quit) {
2319 XEvent event;
2320
2321 if (demo->pause) {
2322 XNextEvent(demo->display, &event);
2323 demo_handle_xlib_event(demo, &event);
2324 } else {
2325 while (XPending(demo->display) > 0) {
2326 XNextEvent(demo->display, &event);
2327 demo_handle_xlib_event(demo, &event);
2328 }
2329 }
2330
Tony Barbour2593b182016-04-19 10:57:58 -06002331 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002332 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002333 demo->curFrame++;
2334 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2335 demo->quit = true;
2336 }
2337}
Tony Barbour153cb062016-12-07 13:43:36 -07002338#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002339static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002340 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002341 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002342 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002343 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002344 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002345 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002346 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002347 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2348 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002349 demo->quit = true;
2350 }
2351 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002352 case XCB_KEY_RELEASE: {
2353 const xcb_key_release_event_t *key =
2354 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002355
Karl Schultze4dc75c2016-02-02 15:37:51 -07002356 switch (key->detail) {
2357 case 0x9: // Escape
2358 demo->quit = true;
2359 break;
2360 case 0x71: // left arrow key
2361 demo->spin_angle += demo->spin_increment;
2362 break;
2363 case 0x72: // right arrow key
2364 demo->spin_angle -= demo->spin_increment;
2365 break;
2366 case 0x41:
2367 demo->pause = !demo->pause;
2368 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002369 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002370 } break;
2371 case XCB_CONFIGURE_NOTIFY: {
2372 const xcb_configure_notify_event_t *cfg =
2373 (const xcb_configure_notify_event_t *)event;
2374 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002375 demo->width = cfg->width;
2376 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002377 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002378 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002379 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002380 default:
2381 break;
2382 }
2383}
2384
Tony Barbour2593b182016-04-19 10:57:58 -06002385static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002386 xcb_flush(demo->connection);
2387
2388 while (!demo->quit) {
2389 xcb_generic_event_t *event;
2390
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002391 if (demo->pause) {
2392 event = xcb_wait_for_event(demo->connection);
2393 } else {
2394 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002395 while(event) {
2396 demo_handle_xcb_event(demo, event);
2397 free(event);
2398 event = xcb_poll_for_event(demo->connection);
2399 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002400 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002401
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002402 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002403 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002404 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002405 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002406 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002407 }
2408}
2409
Tony Barbour2593b182016-04-19 10:57:58 -06002410static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002411 uint32_t value_mask, value_list[32];
2412
Tony Barbour2593b182016-04-19 10:57:58 -06002413 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002414
2415 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2416 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002417 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002418 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002419
Tony Barbour2593b182016-04-19 10:57:58 -06002420 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002421 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2422 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2423 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002424
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002425 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002426 xcb_intern_atom_cookie_t cookie =
2427 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2428 xcb_intern_atom_reply_t *reply =
2429 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002430
Karl Schultze4dc75c2016-02-02 15:37:51 -07002431 xcb_intern_atom_cookie_t cookie2 =
2432 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2433 demo->atom_wm_delete_window =
2434 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002435
Tony Barbour2593b182016-04-19 10:57:58 -06002436 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002437 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002438 &(*demo->atom_wm_delete_window).atom);
2439 free(reply);
2440
Tony Barbour2593b182016-04-19 10:57:58 -06002441 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002442
Karl Schultze4dc75c2016-02-02 15:37:51 -07002443 // Force the x/y coordinates to 100,100 results are identical in consecutive
2444 // runs
2445 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002446 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002447 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002448}
Tony Barbour6b131662016-08-25 13:14:45 -06002449// VK_USE_PLATFORM_XCB_KHR
2450#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002451static void demo_run(struct demo *demo) {
2452 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002453 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002454 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002455 demo->curFrame++;
2456 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2457 demo->quit = true;
2458 }
2459}
2460
2461static void handle_ping(void *data UNUSED,
2462 struct wl_shell_surface *shell_surface,
2463 uint32_t serial) {
2464 wl_shell_surface_pong(shell_surface, serial);
2465}
2466
2467static void handle_configure(void *data UNUSED,
2468 struct wl_shell_surface *shell_surface UNUSED,
2469 uint32_t edges UNUSED, int32_t width UNUSED,
2470 int32_t height UNUSED) {}
2471
2472static void handle_popup_done(void *data UNUSED,
2473 struct wl_shell_surface *shell_surface UNUSED) {}
2474
2475static const struct wl_shell_surface_listener shell_surface_listener = {
2476 handle_ping, handle_configure, handle_popup_done};
2477
2478static void demo_create_window(struct demo *demo) {
2479 demo->window = wl_compositor_create_surface(demo->compositor);
2480 if (!demo->window) {
2481 printf("Can not create wayland_surface from compositor!\n");
2482 fflush(stdout);
2483 exit(1);
2484 }
2485
2486 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2487 if (!demo->shell_surface) {
2488 printf("Can not get shell_surface from wayland_surface!\n");
2489 fflush(stdout);
2490 exit(1);
2491 }
2492 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2493 demo);
2494 wl_shell_surface_set_toplevel(demo->shell_surface);
2495 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2496}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002497#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2498static void demo_run(struct demo *demo) {
2499 if (!demo->prepared)
2500 return;
2501
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002502 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002503 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002504 demo->curFrame++;
2505}
2506#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002507
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002508/*
2509 * Return 1 (true) if all layer names specified in check_names
2510 * can be found in given layer properties.
2511 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002512static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002513 uint32_t layer_count,
2514 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002515 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002516 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002517 for (uint32_t j = 0; j < layer_count; j++) {
2518 if (!strcmp(check_names[i], layers[j].layerName)) {
2519 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002520 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002521 }
2522 }
2523 if (!found) {
2524 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2525 return 0;
2526 }
2527 }
2528 return 1;
2529}
2530
Karl Schultze4dc75c2016-02-02 15:37:51 -07002531static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002532 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002533 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002534 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002535 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002536 char **instance_validation_layers = NULL;
2537 demo->enabled_extension_count = 0;
2538 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002539
Karl Schultz7c50ad62016-04-01 12:07:03 -06002540 char *instance_validation_layers_alt1[] = {
2541 "VK_LAYER_LUNARG_standard_validation"
2542 };
2543
2544 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002545 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2546 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2547 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2548 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002549 };
2550
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002551 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002552 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002553 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002554
Karl Schultz7c50ad62016-04-01 12:07:03 -06002555 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002556 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002557
Karl Schultz7c50ad62016-04-01 12:07:03 -06002558 instance_validation_layers = instance_validation_layers_alt1;
2559 if (instance_layer_count > 0) {
2560 VkLayerProperties *instance_layers =
2561 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2562 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2563 instance_layers);
2564 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002565
Karl Schultz7c50ad62016-04-01 12:07:03 -06002566
2567 validation_found = demo_check_layers(
2568 ARRAY_SIZE(instance_validation_layers_alt1),
2569 instance_validation_layers, instance_layer_count,
2570 instance_layers);
2571 if (validation_found) {
2572 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002573 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2574 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002575 } else {
2576 // use alternative set of validation layers
2577 instance_validation_layers = instance_validation_layers_alt2;
2578 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2579 validation_found = demo_check_layers(
2580 ARRAY_SIZE(instance_validation_layers_alt2),
2581 instance_validation_layers, instance_layer_count,
2582 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002583 validation_layer_count =
2584 ARRAY_SIZE(instance_validation_layers_alt2);
2585 for (uint32_t i = 0; i < validation_layer_count; i++) {
2586 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002587 }
2588 }
2589 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002590 }
Dustin Graves7c287352016-01-27 13:48:06 -07002591
Karl Schultz7c50ad62016-04-01 12:07:03 -06002592 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002593 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002594 "required validation layer.\n\n"
2595 "Please look at the Getting Started guide for additional "
2596 "information.\n",
2597 "vkCreateInstance Failure");
2598 }
Dustin Graves7c287352016-01-27 13:48:06 -07002599 }
2600
2601 /* Look for instance extensions */
2602 VkBool32 surfaceExtFound = 0;
2603 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002604 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002605
Karl Schultze4dc75c2016-02-02 15:37:51 -07002606 err = vkEnumerateInstanceExtensionProperties(
2607 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002608 assert(!err);
2609
Dustin Graves7c287352016-01-27 13:48:06 -07002610 if (instance_extension_count > 0) {
2611 VkExtensionProperties *instance_extensions =
2612 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2613 err = vkEnumerateInstanceExtensionProperties(
2614 NULL, &instance_extension_count, instance_extensions);
2615 assert(!err);
2616 for (uint32_t i = 0; i < instance_extension_count; i++) {
2617 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2618 instance_extensions[i].extensionName)) {
2619 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002620 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002621 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002622 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002623#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002624 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2625 instance_extensions[i].extensionName)) {
2626 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002627 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002628 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2629 }
Tony Barbour153cb062016-12-07 13:43:36 -07002630#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002631 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2632 instance_extensions[i].extensionName)) {
2633 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06002634 demo->extension_names[demo->enabled_extension_count++] =
2635 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2636 }
Tony Barbour153cb062016-12-07 13:43:36 -07002637#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002638 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2639 instance_extensions[i].extensionName)) {
2640 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002641 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002642 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2643 }
Tony Barbour153cb062016-12-07 13:43:36 -07002644#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002645 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2646 instance_extensions[i].extensionName)) {
2647 platformSurfaceExtFound = 1;
2648 demo->extension_names[demo->enabled_extension_count++] =
2649 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2650 }
Tony Barbour153cb062016-12-07 13:43:36 -07002651#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002652 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2653 instance_extensions[i].extensionName)) {
2654 platformSurfaceExtFound = 1;
2655 demo->extension_names[demo->enabled_extension_count++] =
2656 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2657 }
2658#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002659 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2660 instance_extensions[i].extensionName)) {
2661 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002662 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002663 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2664 }
2665 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002666 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002667 }
Dustin Graves7c287352016-01-27 13:48:06 -07002668
2669 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002670 }
Dustin Graves7c287352016-01-27 13:48:06 -07002671
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002672 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002673 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2674 "the " VK_KHR_SURFACE_EXTENSION_NAME
2675 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002676 "Vulkan installable client driver (ICD) installed?\nPlease "
2677 "look at the Getting Started guide for additional "
2678 "information.\n",
2679 "vkCreateInstance Failure");
2680 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002681 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002682#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002683 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2684 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2685 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002686 "Vulkan installable client driver (ICD) installed?\nPlease "
2687 "look at the Getting Started guide for additional "
2688 "information.\n",
2689 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002690#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002691 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2692 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2693 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002694 "Vulkan installable client driver (ICD) installed?\nPlease "
2695 "look at the Getting Started guide for additional "
2696 "information.\n",
2697 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002698#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2699 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2700 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2701 " extension.\n\nDo you have a compatible "
2702 "Vulkan installable client driver (ICD) installed?\nPlease "
2703 "look at the Getting Started guide for additional "
2704 "information.\n",
2705 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002706#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2707 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2708 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2709 " extension.\n\nDo you have a compatible "
2710 "Vulkan installable client driver (ICD) installed?\nPlease "
2711 "look at the Getting Started guide for additional "
2712 "information.\n",
2713 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002714#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002715 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2716 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2717 " extension.\n\nDo you have a compatible "
2718 "Vulkan installable client driver (ICD) installed?\nPlease "
2719 "look at the Getting Started guide for additional "
2720 "information.\n",
2721 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002722#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002723 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002724 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002725 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002726 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002727 .pApplicationName = APP_SHORT_NAME,
2728 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002729 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002730 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002731 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002732 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002733 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002734 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002735 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002736 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002737 .enabledLayerCount = demo->enabled_layer_count,
2738 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2739 .enabledExtensionCount = demo->enabled_extension_count,
2740 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002741 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002742
2743 /*
2744 * This is info for a temp callback to use during CreateInstance.
2745 * After the instance is created, we use the instance-based
2746 * function to register the final callback.
2747 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002748 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002749 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002750 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2751 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002752 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002753 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002754 dbgCreateInfo.flags =
2755 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2756 inst_info.pNext = &dbgCreateInfo;
2757 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002758
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002759 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002760
Chia-I Wu63636062015-10-26 21:10:41 +08002761 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002762 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2763 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002764 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002765 "additional information.\n",
2766 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002767 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002768 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002769 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002770 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002771 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002772 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2773 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002774 "the Getting Started guide for additional information.\n",
2775 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002776 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002777
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002778 /* Make initial call to query gpu_count, then second call for gpu info*/
2779 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2780 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002781
2782 if (gpu_count > 0) {
2783 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2784 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2785 assert(!err);
2786 /* For cube demo we just grab the first physical device */
2787 demo->gpu = physical_devices[0];
2788 free(physical_devices);
2789 } else {
2790 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2791 "Do you have a compatible Vulkan installable client driver (ICD) "
2792 "installed?\nPlease look at the Getting Started guide for "
2793 "additional information.\n",
2794 "vkEnumeratePhysicalDevices Failure");
2795 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002796
Dustin Graves7c287352016-01-27 13:48:06 -07002797 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002798 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002799 VkBool32 swapchainExtFound = 0;
2800 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002801 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002802
Karl Schultze4dc75c2016-02-02 15:37:51 -07002803 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2804 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002805 assert(!err);
2806
Dustin Graves7c287352016-01-27 13:48:06 -07002807 if (device_extension_count > 0) {
2808 VkExtensionProperties *device_extensions =
2809 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2810 err = vkEnumerateDeviceExtensionProperties(
2811 demo->gpu, NULL, &device_extension_count, device_extensions);
2812 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002813
Dustin Graves7c287352016-01-27 13:48:06 -07002814 for (uint32_t i = 0; i < device_extension_count; i++) {
2815 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2816 device_extensions[i].extensionName)) {
2817 swapchainExtFound = 1;
2818 demo->extension_names[demo->enabled_extension_count++] =
2819 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2820 }
2821 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002822 }
Dustin Graves7c287352016-01-27 13:48:06 -07002823
2824 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002825 }
Dustin Graves7c287352016-01-27 13:48:06 -07002826
Tony Barbourcc69f452015-10-08 13:59:42 -06002827 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002828 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2829 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2830 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002831 "Vulkan installable client driver (ICD) installed?\nPlease "
2832 "look at the Getting Started guide for additional "
2833 "information.\n",
2834 "vkCreateInstance Failure");
2835 }
2836
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002837 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002838 demo->CreateDebugReportCallback =
2839 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2840 demo->inst, "vkCreateDebugReportCallbackEXT");
2841 demo->DestroyDebugReportCallback =
2842 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2843 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002844 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002845 ERR_EXIT(
2846 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2847 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002848 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002849 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002850 ERR_EXIT(
2851 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2852 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002853 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002854 demo->DebugReportMessage =
2855 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2856 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002857 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002858 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002859 "vkGetProcAddr Failure");
2860 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002861
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002862 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002863 PFN_vkDebugReportCallbackEXT callback;
2864 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002865 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002866 dbgCreateInfo.pNext = NULL;
2867 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002868 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002869 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002870 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002871 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2872 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002873 switch (err) {
2874 case VK_SUCCESS:
2875 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002876 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002877 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2878 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002879 break;
2880 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002881 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2882 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002883 break;
2884 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002885 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002886 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002887
2888 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002889 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2890 &demo->queue_family_count, NULL);
2891 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002892
Karl Schultze4dc75c2016-02-02 15:37:51 -07002893 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002894 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2895 vkGetPhysicalDeviceQueueFamilyProperties(
2896 demo->gpu, &demo->queue_family_count, demo->queue_props);
2897
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002898 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002899 // If app has specific feature requirements it should check supported
2900 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002901 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002902 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002903
Tony Barbourda2bad52016-01-22 14:36:40 -07002904 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2905 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2906 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2907 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2908 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2909}
2910
Karl Schultze4dc75c2016-02-02 15:37:51 -07002911static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002912 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002913 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06002914 VkDeviceQueueCreateInfo queues[2];
2915 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2916 queues[0].pNext = NULL;
2917 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
2918 queues[0].queueCount = 1;
2919 queues[0].pQueuePriorities = queue_priorities;
2920 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002921
2922 VkDeviceCreateInfo device = {
2923 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2924 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002925 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06002926 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002927 .enabledLayerCount = 0,
2928 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002929 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002930 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2931 .pEnabledFeatures =
2932 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002933 };
Tony Barbour22e06272016-09-07 16:07:56 -06002934 if (demo->separate_present_queue) {
2935 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2936 queues[1].pNext = NULL;
2937 queues[1].queueFamilyIndex = demo->present_queue_family_index;
2938 queues[1].queueCount = 1;
2939 queues[1].pQueuePriorities = queue_priorities;
2940 queues[1].flags = 0;
2941 device.queueCreateInfoCount = 2;
2942 }
Chia-I Wu63636062015-10-26 21:10:41 +08002943 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002944 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002945}
2946
Karl Schultze4dc75c2016-02-02 15:37:51 -07002947static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002948 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002949 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002950
Karl Schultze4dc75c2016-02-02 15:37:51 -07002951// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002952#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002953 VkWin32SurfaceCreateInfoKHR createInfo;
2954 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2955 createInfo.pNext = NULL;
2956 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002957 createInfo.hinstance = demo->connection;
2958 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002959
Karl Schultze4dc75c2016-02-02 15:37:51 -07002960 err =
2961 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07002962#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002963 VkWaylandSurfaceCreateInfoKHR createInfo;
2964 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2965 createInfo.pNext = NULL;
2966 createInfo.flags = 0;
2967 createInfo.display = demo->display;
2968 createInfo.surface = demo->window;
2969
2970 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2971 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002972#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2973 VkAndroidSurfaceCreateInfoKHR createInfo;
2974 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2975 createInfo.pNext = NULL;
2976 createInfo.flags = 0;
2977 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002978
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002979 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07002980#elif defined(VK_USE_PLATFORM_XLIB_KHR)
2981 VkXlibSurfaceCreateInfoKHR createInfo;
2982 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2983 createInfo.pNext = NULL;
2984 createInfo.flags = 0;
2985 createInfo.dpy = demo->display;
2986 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002987
Tony Barbour153cb062016-12-07 13:43:36 -07002988 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06002989 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07002990#elif defined(VK_USE_PLATFORM_XCB_KHR)
2991 VkXcbSurfaceCreateInfoKHR createInfo;
2992 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2993 createInfo.pNext = NULL;
2994 createInfo.flags = 0;
2995 createInfo.connection = demo->connection;
2996 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06002997
Tony Barbour153cb062016-12-07 13:43:36 -07002998 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002999#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003000 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003001
Tony Barbourcc69f452015-10-08 13:59:42 -06003002 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003003 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003004 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
3005 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003006 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003007 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003008 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003009
3010 // Search for a graphics and a present queue in the array of queue
3011 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003012 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3013 uint32_t presentQueueFamilyIndex = UINT32_MAX;
3014 for (i = 0; i < demo->queue_family_count; i++) {
3015 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3016 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3017 graphicsQueueFamilyIndex = i;
3018 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003019
Tony Barbourab2a0362016-09-06 11:40:12 -06003020 if (supportsPresent[i] == VK_TRUE) {
3021 graphicsQueueFamilyIndex = i;
3022 presentQueueFamilyIndex = i;
3023 break;
3024 }
3025 }
3026 }
3027
3028 if (presentQueueFamilyIndex == UINT32_MAX) {
3029 // If didn't find a queue that supports both graphics and present, then
3030 // find a separate present queue.
Mark Lobodzinski5abdfcc2016-10-03 16:00:40 -06003031 for (i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003032 if (supportsPresent[i] == VK_TRUE) {
3033 presentQueueFamilyIndex = i;
3034 break;
3035 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003036 }
3037 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003038
3039 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003040 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3041 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003042 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003043 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003044 }
3045
Tony Barbourab2a0362016-09-06 11:40:12 -06003046 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3047 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003048 demo->separate_present_queue =
3049 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003050 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003051
Tony Barbourda2bad52016-01-22 14:36:40 -07003052 demo_create_device(demo);
3053
3054 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3055 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3056 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3057 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3058 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3059
Tony Barboura2a67812016-07-18 13:21:06 -06003060 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3061 &demo->graphics_queue);
3062
Tony Barbour22e06272016-09-07 16:07:56 -06003063 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003064 demo->present_queue = demo->graphics_queue;
3065 } else {
3066 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3067 &demo->present_queue);
3068 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003069
Ian Elliottaaae5352015-07-06 14:27:58 -06003070 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003071 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003072 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003073 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003074 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003075 VkSurfaceFormatKHR *surfFormats =
3076 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003077 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003078 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003079 assert(!err);
3080 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3081 // the surface has no preferred format. Otherwise, at least one
3082 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003083 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003084 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003085 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003086 assert(formatCount >= 1);
3087 demo->format = surfFormats[0].format;
3088 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003089 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003090
David Pinedo2bb7c932015-06-18 17:03:14 -06003091 demo->quit = false;
3092 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003093
Tony Barbource7524e2016-07-28 12:01:45 -06003094 // Create semaphores to synchronize acquiring presentable buffers before
3095 // rendering and waiting for drawing to be complete before presenting
3096 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3097 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3098 .pNext = NULL,
3099 .flags = 0,
3100 };
Tony Barbource7524e2016-07-28 12:01:45 -06003101
Tony Barbour66a56c32016-09-21 13:10:56 -06003102 // Create fences that we can use to throttle if we get too far
3103 // ahead of the image presents
3104 VkFenceCreateInfo fence_ci = {
3105 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3106 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003107 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003108 };
3109 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3110 vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003111 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003112 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003113 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003114
3115 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3116 &demo->draw_complete_semaphores[i]);
3117 assert(!err);
3118
3119 if (demo->separate_present_queue) {
3120 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3121 &demo->image_ownership_semaphores[i]);
3122 assert(!err);
3123 }
Tony Barbour22e06272016-09-07 16:07:56 -06003124 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003125 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003126
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003127 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003128 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003129}
3130
Tony Barbour153cb062016-12-07 13:43:36 -07003131#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003132static void registry_handle_global(void *data, struct wl_registry *registry,
3133 uint32_t name, const char *interface,
3134 uint32_t version UNUSED) {
3135 struct demo *demo = data;
3136 if (strcmp(interface, "wl_compositor") == 0) {
3137 demo->compositor =
3138 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3139 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3140 * tp xdg_shell */
3141 } else if (strcmp(interface, "wl_shell") == 0) {
3142 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3143 }
3144}
3145
3146static void registry_handle_global_remove(void *data UNUSED,
3147 struct wl_registry *registry UNUSED,
3148 uint32_t name UNUSED) {}
3149
3150static const struct wl_registry_listener registry_listener = {
3151 registry_handle_global, registry_handle_global_remove};
3152#endif
3153
Karl Schultze4dc75c2016-02-02 15:37:51 -07003154static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003155#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003156 const xcb_setup_t *setup;
3157 xcb_screen_iterator_t iter;
3158 int scr;
3159
3160 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003161 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003162 printf("Cannot find a compatible Vulkan installable client driver "
3163 "(ICD).\nExiting ...\n");
3164 fflush(stdout);
3165 exit(1);
3166 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003167
3168 setup = xcb_get_setup(demo->connection);
3169 iter = xcb_setup_roots_iterator(setup);
3170 while (scr-- > 0)
3171 xcb_screen_next(&iter);
3172
3173 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003174#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3175 demo->display = wl_display_connect(NULL);
3176
3177 if (demo->display == NULL) {
3178 printf("Cannot find a compatible Vulkan installable client driver "
3179 "(ICD).\nExiting ...\n");
3180 fflush(stdout);
3181 exit(1);
3182 }
3183
3184 demo->registry = wl_display_get_registry(demo->display);
3185 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3186 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003187#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003188}
3189
Karl Schultze4dc75c2016-02-02 15:37:51 -07003190static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003191 vec3 eye = {0.0f, 3.0f, 5.0f};
3192 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003193 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003194
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003195 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003196 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003197 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003198
Piers Daniell735ee532015-02-23 16:23:13 -07003199 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003200 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003201 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003202 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003203 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003204 if ((strcmp(argv[i], "--present_mode") == 0) &&
3205 (i < argc - 1)) {
3206 demo->presentMode = atoi(argv[i+1]);
3207 i++;
3208 continue;
3209 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003210 if (strcmp(argv[i], "--break") == 0) {
3211 demo->use_break = true;
3212 continue;
3213 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003214 if (strcmp(argv[i], "--validate") == 0) {
3215 demo->validate = true;
3216 continue;
3217 }
Tony Barbour2593b182016-04-19 10:57:58 -06003218 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003219 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003220 continue;
3221 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003222 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3223 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3224 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003225 i++;
3226 continue;
3227 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003228 if (strcmp(argv[i], "--suppress_popups") == 0) {
3229 demo->suppress_popups = true;
3230 continue;
3231 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003232
Cody Northropaf28a532016-09-27 10:50:57 -06003233#if defined(ANDROID)
3234 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3235#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003236 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour291d57b2016-10-21 14:47:07 -06003237 "[--c <framecount>] [--suppress_popups] [--present_mode <present mode enum>]\n"
3238 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3239 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3240 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3241 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3242 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3243 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003244 fflush(stderr);
3245 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003246#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003247 }
3248
Tony Barbour153cb062016-12-07 13:43:36 -07003249 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003250
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003251 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003252
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003253 demo->width = 500;
3254 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003255
Tony Barbour66a56c32016-09-21 13:10:56 -06003256 demo->spin_angle = 4.0f;
3257 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003258 demo->pause = false;
3259
Karl Schultze4dc75c2016-02-02 15:37:51 -07003260 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3261 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003262 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3263 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003264
3265 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003266}
3267
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003268#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003269// Include header required for parsing the command line options.
3270#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003271
Karl Schultze4dc75c2016-02-02 15:37:51 -07003272int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3273 int nCmdShow) {
3274 MSG msg; // message
3275 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003276 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003277 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003278
Karl Schultze4dc75c2016-02-02 15:37:51 -07003279 // Use the CommandLine functions to get the command line arguments.
3280 // Unfortunately, Microsoft outputs
3281 // this information as wide characters for Unicode, and we simply want the
3282 // Ascii version to be compatible
3283 // with the non-Windows side. So, we have to convert the information to
3284 // Ascii character strings.
3285 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3286 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003287 argc = 0;
3288 }
3289
Karl Schultze4dc75c2016-02-02 15:37:51 -07003290 if (argc > 0) {
3291 argv = (char **)malloc(sizeof(char *) * argc);
3292 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003293 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003294 } else {
3295 for (int iii = 0; iii < argc; iii++) {
3296 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003297 size_t numConverted = 0;
3298
Karl Schultze4dc75c2016-02-02 15:37:51 -07003299 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3300 if (argv[iii] != NULL) {
3301 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3302 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003303 }
3304 }
3305 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003306 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003307 argv = NULL;
3308 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003309
3310 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003311
3312 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003313 if (argc > 0 && argv != NULL) {
3314 for (int iii = 0; iii < argc; iii++) {
3315 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003316 free(argv[iii]);
3317 }
3318 }
3319 free(argv);
3320 }
3321
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003322 demo.connection = hInstance;
3323 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003324 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003325 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003326
3327 demo_prepare(&demo);
3328
Karl Schultze4dc75c2016-02-02 15:37:51 -07003329 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003330
3331 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003332 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003333 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003334 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003335 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003336 done = true; // if found, quit app
3337 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003338 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003339 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003340 DispatchMessage(&msg);
3341 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003342 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003343 }
3344
3345 demo_cleanup(&demo);
3346
Karl Schultze4dc75c2016-02-02 15:37:51 -07003347 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003348}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003349#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3350#include <android/log.h>
3351#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003352#include "android_util.h"
3353
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003354static bool initialized = false;
3355static bool active = false;
3356struct demo demo;
3357
3358static int32_t processInput(struct android_app* app, AInputEvent* event) {
3359 return 0;
3360}
3361
3362static void processCommand(struct android_app* app, int32_t cmd) {
3363 switch(cmd) {
3364 case APP_CMD_INIT_WINDOW: {
3365 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003366 // We're getting a new window. If the app is starting up, we
3367 // need to initialize. If the app has already been
3368 // initialized, that means that we lost our previous window,
3369 // which means that we have a lot of work to do. At a minimum,
3370 // we need to destroy the swapchain and surface associated with
3371 // the old window, and create a new surface and swapchain.
3372 // However, since there are a lot of other objects/state that
3373 // is tied to the swapchain, it's easiest to simply cleanup and
3374 // start over (i.e. use a brute-force approach of re-starting
3375 // the app)
3376 if (demo.prepared) {
3377 demo_cleanup(&demo);
3378 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003379
3380 // Parse Intents into argc, argv
3381 // Use the following key to send arguments, i.e.
3382 // --es args "--validate"
3383 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003384 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003385 int argc = 0;
3386 char** argv = get_args(app, key, appTag, &argc);
3387
3388 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3389 for (int i = 0; i < argc; i++)
3390 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3391
3392 demo_init(&demo, argc, argv);
3393
3394 // Free the argv malloc'd by get_args
3395 for (int i = 0; i < argc; i++)
3396 free(argv[i]);
3397
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003398 demo.window = (void*)app->window;
3399 demo_init_vk_swapchain(&demo);
3400 demo_prepare(&demo);
3401 initialized = true;
3402 }
3403 break;
3404 }
3405 case APP_CMD_GAINED_FOCUS: {
3406 active = true;
3407 break;
3408 }
3409 case APP_CMD_LOST_FOCUS: {
3410 active = false;
3411 break;
3412 }
3413 }
3414}
3415
3416void android_main(struct android_app *app)
3417{
3418 app_dummy();
3419
Cody Northrop8707a6e2016-04-26 19:59:19 -06003420#ifdef ANDROID
3421 int vulkanSupport = InitVulkan();
3422 if (vulkanSupport == 0)
3423 return;
3424#endif
3425
Ian Elliottf05d5d12016-05-17 12:03:35 -06003426 demo.prepared = false;
3427
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003428 app->onAppCmd = processCommand;
3429 app->onInputEvent = processInput;
3430
3431 while(1) {
3432 int events;
3433 struct android_poll_source* source;
3434 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3435 if (source) {
3436 source->process(app, source);
3437 }
3438
3439 if (app->destroyRequested != 0) {
3440 demo_cleanup(&demo);
3441 return;
3442 }
3443 }
3444 if (initialized && active) {
3445 demo_run(&demo);
3446 }
3447 }
3448
3449}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003450#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003451int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003452 struct demo demo;
3453
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003454 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003455#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003456 demo_create_xcb_window(&demo);
3457#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3458 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003459#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3460 demo_create_window(&demo);
3461#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003462
Tony Barbourcc69f452015-10-08 13:59:42 -06003463 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003464
3465 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003466
Tony Barbour153cb062016-12-07 13:43:36 -07003467#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003468 demo_run_xcb(&demo);
3469#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3470 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003471#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3472 demo_run(&demo);
3473#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003474
3475 demo_cleanup(&demo);
3476
Karl Schultz0218c002016-03-22 17:06:13 -06003477 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003478}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003479#endif