blob: ddc01f2c6cb90b2e121e12cf393e7ab3b85f82ec [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
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600294#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600295 Display* display;
296 Window xlib_window;
297 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600298
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600299 xcb_connection_t *connection;
300 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600301 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800302 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900303#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
304 struct wl_display *display;
305 struct wl_registry *registry;
306 struct wl_compositor *compositor;
307 struct wl_surface *window;
308 struct wl_shell *shell;
309 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500310#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
311 ANativeWindow* window;
312#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700313 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600314 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700315 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600316 bool use_xlib;
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 Barbour66a56c32016-09-21 13:10:56 -0600358 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600359 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800361 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600362 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
364 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600365 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800368 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500369 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600370 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371 } depth;
372
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
375 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800377 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500378 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600379 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600380 } uniform_data;
381
Karl Schultze4dc75c2016-02-02 15:37:51 -0700382 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500383 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600385 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800386 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600389 mat4x4 projection_matrix;
390 mat4x4 view_matrix;
391 mat4x4 model_matrix;
392
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600393 float spin_angle;
394 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600395 bool pause;
396
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600397 VkShaderModule vert_shader_module;
398 VkShaderModule frag_shader_module;
399
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkDescriptorPool desc_pool;
401 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800402
Tony Barbourd8e504f2015-10-08 14:26:24 -0600403 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800404
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600405 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600406 int32_t curFrame;
407 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600408 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600409 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600410 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700411 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
412 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
413 VkDebugReportCallbackEXT msg_callback;
414 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600415
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600416 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600417 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600418};
419
lenny-lunargfbe22392016-06-06 11:07:53 -0600420VKAPI_ATTR VkBool32 VKAPI_CALL
421dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
422 uint64_t srcObject, size_t location, int32_t msgCode,
423 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600424
425 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600426 char *message = (char *)malloc(strlen(pMsg) + 100);
427
428 assert(message);
429
Cody Northropaf28a532016-09-27 10:50:57 -0600430 // We know we're submitting queues without fences, ignore this
431 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer"))
432 return false;
433
434 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
435 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600436 validation_error = 1;
437 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600438 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
439 validation_error = 1;
440 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600441 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600442 validation_error = 1;
443 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
444 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
445 validation_error = 1;
446 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
447 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600448 validation_error = 1;
449 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600450 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600451 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600452 }
453
454#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600455
Tony Barbour602ca4f2016-09-12 14:02:43 -0600456 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600457 struct demo *demo = (struct demo*) pUserData;
458 if (!demo->suppress_popups)
459 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600460 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600461
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600462#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600463
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600464 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600465 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600466 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600467 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600468 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_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_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600471 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600472 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600473 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600474 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600475 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600476 }
Cody Northropaf28a532016-09-27 10:50:57 -0600477
lenny-lunargfbe22392016-06-06 11:07:53 -0600478#else
Cody Northropaf28a532016-09-27 10:50:57 -0600479
lenny-lunargfbe22392016-06-06 11:07:53 -0600480 printf("%s\n", message);
481 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600482
lenny-lunargfbe22392016-06-06 11:07:53 -0600483#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600484
lenny-lunargfbe22392016-06-06 11:07:53 -0600485 free(message);
486
Cody Northropaf28a532016-09-27 10:50:57 -0600487 //clang-format on
488
lenny-lunargfbe22392016-06-06 11:07:53 -0600489 /*
490 * false indicates that layer should not bail-out of an
491 * API call that had validation failures. This may mean that the
492 * app dies inside the driver due to invalid parameter(s).
493 * That's what would happen without validation layers, so we'll
494 * keep that behavior here.
495 */
496 return false;
497}
498
Ian Elliottcf074d32015-10-16 18:02:43 -0600499// Forward declaration:
500static void demo_resize(struct demo *demo);
501
Karl Schultze4dc75c2016-02-02 15:37:51 -0700502static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
503 VkFlags requirements_mask,
504 uint32_t *typeIndex) {
505 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600506 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700507 if ((typeBits & 1) == 1) {
508 // Type is available, does it match user properties?
509 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
510 requirements_mask) == requirements_mask) {
511 *typeIndex = i;
512 return true;
513 }
514 }
515 typeBits >>= 1;
516 }
517 // No memory types matched, return failure
518 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600519}
520
Karl Schultze4dc75c2016-02-02 15:37:51 -0700521static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600522 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600523
Tony Barbource7524e2016-07-28 12:01:45 -0600524 // This function could get called twice if the texture uses a staging buffer
525 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600527 return;
528
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530 assert(!err);
531
Tony Barbource7524e2016-07-28 12:01:45 -0600532 VkFence fence;
533 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
534 .pNext = NULL,
535 .flags = 0};
536 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700537 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700538 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
539 .pNext = NULL,
540 .waitSemaphoreCount = 0,
541 .pWaitSemaphores = NULL,
542 .pWaitDstStageMask = NULL,
543 .commandBufferCount = 1,
544 .pCommandBuffers = cmd_bufs,
545 .signalSemaphoreCount = 0,
546 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547
Tony Barbource7524e2016-07-28 12:01:45 -0600548 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549 assert(!err);
550
Tony Barbource7524e2016-07-28 12:01:45 -0600551 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552 assert(!err);
553
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600554 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600555 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600556 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557}
558
Karl Schultze4dc75c2016-02-02 15:37:51 -0700559static void demo_set_image_layout(struct demo *demo, VkImage image,
560 VkImageAspectFlags aspectMask,
561 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700562 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600563 VkAccessFlagBits srcAccessMask,
564 VkPipelineStageFlags src_stages,
565 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600566 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600567
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600568 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600569 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600570 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700571 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800572 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600573 .oldLayout = old_image_layout,
574 .newLayout = new_image_layout,
575 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700576 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600577
Tony Barboureb5077b2016-10-19 15:23:36 -0600578 switch (new_image_layout) {
579 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600580 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600581 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
582 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600583
Tony Barboureb5077b2016-10-19 15:23:36 -0600584 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700585 image_memory_barrier.dstAccessMask =
586 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600587 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700588
Tony Barboureb5077b2016-10-19 15:23:36 -0600589 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700590 image_memory_barrier.dstAccessMask =
591 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600592 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700593
Tony Barboureb5077b2016-10-19 15:23:36 -0600594 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700595 image_memory_barrier.dstAccessMask =
596 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600597 break;
598
599 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
600 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
601 break;
602
603 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
604 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
605 break;
606
607 default:
608 image_memory_barrier.dstAccessMask = 0;
609 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600610 }
611
Tony Barbourf165b5d2016-10-03 16:01:41 -0600612
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600613 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600614
Karl Schultze4dc75c2016-02-02 15:37:51 -0700615 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
616 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600617}
618
Karl Schultze4dc75c2016-02-02 15:37:51 -0700619static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700620 const VkCommandBufferBeginInfo cmd_buf_info = {
621 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
622 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600623 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700624 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700625 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800626 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700627 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
628 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800629 };
630 const VkRenderPassBeginInfo rp_begin = {
631 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
632 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800633 .renderPass = demo->render_pass,
634 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800635 .renderArea.offset.x = 0,
636 .renderArea.offset.y = 0,
637 .renderArea.extent.width = demo->width,
638 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600639 .clearValueCount = 2,
640 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700641 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800642 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600644 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800646 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700647 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
648 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
649 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
650 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600651 VkViewport viewport;
652 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700653 viewport.height = (float)demo->height;
654 viewport.width = (float)demo->width;
655 viewport.minDepth = (float)0.0f;
656 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700657 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600658
659 VkRect2D scissor;
660 memset(&scissor, 0, sizeof(scissor));
661 scissor.extent.width = demo->width;
662 scissor.extent.height = demo->height;
663 scissor.offset.x = 0;
664 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700665 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600666 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600667 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600668 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800669 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600670
Tony Barbour22e06272016-09-07 16:07:56 -0600671 if (demo->separate_present_queue) {
672 // We have to transfer ownership from the graphics queue family to the
673 // present queue family to be able to present. Note that we don't have
674 // to transfer from present queue family back to graphics queue family at
675 // the start of the next frame because we don't care about the image's
676 // contents at that point.
677 VkImageMemoryBarrier image_ownership_barrier = {
678 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
679 .pNext = NULL,
680 .srcAccessMask = 0,
681 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
682 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
683 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
684 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
685 .dstQueueFamilyIndex = demo->present_queue_family_index,
686 .image = demo->buffers[demo->current_buffer].image,
687 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
688
689 vkCmdPipelineBarrier(cmd_buf,
690 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600691 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600692 0, NULL, 0, NULL, 1, &image_ownership_barrier);
693 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600694 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600695 assert(!err);
696}
697
Tony Barbour22e06272016-09-07 16:07:56 -0600698void demo_build_image_ownership_cmd(struct demo *demo, int i) {
699 VkResult U_ASSERT_ONLY err;
700
701 const VkCommandBufferBeginInfo cmd_buf_info = {
702 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
703 .pNext = NULL,
704 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
705 .pInheritanceInfo = NULL,
706 };
707 err = vkBeginCommandBuffer(demo->buffers[i].graphics_to_present_cmd,
708 &cmd_buf_info);
709 assert(!err);
710
711 VkImageMemoryBarrier image_ownership_barrier = {
712 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
713 .pNext = NULL,
714 .srcAccessMask = 0,
715 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
716 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
717 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
718 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
719 .dstQueueFamilyIndex = demo->present_queue_family_index,
720 .image = demo->buffers[i].image,
721 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
722
723 vkCmdPipelineBarrier(demo->buffers[i].graphics_to_present_cmd,
724 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
725 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
726 NULL, 0, NULL, 1, &image_ownership_barrier);
727 err = vkEndCommandBuffer(demo->buffers[i].graphics_to_present_cmd);
728 assert(!err);
729}
730
Karl Schultze4dc75c2016-02-02 15:37:51 -0700731void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600732 mat4x4 MVP, Model, VP;
733 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600734 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600735 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600736
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600737 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600739 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600740 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700741 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
742 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600743 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600744
Karl Schultze4dc75c2016-02-02 15:37:51 -0700745 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
746 demo->uniform_data.mem_alloc.allocationSize, 0,
747 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600748 assert(!err);
749
Karl Schultze4dc75c2016-02-02 15:37:51 -0700750 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600751
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600752 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753}
754
Karl Schultze4dc75c2016-02-02 15:37:51 -0700755static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600756 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600757
szdarkhackd322ff02016-10-08 09:51:22 +0300758 // Ensure no more than FRAME_LAG presentations are outstanding
759 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
760 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600761
Ian Elliottaaae5352015-07-06 14:27:58 -0600762 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700763 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour66a56c32016-09-21 13:10:56 -0600764 demo->image_acquired_semaphores[demo->frame_index], demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600765 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600766
Ian Elliottcf074d32015-10-16 18:02:43 -0600767 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
768 // demo->swapchain is out of date (e.g. the window was resized) and
769 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600770 demo->frame_index += 1;
771 demo->frame_index %= FRAME_LAG;
772
Ian Elliottcf074d32015-10-16 18:02:43 -0600773 demo_resize(demo);
774 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600775 return;
776 } else if (err == VK_SUBOPTIMAL_KHR) {
777 // demo->swapchain is not as optimal as it could be, but the platform's
778 // presentation engine will still present the image correctly.
779 } else {
780 assert(!err);
781 }
Tony Barbource7524e2016-07-28 12:01:45 -0600782 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600783 // that the image won't be rendered to until the presentation
784 // engine has fully released ownership to the application, and it is
785 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600786 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600787 VkPipelineStageFlags pipe_stage_flags;
788 VkSubmitInfo submit_info;
789 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
790 submit_info.pNext = NULL;
791 submit_info.pWaitDstStageMask = &pipe_stage_flags;
792 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
793 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600794 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600795 submit_info.commandBufferCount = 1;
796 submit_info.pCommandBuffers = &demo->buffers[demo->current_buffer].cmd;
797 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600798 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura2a67812016-07-18 13:21:06 -0600799 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600800 assert(!err);
801
Tony Barbour22e06272016-09-07 16:07:56 -0600802 if (demo->separate_present_queue) {
803 // If we are using separate queues, change image ownership to the
804 // present queue before presenting, waiting for the draw complete
805 // semaphore and signalling the ownership released semaphore when finished
806 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
807 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600808 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600809 submit_info.commandBufferCount = 1;
810 submit_info.pCommandBuffers =
811 &demo->buffers[demo->current_buffer].graphics_to_present_cmd;
812 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600813 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600814 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
815 assert(!err);
816 }
817
818 // If we are using separate queues we have to wait for image ownership,
819 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600820 VkPresentInfoKHR present = {
821 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600822 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600823 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600824 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600825 ? &demo->image_ownership_semaphores[demo->frame_index]
826 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600827 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700828 .pSwapchains = &demo->swapchain,
829 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600830 };
831
Tony Barboura2a67812016-07-18 13:21:06 -0600832 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600833 demo->frame_index += 1;
834 demo->frame_index %= FRAME_LAG;
835
Ian Elliottcf074d32015-10-16 18:02:43 -0600836 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
837 // demo->swapchain is out of date (e.g. the window was resized) and
838 // must be recreated:
839 demo_resize(demo);
840 } else if (err == VK_SUBOPTIMAL_KHR) {
841 // demo->swapchain is not as optimal as it could be, but the platform's
842 // presentation engine will still present the image correctly.
843 } else {
844 assert(!err);
845 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600846}
847
Karl Schultze4dc75c2016-02-02 15:37:51 -0700848static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600849 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600850 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600851
Ian Elliottb08e0d92015-11-20 11:55:46 -0700852 // Check the surface capabilities and formats
853 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700854 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
855 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600856 assert(!err);
857
Ian Elliott8528eff2015-08-07 15:56:59 -0600858 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700859 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
860 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600861 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600862 VkPresentModeKHR *presentModes =
863 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600864 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700865 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
866 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600867 assert(!err);
868
Ian Elliotta0781972015-08-21 15:09:33 -0600869 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600870 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
871 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
872 // If the surface size is undefined, the size is set to the size
873 // of the images requested, which must fit within the minimum and
874 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600875 swapchainExtent.width = demo->width;
876 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600877
878 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
879 swapchainExtent.width = surfCapabilities.minImageExtent.width;
880 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
881 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
882 }
883
884 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
885 swapchainExtent.height = surfCapabilities.minImageExtent.height;
886 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
887 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
888 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700889 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600890 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700891 swapchainExtent = surfCapabilities.currentExtent;
892 demo->width = surfCapabilities.currentExtent.width;
893 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600894 }
895
Tony Barbour66a56c32016-09-21 13:10:56 -0600896 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600897 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600898 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottb18fdde2016-10-03 12:11:12 -0600899 // There are times when you may wish to use another present mode. The
900 // following code shows how to select them, and the comments provide some
901 // reasons you may wish to use them.
902 //
903 // It should be noted that Vulkan 1.0 doesn't provide a method for
904 // synchronizing rendering with the presentation engine's display. There
905 // is a method provided for throttling rendering with the display, but
906 // there are some presentation engines for which this method will not work.
907 // If an application doesn't throttle its rendering, and if it renders much
908 // faster than the refresh rate of the display, this can waste power on
909 // mobile devices. That is because power is being spent rendering images
910 // that may never be seen.
911//#define DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR
912//#define DESIRE_VK_PRESENT_MODE_MAILBOX_KHR
913//#define DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR
914#if defined(DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR)
915 // 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.
918 for (size_t i = 0; i < presentModeCount; ++i) {
919 if (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR) {
920 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
921 break;
922 }
923 }
924#elif defined(DESIRE_VK_PRESENT_MODE_MAILBOX_KHR)
925 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
926 // generally render a new presentable image every refresh cycle, but are
927 // occasionally early. In this case, the application wants the new image
928 // to be displayed instead of the previously-queued-for-presentation image
929 // that has not yet been displayed.
930 for (size_t i = 0; i < presentModeCount; ++i) {
931 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
932 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
933 break;
934 }
935 }
936#elif defined(DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR)
937 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
938 // render a new presentable image every refresh cycle, but are occasionally
939 // late. In this case (perhaps because of stuttering/latency concerns),
940 // the application wants the late image to be immediately displayed, even
941 // though that may mean some tearing.
942 for (size_t i = 0; i < presentModeCount; ++i) {
943 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
944 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
945 break;
946 }
947 }
948#endif
Ian Elliottaaae5352015-07-06 14:27:58 -0600949
Ian Elliottcf7f7482016-08-19 03:46:58 -0600950 // Determine the number of VkImage's to use in the swap chain.
951 // Application desires to only acquire 1 image at a time (which is
952 // "surfCapabilities.minImageCount").
953 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
954 // If maxImageCount is 0, we can ask for as many images as we want;
955 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700956 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600957 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600958 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600959 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600960 }
961
Tony Barbour9fd6d352015-09-16 15:01:32 -0600962 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700963 if (surfCapabilities.supportedTransforms &
964 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700965 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600966 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700967 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600968 }
969
Tony Barboura2a67812016-07-18 13:21:06 -0600970 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600971 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800972 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700973 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600974 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800975 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600976 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700977 .imageExtent =
978 {
979 .width = swapchainExtent.width, .height = swapchainExtent.height,
980 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700981 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600982 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700983 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700984 .imageArrayLayers = 1,
985 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
986 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600987 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600988 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600989 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600990 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600992 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600993 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700994 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800995 assert(!err);
996
Ian Elliottcf074d32015-10-16 18:02:43 -0600997 // If we just re-created an existing swapchain, we should destroy the old
998 // swapchain at this point.
999 // Note: destroying the swapchain also cleans up all its associated
1000 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001001 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001002 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001003 }
1004
1005 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001006 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001007 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001008
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 VkImage *swapchainImages =
1010 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001011 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001012 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001013 &demo->swapchainImageCount,
1014 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001015 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001016
Karl Schultze4dc75c2016-02-02 15:37:51 -07001017 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
1018 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -06001019 assert(demo->buffers);
1020
Ian Elliotta0781972015-08-21 15:09:33 -06001021 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001022 VkImageViewCreateInfo color_image_view = {
1023 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001024 .pNext = NULL,
1025 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001026 .components =
1027 {
1028 .r = VK_COMPONENT_SWIZZLE_R,
1029 .g = VK_COMPONENT_SWIZZLE_G,
1030 .b = VK_COMPONENT_SWIZZLE_B,
1031 .a = VK_COMPONENT_SWIZZLE_A,
1032 },
1033 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1034 .baseMipLevel = 0,
1035 .levelCount = 1,
1036 .baseArrayLayer = 0,
1037 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001038 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1039 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001040 };
1041
Ian Elliotta0781972015-08-21 15:09:33 -06001042 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001043
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001044 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045
Karl Schultze4dc75c2016-02-02 15:37:51 -07001046 err = vkCreateImageView(demo->device, &color_image_view, NULL,
1047 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001048 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001049
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001050 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001051
Mark Young04fd3d82016-01-25 14:43:50 -07001052 if (NULL != presentModes) {
1053 free(presentModes);
1054 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055}
1056
Karl Schultze4dc75c2016-02-02 15:37:51 -07001057static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001058 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001059 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001060 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001062 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001063 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001064 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001065 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001066 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001067 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001068 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001069 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 .flags = 0,
1071 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001072
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001073 VkImageViewCreateInfo view = {
1074 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001076 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001077 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001078 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1079 .baseMipLevel = 0,
1080 .levelCount = 1,
1081 .baseArrayLayer = 0,
1082 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001083 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001084 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001086
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001087 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001088 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001089 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001090
1091 demo->depth.format = depth_format;
1092
1093 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001094 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001095 assert(!err);
1096
Karl Schultze4dc75c2016-02-02 15:37:51 -07001097 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001098 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001099
Chia-I Wuf48700b2015-11-10 16:21:09 +08001100 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001101 demo->depth.mem_alloc.pNext = NULL;
1102 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1103 demo->depth.mem_alloc.memoryTypeIndex = 0;
1104
Karl Schultze4dc75c2016-02-02 15:37:51 -07001105 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1106 0, /* No requirements */
1107 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001108 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001109
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001110 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001111 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1112 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001113 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001115 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001116 err =
1117 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001118 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119
1120 /* create image view */
1121 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001122 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001123 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001124}
1125
Tony Barbour1a86d032015-09-21 15:17:33 -06001126/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001128 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001129#ifdef __ANDROID__
1130#include <lunarg.ppm.h>
1131 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001132 cPtr = (char*)lunarg_ppm;
1133 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001134 return false;
1135 }
1136 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001137 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001138 if (rgba_data == NULL) {
1139 return true;
1140 }
1141 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001142 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001143 return false;
1144 }
1145 while(strncmp(cPtr++, "\n", 1));
1146
1147 for (int y = 0; y < *height; y++) {
1148 uint8_t *rowPtr = rgba_data;
1149 for (int x = 0; x < *width; x++) {
1150 memcpy(rowPtr, cPtr, 3);
1151 rowPtr[3] = 255; /* Alpha of 1 */
1152 rowPtr += 4;
1153 cPtr += 3;
1154 }
1155 rgba_data += layout->rowPitch;
1156 }
1157
1158 return true;
1159#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001160 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001161 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001162
Tony Barbour1a86d032015-09-21 15:17:33 -06001163 if (!fPtr)
1164 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001165
Tony Barbour1a86d032015-09-21 15:17:33 -06001166 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001167 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1168 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001169 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001170 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001171
Tony Barbour1a86d032015-09-21 15:17:33 -06001172 do {
1173 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001174 if (cPtr == NULL) {
1175 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001176 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001177 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001178 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001179
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001180 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001181 if (rgba_data == NULL) {
1182 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001183 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001184 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001185 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001186 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001187 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1188 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001189 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001190 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001191
Karl Schultze4dc75c2016-02-02 15:37:51 -07001192 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001193 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001194 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001195 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001196 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001197 rowPtr[3] = 255; /* Alpha of 1 */
1198 rowPtr += 4;
1199 }
1200 rgba_data += layout->rowPitch;
1201 }
1202 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001203 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001204#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001205}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001206
Karl Schultze4dc75c2016-02-02 15:37:51 -07001207static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001208 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001209 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001210 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001211 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001212 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001213 int32_t tex_width;
1214 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001215 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001216 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217
Karl Schultze4dc75c2016-02-02 15:37:51 -07001218 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001219 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001220 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001221
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001222 tex_obj->tex_width = tex_width;
1223 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001224
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001225 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001226 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001227 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001228 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001229 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001230 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001232 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001233 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001235 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001236 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001237 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001239
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001240 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001241
Karl Schultze4dc75c2016-02-02 15:37:51 -07001242 err =
1243 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001244 assert(!err);
1245
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001246 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001247
Chia-I Wuf48700b2015-11-10 16:21:09 +08001248 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001249 tex_obj->mem_alloc.pNext = NULL;
1250 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1251 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252
Karl Schultze4dc75c2016-02-02 15:37:51 -07001253 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1254 required_props,
1255 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001256 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001257
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001258 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001259 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001260 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001261 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001262
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001263 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001264 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001265 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001267 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001268 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001269 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001270 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001271 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001272 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001273 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001274 void *data;
1275
Karl Schultze4dc75c2016-02-02 15:37:51 -07001276 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1277 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001278
Karl Schultze4dc75c2016-02-02 15:37:51 -07001279 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1280 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001281 assert(!err);
1282
1283 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1284 fprintf(stderr, "Error loading texture: %s\n", filename);
1285 }
1286
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001287 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001288 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001289
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001290 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001291}
1292
Karl Schultze4dc75c2016-02-02 15:37:51 -07001293static void demo_destroy_texture_image(struct demo *demo,
1294 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001295 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001296 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1297 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001298}
1299
Karl Schultze4dc75c2016-02-02 15:37:51 -07001300static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001301 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001302 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001303 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001304
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001305 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001306
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001308 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001309
Karl Schultze4dc75c2016-02-02 15:37:51 -07001310 if ((props.linearTilingFeatures &
1311 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1312 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001313 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001314 demo_prepare_texture_image(
1315 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1316 VK_IMAGE_USAGE_SAMPLED_BIT,
1317 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1318 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001319 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1320 // shader to run until layout transition completes
1321 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1322 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1323 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1324 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001325 } else if (props.optimalTilingFeatures &
1326 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001327 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001328 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001329
1330 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001331 demo_prepare_texture_image(
1332 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1333 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1334 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1335 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001336
Karl Schultze4dc75c2016-02-02 15:37:51 -07001337 demo_prepare_texture_image(
1338 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1339 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1340 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001341
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001342 demo_set_image_layout(demo, staging_texture.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_SRC_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 Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001350 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001351 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001352 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001353 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001354 VK_ACCESS_HOST_WRITE_BIT,
1355 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1356 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001357
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001358 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001359 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1360 .srcOffset = {0, 0, 0},
1361 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1362 .dstOffset = {0, 0, 0},
1363 .extent = {staging_texture.tex_width,
1364 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001365 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 vkCmdCopyImage(
1367 demo->cmd, staging_texture.image,
1368 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1369 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001370
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001371 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001372 VK_IMAGE_ASPECT_COLOR_BIT,
1373 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001374 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001375 VK_ACCESS_TRANSFER_WRITE_BIT,
1376 VK_PIPELINE_STAGE_TRANSFER_BIT,
1377 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001378
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001379 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001380
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001381 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001382 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001383 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1384 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001385 }
1386
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001387 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001388 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001390 .magFilter = VK_FILTER_NEAREST,
1391 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001392 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001393 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1394 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1395 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001397 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001398 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001399 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 .minLod = 0.0f,
1401 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001402 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001403 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001405
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001406 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001407 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001409 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001410 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001411 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001412 .components =
1413 {
1414 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1415 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1416 },
1417 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001418 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001420
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001422 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001423 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424 assert(!err);
1425
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001426 /* create image view */
1427 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001428 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001429 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430 assert(!err);
1431 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433
Karl Schultze4dc75c2016-02-02 15:37:51 -07001434void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001435 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001436 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001437 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001438 int i;
1439 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001440 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001441 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001442 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001443
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001444 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001445 mat4x4_mul(MVP, VP, demo->model_matrix);
1446 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001447 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001448
Karl Schultze4dc75c2016-02-02 15:37:51 -07001449 for (i = 0; i < 12 * 3; i++) {
1450 data.position[i][0] = g_vertex_buffer_data[i * 3];
1451 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1452 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001453 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001454 data.attr[i][0] = g_uv_buffer_data[2 * i];
1455 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001456 data.attr[i][2] = 0;
1457 data.attr[i][3] = 0;
1458 }
1459
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001460 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001461 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001462 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001463 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001464 err =
1465 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001466 assert(!err);
1467
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1469 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001470
Chia-I Wuf48700b2015-11-10 16:21:09 +08001471 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001472 demo->uniform_data.mem_alloc.pNext = NULL;
1473 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1474 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1475
Karl Schultze4dc75c2016-02-02 15:37:51 -07001476 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001477 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1478 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001479 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001480 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001481
Karl Schultze4dc75c2016-02-02 15:37:51 -07001482 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1483 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001484 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001485
Karl Schultze4dc75c2016-02-02 15:37:51 -07001486 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1487 demo->uniform_data.mem_alloc.allocationSize, 0,
1488 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001489 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001490
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001491 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001492
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001493 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001494
Karl Schultze4dc75c2016-02-02 15:37:51 -07001495 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1496 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001497 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001498
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001499 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1500 demo->uniform_data.buffer_info.offset = 0;
1501 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001502}
1503
Karl Schultze4dc75c2016-02-02 15:37:51 -07001504static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001505 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001506 [0] =
1507 {
1508 .binding = 0,
1509 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1510 .descriptorCount = 1,
1511 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1512 .pImmutableSamplers = NULL,
1513 },
1514 [1] =
1515 {
1516 .binding = 1,
1517 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1518 .descriptorCount = DEMO_TEXTURE_COUNT,
1519 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1520 .pImmutableSamplers = NULL,
1521 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001522 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001523 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001524 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001525 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001526 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001527 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001528 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001529 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001530
Karl Schultze4dc75c2016-02-02 15:37:51 -07001531 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1532 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001533 assert(!err);
1534
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001535 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001536 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1537 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001538 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001540 };
1541
Karl Schultze4dc75c2016-02-02 15:37:51 -07001542 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001543 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001545}
1546
Karl Schultze4dc75c2016-02-02 15:37:51 -07001547static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001548 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1549 // because at the start of the renderpass, we don't care about their contents.
1550 // At the start of the subpass, the color attachment's layout will be transitioned
1551 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1552 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1553 // the renderpass, the color attachment's layout will be transitioned to
1554 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1555 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001556 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001557 [0] =
1558 {
1559 .format = demo->format,
1560 .samples = VK_SAMPLE_COUNT_1_BIT,
1561 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1562 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1563 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1564 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001565 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001566 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001567 },
1568 [1] =
1569 {
1570 .format = demo->depth.format,
1571 .samples = VK_SAMPLE_COUNT_1_BIT,
1572 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1573 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1574 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1575 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1576 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001577 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001578 .finalLayout =
1579 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1580 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001581 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001582 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001583 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001584 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001585 const VkAttachmentReference depth_reference = {
1586 .attachment = 1,
1587 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1588 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001589 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001590 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1591 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001592 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001593 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001594 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001595 .pColorAttachments = &color_reference,
1596 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001597 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001598 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001599 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001600 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001601 const VkRenderPassCreateInfo rp_info = {
1602 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1603 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001604 .attachmentCount = 2,
1605 .pAttachments = attachments,
1606 .subpassCount = 1,
1607 .pSubpasses = &subpass,
1608 .dependencyCount = 0,
1609 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001610 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001611 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001612
Chia-I Wu63636062015-10-26 21:10:41 +08001613 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001614 assert(!err);
1615}
1616
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001617//TODO: Merge shader reading
1618#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001619static VkShaderModule
1620demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001621 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001622 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001623 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001624
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001625 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1626 moduleCreateInfo.pNext = NULL;
1627
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001628 moduleCreateInfo.codeSize = size;
1629 moduleCreateInfo.pCode = code;
1630 moduleCreateInfo.flags = 0;
1631 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1632 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001633
1634 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001635}
1636
Karl Schultze4dc75c2016-02-02 15:37:51 -07001637char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001638 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001639 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001640 void *shader_code;
1641
1642 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001643 if (!fp)
1644 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001645
1646 fseek(fp, 0L, SEEK_END);
1647 size = ftell(fp);
1648
1649 fseek(fp, 0L, SEEK_SET);
1650
1651 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001652 retval = fread(shader_code, size, 1, fp);
1653 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001654
1655 *psize = size;
1656
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001657 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001658 return shader_code;
1659}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001660#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001661
Karl Schultze4dc75c2016-02-02 15:37:51 -07001662static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001663#ifdef __ANDROID__
1664 VkShaderModuleCreateInfo sh_info = {};
1665 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1666
1667#include "cube.vert.h"
1668 sh_info.codeSize = sizeof(cube_vert);
1669 sh_info.pCode = cube_vert;
1670 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1671 assert(!err);
1672#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001673 void *vertShaderCode;
1674 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001675
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001676 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1677
Karl Schultze4dc75c2016-02-02 15:37:51 -07001678 demo->vert_shader_module =
1679 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001680
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001681 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001682#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001683
1684 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001685}
1686
Karl Schultze4dc75c2016-02-02 15:37:51 -07001687static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001688#ifdef __ANDROID__
1689 VkShaderModuleCreateInfo sh_info = {};
1690 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1691
1692#include "cube.frag.h"
1693 sh_info.codeSize = sizeof(cube_frag);
1694 sh_info.pCode = cube_frag;
1695 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1696 assert(!err);
1697#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001698 void *fragShaderCode;
1699 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001700
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001701 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001702
Karl Schultze4dc75c2016-02-02 15:37:51 -07001703 demo->frag_shader_module =
1704 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001705
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001706 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001707#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001708
1709 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710}
1711
Karl Schultze4dc75c2016-02-02 15:37:51 -07001712static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001713 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001714 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001715 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001716 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001717 VkPipelineRasterizationStateCreateInfo rs;
1718 VkPipelineColorBlendStateCreateInfo cb;
1719 VkPipelineDepthStencilStateCreateInfo ds;
1720 VkPipelineViewportStateCreateInfo vp;
1721 VkPipelineMultisampleStateCreateInfo ms;
1722 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1723 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001724 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001725
Piers Daniellba839d12015-09-29 13:01:09 -06001726 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1727 memset(&dynamicState, 0, sizeof dynamicState);
1728 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1729 dynamicState.pDynamicStates = dynamicStateEnables;
1730
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001731 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001732 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001733 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001735 memset(&vi, 0, sizeof(vi));
1736 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1737
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001739 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001740 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001741
1742 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001743 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1744 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001745 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001746 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001747 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001748 rs.rasterizerDiscardEnable = VK_FALSE;
1749 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001750 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751
1752 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001753 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1754 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001755 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001756 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001757 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001758 cb.attachmentCount = 1;
1759 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001760
Tony Barbour29645d02015-01-16 14:27:35 -07001761 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001762 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001763 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001764 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1765 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001766 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001767 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1768 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001769
1770 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001771 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001772 ds.depthTestEnable = VK_TRUE;
1773 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001774 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001775 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001776 ds.back.failOp = VK_STENCIL_OP_KEEP;
1777 ds.back.passOp = VK_STENCIL_OP_KEEP;
1778 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001779 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001780 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001781
Tony Barbour29645d02015-01-16 14:27:35 -07001782 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001783 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001784 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001785 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001787 // Two stages: vs and fs
1788 pipeline.stageCount = 2;
1789 VkPipelineShaderStageCreateInfo shaderStages[2];
1790 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1791
Karl Schultze4dc75c2016-02-02 15:37:51 -07001792 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1793 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001794 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001795 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001796
Karl Schultze4dc75c2016-02-02 15:37:51 -07001797 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1798 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001799 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001800 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001801
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001802 memset(&pipelineCache, 0, sizeof(pipelineCache));
1803 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001804
Karl Schultze4dc75c2016-02-02 15:37:51 -07001805 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1806 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001807 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001808
Karl Schultze4dc75c2016-02-02 15:37:51 -07001809 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001810 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001811 pipeline.pRasterizationState = &rs;
1812 pipeline.pColorBlendState = &cb;
1813 pipeline.pMultisampleState = &ms;
1814 pipeline.pViewportState = &vp;
1815 pipeline.pDepthStencilState = &ds;
1816 pipeline.pStages = shaderStages;
1817 pipeline.renderPass = demo->render_pass;
1818 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001819
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001820 pipeline.renderPass = demo->render_pass;
1821
Karl Schultze4dc75c2016-02-02 15:37:51 -07001822 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1823 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001824 assert(!err);
1825
Chia-I Wu63636062015-10-26 21:10:41 +08001826 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1827 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001828}
1829
Karl Schultze4dc75c2016-02-02 15:37:51 -07001830static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001831 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001832 [0] =
1833 {
1834 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1835 .descriptorCount = 1,
1836 },
1837 [1] =
1838 {
1839 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1840 .descriptorCount = DEMO_TEXTURE_COUNT,
1841 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001842 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001843 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001844 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001845 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001846 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001847 .poolSizeCount = 2,
1848 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001849 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001850 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001851
Karl Schultze4dc75c2016-02-02 15:37:51 -07001852 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1853 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001854 assert(!err);
1855}
1856
Karl Schultze4dc75c2016-02-02 15:37:51 -07001857static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001858 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001859 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001860 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001861 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001862
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001863 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001864 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001865 .pNext = NULL,
1866 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001867 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001868 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001869 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001870 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001871
Chia-I Wuae721ba2015-05-25 16:27:55 +08001872 memset(&tex_descs, 0, sizeof(tex_descs));
1873 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001874 tex_descs[i].sampler = demo->textures[i].sampler;
1875 tex_descs[i].imageView = demo->textures[i].view;
1876 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001877 }
1878
1879 memset(&writes, 0, sizeof(writes));
1880
1881 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001882 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001883 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001884 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001885 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001886
1887 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001888 writes[1].dstSet = demo->desc_set;
1889 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001890 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001891 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001892 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001893
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001894 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001895}
1896
Karl Schultze4dc75c2016-02-02 15:37:51 -07001897static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001898 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001899 attachments[1] = demo->depth.view;
1900
Chia-I Wuf973e322015-07-08 13:34:24 +08001901 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001902 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1903 .pNext = NULL,
1904 .renderPass = demo->render_pass,
1905 .attachmentCount = 2,
1906 .pAttachments = attachments,
1907 .width = demo->width,
1908 .height = demo->height,
1909 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001910 };
1911 VkResult U_ASSERT_ONLY err;
1912 uint32_t i;
1913
Karl Schultze4dc75c2016-02-02 15:37:51 -07001914 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1915 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001916 assert(demo->framebuffers);
1917
1918 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001919 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001920 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1921 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001922 assert(!err);
1923 }
1924}
1925
Karl Schultze4dc75c2016-02-02 15:37:51 -07001926static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001927 VkResult U_ASSERT_ONLY err;
1928
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001929 const VkCommandPoolCreateInfo cmd_pool_info = {
1930 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001931 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001932 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001933 .flags = 0,
1934 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001935 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1936 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001937 assert(!err);
1938
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001939 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001940 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001941 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001942 .commandPool = demo->cmd_pool,
1943 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001944 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001945 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001946 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1947 assert(!err);
1948 VkCommandBufferBeginInfo cmd_buf_info = {
1949 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1950 .pNext = NULL,
1951 .flags = 0,
1952 .pInheritanceInfo = NULL,
1953 };
1954 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1955 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956
1957 demo_prepare_buffers(demo);
1958 demo_prepare_depth(demo);
1959 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001960 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001961
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001962 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001963 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001965
Ian Elliotta0781972015-08-21 15:09:33 -06001966 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001967 err =
1968 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001969 assert(!err);
1970 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001971
Tony Barbour22e06272016-09-07 16:07:56 -06001972 if (demo->separate_present_queue) {
1973 const VkCommandPoolCreateInfo cmd_pool_info = {
1974 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1975 .pNext = NULL,
1976 .queueFamilyIndex = demo->present_queue_family_index,
1977 .flags = 0,
1978 };
1979 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1980 &demo->present_cmd_pool);
1981 assert(!err);
1982 const VkCommandBufferAllocateInfo cmd = {
1983 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1984 .pNext = NULL,
1985 .commandPool = demo->present_cmd_pool,
1986 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1987 .commandBufferCount = 1,
1988 };
1989 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1990 err = vkAllocateCommandBuffers(
1991 demo->device, &cmd, &demo->buffers[i].graphics_to_present_cmd);
1992 assert(!err);
1993 demo_build_image_ownership_cmd(demo, i);
1994 }
1995 }
1996
Chia-I Wu63ea9262015-03-26 13:14:16 +08001997 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001998 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001999
Chia-I Wuf973e322015-07-08 13:34:24 +08002000 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002001
Ian Elliotta0781972015-08-21 15:09:33 -06002002 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002003 demo->current_buffer = i;
2004 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
2005 }
2006
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002007 /*
2008 * Prepare functions above may generate pipeline commands
2009 * that need to be flushed before beginning the render loop.
2010 */
2011 demo_flush_init_cmd(demo);
2012
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002013 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002014 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002015}
2016
Karl Schultze4dc75c2016-02-02 15:37:51 -07002017static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002018 uint32_t i;
2019
2020 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002021 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002022
Tony Barbour66a56c32016-09-21 13:10:56 -06002023 // Wait for fences from present operations
2024 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002025 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002026 vkDestroyFence(demo->device, demo->fences[i], NULL);
2027 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2028 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2029 if (demo->separate_present_queue) {
2030 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2031 }
2032 }
2033
Tony Barbourd8e504f2015-10-08 14:26:24 -06002034 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002035 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002036 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06002037 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002038 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002039
Chia-I Wu63636062015-10-26 21:10:41 +08002040 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2041 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2042 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2043 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2044 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002045
2046 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002047 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2048 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2049 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2050 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002051 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002052 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002053
Chia-I Wu63636062015-10-26 21:10:41 +08002054 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2055 vkDestroyImage(demo->device, demo->depth.image, NULL);
2056 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002057
Chia-I Wu63636062015-10-26 21:10:41 +08002058 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2059 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002060
Ian Elliotta0781972015-08-21 15:09:33 -06002061 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002062 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002063 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2064 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06002065 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002066 free(demo->buffers);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002067 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002068 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002069
Tony Barbour22e06272016-09-07 16:07:56 -06002070 if (demo->separate_present_queue) {
2071 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002072 }
Chia-I Wu63636062015-10-26 21:10:41 +08002073 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002074 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002075 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002076 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002077 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002078 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002079
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002080#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002081 if (demo->use_xlib) {
2082 XDestroyWindow(demo->display, demo->xlib_window);
2083 XCloseDisplay(demo->display);
2084 } else {
2085 xcb_destroy_window(demo->connection, demo->xcb_window);
2086 xcb_disconnect(demo->connection);
2087 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002088 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002089#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002090 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002091 xcb_disconnect(demo->connection);
2092 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002093#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2094 wl_shell_surface_destroy(demo->shell_surface);
2095 wl_surface_destroy(demo->window);
2096 wl_shell_destroy(demo->shell);
2097 wl_compositor_destroy(demo->compositor);
2098 wl_registry_destroy(demo->registry);
2099 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002100#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002101}
2102
Karl Schultze4dc75c2016-02-02 15:37:51 -07002103static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002104 uint32_t i;
2105
Mike Stroyanbb60b382015-10-28 09:46:57 -06002106 // Don't react to resize until after first initialization.
2107 if (!demo->prepared) {
2108 return;
2109 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002110 // In order to properly resize the window, we must re-create the swapchain
2111 // AND redo the command buffers, etc.
2112 //
2113 // First, perform part of the demo_cleanup() function:
2114 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002115 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002116
2117 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002118 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002119 }
2120 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002121 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002122
Chia-I Wu63636062015-10-26 21:10:41 +08002123 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2124 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2125 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2126 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2127 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002128
2129 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002130 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2131 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2132 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2133 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002134 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002135
Chia-I Wu63636062015-10-26 21:10:41 +08002136 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2137 vkDestroyImage(demo->device, demo->depth.image, NULL);
2138 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002139
Chia-I Wu63636062015-10-26 21:10:41 +08002140 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2141 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002142
2143 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002144 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002145 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2146 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002147 }
Chia-I Wu63636062015-10-26 21:10:41 +08002148 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002149 if (demo->separate_present_queue) {
2150 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2151 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002152 free(demo->buffers);
2153
Ian Elliottcf074d32015-10-16 18:02:43 -06002154 // Second, re-perform the demo_prepare() function, which will re-create the
2155 // swapchain:
2156 demo_prepare(demo);
2157}
2158
David Pinedo2bb7c932015-06-18 17:03:14 -06002159// On MS-Windows, make this a global, so it's available to WndProc()
2160struct demo demo;
2161
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002162#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002163static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002164 if (!demo->prepared)
2165 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002166
Ian Elliott639ca472015-04-16 15:23:05 -06002167 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002168 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002169 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002170 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002171 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002172 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002173}
Ian Elliott639ca472015-04-16 15:23:05 -06002174
2175// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002176LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2177 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002178 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002179 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002180 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002181 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002182 // The validation callback calls MessageBox which can generate paint
2183 // events - don't make more Vulkan calls if we got here from the
2184 // callback
2185 if (!in_callback) {
2186 demo_run(&demo);
2187 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002188 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002189 case WM_GETMINMAXINFO: // set window's minimum size
2190 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2191 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002192 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002193 // Resize the application to the new window size, except when
2194 // it was minimized. Vulkan doesn't support images or swapchains
2195 // with width=0 and height=0.
2196 if (wParam != SIZE_MINIMIZED) {
2197 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002198 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002199 demo_resize(&demo);
2200 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002201 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002202 default:
2203 break;
2204 }
2205 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2206}
2207
Karl Schultze4dc75c2016-02-02 15:37:51 -07002208static void demo_create_window(struct demo *demo) {
2209 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002210
2211 // Initialize the window class structure:
2212 win_class.cbSize = sizeof(WNDCLASSEX);
2213 win_class.style = CS_HREDRAW | CS_VREDRAW;
2214 win_class.lpfnWndProc = WndProc;
2215 win_class.cbClsExtra = 0;
2216 win_class.cbWndExtra = 0;
2217 win_class.hInstance = demo->connection; // hInstance
2218 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2219 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2220 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2221 win_class.lpszMenuName = NULL;
2222 win_class.lpszClassName = demo->name;
2223 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2224 // Register window class:
2225 if (!RegisterClassEx(&win_class)) {
2226 // It didn't work, so try to give a useful error:
2227 printf("Unexpected error trying to start the application!\n");
2228 fflush(stdout);
2229 exit(1);
2230 }
2231 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002232 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002233 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002234 demo->window = CreateWindowEx(0,
2235 demo->name, // class name
2236 demo->name, // app name
2237 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002238 WS_VISIBLE | WS_SYSMENU,
2239 100, 100, // x/y coords
2240 wr.right - wr.left, // width
2241 wr.bottom - wr.top, // height
2242 NULL, // handle to parent
2243 NULL, // handle to menu
2244 demo->connection, // hInstance
2245 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002246 if (!demo->window) {
2247 // It didn't work, so try to give a useful error:
2248 printf("Cannot create a window in which to draw!\n");
2249 fflush(stdout);
2250 exit(1);
2251 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002252 // Window client area size must be at least 1 pixel high, to prevent crash.
2253 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2254 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002255}
Tony Barbour8295ac42016-08-08 11:04:17 -06002256#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002257static void demo_create_xlib_window(struct demo *demo) {
2258
2259 demo->display = XOpenDisplay(NULL);
2260 long visualMask = VisualScreenMask;
2261 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002262 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002263 vInfoTemplate.screen = DefaultScreen(demo->display);
2264 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2265 &vInfoTemplate, &numberOfVisuals);
2266
2267 Colormap colormap = XCreateColormap(
2268 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2269 visualInfo->visual, AllocNone);
2270
Rene Lindsay11612722016-06-13 17:20:39 -06002271 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002272 windowAttributes.colormap = colormap;
2273 windowAttributes.background_pixel = 0xFFFFFFFF;
2274 windowAttributes.border_pixel = 0;
2275 windowAttributes.event_mask =
2276 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2277
2278 demo->xlib_window = XCreateWindow(
2279 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2280 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2281 visualInfo->visual,
2282 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2283
2284 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2285 XMapWindow(demo->display, demo->xlib_window);
2286 XFlush(demo->display);
2287 demo->xlib_wm_delete_window =
2288 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2289}
2290static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2291 switch(event->type) {
2292 case ClientMessage:
2293 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2294 demo->quit = true;
2295 break;
2296 case KeyPress:
2297 switch (event->xkey.keycode) {
2298 case 0x9: // Escape
2299 demo->quit = true;
2300 break;
2301 case 0x71: // left arrow key
2302 demo->spin_angle += demo->spin_increment;
2303 break;
2304 case 0x72: // right arrow key
2305 demo->spin_angle -= demo->spin_increment;
2306 break;
2307 case 0x41:
2308 demo->pause = !demo->pause;
2309 break;
2310 }
2311 break;
2312 case ConfigureNotify:
2313 if ((demo->width != event->xconfigure.width) ||
2314 (demo->height != event->xconfigure.height)) {
2315 demo->width = event->xconfigure.width;
2316 demo->height = event->xconfigure.height;
2317 demo_resize(demo);
2318 }
2319 break;
2320 default:
2321 break;
2322 }
2323
2324}
2325
2326static void demo_run_xlib(struct demo *demo) {
2327
2328 while (!demo->quit) {
2329 XEvent event;
2330
2331 if (demo->pause) {
2332 XNextEvent(demo->display, &event);
2333 demo_handle_xlib_event(demo, &event);
2334 } else {
2335 while (XPending(demo->display) > 0) {
2336 XNextEvent(demo->display, &event);
2337 demo_handle_xlib_event(demo, &event);
2338 }
2339 }
2340
Tony Barbour2593b182016-04-19 10:57:58 -06002341 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002342 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002343 demo->curFrame++;
2344 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2345 demo->quit = true;
2346 }
2347}
Tony Barbour8295ac42016-08-08 11:04:17 -06002348#endif // VK_USE_PLATFORM_XLIB_KHR
2349#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002350static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002351 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002352 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002353 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002354 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002355 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002356 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002357 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002358 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2359 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002360 demo->quit = true;
2361 }
2362 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002363 case XCB_KEY_RELEASE: {
2364 const xcb_key_release_event_t *key =
2365 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002366
Karl Schultze4dc75c2016-02-02 15:37:51 -07002367 switch (key->detail) {
2368 case 0x9: // Escape
2369 demo->quit = true;
2370 break;
2371 case 0x71: // left arrow key
2372 demo->spin_angle += demo->spin_increment;
2373 break;
2374 case 0x72: // right arrow key
2375 demo->spin_angle -= demo->spin_increment;
2376 break;
2377 case 0x41:
2378 demo->pause = !demo->pause;
2379 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002380 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002381 } break;
2382 case XCB_CONFIGURE_NOTIFY: {
2383 const xcb_configure_notify_event_t *cfg =
2384 (const xcb_configure_notify_event_t *)event;
2385 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002386 demo->width = cfg->width;
2387 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002388 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002389 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002390 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002391 default:
2392 break;
2393 }
2394}
2395
Tony Barbour2593b182016-04-19 10:57:58 -06002396static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002397 xcb_flush(demo->connection);
2398
2399 while (!demo->quit) {
2400 xcb_generic_event_t *event;
2401
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002402 if (demo->pause) {
2403 event = xcb_wait_for_event(demo->connection);
2404 } else {
2405 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002406 while(event) {
2407 demo_handle_xcb_event(demo, event);
2408 free(event);
2409 event = xcb_poll_for_event(demo->connection);
2410 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002411 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002412
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002413 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002414 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002415 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002416 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002417 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002418 }
2419}
2420
Tony Barbour2593b182016-04-19 10:57:58 -06002421static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002422 uint32_t value_mask, value_list[32];
2423
Tony Barbour2593b182016-04-19 10:57:58 -06002424 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002425
2426 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2427 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002428 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002429 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002430
Tony Barbour2593b182016-04-19 10:57:58 -06002431 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002432 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2433 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2434 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002435
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002436 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002437 xcb_intern_atom_cookie_t cookie =
2438 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2439 xcb_intern_atom_reply_t *reply =
2440 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002441
Karl Schultze4dc75c2016-02-02 15:37:51 -07002442 xcb_intern_atom_cookie_t cookie2 =
2443 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2444 demo->atom_wm_delete_window =
2445 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002446
Tony Barbour2593b182016-04-19 10:57:58 -06002447 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002448 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002449 &(*demo->atom_wm_delete_window).atom);
2450 free(reply);
2451
Tony Barbour2593b182016-04-19 10:57:58 -06002452 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002453
Karl Schultze4dc75c2016-02-02 15:37:51 -07002454 // Force the x/y coordinates to 100,100 results are identical in consecutive
2455 // runs
2456 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002457 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002458 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002459}
Tony Barbour6b131662016-08-25 13:14:45 -06002460// VK_USE_PLATFORM_XCB_KHR
2461#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002462static void demo_run(struct demo *demo) {
2463 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002464 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002465 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002466 demo->curFrame++;
2467 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2468 demo->quit = true;
2469 }
2470}
2471
2472static void handle_ping(void *data UNUSED,
2473 struct wl_shell_surface *shell_surface,
2474 uint32_t serial) {
2475 wl_shell_surface_pong(shell_surface, serial);
2476}
2477
2478static void handle_configure(void *data UNUSED,
2479 struct wl_shell_surface *shell_surface UNUSED,
2480 uint32_t edges UNUSED, int32_t width UNUSED,
2481 int32_t height UNUSED) {}
2482
2483static void handle_popup_done(void *data UNUSED,
2484 struct wl_shell_surface *shell_surface UNUSED) {}
2485
2486static const struct wl_shell_surface_listener shell_surface_listener = {
2487 handle_ping, handle_configure, handle_popup_done};
2488
2489static void demo_create_window(struct demo *demo) {
2490 demo->window = wl_compositor_create_surface(demo->compositor);
2491 if (!demo->window) {
2492 printf("Can not create wayland_surface from compositor!\n");
2493 fflush(stdout);
2494 exit(1);
2495 }
2496
2497 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2498 if (!demo->shell_surface) {
2499 printf("Can not get shell_surface from wayland_surface!\n");
2500 fflush(stdout);
2501 exit(1);
2502 }
2503 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2504 demo);
2505 wl_shell_surface_set_toplevel(demo->shell_surface);
2506 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2507}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002508#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2509static void demo_run(struct demo *demo) {
2510 if (!demo->prepared)
2511 return;
2512
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002513 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002514 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002515 demo->curFrame++;
2516}
2517#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002518
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002519/*
2520 * Return 1 (true) if all layer names specified in check_names
2521 * can be found in given layer properties.
2522 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002523static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002524 uint32_t layer_count,
2525 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002526 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002527 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002528 for (uint32_t j = 0; j < layer_count; j++) {
2529 if (!strcmp(check_names[i], layers[j].layerName)) {
2530 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002531 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002532 }
2533 }
2534 if (!found) {
2535 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2536 return 0;
2537 }
2538 }
2539 return 1;
2540}
2541
Karl Schultze4dc75c2016-02-02 15:37:51 -07002542static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002543 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002544 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002545 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002546 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002547 char **instance_validation_layers = NULL;
2548 demo->enabled_extension_count = 0;
2549 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002550
Karl Schultz7c50ad62016-04-01 12:07:03 -06002551 char *instance_validation_layers_alt1[] = {
2552 "VK_LAYER_LUNARG_standard_validation"
2553 };
2554
2555 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002556 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2557 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2558 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2559 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002560 };
2561
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002562 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002563 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002564 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002565
Karl Schultz7c50ad62016-04-01 12:07:03 -06002566 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002567 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002568
Karl Schultz7c50ad62016-04-01 12:07:03 -06002569 instance_validation_layers = instance_validation_layers_alt1;
2570 if (instance_layer_count > 0) {
2571 VkLayerProperties *instance_layers =
2572 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2573 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2574 instance_layers);
2575 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002576
Karl Schultz7c50ad62016-04-01 12:07:03 -06002577
2578 validation_found = demo_check_layers(
2579 ARRAY_SIZE(instance_validation_layers_alt1),
2580 instance_validation_layers, instance_layer_count,
2581 instance_layers);
2582 if (validation_found) {
2583 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002584 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2585 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002586 } else {
2587 // use alternative set of validation layers
2588 instance_validation_layers = instance_validation_layers_alt2;
2589 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2590 validation_found = demo_check_layers(
2591 ARRAY_SIZE(instance_validation_layers_alt2),
2592 instance_validation_layers, instance_layer_count,
2593 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002594 validation_layer_count =
2595 ARRAY_SIZE(instance_validation_layers_alt2);
2596 for (uint32_t i = 0; i < validation_layer_count; i++) {
2597 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002598 }
2599 }
2600 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002601 }
Dustin Graves7c287352016-01-27 13:48:06 -07002602
Karl Schultz7c50ad62016-04-01 12:07:03 -06002603 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002604 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002605 "required validation layer.\n\n"
2606 "Please look at the Getting Started guide for additional "
2607 "information.\n",
2608 "vkCreateInstance Failure");
2609 }
Dustin Graves7c287352016-01-27 13:48:06 -07002610 }
2611
2612 /* Look for instance extensions */
2613 VkBool32 surfaceExtFound = 0;
2614 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002615#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002616 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002617#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002618 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002619
Karl Schultze4dc75c2016-02-02 15:37:51 -07002620 err = vkEnumerateInstanceExtensionProperties(
2621 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002622 assert(!err);
2623
Dustin Graves7c287352016-01-27 13:48:06 -07002624 if (instance_extension_count > 0) {
2625 VkExtensionProperties *instance_extensions =
2626 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2627 err = vkEnumerateInstanceExtensionProperties(
2628 NULL, &instance_extension_count, instance_extensions);
2629 assert(!err);
2630 for (uint32_t i = 0; i < instance_extension_count; i++) {
2631 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2632 instance_extensions[i].extensionName)) {
2633 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002634 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002635 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002636 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002637#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002638 if (!strcmp(VK_KHR_WIN32_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_WIN32_SURFACE_EXTENSION_NAME;
2643 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002644#endif
2645#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002646 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2647 instance_extensions[i].extensionName)) {
2648 platformSurfaceExtFound = 1;
2649 xlibSurfaceExtFound = 1;
2650 demo->extension_names[demo->enabled_extension_count++] =
2651 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2652 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002653#endif
2654#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002655 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2656 instance_extensions[i].extensionName)) {
2657 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002658 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002659 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2660 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002661#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002662#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2663 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2664 instance_extensions[i].extensionName)) {
2665 platformSurfaceExtFound = 1;
2666 demo->extension_names[demo->enabled_extension_count++] =
2667 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2668 }
2669#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002670#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002671 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2672 instance_extensions[i].extensionName)) {
2673 platformSurfaceExtFound = 1;
2674 demo->extension_names[demo->enabled_extension_count++] =
2675 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2676 }
2677#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002678 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2679 instance_extensions[i].extensionName)) {
2680 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002681 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002682 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2683 }
2684 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002685 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002686 }
Dustin Graves7c287352016-01-27 13:48:06 -07002687
2688 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002689 }
Dustin Graves7c287352016-01-27 13:48:06 -07002690
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002691 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002692 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2693 "the " VK_KHR_SURFACE_EXTENSION_NAME
2694 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002695 "Vulkan installable client driver (ICD) installed?\nPlease "
2696 "look at the Getting Started guide for additional "
2697 "information.\n",
2698 "vkCreateInstance Failure");
2699 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002700 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002701#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002702 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2703 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2704 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002705 "Vulkan installable client driver (ICD) installed?\nPlease "
2706 "look at the Getting Started guide for additional "
2707 "information.\n",
2708 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002709#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002710 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2711 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2712 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002713 "Vulkan installable client driver (ICD) installed?\nPlease "
2714 "look at the Getting Started guide for additional "
2715 "information.\n",
2716 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002717#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2718 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2719 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2720 " extension.\n\nDo you have a compatible "
2721 "Vulkan installable client driver (ICD) installed?\nPlease "
2722 "look at the Getting Started guide for additional "
2723 "information.\n",
2724 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002725#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2726 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2727 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2728 " extension.\n\nDo you have a compatible "
2729 "Vulkan installable client driver (ICD) installed?\nPlease "
2730 "look at the Getting Started guide for additional "
2731 "information.\n",
2732 "vkCreateInstance Failure");
2733#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002734 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002735#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002736 if (demo->use_xlib && !xlibSurfaceExtFound) {
2737 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2738 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2739 " extension.\n\nDo you have a compatible "
2740 "Vulkan installable client driver (ICD) installed?\nPlease "
2741 "look at the Getting Started guide for additional "
2742 "information.\n",
2743 "vkCreateInstance Failure");
2744 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002745#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002746 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002747 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002748 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002749 .pApplicationName = APP_SHORT_NAME,
2750 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002751 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002752 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002753 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002754 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002755 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002756 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002757 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002758 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002759 .enabledLayerCount = demo->enabled_layer_count,
2760 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2761 .enabledExtensionCount = demo->enabled_extension_count,
2762 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002763 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002764
2765 /*
2766 * This is info for a temp callback to use during CreateInstance.
2767 * After the instance is created, we use the instance-based
2768 * function to register the final callback.
2769 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002770 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002771 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002772 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2773 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002774 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002775 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002776 dbgCreateInfo.flags =
2777 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2778 inst_info.pNext = &dbgCreateInfo;
2779 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002780
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002781 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002782
Chia-I Wu63636062015-10-26 21:10:41 +08002783 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002784 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2785 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002786 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002787 "additional information.\n",
2788 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002789 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002790 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002791 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002792 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002793 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002794 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2795 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002796 "the Getting Started guide for additional information.\n",
2797 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002798 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002799
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002800 /* Make initial call to query gpu_count, then second call for gpu info*/
2801 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2802 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002803
2804 if (gpu_count > 0) {
2805 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2806 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2807 assert(!err);
2808 /* For cube demo we just grab the first physical device */
2809 demo->gpu = physical_devices[0];
2810 free(physical_devices);
2811 } else {
2812 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2813 "Do you have a compatible Vulkan installable client driver (ICD) "
2814 "installed?\nPlease look at the Getting Started guide for "
2815 "additional information.\n",
2816 "vkEnumeratePhysicalDevices Failure");
2817 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002818
Dustin Graves7c287352016-01-27 13:48:06 -07002819 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002820 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002821 VkBool32 swapchainExtFound = 0;
2822 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002823 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002824
Karl Schultze4dc75c2016-02-02 15:37:51 -07002825 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2826 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002827 assert(!err);
2828
Dustin Graves7c287352016-01-27 13:48:06 -07002829 if (device_extension_count > 0) {
2830 VkExtensionProperties *device_extensions =
2831 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2832 err = vkEnumerateDeviceExtensionProperties(
2833 demo->gpu, NULL, &device_extension_count, device_extensions);
2834 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002835
Dustin Graves7c287352016-01-27 13:48:06 -07002836 for (uint32_t i = 0; i < device_extension_count; i++) {
2837 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2838 device_extensions[i].extensionName)) {
2839 swapchainExtFound = 1;
2840 demo->extension_names[demo->enabled_extension_count++] =
2841 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2842 }
2843 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002844 }
Dustin Graves7c287352016-01-27 13:48:06 -07002845
2846 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002847 }
Dustin Graves7c287352016-01-27 13:48:06 -07002848
Tony Barbourcc69f452015-10-08 13:59:42 -06002849 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002850 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2851 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2852 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002853 "Vulkan installable client driver (ICD) installed?\nPlease "
2854 "look at the Getting Started guide for additional "
2855 "information.\n",
2856 "vkCreateInstance Failure");
2857 }
2858
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002859 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002860 demo->CreateDebugReportCallback =
2861 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2862 demo->inst, "vkCreateDebugReportCallbackEXT");
2863 demo->DestroyDebugReportCallback =
2864 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2865 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002866 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002867 ERR_EXIT(
2868 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2869 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002870 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002871 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002872 ERR_EXIT(
2873 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2874 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002875 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002876 demo->DebugReportMessage =
2877 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2878 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002879 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002880 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002881 "vkGetProcAddr Failure");
2882 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002883
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002884 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002885 PFN_vkDebugReportCallbackEXT callback;
2886 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002887 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002888 dbgCreateInfo.pNext = NULL;
2889 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002890 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002891 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002892 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002893 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2894 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002895 switch (err) {
2896 case VK_SUCCESS:
2897 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002898 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002899 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2900 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002901 break;
2902 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002903 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2904 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002905 break;
2906 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002907 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002908 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002909
2910 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002911 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2912 &demo->queue_family_count, NULL);
2913 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002914
Karl Schultze4dc75c2016-02-02 15:37:51 -07002915 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002916 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2917 vkGetPhysicalDeviceQueueFamilyProperties(
2918 demo->gpu, &demo->queue_family_count, demo->queue_props);
2919
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002920 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002921 // If app has specific feature requirements it should check supported
2922 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002923 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002924 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002925
Tony Barbourda2bad52016-01-22 14:36:40 -07002926 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2927 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2928 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2929 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2930 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2931}
2932
Karl Schultze4dc75c2016-02-02 15:37:51 -07002933static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002934 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002935 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06002936 VkDeviceQueueCreateInfo queues[2];
2937 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2938 queues[0].pNext = NULL;
2939 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
2940 queues[0].queueCount = 1;
2941 queues[0].pQueuePriorities = queue_priorities;
2942 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002943
2944 VkDeviceCreateInfo device = {
2945 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2946 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002947 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06002948 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002949 .enabledLayerCount = 0,
2950 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002951 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002952 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2953 .pEnabledFeatures =
2954 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002955 };
Tony Barbour22e06272016-09-07 16:07:56 -06002956 if (demo->separate_present_queue) {
2957 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2958 queues[1].pNext = NULL;
2959 queues[1].queueFamilyIndex = demo->present_queue_family_index;
2960 queues[1].queueCount = 1;
2961 queues[1].pQueuePriorities = queue_priorities;
2962 queues[1].flags = 0;
2963 device.queueCreateInfoCount = 2;
2964 }
Chia-I Wu63636062015-10-26 21:10:41 +08002965 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002966 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002967}
2968
Karl Schultze4dc75c2016-02-02 15:37:51 -07002969static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002970 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002971 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002972
Karl Schultze4dc75c2016-02-02 15:37:51 -07002973// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002974#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002975 VkWin32SurfaceCreateInfoKHR createInfo;
2976 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2977 createInfo.pNext = NULL;
2978 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002979 createInfo.hinstance = demo->connection;
2980 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002981
Karl Schultze4dc75c2016-02-02 15:37:51 -07002982 err =
2983 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002984#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2985 VkWaylandSurfaceCreateInfoKHR createInfo;
2986 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2987 createInfo.pNext = NULL;
2988 createInfo.flags = 0;
2989 createInfo.display = demo->display;
2990 createInfo.surface = demo->window;
2991
2992 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2993 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002994#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2995 VkAndroidSurfaceCreateInfoKHR createInfo;
2996 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2997 createInfo.pNext = NULL;
2998 createInfo.flags = 0;
2999 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003000
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003001 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3002#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003003 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003004#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003005 VkXlibSurfaceCreateInfoKHR createInfo;
3006 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3007 createInfo.pNext = NULL;
3008 createInfo.flags = 0;
3009 createInfo.dpy = demo->display;
3010 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003011
Tony Barbour2593b182016-04-19 10:57:58 -06003012 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
3013 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003014#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003015 }
3016 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003017#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003018 VkXcbSurfaceCreateInfoKHR createInfo;
3019 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3020 createInfo.pNext = NULL;
3021 createInfo.flags = 0;
3022 createInfo.connection = demo->connection;
3023 createInfo.window = demo->xcb_window;
3024
3025 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003026#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003027 }
Tony Barbour2593b182016-04-19 10:57:58 -06003028 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003029
Tony Barbourcc69f452015-10-08 13:59:42 -06003030 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003031 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003032 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
3033 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003034 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003035 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003036 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003037
3038 // Search for a graphics and a present queue in the array of queue
3039 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003040 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3041 uint32_t presentQueueFamilyIndex = UINT32_MAX;
3042 for (i = 0; i < demo->queue_family_count; i++) {
3043 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3044 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3045 graphicsQueueFamilyIndex = i;
3046 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003047
Tony Barbourab2a0362016-09-06 11:40:12 -06003048 if (supportsPresent[i] == VK_TRUE) {
3049 graphicsQueueFamilyIndex = i;
3050 presentQueueFamilyIndex = i;
3051 break;
3052 }
3053 }
3054 }
3055
3056 if (presentQueueFamilyIndex == UINT32_MAX) {
3057 // If didn't find a queue that supports both graphics and present, then
3058 // find a separate present queue.
Mark Lobodzinski5abdfcc2016-10-03 16:00:40 -06003059 for (i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003060 if (supportsPresent[i] == VK_TRUE) {
3061 presentQueueFamilyIndex = i;
3062 break;
3063 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003064 }
3065 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003066
3067 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003068 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3069 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003070 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003071 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003072 }
3073
Tony Barbourab2a0362016-09-06 11:40:12 -06003074 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3075 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003076 demo->separate_present_queue =
3077 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003078 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003079
Tony Barbourda2bad52016-01-22 14:36:40 -07003080 demo_create_device(demo);
3081
3082 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3083 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3084 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3085 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3086 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3087
Tony Barboura2a67812016-07-18 13:21:06 -06003088 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3089 &demo->graphics_queue);
3090
Tony Barbour22e06272016-09-07 16:07:56 -06003091 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003092 demo->present_queue = demo->graphics_queue;
3093 } else {
3094 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3095 &demo->present_queue);
3096 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003097
Ian Elliottaaae5352015-07-06 14:27:58 -06003098 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003099 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003100 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003101 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003102 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003103 VkSurfaceFormatKHR *surfFormats =
3104 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003105 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003106 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003107 assert(!err);
3108 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3109 // the surface has no preferred format. Otherwise, at least one
3110 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003111 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003112 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003113 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003114 assert(formatCount >= 1);
3115 demo->format = surfFormats[0].format;
3116 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003117 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003118
David Pinedo2bb7c932015-06-18 17:03:14 -06003119 demo->quit = false;
3120 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003121
Tony Barbource7524e2016-07-28 12:01:45 -06003122 // Create semaphores to synchronize acquiring presentable buffers before
3123 // rendering and waiting for drawing to be complete before presenting
3124 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3125 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3126 .pNext = NULL,
3127 .flags = 0,
3128 };
Tony Barbource7524e2016-07-28 12:01:45 -06003129
Tony Barbour66a56c32016-09-21 13:10:56 -06003130 // Create fences that we can use to throttle if we get too far
3131 // ahead of the image presents
3132 VkFenceCreateInfo fence_ci = {
3133 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3134 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003135 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003136 };
3137 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3138 vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003139 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003140 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003141 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003142
3143 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3144 &demo->draw_complete_semaphores[i]);
3145 assert(!err);
3146
3147 if (demo->separate_present_queue) {
3148 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3149 &demo->image_ownership_semaphores[i]);
3150 assert(!err);
3151 }
Tony Barbour22e06272016-09-07 16:07:56 -06003152 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003153 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003154
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003155 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003156 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003157}
3158
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003159#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3160static void registry_handle_global(void *data, struct wl_registry *registry,
3161 uint32_t name, const char *interface,
3162 uint32_t version UNUSED) {
3163 struct demo *demo = data;
3164 if (strcmp(interface, "wl_compositor") == 0) {
3165 demo->compositor =
3166 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3167 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3168 * tp xdg_shell */
3169 } else if (strcmp(interface, "wl_shell") == 0) {
3170 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3171 }
3172}
3173
3174static void registry_handle_global_remove(void *data UNUSED,
3175 struct wl_registry *registry UNUSED,
3176 uint32_t name UNUSED) {}
3177
3178static const struct wl_registry_listener registry_listener = {
3179 registry_handle_global, registry_handle_global_remove};
3180#endif
3181
Karl Schultze4dc75c2016-02-02 15:37:51 -07003182static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003183#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003184 const xcb_setup_t *setup;
3185 xcb_screen_iterator_t iter;
3186 int scr;
3187
3188 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003189 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003190 printf("Cannot find a compatible Vulkan installable client driver "
3191 "(ICD).\nExiting ...\n");
3192 fflush(stdout);
3193 exit(1);
3194 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003195
3196 setup = xcb_get_setup(demo->connection);
3197 iter = xcb_setup_roots_iterator(setup);
3198 while (scr-- > 0)
3199 xcb_screen_next(&iter);
3200
3201 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003202#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3203 demo->display = wl_display_connect(NULL);
3204
3205 if (demo->display == NULL) {
3206 printf("Cannot find a compatible Vulkan installable client driver "
3207 "(ICD).\nExiting ...\n");
3208 fflush(stdout);
3209 exit(1);
3210 }
3211
3212 demo->registry = wl_display_get_registry(demo->display);
3213 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3214 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003215#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003216}
3217
Karl Schultze4dc75c2016-02-02 15:37:51 -07003218static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003219 vec3 eye = {0.0f, 3.0f, 5.0f};
3220 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003221 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003222
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003223 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003224 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003225
Piers Daniell735ee532015-02-23 16:23:13 -07003226 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003227 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003228 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003229 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003230 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003231 if (strcmp(argv[i], "--break") == 0) {
3232 demo->use_break = true;
3233 continue;
3234 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003235 if (strcmp(argv[i], "--validate") == 0) {
3236 demo->validate = true;
3237 continue;
3238 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003239#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003240 if (strcmp(argv[i], "--xlib") == 0) {
3241 demo->use_xlib = true;
3242 continue;
3243 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003244#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003245 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3246 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3247 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003248 i++;
3249 continue;
3250 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003251 if (strcmp(argv[i], "--suppress_popups") == 0) {
3252 demo->suppress_popups = true;
3253 continue;
3254 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003255
Cody Northropaf28a532016-09-27 10:50:57 -06003256#if defined(ANDROID)
3257 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3258#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003259 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003260#if defined(VK_USE_PLATFORM_XLIB_KHR)
3261 "[--xlib] "
3262#endif
lenny-lunargfbe22392016-06-06 11:07:53 -06003263 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003264 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003265 fflush(stderr);
3266 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003267#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003268 }
3269
Tony Barbour2593b182016-04-19 10:57:58 -06003270 if (!demo->use_xlib)
3271 demo_init_connection(demo);
3272
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003273 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003274
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003275 demo->width = 500;
3276 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003277
Tony Barbour66a56c32016-09-21 13:10:56 -06003278 demo->spin_angle = 4.0f;
3279 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003280 demo->pause = false;
3281
Karl Schultze4dc75c2016-02-02 15:37:51 -07003282 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3283 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003284 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3285 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003286
3287 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003288}
3289
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003290#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003291// Include header required for parsing the command line options.
3292#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003293
Karl Schultze4dc75c2016-02-02 15:37:51 -07003294int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3295 int nCmdShow) {
3296 MSG msg; // message
3297 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003298 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003299 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003300
Karl Schultze4dc75c2016-02-02 15:37:51 -07003301 // Use the CommandLine functions to get the command line arguments.
3302 // Unfortunately, Microsoft outputs
3303 // this information as wide characters for Unicode, and we simply want the
3304 // Ascii version to be compatible
3305 // with the non-Windows side. So, we have to convert the information to
3306 // Ascii character strings.
3307 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3308 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003309 argc = 0;
3310 }
3311
Karl Schultze4dc75c2016-02-02 15:37:51 -07003312 if (argc > 0) {
3313 argv = (char **)malloc(sizeof(char *) * argc);
3314 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003315 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003316 } else {
3317 for (int iii = 0; iii < argc; iii++) {
3318 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003319 size_t numConverted = 0;
3320
Karl Schultze4dc75c2016-02-02 15:37:51 -07003321 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3322 if (argv[iii] != NULL) {
3323 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3324 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003325 }
3326 }
3327 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003328 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003329 argv = NULL;
3330 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003331
3332 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003333
3334 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003335 if (argc > 0 && argv != NULL) {
3336 for (int iii = 0; iii < argc; iii++) {
3337 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003338 free(argv[iii]);
3339 }
3340 }
3341 free(argv);
3342 }
3343
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003344 demo.connection = hInstance;
3345 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003346 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003347 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003348
3349 demo_prepare(&demo);
3350
Karl Schultze4dc75c2016-02-02 15:37:51 -07003351 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003352
3353 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003354 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003355 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003356 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003357 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003358 done = true; // if found, quit app
3359 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003360 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003361 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003362 DispatchMessage(&msg);
3363 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003364 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003365 }
3366
3367 demo_cleanup(&demo);
3368
Karl Schultze4dc75c2016-02-02 15:37:51 -07003369 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003370}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003371#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3372#include <android/log.h>
3373#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003374#include "android_util.h"
3375
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003376static bool initialized = false;
3377static bool active = false;
3378struct demo demo;
3379
3380static int32_t processInput(struct android_app* app, AInputEvent* event) {
3381 return 0;
3382}
3383
3384static void processCommand(struct android_app* app, int32_t cmd) {
3385 switch(cmd) {
3386 case APP_CMD_INIT_WINDOW: {
3387 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003388 // We're getting a new window. If the app is starting up, we
3389 // need to initialize. If the app has already been
3390 // initialized, that means that we lost our previous window,
3391 // which means that we have a lot of work to do. At a minimum,
3392 // we need to destroy the swapchain and surface associated with
3393 // the old window, and create a new surface and swapchain.
3394 // However, since there are a lot of other objects/state that
3395 // is tied to the swapchain, it's easiest to simply cleanup and
3396 // start over (i.e. use a brute-force approach of re-starting
3397 // the app)
3398 if (demo.prepared) {
3399 demo_cleanup(&demo);
3400 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003401
3402 // Parse Intents into argc, argv
3403 // Use the following key to send arguments, i.e.
3404 // --es args "--validate"
3405 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003406 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003407 int argc = 0;
3408 char** argv = get_args(app, key, appTag, &argc);
3409
3410 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3411 for (int i = 0; i < argc; i++)
3412 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3413
3414 demo_init(&demo, argc, argv);
3415
3416 // Free the argv malloc'd by get_args
3417 for (int i = 0; i < argc; i++)
3418 free(argv[i]);
3419
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003420 demo.window = (void*)app->window;
3421 demo_init_vk_swapchain(&demo);
3422 demo_prepare(&demo);
3423 initialized = true;
3424 }
3425 break;
3426 }
3427 case APP_CMD_GAINED_FOCUS: {
3428 active = true;
3429 break;
3430 }
3431 case APP_CMD_LOST_FOCUS: {
3432 active = false;
3433 break;
3434 }
3435 }
3436}
3437
3438void android_main(struct android_app *app)
3439{
3440 app_dummy();
3441
Cody Northrop8707a6e2016-04-26 19:59:19 -06003442#ifdef ANDROID
3443 int vulkanSupport = InitVulkan();
3444 if (vulkanSupport == 0)
3445 return;
3446#endif
3447
Ian Elliottf05d5d12016-05-17 12:03:35 -06003448 demo.prepared = false;
3449
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003450 app->onAppCmd = processCommand;
3451 app->onInputEvent = processInput;
3452
3453 while(1) {
3454 int events;
3455 struct android_poll_source* source;
3456 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3457 if (source) {
3458 source->process(app, source);
3459 }
3460
3461 if (app->destroyRequested != 0) {
3462 demo_cleanup(&demo);
3463 return;
3464 }
3465 }
3466 if (initialized && active) {
3467 demo_run(&demo);
3468 }
3469 }
3470
3471}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003472#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003473int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003474 struct demo demo;
3475
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003476 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003477#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003478 if (demo.use_xlib)
3479 demo_create_xlib_window(&demo);
3480 else
3481 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003482#elif defined(VK_USE_PLATFORM_XCB_KHR)
3483 demo_create_xcb_window(&demo);
3484#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3485 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003486#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3487 demo_create_window(&demo);
3488#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003489
Tony Barbourcc69f452015-10-08 13:59:42 -06003490 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003491
3492 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003493
Tony Barbour8295ac42016-08-08 11:04:17 -06003494#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003495 if (demo.use_xlib)
3496 demo_run_xlib(&demo);
3497 else
3498 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003499#elif defined(VK_USE_PLATFORM_XCB_KHR)
3500 demo_run_xcb(&demo);
3501#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3502 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003503#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3504 demo_run(&demo);
3505#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003506
3507 demo_cleanup(&demo);
3508
Karl Schultz0218c002016-03-22 17:06:13 -06003509 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003510}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003511#endif