blob: fb8b7c1ff056471f3980078dd80e519be771353d [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;
Tony Barbour78d6b572016-11-14 14:46:33 -0700296#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600297 Window xlib_window;
298 Atom xlib_wm_delete_window;
Tony Barbour78d6b572016-11-14 14:46:33 -0700299#endif
300#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600301 xcb_connection_t *connection;
302 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600303 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800304 xcb_intern_atom_reply_t *atom_wm_delete_window;
Tony Barbour78d6b572016-11-14 14:46:33 -0700305#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900306#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
307 struct wl_display *display;
308 struct wl_registry *registry;
309 struct wl_compositor *compositor;
310 struct wl_surface *window;
311 struct wl_shell *shell;
312 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500313#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
314 ANativeWindow* window;
315#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700316 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600317 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700318 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600319 bool use_xlib;
Tony Barbour22e06272016-09-07 16:07:56 -0600320 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600323 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600325 VkQueue graphics_queue;
326 VkQueue present_queue;
327 uint32_t graphics_queue_family_index;
328 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600329 VkSemaphore image_acquired_semaphores[FRAME_LAG];
330 VkSemaphore draw_complete_semaphores[FRAME_LAG];
331 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600332 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600333 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600334 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
Tony Barbourda2bad52016-01-22 14:36:40 -0700336 uint32_t enabled_extension_count;
337 uint32_t enabled_layer_count;
338 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600339 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700340
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600342 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600343 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344
Karl Schultze4dc75c2016-02-02 15:37:51 -0700345 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
346 fpGetPhysicalDeviceSurfaceSupportKHR;
347 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
348 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
349 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
350 fpGetPhysicalDeviceSurfaceFormatsKHR;
351 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
352 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600353 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
354 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
355 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
356 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
357 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600358 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600359 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600360 SwapchainBuffers *buffers;
Tony Barbour291d57b2016-10-21 14:47:07 -0600361 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600362 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600363 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800365 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600366 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367
368 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600371 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800372 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500373 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600374 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375 } depth;
376
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600377 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600378 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379
380 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600381 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800382 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500383 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600384 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600385 } uniform_data;
386
Karl Schultze4dc75c2016-02-02 15:37:51 -0700387 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500388 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600389 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600390 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800391 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600393
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600394 mat4x4 projection_matrix;
395 mat4x4 view_matrix;
396 mat4x4 model_matrix;
397
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600398 float spin_angle;
399 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600400 bool pause;
401
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600402 VkShaderModule vert_shader_module;
403 VkShaderModule frag_shader_module;
404
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 VkDescriptorPool desc_pool;
406 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800407
Tony Barbourd8e504f2015-10-08 14:26:24 -0600408 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800409
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600410 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600411 int32_t curFrame;
412 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600413 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600414 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600415 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700416 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
417 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
418 VkDebugReportCallbackEXT msg_callback;
419 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600420
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600421 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600422 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600423};
424
lenny-lunargfbe22392016-06-06 11:07:53 -0600425VKAPI_ATTR VkBool32 VKAPI_CALL
426dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
427 uint64_t srcObject, size_t location, int32_t msgCode,
428 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600429
430 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600431 char *message = (char *)malloc(strlen(pMsg) + 100);
432
433 assert(message);
434
Cody Northropaf28a532016-09-27 10:50:57 -0600435 // We know we're submitting queues without fences, ignore this
436 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer"))
437 return false;
438
439 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
440 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600441 validation_error = 1;
442 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600443 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
444 validation_error = 1;
445 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600446 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600447 validation_error = 1;
448 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
449 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
450 validation_error = 1;
451 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
452 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600453 validation_error = 1;
454 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600455 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600456 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600457 }
458
459#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600460
Tony Barbour602ca4f2016-09-12 14:02:43 -0600461 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600462 struct demo *demo = (struct demo*) pUserData;
463 if (!demo->suppress_popups)
464 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600465 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600466
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600467#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600468
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600469 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600470 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600471 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600472 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600473 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600474 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600475 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600476 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600477 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600478 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600479 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600480 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600481 }
Cody Northropaf28a532016-09-27 10:50:57 -0600482
lenny-lunargfbe22392016-06-06 11:07:53 -0600483#else
Cody Northropaf28a532016-09-27 10:50:57 -0600484
lenny-lunargfbe22392016-06-06 11:07:53 -0600485 printf("%s\n", message);
486 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600487
lenny-lunargfbe22392016-06-06 11:07:53 -0600488#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600489
lenny-lunargfbe22392016-06-06 11:07:53 -0600490 free(message);
491
Cody Northropaf28a532016-09-27 10:50:57 -0600492 //clang-format on
493
lenny-lunargfbe22392016-06-06 11:07:53 -0600494 /*
495 * false indicates that layer should not bail-out of an
496 * API call that had validation failures. This may mean that the
497 * app dies inside the driver due to invalid parameter(s).
498 * That's what would happen without validation layers, so we'll
499 * keep that behavior here.
500 */
501 return false;
502}
503
Ian Elliottcf074d32015-10-16 18:02:43 -0600504// Forward declaration:
505static void demo_resize(struct demo *demo);
506
Karl Schultze4dc75c2016-02-02 15:37:51 -0700507static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
508 VkFlags requirements_mask,
509 uint32_t *typeIndex) {
510 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600511 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700512 if ((typeBits & 1) == 1) {
513 // Type is available, does it match user properties?
514 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
515 requirements_mask) == requirements_mask) {
516 *typeIndex = i;
517 return true;
518 }
519 }
520 typeBits >>= 1;
521 }
522 // No memory types matched, return failure
523 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600524}
525
Karl Schultze4dc75c2016-02-02 15:37:51 -0700526static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600527 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528
Tony Barbource7524e2016-07-28 12:01:45 -0600529 // This function could get called twice if the texture uses a staging buffer
530 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600531 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532 return;
533
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600534 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600535 assert(!err);
536
Tony Barbource7524e2016-07-28 12:01:45 -0600537 VkFence fence;
538 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
539 .pNext = NULL,
540 .flags = 0};
541 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700542 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700543 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
544 .pNext = NULL,
545 .waitSemaphoreCount = 0,
546 .pWaitSemaphores = NULL,
547 .pWaitDstStageMask = NULL,
548 .commandBufferCount = 1,
549 .pCommandBuffers = cmd_bufs,
550 .signalSemaphoreCount = 0,
551 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552
Tony Barbource7524e2016-07-28 12:01:45 -0600553 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600554 assert(!err);
555
Tony Barbource7524e2016-07-28 12:01:45 -0600556 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557 assert(!err);
558
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600559 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600560 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600561 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600562}
563
Karl Schultze4dc75c2016-02-02 15:37:51 -0700564static void demo_set_image_layout(struct demo *demo, VkImage image,
565 VkImageAspectFlags aspectMask,
566 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700567 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600568 VkAccessFlagBits srcAccessMask,
569 VkPipelineStageFlags src_stages,
570 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600571 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600572
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600573 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600574 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600575 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700576 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800577 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600578 .oldLayout = old_image_layout,
579 .newLayout = new_image_layout,
580 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700581 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582
Tony Barboureb5077b2016-10-19 15:23:36 -0600583 switch (new_image_layout) {
584 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600586 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
587 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600588
Tony Barboureb5077b2016-10-19 15:23:36 -0600589 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700590 image_memory_barrier.dstAccessMask =
591 VK_ACCESS_COLOR_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_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700595 image_memory_barrier.dstAccessMask =
596 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600597 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700598
Tony Barboureb5077b2016-10-19 15:23:36 -0600599 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700600 image_memory_barrier.dstAccessMask =
601 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600602 break;
603
604 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
605 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
606 break;
607
608 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
609 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
610 break;
611
612 default:
613 image_memory_barrier.dstAccessMask = 0;
614 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600615 }
616
Tony Barbourf165b5d2016-10-03 16:01:41 -0600617
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600618 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600619
Karl Schultze4dc75c2016-02-02 15:37:51 -0700620 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
621 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600622}
623
Karl Schultze4dc75c2016-02-02 15:37:51 -0700624static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700625 const VkCommandBufferBeginInfo cmd_buf_info = {
626 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
627 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600628 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700629 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700630 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800631 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700632 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
633 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800634 };
635 const VkRenderPassBeginInfo rp_begin = {
636 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
637 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800638 .renderPass = demo->render_pass,
639 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800640 .renderArea.offset.x = 0,
641 .renderArea.offset.y = 0,
642 .renderArea.extent.width = demo->width,
643 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600644 .clearValueCount = 2,
645 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700646 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800647 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600648
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600649 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800651 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700652 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
653 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
654 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
655 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600656 VkViewport viewport;
657 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700658 viewport.height = (float)demo->height;
659 viewport.width = (float)demo->width;
660 viewport.minDepth = (float)0.0f;
661 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700662 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600663
664 VkRect2D scissor;
665 memset(&scissor, 0, sizeof(scissor));
666 scissor.extent.width = demo->width;
667 scissor.extent.height = demo->height;
668 scissor.offset.x = 0;
669 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700670 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600671 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600672 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600673 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800674 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600675
Tony Barbour22e06272016-09-07 16:07:56 -0600676 if (demo->separate_present_queue) {
677 // We have to transfer ownership from the graphics queue family to the
678 // present queue family to be able to present. Note that we don't have
679 // to transfer from present queue family back to graphics queue family at
680 // the start of the next frame because we don't care about the image's
681 // contents at that point.
682 VkImageMemoryBarrier image_ownership_barrier = {
683 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
684 .pNext = NULL,
685 .srcAccessMask = 0,
686 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
687 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
688 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
689 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
690 .dstQueueFamilyIndex = demo->present_queue_family_index,
691 .image = demo->buffers[demo->current_buffer].image,
692 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
693
694 vkCmdPipelineBarrier(cmd_buf,
695 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600696 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600697 0, NULL, 0, NULL, 1, &image_ownership_barrier);
698 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600699 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700 assert(!err);
701}
702
Tony Barbour22e06272016-09-07 16:07:56 -0600703void demo_build_image_ownership_cmd(struct demo *demo, int i) {
704 VkResult U_ASSERT_ONLY err;
705
706 const VkCommandBufferBeginInfo cmd_buf_info = {
707 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
708 .pNext = NULL,
709 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
710 .pInheritanceInfo = NULL,
711 };
712 err = vkBeginCommandBuffer(demo->buffers[i].graphics_to_present_cmd,
713 &cmd_buf_info);
714 assert(!err);
715
716 VkImageMemoryBarrier image_ownership_barrier = {
717 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
718 .pNext = NULL,
719 .srcAccessMask = 0,
720 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
721 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
722 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
723 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
724 .dstQueueFamilyIndex = demo->present_queue_family_index,
725 .image = demo->buffers[i].image,
726 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
727
728 vkCmdPipelineBarrier(demo->buffers[i].graphics_to_present_cmd,
729 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
730 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
731 NULL, 0, NULL, 1, &image_ownership_barrier);
732 err = vkEndCommandBuffer(demo->buffers[i].graphics_to_present_cmd);
733 assert(!err);
734}
735
Karl Schultze4dc75c2016-02-02 15:37:51 -0700736void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600737 mat4x4 MVP, Model, VP;
738 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600739 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600740 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600741
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600742 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600744 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600745 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700746 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
747 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600748 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600749
Karl Schultze4dc75c2016-02-02 15:37:51 -0700750 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
751 demo->uniform_data.mem_alloc.allocationSize, 0,
752 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753 assert(!err);
754
Karl Schultze4dc75c2016-02-02 15:37:51 -0700755 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600756
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600757 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600758}
759
Karl Schultze4dc75c2016-02-02 15:37:51 -0700760static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600761 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600762
szdarkhackd322ff02016-10-08 09:51:22 +0300763 // Ensure no more than FRAME_LAG presentations are outstanding
764 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
765 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600766
Ian Elliottaaae5352015-07-06 14:27:58 -0600767 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700768 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour66a56c32016-09-21 13:10:56 -0600769 demo->image_acquired_semaphores[demo->frame_index], demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600770 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600771
Ian Elliottcf074d32015-10-16 18:02:43 -0600772 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
773 // demo->swapchain is out of date (e.g. the window was resized) and
774 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600775 demo->frame_index += 1;
776 demo->frame_index %= FRAME_LAG;
777
Ian Elliottcf074d32015-10-16 18:02:43 -0600778 demo_resize(demo);
779 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600780 return;
781 } else if (err == VK_SUBOPTIMAL_KHR) {
782 // demo->swapchain is not as optimal as it could be, but the platform's
783 // presentation engine will still present the image correctly.
784 } else {
785 assert(!err);
786 }
Tony Barbource7524e2016-07-28 12:01:45 -0600787 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600788 // that the image won't be rendered to until the presentation
789 // engine has fully released ownership to the application, and it is
790 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600791 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600792 VkPipelineStageFlags pipe_stage_flags;
793 VkSubmitInfo submit_info;
794 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
795 submit_info.pNext = NULL;
796 submit_info.pWaitDstStageMask = &pipe_stage_flags;
797 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
798 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600799 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600800 submit_info.commandBufferCount = 1;
801 submit_info.pCommandBuffers = &demo->buffers[demo->current_buffer].cmd;
802 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600803 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura2a67812016-07-18 13:21:06 -0600804 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600805 assert(!err);
806
Tony Barbour22e06272016-09-07 16:07:56 -0600807 if (demo->separate_present_queue) {
808 // If we are using separate queues, change image ownership to the
809 // present queue before presenting, waiting for the draw complete
810 // semaphore and signalling the ownership released semaphore when finished
811 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
812 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600813 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600814 submit_info.commandBufferCount = 1;
815 submit_info.pCommandBuffers =
816 &demo->buffers[demo->current_buffer].graphics_to_present_cmd;
817 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600818 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600819 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
820 assert(!err);
821 }
822
823 // If we are using separate queues we have to wait for image ownership,
824 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600825 VkPresentInfoKHR present = {
826 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600827 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600828 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600829 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600830 ? &demo->image_ownership_semaphores[demo->frame_index]
831 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600832 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700833 .pSwapchains = &demo->swapchain,
834 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600835 };
836
Tony Barboura2a67812016-07-18 13:21:06 -0600837 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600838 demo->frame_index += 1;
839 demo->frame_index %= FRAME_LAG;
840
Ian Elliottcf074d32015-10-16 18:02:43 -0600841 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
842 // demo->swapchain is out of date (e.g. the window was resized) and
843 // must be recreated:
844 demo_resize(demo);
845 } else if (err == VK_SUBOPTIMAL_KHR) {
846 // demo->swapchain is not as optimal as it could be, but the platform's
847 // presentation engine will still present the image correctly.
848 } else {
849 assert(!err);
850 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851}
852
Karl Schultze4dc75c2016-02-02 15:37:51 -0700853static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600854 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600855 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600856
Ian Elliottb08e0d92015-11-20 11:55:46 -0700857 // Check the surface capabilities and formats
858 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700859 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
860 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600861 assert(!err);
862
Ian Elliott8528eff2015-08-07 15:56:59 -0600863 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700864 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
865 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600866 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600867 VkPresentModeKHR *presentModes =
868 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600869 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700870 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
871 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600872 assert(!err);
873
Ian Elliotta0781972015-08-21 15:09:33 -0600874 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600875 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
876 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
877 // If the surface size is undefined, the size is set to the size
878 // of the images requested, which must fit within the minimum and
879 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600880 swapchainExtent.width = demo->width;
881 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600882
883 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
884 swapchainExtent.width = surfCapabilities.minImageExtent.width;
885 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
886 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
887 }
888
889 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
890 swapchainExtent.height = surfCapabilities.minImageExtent.height;
891 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
892 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
893 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700894 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600895 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700896 swapchainExtent = surfCapabilities.currentExtent;
897 demo->width = surfCapabilities.currentExtent.width;
898 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600899 }
900
Tony Barbour66a56c32016-09-21 13:10:56 -0600901 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600902 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600903 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -0600904
Ian Elliottb18fdde2016-10-03 12:11:12 -0600905 // There are times when you may wish to use another present mode. The
906 // following code shows how to select them, and the comments provide some
907 // reasons you may wish to use them.
908 //
909 // It should be noted that Vulkan 1.0 doesn't provide a method for
910 // synchronizing rendering with the presentation engine's display. There
911 // is a method provided for throttling rendering with the display, but
912 // there are some presentation engines for which this method will not work.
913 // If an application doesn't throttle its rendering, and if it renders much
914 // faster than the refresh rate of the display, this can waste power on
915 // mobile devices. That is because power is being spent rendering images
916 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -0600917
Ian Elliottb18fdde2016-10-03 12:11:12 -0600918 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
919 // tearing, or have some way of synchronizing their rendering with the
920 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600921 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
922 // generally render a new presentable image every refresh cycle, but are
923 // occasionally early. In this case, the application wants the new image
924 // to be displayed instead of the previously-queued-for-presentation image
925 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600926 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
927 // render a new presentable image every refresh cycle, but are occasionally
928 // late. In this case (perhaps because of stuttering/latency concerns),
929 // the application wants the late image to be immediately displayed, even
930 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -0600931
932 if (demo->presentMode != swapchainPresentMode) {
933
934 for (size_t i = 0; i < presentModeCount; ++i) {
935 if (presentModes[i] == demo->presentMode) {
936 swapchainPresentMode = demo->presentMode;
937 break;
938 }
Ian Elliottb18fdde2016-10-03 12:11:12 -0600939 }
940 }
Tony Barbour291d57b2016-10-21 14:47:07 -0600941 if (swapchainPresentMode != demo->presentMode) {
942 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
943 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600944
Ian Elliottcf7f7482016-08-19 03:46:58 -0600945 // Determine the number of VkImage's to use in the swap chain.
946 // Application desires to only acquire 1 image at a time (which is
947 // "surfCapabilities.minImageCount").
948 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
949 // If maxImageCount is 0, we can ask for as many images as we want;
950 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700951 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600952 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600953 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600954 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600955 }
956
Tony Barbour9fd6d352015-09-16 15:01:32 -0600957 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700958 if (surfCapabilities.supportedTransforms &
959 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700960 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600961 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700962 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600963 }
964
Tony Barboura2a67812016-07-18 13:21:06 -0600965 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600966 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800967 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700968 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600969 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800970 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600971 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700972 .imageExtent =
973 {
974 .width = swapchainExtent.width, .height = swapchainExtent.height,
975 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700976 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600977 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700978 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700979 .imageArrayLayers = 1,
980 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
981 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600982 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600983 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600984 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600985 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600986 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600987 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600988 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700989 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800990 assert(!err);
991
Ian Elliottcf074d32015-10-16 18:02:43 -0600992 // If we just re-created an existing swapchain, we should destroy the old
993 // swapchain at this point.
994 // Note: destroying the swapchain also cleans up all its associated
995 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800996 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700997 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600998 }
999
1000 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001001 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001002 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001003
Karl Schultze4dc75c2016-02-02 15:37:51 -07001004 VkImage *swapchainImages =
1005 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001006 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001007 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001008 &demo->swapchainImageCount,
1009 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001010 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001011
Karl Schultze4dc75c2016-02-02 15:37:51 -07001012 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
1013 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -06001014 assert(demo->buffers);
1015
Ian Elliotta0781972015-08-21 15:09:33 -06001016 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001017 VkImageViewCreateInfo color_image_view = {
1018 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001019 .pNext = NULL,
1020 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001021 .components =
1022 {
1023 .r = VK_COMPONENT_SWIZZLE_R,
1024 .g = VK_COMPONENT_SWIZZLE_G,
1025 .b = VK_COMPONENT_SWIZZLE_B,
1026 .a = VK_COMPONENT_SWIZZLE_A,
1027 },
1028 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1029 .baseMipLevel = 0,
1030 .levelCount = 1,
1031 .baseArrayLayer = 0,
1032 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001033 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1034 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035 };
1036
Ian Elliotta0781972015-08-21 15:09:33 -06001037 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001039 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001040
Karl Schultze4dc75c2016-02-02 15:37:51 -07001041 err = vkCreateImageView(demo->device, &color_image_view, NULL,
1042 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001043 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001044
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001046
Mark Young04fd3d82016-01-25 14:43:50 -07001047 if (NULL != presentModes) {
1048 free(presentModes);
1049 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001050}
1051
Karl Schultze4dc75c2016-02-02 15:37:51 -07001052static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001053 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001054 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001057 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001058 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001059 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001060 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001061 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001062 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001063 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001064 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001065 .flags = 0,
1066 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001067
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001068 VkImageViewCreateInfo view = {
1069 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001071 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001072 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001073 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1074 .baseMipLevel = 0,
1075 .levelCount = 1,
1076 .baseArrayLayer = 0,
1077 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001079 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001080 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001081
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001082 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001083 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001084 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085
1086 demo->depth.format = depth_format;
1087
1088 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001089 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001090 assert(!err);
1091
Karl Schultze4dc75c2016-02-02 15:37:51 -07001092 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001093 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001094
Chia-I Wuf48700b2015-11-10 16:21:09 +08001095 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001096 demo->depth.mem_alloc.pNext = NULL;
1097 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1098 demo->depth.mem_alloc.memoryTypeIndex = 0;
1099
Karl Schultze4dc75c2016-02-02 15:37:51 -07001100 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1101 0, /* No requirements */
1102 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001103 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001104
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001105 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001106 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1107 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001108 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001109
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001110 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001111 err =
1112 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001113 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114
1115 /* create image view */
1116 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001117 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001118 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119}
1120
Tony Barbour1a86d032015-09-21 15:17:33 -06001121/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001122bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001123 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001124#ifdef __ANDROID__
1125#include <lunarg.ppm.h>
1126 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001127 cPtr = (char*)lunarg_ppm;
1128 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001129 return false;
1130 }
1131 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001132 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001133 if (rgba_data == NULL) {
1134 return true;
1135 }
1136 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001137 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001138 return false;
1139 }
1140 while(strncmp(cPtr++, "\n", 1));
1141
1142 for (int y = 0; y < *height; y++) {
1143 uint8_t *rowPtr = rgba_data;
1144 for (int x = 0; x < *width; x++) {
1145 memcpy(rowPtr, cPtr, 3);
1146 rowPtr[3] = 255; /* Alpha of 1 */
1147 rowPtr += 4;
1148 cPtr += 3;
1149 }
1150 rgba_data += layout->rowPitch;
1151 }
1152
1153 return true;
1154#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001155 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001156 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001157
Tony Barbour1a86d032015-09-21 15:17:33 -06001158 if (!fPtr)
1159 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001160
Tony Barbour1a86d032015-09-21 15:17:33 -06001161 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001162 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1163 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001164 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001165 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001166
Tony Barbour1a86d032015-09-21 15:17:33 -06001167 do {
1168 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001169 if (cPtr == NULL) {
1170 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001171 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001172 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001173 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001174
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001175 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001176 if (rgba_data == NULL) {
1177 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001178 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001179 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001180 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001181 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001182 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1183 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001184 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001185 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001186
Karl Schultze4dc75c2016-02-02 15:37:51 -07001187 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001188 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001189 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001190 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001191 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001192 rowPtr[3] = 255; /* Alpha of 1 */
1193 rowPtr += 4;
1194 }
1195 rgba_data += layout->rowPitch;
1196 }
1197 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001198 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001199#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001200}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001201
Karl Schultze4dc75c2016-02-02 15:37:51 -07001202static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001203 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001204 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001205 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001206 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001207 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001208 int32_t tex_width;
1209 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001210 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001211 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001212
Karl Schultze4dc75c2016-02-02 15:37:51 -07001213 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001214 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001215 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001217 tex_obj->tex_width = tex_width;
1218 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001220 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001221 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001222 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001223 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001224 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001225 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001227 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001228 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001229 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001230 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001232 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001233 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001235 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001236
Karl Schultze4dc75c2016-02-02 15:37:51 -07001237 err =
1238 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001239 assert(!err);
1240
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001241 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001242
Chia-I Wuf48700b2015-11-10 16:21:09 +08001243 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001244 tex_obj->mem_alloc.pNext = NULL;
1245 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1246 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001247
Karl Schultze4dc75c2016-02-02 15:37:51 -07001248 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1249 required_props,
1250 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001251 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001252
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001253 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001254 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001255 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001256 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001257
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001258 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001259 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001260 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001261
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001262 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001263 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001264 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001265 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001266 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001267 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001268 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001269 void *data;
1270
Karl Schultze4dc75c2016-02-02 15:37:51 -07001271 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1272 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273
Karl Schultze4dc75c2016-02-02 15:37:51 -07001274 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1275 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001276 assert(!err);
1277
1278 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1279 fprintf(stderr, "Error loading texture: %s\n", filename);
1280 }
1281
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001282 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001283 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001284
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001285 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001286}
1287
Karl Schultze4dc75c2016-02-02 15:37:51 -07001288static void demo_destroy_texture_image(struct demo *demo,
1289 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001290 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001291 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1292 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001293}
1294
Karl Schultze4dc75c2016-02-02 15:37:51 -07001295static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001296 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001297 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001298 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001299
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001300 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001301
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001303 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001304
Karl Schultze4dc75c2016-02-02 15:37:51 -07001305 if ((props.linearTilingFeatures &
1306 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1307 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001308 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001309 demo_prepare_texture_image(
1310 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1311 VK_IMAGE_USAGE_SAMPLED_BIT,
1312 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1313 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001314 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1315 // shader to run until layout transition completes
1316 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1317 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1318 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1319 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001320 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 } else if (props.optimalTilingFeatures &
1322 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001323 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001324
Tony Barbour16156cb2016-10-19 14:15:49 -06001325 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001326 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001327 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001328 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1329 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1330 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001331
Karl Schultze4dc75c2016-02-02 15:37:51 -07001332 demo_prepare_texture_image(
1333 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1334 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1335 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001336
Tony Barbour16156cb2016-10-19 14:15:49 -06001337 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001338 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001339 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001340 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001341 VK_ACCESS_HOST_WRITE_BIT,
1342 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1343 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001344
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001345 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001346 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001347 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001348 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001349 VK_ACCESS_HOST_WRITE_BIT,
1350 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1351 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001352
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001353 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001354 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1355 .srcOffset = {0, 0, 0},
1356 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1357 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001358 .extent = {demo->staging_texture.tex_width,
1359 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001360 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001361 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001362 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001363 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1364 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001365
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001366 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001367 VK_IMAGE_ASPECT_COLOR_BIT,
1368 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001369 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001370 VK_ACCESS_TRANSFER_WRITE_BIT,
1371 VK_PIPELINE_STAGE_TRANSFER_BIT,
1372 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001373
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001374 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001375 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1376 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001377 }
1378
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001379 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001382 .magFilter = VK_FILTER_NEAREST,
1383 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001384 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001385 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1386 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1387 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001389 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001390 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001391 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392 .minLod = 0.0f,
1393 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001394 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001395 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001397
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001398 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001399 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001401 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001402 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001403 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001404 .components =
1405 {
1406 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1407 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1408 },
1409 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001410 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001412
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001414 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001415 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416 assert(!err);
1417
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418 /* create image view */
1419 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001420 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001421 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422 assert(!err);
1423 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425
Karl Schultze4dc75c2016-02-02 15:37:51 -07001426void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001427 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001428 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001429 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001430 int i;
1431 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001432 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001433 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001435
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001436 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001437 mat4x4_mul(MVP, VP, demo->model_matrix);
1438 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001439 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001440
Karl Schultze4dc75c2016-02-02 15:37:51 -07001441 for (i = 0; i < 12 * 3; i++) {
1442 data.position[i][0] = g_vertex_buffer_data[i * 3];
1443 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1444 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001445 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001446 data.attr[i][0] = g_uv_buffer_data[2 * i];
1447 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001448 data.attr[i][2] = 0;
1449 data.attr[i][3] = 0;
1450 }
1451
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001452 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001453 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001454 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001455 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001456 err =
1457 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001458 assert(!err);
1459
Karl Schultze4dc75c2016-02-02 15:37:51 -07001460 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1461 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001462
Chia-I Wuf48700b2015-11-10 16:21:09 +08001463 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001464 demo->uniform_data.mem_alloc.pNext = NULL;
1465 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1466 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1467
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001469 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1470 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001471 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001472 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001473
Karl Schultze4dc75c2016-02-02 15:37:51 -07001474 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1475 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001476 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001477
Karl Schultze4dc75c2016-02-02 15:37:51 -07001478 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1479 demo->uniform_data.mem_alloc.allocationSize, 0,
1480 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001481 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001482
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001483 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001484
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001485 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001486
Karl Schultze4dc75c2016-02-02 15:37:51 -07001487 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1488 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001489 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001490
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001491 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1492 demo->uniform_data.buffer_info.offset = 0;
1493 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001494}
1495
Karl Schultze4dc75c2016-02-02 15:37:51 -07001496static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001497 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001498 [0] =
1499 {
1500 .binding = 0,
1501 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1502 .descriptorCount = 1,
1503 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1504 .pImmutableSamplers = NULL,
1505 },
1506 [1] =
1507 {
1508 .binding = 1,
1509 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1510 .descriptorCount = DEMO_TEXTURE_COUNT,
1511 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1512 .pImmutableSamplers = NULL,
1513 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001514 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001515 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001517 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001518 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001519 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001521 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522
Karl Schultze4dc75c2016-02-02 15:37:51 -07001523 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1524 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001525 assert(!err);
1526
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001527 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001528 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1529 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001530 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001531 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001532 };
1533
Karl Schultze4dc75c2016-02-02 15:37:51 -07001534 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001535 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001536 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537}
1538
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001540 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1541 // because at the start of the renderpass, we don't care about their contents.
1542 // At the start of the subpass, the color attachment's layout will be transitioned
1543 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1544 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1545 // the renderpass, the color attachment's layout will be transitioned to
1546 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1547 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001548 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001549 [0] =
1550 {
1551 .format = demo->format,
1552 .samples = VK_SAMPLE_COUNT_1_BIT,
1553 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1554 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1555 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1556 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001557 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001558 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001559 },
1560 [1] =
1561 {
1562 .format = demo->depth.format,
1563 .samples = VK_SAMPLE_COUNT_1_BIT,
1564 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1565 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1566 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1567 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1568 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001569 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001570 .finalLayout =
1571 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1572 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001573 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001574 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001575 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001576 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001577 const VkAttachmentReference depth_reference = {
1578 .attachment = 1,
1579 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1580 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001581 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001582 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1583 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001584 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001585 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001586 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001587 .pColorAttachments = &color_reference,
1588 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001589 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001590 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001591 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001592 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001593 const VkRenderPassCreateInfo rp_info = {
1594 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1595 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001596 .attachmentCount = 2,
1597 .pAttachments = attachments,
1598 .subpassCount = 1,
1599 .pSubpasses = &subpass,
1600 .dependencyCount = 0,
1601 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001602 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001603 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001604
Chia-I Wu63636062015-10-26 21:10:41 +08001605 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001606 assert(!err);
1607}
1608
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001609//TODO: Merge shader reading
1610#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001611static VkShaderModule
1612demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001613 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001614 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001615 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001616
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001617 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1618 moduleCreateInfo.pNext = NULL;
1619
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001620 moduleCreateInfo.codeSize = size;
1621 moduleCreateInfo.pCode = code;
1622 moduleCreateInfo.flags = 0;
1623 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1624 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001625
1626 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627}
1628
Karl Schultze4dc75c2016-02-02 15:37:51 -07001629char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001630 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001631 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001632 void *shader_code;
1633
1634 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001635 if (!fp)
1636 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001637
1638 fseek(fp, 0L, SEEK_END);
1639 size = ftell(fp);
1640
1641 fseek(fp, 0L, SEEK_SET);
1642
1643 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001644 retval = fread(shader_code, size, 1, fp);
1645 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001646
1647 *psize = size;
1648
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001649 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001650 return shader_code;
1651}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001652#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001653
Karl Schultze4dc75c2016-02-02 15:37:51 -07001654static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001655#ifdef __ANDROID__
1656 VkShaderModuleCreateInfo sh_info = {};
1657 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1658
1659#include "cube.vert.h"
1660 sh_info.codeSize = sizeof(cube_vert);
1661 sh_info.pCode = cube_vert;
1662 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1663 assert(!err);
1664#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001665 void *vertShaderCode;
1666 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001667
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001668 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1669
Karl Schultze4dc75c2016-02-02 15:37:51 -07001670 demo->vert_shader_module =
1671 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001672
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001673 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001674#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001675
1676 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001677}
1678
Karl Schultze4dc75c2016-02-02 15:37:51 -07001679static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001680#ifdef __ANDROID__
1681 VkShaderModuleCreateInfo sh_info = {};
1682 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1683
1684#include "cube.frag.h"
1685 sh_info.codeSize = sizeof(cube_frag);
1686 sh_info.pCode = cube_frag;
1687 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1688 assert(!err);
1689#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001690 void *fragShaderCode;
1691 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001692
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001693 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001694
Karl Schultze4dc75c2016-02-02 15:37:51 -07001695 demo->frag_shader_module =
1696 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001697
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001698 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001699#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001700
1701 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001702}
1703
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001705 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001706 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001707 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001708 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001709 VkPipelineRasterizationStateCreateInfo rs;
1710 VkPipelineColorBlendStateCreateInfo cb;
1711 VkPipelineDepthStencilStateCreateInfo ds;
1712 VkPipelineViewportStateCreateInfo vp;
1713 VkPipelineMultisampleStateCreateInfo ms;
1714 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1715 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001716 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717
Piers Daniellba839d12015-09-29 13:01:09 -06001718 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1719 memset(&dynamicState, 0, sizeof dynamicState);
1720 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1721 dynamicState.pDynamicStates = dynamicStateEnables;
1722
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001724 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001725 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001727 memset(&vi, 0, sizeof(vi));
1728 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1729
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001731 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001732 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733
1734 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001735 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1736 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001737 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001738 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001739 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001740 rs.rasterizerDiscardEnable = VK_FALSE;
1741 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001742 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743
1744 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001745 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1746 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001747 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001748 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001749 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001750 cb.attachmentCount = 1;
1751 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752
Tony Barbour29645d02015-01-16 14:27:35 -07001753 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001754 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001755 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001756 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1757 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001758 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001759 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1760 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001761
1762 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001763 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001764 ds.depthTestEnable = VK_TRUE;
1765 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001766 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001767 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001768 ds.back.failOp = VK_STENCIL_OP_KEEP;
1769 ds.back.passOp = VK_STENCIL_OP_KEEP;
1770 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001771 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001772 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001773
Tony Barbour29645d02015-01-16 14:27:35 -07001774 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001775 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001776 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001777 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001778
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001779 // Two stages: vs and fs
1780 pipeline.stageCount = 2;
1781 VkPipelineShaderStageCreateInfo shaderStages[2];
1782 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1783
Karl Schultze4dc75c2016-02-02 15:37:51 -07001784 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1785 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001786 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001787 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001788
Karl Schultze4dc75c2016-02-02 15:37:51 -07001789 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1790 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001791 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001792 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001793
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001794 memset(&pipelineCache, 0, sizeof(pipelineCache));
1795 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796
Karl Schultze4dc75c2016-02-02 15:37:51 -07001797 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1798 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001799 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001800
Karl Schultze4dc75c2016-02-02 15:37:51 -07001801 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001802 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001803 pipeline.pRasterizationState = &rs;
1804 pipeline.pColorBlendState = &cb;
1805 pipeline.pMultisampleState = &ms;
1806 pipeline.pViewportState = &vp;
1807 pipeline.pDepthStencilState = &ds;
1808 pipeline.pStages = shaderStages;
1809 pipeline.renderPass = demo->render_pass;
1810 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001811
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001812 pipeline.renderPass = demo->render_pass;
1813
Karl Schultze4dc75c2016-02-02 15:37:51 -07001814 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1815 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001816 assert(!err);
1817
Chia-I Wu63636062015-10-26 21:10:41 +08001818 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1819 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820}
1821
Karl Schultze4dc75c2016-02-02 15:37:51 -07001822static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001823 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001824 [0] =
1825 {
1826 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1827 .descriptorCount = 1,
1828 },
1829 [1] =
1830 {
1831 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1832 .descriptorCount = DEMO_TEXTURE_COUNT,
1833 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001834 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001835 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001836 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001837 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001838 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001839 .poolSizeCount = 2,
1840 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001841 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001842 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001843
Karl Schultze4dc75c2016-02-02 15:37:51 -07001844 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1845 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001846 assert(!err);
1847}
1848
Karl Schultze4dc75c2016-02-02 15:37:51 -07001849static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001850 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001851 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001852 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001853 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001854
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001855 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001856 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001857 .pNext = NULL,
1858 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001859 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001860 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001861 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001862 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001863
Chia-I Wuae721ba2015-05-25 16:27:55 +08001864 memset(&tex_descs, 0, sizeof(tex_descs));
1865 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001866 tex_descs[i].sampler = demo->textures[i].sampler;
1867 tex_descs[i].imageView = demo->textures[i].view;
1868 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001869 }
1870
1871 memset(&writes, 0, sizeof(writes));
1872
1873 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001874 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001875 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001876 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001877 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001878
1879 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001880 writes[1].dstSet = demo->desc_set;
1881 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001882 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001883 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001884 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001885
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001886 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001887}
1888
Karl Schultze4dc75c2016-02-02 15:37:51 -07001889static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001890 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001891 attachments[1] = demo->depth.view;
1892
Chia-I Wuf973e322015-07-08 13:34:24 +08001893 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001894 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1895 .pNext = NULL,
1896 .renderPass = demo->render_pass,
1897 .attachmentCount = 2,
1898 .pAttachments = attachments,
1899 .width = demo->width,
1900 .height = demo->height,
1901 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001902 };
1903 VkResult U_ASSERT_ONLY err;
1904 uint32_t i;
1905
Karl Schultze4dc75c2016-02-02 15:37:51 -07001906 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1907 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001908 assert(demo->framebuffers);
1909
1910 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001911 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001912 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1913 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001914 assert(!err);
1915 }
1916}
1917
Karl Schultze4dc75c2016-02-02 15:37:51 -07001918static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001919 VkResult U_ASSERT_ONLY err;
1920
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001921 const VkCommandPoolCreateInfo cmd_pool_info = {
1922 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001923 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001924 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001925 .flags = 0,
1926 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001927 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1928 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001929 assert(!err);
1930
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001931 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001932 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001933 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001934 .commandPool = demo->cmd_pool,
1935 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001936 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001937 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001938 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1939 assert(!err);
1940 VkCommandBufferBeginInfo cmd_buf_info = {
1941 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1942 .pNext = NULL,
1943 .flags = 0,
1944 .pInheritanceInfo = NULL,
1945 };
1946 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1947 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948
1949 demo_prepare_buffers(demo);
1950 demo_prepare_depth(demo);
1951 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001952 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001954 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001955 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957
Ian Elliotta0781972015-08-21 15:09:33 -06001958 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001959 err =
1960 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001961 assert(!err);
1962 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001963
Tony Barbour22e06272016-09-07 16:07:56 -06001964 if (demo->separate_present_queue) {
1965 const VkCommandPoolCreateInfo cmd_pool_info = {
1966 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1967 .pNext = NULL,
1968 .queueFamilyIndex = demo->present_queue_family_index,
1969 .flags = 0,
1970 };
1971 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1972 &demo->present_cmd_pool);
1973 assert(!err);
1974 const VkCommandBufferAllocateInfo cmd = {
1975 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1976 .pNext = NULL,
1977 .commandPool = demo->present_cmd_pool,
1978 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1979 .commandBufferCount = 1,
1980 };
1981 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1982 err = vkAllocateCommandBuffers(
1983 demo->device, &cmd, &demo->buffers[i].graphics_to_present_cmd);
1984 assert(!err);
1985 demo_build_image_ownership_cmd(demo, i);
1986 }
1987 }
1988
Chia-I Wu63ea9262015-03-26 13:14:16 +08001989 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001990 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001991
Chia-I Wuf973e322015-07-08 13:34:24 +08001992 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001993
Ian Elliotta0781972015-08-21 15:09:33 -06001994 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001995 demo->current_buffer = i;
1996 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1997 }
1998
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001999 /*
2000 * Prepare functions above may generate pipeline commands
2001 * that need to be flushed before beginning the render loop.
2002 */
2003 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002004 if (demo->staging_texture.image) {
2005 demo_destroy_texture_image(demo, &demo->staging_texture);
2006 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002007
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002008 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002009 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010}
2011
Karl Schultze4dc75c2016-02-02 15:37:51 -07002012static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002013 uint32_t i;
2014
2015 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002016 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002017
Tony Barbour66a56c32016-09-21 13:10:56 -06002018 // Wait for fences from present operations
2019 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002020 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002021 vkDestroyFence(demo->device, demo->fences[i], NULL);
2022 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2023 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2024 if (demo->separate_present_queue) {
2025 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2026 }
2027 }
2028
Tony Barbourd8e504f2015-10-08 14:26:24 -06002029 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002030 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002031 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06002032 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002033 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002034
Chia-I Wu63636062015-10-26 21:10:41 +08002035 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2036 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2037 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2038 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2039 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002040
2041 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002042 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2043 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2044 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2045 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002046 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002047 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002048
Chia-I Wu63636062015-10-26 21:10:41 +08002049 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2050 vkDestroyImage(demo->device, demo->depth.image, NULL);
2051 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002052
Chia-I Wu63636062015-10-26 21:10:41 +08002053 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2054 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002055
Ian Elliotta0781972015-08-21 15:09:33 -06002056 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002057 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002058 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2059 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06002060 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002061 free(demo->buffers);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002062 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002063 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002064
Tony Barbour22e06272016-09-07 16:07:56 -06002065 if (demo->separate_present_queue) {
2066 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002067 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002068 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002069 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002070 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002071 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002072 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002073 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002074 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002075
Tony Barbour78d6b572016-11-14 14:46:33 -07002076#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002077 if (demo->use_xlib) {
2078 XDestroyWindow(demo->display, demo->xlib_window);
2079 XCloseDisplay(demo->display);
2080 } else {
2081 xcb_destroy_window(demo->connection, demo->xcb_window);
2082 xcb_disconnect(demo->connection);
2083 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002084 free(demo->atom_wm_delete_window);
Tony Barbour78d6b572016-11-14 14:46:33 -07002085#elif defined(VK_USE_PLATFORM_XLIB_KHR)
2086 XDestroyWindow(demo->display, demo->xlib_window);
2087 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002088#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002089 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002090 xcb_disconnect(demo->connection);
2091 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002092#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2093 wl_shell_surface_destroy(demo->shell_surface);
2094 wl_surface_destroy(demo->window);
2095 wl_shell_destroy(demo->shell);
2096 wl_compositor_destroy(demo->compositor);
2097 wl_registry_destroy(demo->registry);
2098 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002099#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002100}
2101
Karl Schultze4dc75c2016-02-02 15:37:51 -07002102static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002103 uint32_t i;
2104
Mike Stroyanbb60b382015-10-28 09:46:57 -06002105 // Don't react to resize until after first initialization.
2106 if (!demo->prepared) {
2107 return;
2108 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002109 // In order to properly resize the window, we must re-create the swapchain
2110 // AND redo the command buffers, etc.
2111 //
2112 // First, perform part of the demo_cleanup() function:
2113 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002114 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002115
2116 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002117 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002118 }
2119 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002120 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002121
Chia-I Wu63636062015-10-26 21:10:41 +08002122 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2123 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2124 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2125 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2126 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002127
2128 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002129 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2130 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2131 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2132 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002133 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002134
Chia-I Wu63636062015-10-26 21:10:41 +08002135 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2136 vkDestroyImage(demo->device, demo->depth.image, NULL);
2137 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002138
Chia-I Wu63636062015-10-26 21:10:41 +08002139 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2140 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002141
2142 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002143 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002144 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2145 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002146 }
Chia-I Wu63636062015-10-26 21:10:41 +08002147 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002148 if (demo->separate_present_queue) {
2149 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2150 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002151 free(demo->buffers);
2152
Ian Elliottcf074d32015-10-16 18:02:43 -06002153 // Second, re-perform the demo_prepare() function, which will re-create the
2154 // swapchain:
2155 demo_prepare(demo);
2156}
2157
David Pinedo2bb7c932015-06-18 17:03:14 -06002158// On MS-Windows, make this a global, so it's available to WndProc()
2159struct demo demo;
2160
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002161#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002162static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002163 if (!demo->prepared)
2164 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002165
Ian Elliott639ca472015-04-16 15:23:05 -06002166 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002167 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002168 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002169 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002170 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002171 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002172}
Ian Elliott639ca472015-04-16 15:23:05 -06002173
2174// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002175LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2176 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002177 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002178 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002179 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002180 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002181 // The validation callback calls MessageBox which can generate paint
2182 // events - don't make more Vulkan calls if we got here from the
2183 // callback
2184 if (!in_callback) {
2185 demo_run(&demo);
2186 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002187 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002188 case WM_GETMINMAXINFO: // set window's minimum size
2189 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2190 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002191 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002192 // Resize the application to the new window size, except when
2193 // it was minimized. Vulkan doesn't support images or swapchains
2194 // with width=0 and height=0.
2195 if (wParam != SIZE_MINIMIZED) {
2196 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002197 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002198 demo_resize(&demo);
2199 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002200 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002201 default:
2202 break;
2203 }
2204 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2205}
2206
Karl Schultze4dc75c2016-02-02 15:37:51 -07002207static void demo_create_window(struct demo *demo) {
2208 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002209
2210 // Initialize the window class structure:
2211 win_class.cbSize = sizeof(WNDCLASSEX);
2212 win_class.style = CS_HREDRAW | CS_VREDRAW;
2213 win_class.lpfnWndProc = WndProc;
2214 win_class.cbClsExtra = 0;
2215 win_class.cbWndExtra = 0;
2216 win_class.hInstance = demo->connection; // hInstance
2217 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2218 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2219 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2220 win_class.lpszMenuName = NULL;
2221 win_class.lpszClassName = demo->name;
2222 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2223 // Register window class:
2224 if (!RegisterClassEx(&win_class)) {
2225 // It didn't work, so try to give a useful error:
2226 printf("Unexpected error trying to start the application!\n");
2227 fflush(stdout);
2228 exit(1);
2229 }
2230 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002231 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002232 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002233 demo->window = CreateWindowEx(0,
2234 demo->name, // class name
2235 demo->name, // app name
2236 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002237 WS_VISIBLE | WS_SYSMENU,
2238 100, 100, // x/y coords
2239 wr.right - wr.left, // width
2240 wr.bottom - wr.top, // height
2241 NULL, // handle to parent
2242 NULL, // handle to menu
2243 demo->connection, // hInstance
2244 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002245 if (!demo->window) {
2246 // It didn't work, so try to give a useful error:
2247 printf("Cannot create a window in which to draw!\n");
2248 fflush(stdout);
2249 exit(1);
2250 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002251 // Window client area size must be at least 1 pixel high, to prevent crash.
2252 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2253 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002254}
Tony Barbour8295ac42016-08-08 11:04:17 -06002255#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002256static void demo_create_xlib_window(struct demo *demo) {
2257
2258 demo->display = XOpenDisplay(NULL);
2259 long visualMask = VisualScreenMask;
2260 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002261 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002262 vInfoTemplate.screen = DefaultScreen(demo->display);
2263 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2264 &vInfoTemplate, &numberOfVisuals);
2265
2266 Colormap colormap = XCreateColormap(
2267 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2268 visualInfo->visual, AllocNone);
2269
Rene Lindsay11612722016-06-13 17:20:39 -06002270 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002271 windowAttributes.colormap = colormap;
2272 windowAttributes.background_pixel = 0xFFFFFFFF;
2273 windowAttributes.border_pixel = 0;
2274 windowAttributes.event_mask =
2275 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2276
2277 demo->xlib_window = XCreateWindow(
2278 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2279 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2280 visualInfo->visual,
2281 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2282
2283 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2284 XMapWindow(demo->display, demo->xlib_window);
2285 XFlush(demo->display);
2286 demo->xlib_wm_delete_window =
2287 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2288}
2289static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2290 switch(event->type) {
2291 case ClientMessage:
2292 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2293 demo->quit = true;
2294 break;
2295 case KeyPress:
2296 switch (event->xkey.keycode) {
2297 case 0x9: // Escape
2298 demo->quit = true;
2299 break;
2300 case 0x71: // left arrow key
2301 demo->spin_angle += demo->spin_increment;
2302 break;
2303 case 0x72: // right arrow key
2304 demo->spin_angle -= demo->spin_increment;
2305 break;
2306 case 0x41:
2307 demo->pause = !demo->pause;
2308 break;
2309 }
2310 break;
2311 case ConfigureNotify:
2312 if ((demo->width != event->xconfigure.width) ||
2313 (demo->height != event->xconfigure.height)) {
2314 demo->width = event->xconfigure.width;
2315 demo->height = event->xconfigure.height;
2316 demo_resize(demo);
2317 }
2318 break;
2319 default:
2320 break;
2321 }
2322
2323}
2324
2325static void demo_run_xlib(struct demo *demo) {
2326
2327 while (!demo->quit) {
2328 XEvent event;
2329
2330 if (demo->pause) {
2331 XNextEvent(demo->display, &event);
2332 demo_handle_xlib_event(demo, &event);
2333 } else {
2334 while (XPending(demo->display) > 0) {
2335 XNextEvent(demo->display, &event);
2336 demo_handle_xlib_event(demo, &event);
2337 }
2338 }
2339
Tony Barbour2593b182016-04-19 10:57:58 -06002340 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002341 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002342 demo->curFrame++;
2343 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2344 demo->quit = true;
2345 }
2346}
Tony Barbour8295ac42016-08-08 11:04:17 -06002347#endif // VK_USE_PLATFORM_XLIB_KHR
2348#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002349static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002350 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002351 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002352 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002353 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002354 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002355 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002356 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002357 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2358 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002359 demo->quit = true;
2360 }
2361 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002362 case XCB_KEY_RELEASE: {
2363 const xcb_key_release_event_t *key =
2364 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002365
Karl Schultze4dc75c2016-02-02 15:37:51 -07002366 switch (key->detail) {
2367 case 0x9: // Escape
2368 demo->quit = true;
2369 break;
2370 case 0x71: // left arrow key
2371 demo->spin_angle += demo->spin_increment;
2372 break;
2373 case 0x72: // right arrow key
2374 demo->spin_angle -= demo->spin_increment;
2375 break;
2376 case 0x41:
2377 demo->pause = !demo->pause;
2378 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002379 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002380 } break;
2381 case XCB_CONFIGURE_NOTIFY: {
2382 const xcb_configure_notify_event_t *cfg =
2383 (const xcb_configure_notify_event_t *)event;
2384 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002385 demo->width = cfg->width;
2386 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002387 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002388 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002389 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002390 default:
2391 break;
2392 }
2393}
2394
Tony Barbour2593b182016-04-19 10:57:58 -06002395static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002396 xcb_flush(demo->connection);
2397
2398 while (!demo->quit) {
2399 xcb_generic_event_t *event;
2400
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002401 if (demo->pause) {
2402 event = xcb_wait_for_event(demo->connection);
2403 } else {
2404 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002405 while(event) {
2406 demo_handle_xcb_event(demo, event);
2407 free(event);
2408 event = xcb_poll_for_event(demo->connection);
2409 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002410 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002411
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002412 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002413 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002414 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002415 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002416 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002417 }
2418}
2419
Tony Barbour2593b182016-04-19 10:57:58 -06002420static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002421 uint32_t value_mask, value_list[32];
2422
Tony Barbour2593b182016-04-19 10:57:58 -06002423 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002424
2425 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2426 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002427 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002428 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002429
Tony Barbour2593b182016-04-19 10:57:58 -06002430 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002431 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2432 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2433 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002434
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002435 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002436 xcb_intern_atom_cookie_t cookie =
2437 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2438 xcb_intern_atom_reply_t *reply =
2439 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002440
Karl Schultze4dc75c2016-02-02 15:37:51 -07002441 xcb_intern_atom_cookie_t cookie2 =
2442 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2443 demo->atom_wm_delete_window =
2444 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002445
Tony Barbour2593b182016-04-19 10:57:58 -06002446 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002447 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002448 &(*demo->atom_wm_delete_window).atom);
2449 free(reply);
2450
Tony Barbour2593b182016-04-19 10:57:58 -06002451 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002452
Karl Schultze4dc75c2016-02-02 15:37:51 -07002453 // Force the x/y coordinates to 100,100 results are identical in consecutive
2454 // runs
2455 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002456 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002457 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002458}
Tony Barbour6b131662016-08-25 13:14:45 -06002459// VK_USE_PLATFORM_XCB_KHR
2460#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002461static void demo_run(struct demo *demo) {
2462 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002463 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002464 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002465 demo->curFrame++;
2466 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2467 demo->quit = true;
2468 }
2469}
2470
2471static void handle_ping(void *data UNUSED,
2472 struct wl_shell_surface *shell_surface,
2473 uint32_t serial) {
2474 wl_shell_surface_pong(shell_surface, serial);
2475}
2476
2477static void handle_configure(void *data UNUSED,
2478 struct wl_shell_surface *shell_surface UNUSED,
2479 uint32_t edges UNUSED, int32_t width UNUSED,
2480 int32_t height UNUSED) {}
2481
2482static void handle_popup_done(void *data UNUSED,
2483 struct wl_shell_surface *shell_surface UNUSED) {}
2484
2485static const struct wl_shell_surface_listener shell_surface_listener = {
2486 handle_ping, handle_configure, handle_popup_done};
2487
2488static void demo_create_window(struct demo *demo) {
2489 demo->window = wl_compositor_create_surface(demo->compositor);
2490 if (!demo->window) {
2491 printf("Can not create wayland_surface from compositor!\n");
2492 fflush(stdout);
2493 exit(1);
2494 }
2495
2496 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2497 if (!demo->shell_surface) {
2498 printf("Can not get shell_surface from wayland_surface!\n");
2499 fflush(stdout);
2500 exit(1);
2501 }
2502 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2503 demo);
2504 wl_shell_surface_set_toplevel(demo->shell_surface);
2505 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2506}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002507#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2508static void demo_run(struct demo *demo) {
2509 if (!demo->prepared)
2510 return;
2511
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002512 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002513 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002514 demo->curFrame++;
2515}
2516#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002517
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002518/*
2519 * Return 1 (true) if all layer names specified in check_names
2520 * can be found in given layer properties.
2521 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002522static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002523 uint32_t layer_count,
2524 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002525 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002526 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002527 for (uint32_t j = 0; j < layer_count; j++) {
2528 if (!strcmp(check_names[i], layers[j].layerName)) {
2529 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002530 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002531 }
2532 }
2533 if (!found) {
2534 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2535 return 0;
2536 }
2537 }
2538 return 1;
2539}
2540
Karl Schultze4dc75c2016-02-02 15:37:51 -07002541static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002542 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002543 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002544 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002545 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002546 char **instance_validation_layers = NULL;
2547 demo->enabled_extension_count = 0;
2548 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002549
Karl Schultz7c50ad62016-04-01 12:07:03 -06002550 char *instance_validation_layers_alt1[] = {
2551 "VK_LAYER_LUNARG_standard_validation"
2552 };
2553
2554 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002555 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2556 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2557 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2558 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002559 };
2560
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002561 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002562 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002563 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002564
Karl Schultz7c50ad62016-04-01 12:07:03 -06002565 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002566 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002567
Karl Schultz7c50ad62016-04-01 12:07:03 -06002568 instance_validation_layers = instance_validation_layers_alt1;
2569 if (instance_layer_count > 0) {
2570 VkLayerProperties *instance_layers =
2571 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2572 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2573 instance_layers);
2574 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002575
Karl Schultz7c50ad62016-04-01 12:07:03 -06002576
2577 validation_found = demo_check_layers(
2578 ARRAY_SIZE(instance_validation_layers_alt1),
2579 instance_validation_layers, instance_layer_count,
2580 instance_layers);
2581 if (validation_found) {
2582 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002583 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2584 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002585 } else {
2586 // use alternative set of validation layers
2587 instance_validation_layers = instance_validation_layers_alt2;
2588 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2589 validation_found = demo_check_layers(
2590 ARRAY_SIZE(instance_validation_layers_alt2),
2591 instance_validation_layers, instance_layer_count,
2592 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002593 validation_layer_count =
2594 ARRAY_SIZE(instance_validation_layers_alt2);
2595 for (uint32_t i = 0; i < validation_layer_count; i++) {
2596 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002597 }
2598 }
2599 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002600 }
Dustin Graves7c287352016-01-27 13:48:06 -07002601
Karl Schultz7c50ad62016-04-01 12:07:03 -06002602 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002603 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002604 "required validation layer.\n\n"
2605 "Please look at the Getting Started guide for additional "
2606 "information.\n",
2607 "vkCreateInstance Failure");
2608 }
Dustin Graves7c287352016-01-27 13:48:06 -07002609 }
2610
2611 /* Look for instance extensions */
2612 VkBool32 surfaceExtFound = 0;
2613 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002614#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002615 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002616#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002617 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002618
Karl Schultze4dc75c2016-02-02 15:37:51 -07002619 err = vkEnumerateInstanceExtensionProperties(
2620 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002621 assert(!err);
2622
Dustin Graves7c287352016-01-27 13:48:06 -07002623 if (instance_extension_count > 0) {
2624 VkExtensionProperties *instance_extensions =
2625 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2626 err = vkEnumerateInstanceExtensionProperties(
2627 NULL, &instance_extension_count, instance_extensions);
2628 assert(!err);
2629 for (uint32_t i = 0; i < instance_extension_count; i++) {
2630 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2631 instance_extensions[i].extensionName)) {
2632 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002633 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002634 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002635 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002636#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002637 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2638 instance_extensions[i].extensionName)) {
2639 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002640 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002641 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2642 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002643#endif
2644#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002645 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2646 instance_extensions[i].extensionName)) {
2647 platformSurfaceExtFound = 1;
2648 xlibSurfaceExtFound = 1;
2649 demo->extension_names[demo->enabled_extension_count++] =
2650 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2651 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002652#endif
2653#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002654 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2655 instance_extensions[i].extensionName)) {
2656 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002657 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002658 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2659 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002660#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002661#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2662 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2663 instance_extensions[i].extensionName)) {
2664 platformSurfaceExtFound = 1;
2665 demo->extension_names[demo->enabled_extension_count++] =
2666 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2667 }
2668#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002669#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002670 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2671 instance_extensions[i].extensionName)) {
2672 platformSurfaceExtFound = 1;
2673 demo->extension_names[demo->enabled_extension_count++] =
2674 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2675 }
2676#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002677 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2678 instance_extensions[i].extensionName)) {
2679 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002680 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002681 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2682 }
2683 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002684 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002685 }
Dustin Graves7c287352016-01-27 13:48:06 -07002686
2687 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002688 }
Dustin Graves7c287352016-01-27 13:48:06 -07002689
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002690 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002691 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2692 "the " VK_KHR_SURFACE_EXTENSION_NAME
2693 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002694 "Vulkan installable client driver (ICD) installed?\nPlease "
2695 "look at the Getting Started guide for additional "
2696 "information.\n",
2697 "vkCreateInstance Failure");
2698 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002699 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002700#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002701 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2702 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2703 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002704 "Vulkan installable client driver (ICD) installed?\nPlease "
2705 "look at the Getting Started guide for additional "
2706 "information.\n",
2707 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002708#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002709 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2710 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2711 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002712 "Vulkan installable client driver (ICD) installed?\nPlease "
2713 "look at the Getting Started guide for additional "
2714 "information.\n",
2715 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002716#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2717 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2718 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2719 " extension.\n\nDo you have a compatible "
2720 "Vulkan installable client driver (ICD) installed?\nPlease "
2721 "look at the Getting Started guide for additional "
2722 "information.\n",
2723 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002724#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2725 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2726 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2727 " extension.\n\nDo you have a compatible "
2728 "Vulkan installable client driver (ICD) installed?\nPlease "
2729 "look at the Getting Started guide for additional "
2730 "information.\n",
2731 "vkCreateInstance Failure");
2732#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002733 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002734#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002735 if (demo->use_xlib && !xlibSurfaceExtFound) {
2736 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2737 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2738 " extension.\n\nDo you have a compatible "
2739 "Vulkan installable client driver (ICD) installed?\nPlease "
2740 "look at the Getting Started guide for additional "
2741 "information.\n",
2742 "vkCreateInstance Failure");
2743 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002744#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002745 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002746 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002747 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002748 .pApplicationName = APP_SHORT_NAME,
2749 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002750 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002751 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002752 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002753 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002754 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002755 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002756 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002757 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002758 .enabledLayerCount = demo->enabled_layer_count,
2759 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2760 .enabledExtensionCount = demo->enabled_extension_count,
2761 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002762 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002763
2764 /*
2765 * This is info for a temp callback to use during CreateInstance.
2766 * After the instance is created, we use the instance-based
2767 * function to register the final callback.
2768 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002769 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002770 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002771 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2772 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002773 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002774 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002775 dbgCreateInfo.flags =
2776 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2777 inst_info.pNext = &dbgCreateInfo;
2778 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002779
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002780 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002781
Chia-I Wu63636062015-10-26 21:10:41 +08002782 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002783 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2784 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002785 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002786 "additional information.\n",
2787 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002788 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002789 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002790 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002791 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002792 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002793 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2794 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002795 "the Getting Started guide for additional information.\n",
2796 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002797 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002798
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002799 /* Make initial call to query gpu_count, then second call for gpu info*/
2800 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2801 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002802
2803 if (gpu_count > 0) {
2804 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2805 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2806 assert(!err);
2807 /* For cube demo we just grab the first physical device */
2808 demo->gpu = physical_devices[0];
2809 free(physical_devices);
2810 } else {
2811 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2812 "Do you have a compatible Vulkan installable client driver (ICD) "
2813 "installed?\nPlease look at the Getting Started guide for "
2814 "additional information.\n",
2815 "vkEnumeratePhysicalDevices Failure");
2816 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002817
Dustin Graves7c287352016-01-27 13:48:06 -07002818 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002819 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002820 VkBool32 swapchainExtFound = 0;
2821 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002822 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002823
Karl Schultze4dc75c2016-02-02 15:37:51 -07002824 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2825 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002826 assert(!err);
2827
Dustin Graves7c287352016-01-27 13:48:06 -07002828 if (device_extension_count > 0) {
2829 VkExtensionProperties *device_extensions =
2830 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2831 err = vkEnumerateDeviceExtensionProperties(
2832 demo->gpu, NULL, &device_extension_count, device_extensions);
2833 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002834
Dustin Graves7c287352016-01-27 13:48:06 -07002835 for (uint32_t i = 0; i < device_extension_count; i++) {
2836 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2837 device_extensions[i].extensionName)) {
2838 swapchainExtFound = 1;
2839 demo->extension_names[demo->enabled_extension_count++] =
2840 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2841 }
2842 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002843 }
Dustin Graves7c287352016-01-27 13:48:06 -07002844
2845 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002846 }
Dustin Graves7c287352016-01-27 13:48:06 -07002847
Tony Barbourcc69f452015-10-08 13:59:42 -06002848 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002849 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2850 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2851 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002852 "Vulkan installable client driver (ICD) installed?\nPlease "
2853 "look at the Getting Started guide for additional "
2854 "information.\n",
2855 "vkCreateInstance Failure");
2856 }
2857
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002858 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002859 demo->CreateDebugReportCallback =
2860 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2861 demo->inst, "vkCreateDebugReportCallbackEXT");
2862 demo->DestroyDebugReportCallback =
2863 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2864 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002865 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002866 ERR_EXIT(
2867 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2868 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002869 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002870 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002871 ERR_EXIT(
2872 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2873 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002874 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002875 demo->DebugReportMessage =
2876 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2877 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002878 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002879 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002880 "vkGetProcAddr Failure");
2881 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002882
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002883 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002884 PFN_vkDebugReportCallbackEXT callback;
2885 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002886 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002887 dbgCreateInfo.pNext = NULL;
2888 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002889 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002890 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002891 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002892 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2893 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002894 switch (err) {
2895 case VK_SUCCESS:
2896 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002897 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002898 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2899 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002900 break;
2901 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002902 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2903 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002904 break;
2905 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002906 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002907 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002908
2909 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002910 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2911 &demo->queue_family_count, NULL);
2912 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002913
Karl Schultze4dc75c2016-02-02 15:37:51 -07002914 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002915 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2916 vkGetPhysicalDeviceQueueFamilyProperties(
2917 demo->gpu, &demo->queue_family_count, demo->queue_props);
2918
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002919 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002920 // If app has specific feature requirements it should check supported
2921 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002922 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002923 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002924
Tony Barbourda2bad52016-01-22 14:36:40 -07002925 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2926 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2927 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2928 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2929 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2930}
2931
Karl Schultze4dc75c2016-02-02 15:37:51 -07002932static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002933 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002934 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06002935 VkDeviceQueueCreateInfo queues[2];
2936 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2937 queues[0].pNext = NULL;
2938 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
2939 queues[0].queueCount = 1;
2940 queues[0].pQueuePriorities = queue_priorities;
2941 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002942
2943 VkDeviceCreateInfo device = {
2944 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2945 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002946 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06002947 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002948 .enabledLayerCount = 0,
2949 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002950 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002951 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2952 .pEnabledFeatures =
2953 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002954 };
Tony Barbour22e06272016-09-07 16:07:56 -06002955 if (demo->separate_present_queue) {
2956 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2957 queues[1].pNext = NULL;
2958 queues[1].queueFamilyIndex = demo->present_queue_family_index;
2959 queues[1].queueCount = 1;
2960 queues[1].pQueuePriorities = queue_priorities;
2961 queues[1].flags = 0;
2962 device.queueCreateInfoCount = 2;
2963 }
Chia-I Wu63636062015-10-26 21:10:41 +08002964 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002965 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002966}
2967
Karl Schultze4dc75c2016-02-02 15:37:51 -07002968static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002969 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002970 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002971
Karl Schultze4dc75c2016-02-02 15:37:51 -07002972// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002973#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002974 VkWin32SurfaceCreateInfoKHR createInfo;
2975 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2976 createInfo.pNext = NULL;
2977 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002978 createInfo.hinstance = demo->connection;
2979 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002980
Karl Schultze4dc75c2016-02-02 15:37:51 -07002981 err =
2982 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002983#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2984 VkWaylandSurfaceCreateInfoKHR createInfo;
2985 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2986 createInfo.pNext = NULL;
2987 createInfo.flags = 0;
2988 createInfo.display = demo->display;
2989 createInfo.surface = demo->window;
2990
2991 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2992 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002993#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2994 VkAndroidSurfaceCreateInfoKHR createInfo;
2995 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2996 createInfo.pNext = NULL;
2997 createInfo.flags = 0;
2998 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002999
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003000 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3001#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003002 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003003#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003004 VkXlibSurfaceCreateInfoKHR createInfo;
3005 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3006 createInfo.pNext = NULL;
3007 createInfo.flags = 0;
3008 createInfo.dpy = demo->display;
3009 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003010
Tony Barbour2593b182016-04-19 10:57:58 -06003011 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
3012 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003013#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003014 }
3015 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003016#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003017 VkXcbSurfaceCreateInfoKHR createInfo;
3018 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3019 createInfo.pNext = NULL;
3020 createInfo.flags = 0;
3021 createInfo.connection = demo->connection;
3022 createInfo.window = demo->xcb_window;
3023
3024 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003025#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003026 }
Tony Barbour2593b182016-04-19 10:57:58 -06003027 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003028
Tony Barbourcc69f452015-10-08 13:59:42 -06003029 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003030 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003031 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
3032 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003033 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003034 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003035 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003036
3037 // Search for a graphics and a present queue in the array of queue
3038 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003039 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3040 uint32_t presentQueueFamilyIndex = UINT32_MAX;
3041 for (i = 0; i < demo->queue_family_count; i++) {
3042 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3043 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3044 graphicsQueueFamilyIndex = i;
3045 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003046
Tony Barbourab2a0362016-09-06 11:40:12 -06003047 if (supportsPresent[i] == VK_TRUE) {
3048 graphicsQueueFamilyIndex = i;
3049 presentQueueFamilyIndex = i;
3050 break;
3051 }
3052 }
3053 }
3054
3055 if (presentQueueFamilyIndex == UINT32_MAX) {
3056 // If didn't find a queue that supports both graphics and present, then
3057 // find a separate present queue.
Mark Lobodzinski5abdfcc2016-10-03 16:00:40 -06003058 for (i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003059 if (supportsPresent[i] == VK_TRUE) {
3060 presentQueueFamilyIndex = i;
3061 break;
3062 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003063 }
3064 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003065
3066 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003067 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3068 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003069 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003070 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003071 }
3072
Tony Barbourab2a0362016-09-06 11:40:12 -06003073 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3074 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003075 demo->separate_present_queue =
3076 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003077 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003078
Tony Barbourda2bad52016-01-22 14:36:40 -07003079 demo_create_device(demo);
3080
3081 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3082 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3083 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3084 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3085 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3086
Tony Barboura2a67812016-07-18 13:21:06 -06003087 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3088 &demo->graphics_queue);
3089
Tony Barbour22e06272016-09-07 16:07:56 -06003090 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003091 demo->present_queue = demo->graphics_queue;
3092 } else {
3093 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3094 &demo->present_queue);
3095 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003096
Ian Elliottaaae5352015-07-06 14:27:58 -06003097 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003098 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003099 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003100 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003101 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003102 VkSurfaceFormatKHR *surfFormats =
3103 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003104 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003105 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003106 assert(!err);
3107 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3108 // the surface has no preferred format. Otherwise, at least one
3109 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003110 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003111 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003112 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003113 assert(formatCount >= 1);
3114 demo->format = surfFormats[0].format;
3115 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003116 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003117
David Pinedo2bb7c932015-06-18 17:03:14 -06003118 demo->quit = false;
3119 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003120
Tony Barbource7524e2016-07-28 12:01:45 -06003121 // Create semaphores to synchronize acquiring presentable buffers before
3122 // rendering and waiting for drawing to be complete before presenting
3123 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3124 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3125 .pNext = NULL,
3126 .flags = 0,
3127 };
Tony Barbource7524e2016-07-28 12:01:45 -06003128
Tony Barbour66a56c32016-09-21 13:10:56 -06003129 // Create fences that we can use to throttle if we get too far
3130 // ahead of the image presents
3131 VkFenceCreateInfo fence_ci = {
3132 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3133 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003134 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003135 };
3136 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3137 vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003138 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003139 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003140 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003141
3142 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3143 &demo->draw_complete_semaphores[i]);
3144 assert(!err);
3145
3146 if (demo->separate_present_queue) {
3147 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3148 &demo->image_ownership_semaphores[i]);
3149 assert(!err);
3150 }
Tony Barbour22e06272016-09-07 16:07:56 -06003151 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003152 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003153
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003154 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003155 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003156}
3157
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003158#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3159static void registry_handle_global(void *data, struct wl_registry *registry,
3160 uint32_t name, const char *interface,
3161 uint32_t version UNUSED) {
3162 struct demo *demo = data;
3163 if (strcmp(interface, "wl_compositor") == 0) {
3164 demo->compositor =
3165 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3166 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3167 * tp xdg_shell */
3168 } else if (strcmp(interface, "wl_shell") == 0) {
3169 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3170 }
3171}
3172
3173static void registry_handle_global_remove(void *data UNUSED,
3174 struct wl_registry *registry UNUSED,
3175 uint32_t name UNUSED) {}
3176
3177static const struct wl_registry_listener registry_listener = {
3178 registry_handle_global, registry_handle_global_remove};
3179#endif
3180
Karl Schultze4dc75c2016-02-02 15:37:51 -07003181static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003182#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003183 const xcb_setup_t *setup;
3184 xcb_screen_iterator_t iter;
3185 int scr;
3186
3187 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003188 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003189 printf("Cannot find a compatible Vulkan installable client driver "
3190 "(ICD).\nExiting ...\n");
3191 fflush(stdout);
3192 exit(1);
3193 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003194
3195 setup = xcb_get_setup(demo->connection);
3196 iter = xcb_setup_roots_iterator(setup);
3197 while (scr-- > 0)
3198 xcb_screen_next(&iter);
3199
3200 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003201#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3202 demo->display = wl_display_connect(NULL);
3203
3204 if (demo->display == NULL) {
3205 printf("Cannot find a compatible Vulkan installable client driver "
3206 "(ICD).\nExiting ...\n");
3207 fflush(stdout);
3208 exit(1);
3209 }
3210
3211 demo->registry = wl_display_get_registry(demo->display);
3212 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3213 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003214#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003215}
3216
Karl Schultze4dc75c2016-02-02 15:37:51 -07003217static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003218 vec3 eye = {0.0f, 3.0f, 5.0f};
3219 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003220 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003221
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003222 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003223 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
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 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003231 if ((strcmp(argv[i], "--present_mode") == 0) &&
3232 (i < argc - 1)) {
3233 demo->presentMode = atoi(argv[i+1]);
3234 i++;
3235 continue;
3236 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003237 if (strcmp(argv[i], "--break") == 0) {
3238 demo->use_break = true;
3239 continue;
3240 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003241 if (strcmp(argv[i], "--validate") == 0) {
3242 demo->validate = true;
3243 continue;
3244 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003245#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003246 if (strcmp(argv[i], "--xlib") == 0) {
3247 demo->use_xlib = true;
3248 continue;
3249 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003250#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003251 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3252 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3253 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003254 i++;
3255 continue;
3256 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003257 if (strcmp(argv[i], "--suppress_popups") == 0) {
3258 demo->suppress_popups = true;
3259 continue;
3260 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003261
Cody Northropaf28a532016-09-27 10:50:57 -06003262#if defined(ANDROID)
3263 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3264#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003265 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003266#if defined(VK_USE_PLATFORM_XLIB_KHR)
3267 "[--xlib] "
3268#endif
Tony Barbour291d57b2016-10-21 14:47:07 -06003269 "[--c <framecount>] [--suppress_popups] [--present_mode <present mode enum>]\n"
3270 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3271 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3272 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3273 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3274 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3275 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003276 fflush(stderr);
3277 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003278#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003279 }
3280
Tony Barbour2593b182016-04-19 10:57:58 -06003281 if (!demo->use_xlib)
3282 demo_init_connection(demo);
3283
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003284 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003285
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003286 demo->width = 500;
3287 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003288
Tony Barbour66a56c32016-09-21 13:10:56 -06003289 demo->spin_angle = 4.0f;
3290 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003291 demo->pause = false;
3292
Karl Schultze4dc75c2016-02-02 15:37:51 -07003293 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3294 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003295 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3296 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003297
3298 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003299}
3300
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003301#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003302// Include header required for parsing the command line options.
3303#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003304
Karl Schultze4dc75c2016-02-02 15:37:51 -07003305int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3306 int nCmdShow) {
3307 MSG msg; // message
3308 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003309 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003310 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003311
Karl Schultze4dc75c2016-02-02 15:37:51 -07003312 // Use the CommandLine functions to get the command line arguments.
3313 // Unfortunately, Microsoft outputs
3314 // this information as wide characters for Unicode, and we simply want the
3315 // Ascii version to be compatible
3316 // with the non-Windows side. So, we have to convert the information to
3317 // Ascii character strings.
3318 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3319 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003320 argc = 0;
3321 }
3322
Karl Schultze4dc75c2016-02-02 15:37:51 -07003323 if (argc > 0) {
3324 argv = (char **)malloc(sizeof(char *) * argc);
3325 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003326 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003327 } else {
3328 for (int iii = 0; iii < argc; iii++) {
3329 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003330 size_t numConverted = 0;
3331
Karl Schultze4dc75c2016-02-02 15:37:51 -07003332 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3333 if (argv[iii] != NULL) {
3334 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3335 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003336 }
3337 }
3338 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003339 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003340 argv = NULL;
3341 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003342
3343 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003344
3345 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003346 if (argc > 0 && argv != NULL) {
3347 for (int iii = 0; iii < argc; iii++) {
3348 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003349 free(argv[iii]);
3350 }
3351 }
3352 free(argv);
3353 }
3354
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003355 demo.connection = hInstance;
3356 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003357 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003358 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003359
3360 demo_prepare(&demo);
3361
Karl Schultze4dc75c2016-02-02 15:37:51 -07003362 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003363
3364 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003365 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003366 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003367 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003368 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003369 done = true; // if found, quit app
3370 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003371 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003372 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003373 DispatchMessage(&msg);
3374 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003375 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003376 }
3377
3378 demo_cleanup(&demo);
3379
Karl Schultze4dc75c2016-02-02 15:37:51 -07003380 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003381}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003382#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3383#include <android/log.h>
3384#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003385#include "android_util.h"
3386
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003387static bool initialized = false;
3388static bool active = false;
3389struct demo demo;
3390
3391static int32_t processInput(struct android_app* app, AInputEvent* event) {
3392 return 0;
3393}
3394
3395static void processCommand(struct android_app* app, int32_t cmd) {
3396 switch(cmd) {
3397 case APP_CMD_INIT_WINDOW: {
3398 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003399 // We're getting a new window. If the app is starting up, we
3400 // need to initialize. If the app has already been
3401 // initialized, that means that we lost our previous window,
3402 // which means that we have a lot of work to do. At a minimum,
3403 // we need to destroy the swapchain and surface associated with
3404 // the old window, and create a new surface and swapchain.
3405 // However, since there are a lot of other objects/state that
3406 // is tied to the swapchain, it's easiest to simply cleanup and
3407 // start over (i.e. use a brute-force approach of re-starting
3408 // the app)
3409 if (demo.prepared) {
3410 demo_cleanup(&demo);
3411 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003412
3413 // Parse Intents into argc, argv
3414 // Use the following key to send arguments, i.e.
3415 // --es args "--validate"
3416 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003417 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003418 int argc = 0;
3419 char** argv = get_args(app, key, appTag, &argc);
3420
3421 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3422 for (int i = 0; i < argc; i++)
3423 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3424
3425 demo_init(&demo, argc, argv);
3426
3427 // Free the argv malloc'd by get_args
3428 for (int i = 0; i < argc; i++)
3429 free(argv[i]);
3430
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003431 demo.window = (void*)app->window;
3432 demo_init_vk_swapchain(&demo);
3433 demo_prepare(&demo);
3434 initialized = true;
3435 }
3436 break;
3437 }
3438 case APP_CMD_GAINED_FOCUS: {
3439 active = true;
3440 break;
3441 }
3442 case APP_CMD_LOST_FOCUS: {
3443 active = false;
3444 break;
3445 }
3446 }
3447}
3448
3449void android_main(struct android_app *app)
3450{
3451 app_dummy();
3452
Cody Northrop8707a6e2016-04-26 19:59:19 -06003453#ifdef ANDROID
3454 int vulkanSupport = InitVulkan();
3455 if (vulkanSupport == 0)
3456 return;
3457#endif
3458
Ian Elliottf05d5d12016-05-17 12:03:35 -06003459 demo.prepared = false;
3460
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003461 app->onAppCmd = processCommand;
3462 app->onInputEvent = processInput;
3463
3464 while(1) {
3465 int events;
3466 struct android_poll_source* source;
3467 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3468 if (source) {
3469 source->process(app, source);
3470 }
3471
3472 if (app->destroyRequested != 0) {
3473 demo_cleanup(&demo);
3474 return;
3475 }
3476 }
3477 if (initialized && active) {
3478 demo_run(&demo);
3479 }
3480 }
3481
3482}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003483#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003484int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003485 struct demo demo;
3486
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003487 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003488#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003489 if (demo.use_xlib)
3490 demo_create_xlib_window(&demo);
3491 else
3492 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003493#elif defined(VK_USE_PLATFORM_XCB_KHR)
3494 demo_create_xcb_window(&demo);
3495#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07003496 demo.use_xlib = true;
Tony Barbour8295ac42016-08-08 11:04:17 -06003497 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003498#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3499 demo_create_window(&demo);
3500#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003501
Tony Barbourcc69f452015-10-08 13:59:42 -06003502 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003503
3504 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003505
Tony Barbour8295ac42016-08-08 11:04:17 -06003506#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003507 if (demo.use_xlib)
3508 demo_run_xlib(&demo);
3509 else
3510 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003511#elif defined(VK_USE_PLATFORM_XCB_KHR)
3512 demo_run_xcb(&demo);
3513#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3514 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003515#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3516 demo_run(&demo);
3517#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003518
3519 demo_cleanup(&demo);
3520
Karl Schultz0218c002016-03-22 17:06:13 -06003521 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003522}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003523#endif