blob: 543a81ee959096b86cfadf22bc8139c9b41a65d8 [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
2* Copyright (c) 2015-2016 The Khronos Group Inc.
3* Copyright (c) 2015-2016 Valve Corporation
4* Copyright (c) 2015-2016 LunarG, Inc.
5*
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Author: Chia-I Wu <olv@lunarg.com>
19* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20* Author: Ian Elliott <ian@LunarG.com>
21* Author: Jon Ashburn <jon@lunarg.com>
22* Author: Gwan-gyeong Mun <elongbug@gmail.com>
Tony Barbour66a56c32016-09-21 13:10:56 -060023* Author: Tony Barbour <tony@LunarG.com>
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090024*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070025
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060026#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
31#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070032#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090033#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060034#include <X11/Xutil.h>
35#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036
Ian Elliott639ca472015-04-16 15:23:05 -060037#ifdef _WIN32
38#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060039#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060040#endif // _WIN32
41
Cody Northrop8707a6e2016-04-26 19:59:19 -060042#ifdef ANDROID
43#include "vulkan_wrapper.h"
44#else
David Pinedoc5193c12015-11-06 12:54:48 -070045#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060046#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070047
David Pinedo541526f2015-11-24 09:00:24 -070048#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060049#include "linmath.h"
50
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060051#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054
Tony Barbour66a56c32016-09-21 13:10:56 -060055// Allow a maximum of two outstanding presentation operations.
56#define FRAME_LAG 2
57
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060058#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
59
Tony Barbourfdc2d352015-04-22 09:02:32 -060060#if defined(NDEBUG) && defined(__GNUC__)
61#define U_ASSERT_ONLY __attribute__((unused))
62#else
63#define U_ASSERT_ONLY
64#endif
65
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090066#if defined(__GNUC__)
67#define UNUSED __attribute__((unused))
68#else
69#define UNUSED
70#endif
71
Ian Elliott07264132015-04-28 11:35:02 -060072#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060073bool in_callback = false;
Karl Schultze4dc75c2016-02-02 15:37:51 -070074#define ERR_EXIT(err_msg, err_class) \
75 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060076 if (!demo->suppress_popups) \
77 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070078 exit(1); \
79 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060080
Michael Lentinec6cde4b2016-04-18 13:20:45 -050081#elif defined __ANDROID__
82#include <android/log.h>
83#define ERR_EXIT(err_msg, err_class) \
84 do { \
85 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
86 exit(1); \
87 } while (0)
88#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070089#define ERR_EXIT(err_msg, err_class) \
90 do { \
91 printf(err_msg); \
92 fflush(stdout); \
93 exit(1); \
94 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050095#endif
Ian Elliott07264132015-04-28 11:35:02 -060096
Karl Schultze4dc75c2016-02-02 15:37:51 -070097#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
98 { \
99 demo->fp##entrypoint = \
100 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
101 if (demo->fp##entrypoint == NULL) { \
102 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
103 "vkGetInstanceProcAddr Failure"); \
104 } \
105 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600106
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600107static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
108
Karl Schultze4dc75c2016-02-02 15:37:51 -0700109#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
110 { \
111 if (!g_gdpa) \
112 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
113 demo->inst, "vkGetDeviceProcAddr"); \
114 demo->fp##entrypoint = \
115 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
116 if (demo->fp##entrypoint == NULL) { \
117 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
118 "vkGetDeviceProcAddr Failure"); \
119 } \
120 }
Ian Elliott673898b2015-06-22 15:07:49 -0600121
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600122/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700123 * structure to track all objects related to a texture.
124 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600125struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600126 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700127
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600128 VkImage image;
129 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600130
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800131 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500132 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600133 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700134 int32_t tex_width, tex_height;
135};
136
Karl Schultze4dc75c2016-02-02 15:37:51 -0700137static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600138
Karl Schultz0218c002016-03-22 17:06:13 -0600139static int validation_error = 0;
140
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600141struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700143 float mvp[4][4];
144 float position[12 * 3][4];
145 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146};
147
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600148struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700150 float mvp[4][4];
151 float position[12 * 3][4];
152 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600153};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600154
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600155//--------------------------------------------------------------------------------------
156// Mesh and VertexFormat Data
157//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700158// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600159static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 -1.0f,-1.0f, 1.0f,
162 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 -1.0f, 1.0f,-1.0f,
165 -1.0f,-1.0f,-1.0f,
166
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f, 1.0f,-1.0f,
169 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600176 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600179 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 -1.0f, 1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 1.0f, 1.0f, 1.0f,
190 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 1.0f,-1.0f,-1.0f,
193 1.0f, 1.0f,-1.0f,
194
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600197 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 1.0f,-1.0f, 1.0f,
200 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600201};
202
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600203static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700204 0.0f, 1.0f, // -X side
205 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800207 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208 0.0f, 0.0f,
209 0.0f, 1.0f,
210
Rene Lindsay76867e82016-07-29 10:49:11 -0700211 1.0f, 1.0f, // -Z side
212 0.0f, 0.0f,
213 0.0f, 1.0f,
214 1.0f, 1.0f,
215 1.0f, 0.0f,
216 0.0f, 0.0f,
217
218 1.0f, 0.0f, // -Y side
219 1.0f, 1.0f,
220 0.0f, 1.0f,
221 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600222 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224
Rene Lindsay76867e82016-07-29 10:49:11 -0700225 1.0f, 0.0f, // +Y side
226 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231
Rene Lindsay76867e82016-07-29 10:49:11 -0700232 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700234 0.0f, 1.0f,
235 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700238
239 0.0f, 0.0f, // +Z side
240 0.0f, 1.0f,
241 1.0f, 0.0f,
242 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600243 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700244 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600245};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700246// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249 int i;
250
251 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700252 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600253 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
254 }
255 printf("\n");
256 fflush(stdout);
257}
258
Karl Schultze4dc75c2016-02-02 15:37:51 -0700259void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600260 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600262 printf("\n");
263 fflush(stdout);
264}
265
Karl Schultze4dc75c2016-02-02 15:37:51 -0700266VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600267BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
268 uint64_t srcObject, size_t location, int32_t msgCode,
269 const char *pLayerPrefix, const char *pMsg,
270 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700271#ifndef WIN32
272 raise(SIGTRAP);
273#else
274 DebugBreak();
275#endif
276
277 return false;
278}
279
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600280typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600281 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800282 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600283 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600284 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600285} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600286
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600287struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500288#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600289#define APP_NAME_STR_LEN 80
290 HINSTANCE connection; // hInstance - Windows Instance
291 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700292 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600293 POINT minsize; // minimum window size
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600294#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600295 Display* display;
296 Window xlib_window;
297 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600298
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600299 xcb_connection_t *connection;
300 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600301 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800302 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900303#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
304 struct wl_display *display;
305 struct wl_registry *registry;
306 struct wl_compositor *compositor;
307 struct wl_surface *window;
308 struct wl_shell *shell;
309 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500310#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
311 ANativeWindow* window;
312#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700313 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600314 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700315 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600316 bool use_xlib;
Tony Barbour22e06272016-09-07 16:07:56 -0600317 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600320 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600322 VkQueue graphics_queue;
323 VkQueue present_queue;
324 uint32_t graphics_queue_family_index;
325 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600326 VkSemaphore image_acquired_semaphores[FRAME_LAG];
327 VkSemaphore draw_complete_semaphores[FRAME_LAG];
328 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600329 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600330 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600331 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332
Tony Barbourda2bad52016-01-22 14:36:40 -0700333 uint32_t enabled_extension_count;
334 uint32_t enabled_layer_count;
335 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600336 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700337
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600339 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600340 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341
Karl Schultze4dc75c2016-02-02 15:37:51 -0700342 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
343 fpGetPhysicalDeviceSurfaceSupportKHR;
344 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
345 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
346 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
347 fpGetPhysicalDeviceSurfaceFormatsKHR;
348 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
349 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600350 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
351 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
352 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
353 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
354 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600355 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600356 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600357 SwapchainBuffers *buffers;
Tony Barbour66a56c32016-09-21 13:10:56 -0600358 VkFence fences[FRAME_LAG];
359 bool fencesInited[FRAME_LAG];
360 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800362 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600363 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
365 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600368 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800369 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500370 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600371 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372 } depth;
373
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600374 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
376 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800378 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500379 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600380 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600381 } uniform_data;
382
Karl Schultze4dc75c2016-02-02 15:37:51 -0700383 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500384 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600386 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800387 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600389
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600390 mat4x4 projection_matrix;
391 mat4x4 view_matrix;
392 mat4x4 model_matrix;
393
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600394 float spin_angle;
395 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600396 bool pause;
397
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600398 VkShaderModule vert_shader_module;
399 VkShaderModule frag_shader_module;
400
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 VkDescriptorPool desc_pool;
402 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800403
Tony Barbourd8e504f2015-10-08 14:26:24 -0600404 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800405
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600406 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600407 int32_t curFrame;
408 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600409 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600410 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600411 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700412 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
413 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
414 VkDebugReportCallbackEXT msg_callback;
415 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600416
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600417 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600418 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600419};
420
lenny-lunargfbe22392016-06-06 11:07:53 -0600421VKAPI_ATTR VkBool32 VKAPI_CALL
422dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
423 uint64_t srcObject, size_t location, int32_t msgCode,
424 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600425
426 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600427 char *message = (char *)malloc(strlen(pMsg) + 100);
428
429 assert(message);
430
Cody Northropaf28a532016-09-27 10:50:57 -0600431 // We know we're submitting queues without fences, ignore this
432 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer"))
433 return false;
434
435 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
436 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600437 validation_error = 1;
438 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600439 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
440 validation_error = 1;
441 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
442 sprintf(message, "PERFOERMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
443 validation_error = 1;
444 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
445 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
446 validation_error = 1;
447 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
448 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600449 validation_error = 1;
450 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600451 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600452 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600453 }
454
455#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600456
Tony Barbour602ca4f2016-09-12 14:02:43 -0600457 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600458 struct demo *demo = (struct demo*) pUserData;
459 if (!demo->suppress_popups)
460 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600461 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600462
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600463#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600464
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600465 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600466 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600467 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600468 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600469 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600470 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600471 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600472 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600473 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600474 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600475 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600476 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600477 }
Cody Northropaf28a532016-09-27 10:50:57 -0600478
lenny-lunargfbe22392016-06-06 11:07:53 -0600479#else
Cody Northropaf28a532016-09-27 10:50:57 -0600480
lenny-lunargfbe22392016-06-06 11:07:53 -0600481 printf("%s\n", message);
482 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600483
lenny-lunargfbe22392016-06-06 11:07:53 -0600484#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600485
lenny-lunargfbe22392016-06-06 11:07:53 -0600486 free(message);
487
Cody Northropaf28a532016-09-27 10:50:57 -0600488 //clang-format on
489
lenny-lunargfbe22392016-06-06 11:07:53 -0600490 /*
491 * false indicates that layer should not bail-out of an
492 * API call that had validation failures. This may mean that the
493 * app dies inside the driver due to invalid parameter(s).
494 * That's what would happen without validation layers, so we'll
495 * keep that behavior here.
496 */
497 return false;
498}
499
Ian Elliottcf074d32015-10-16 18:02:43 -0600500// Forward declaration:
501static void demo_resize(struct demo *demo);
502
Karl Schultze4dc75c2016-02-02 15:37:51 -0700503static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
504 VkFlags requirements_mask,
505 uint32_t *typeIndex) {
506 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600507 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700508 if ((typeBits & 1) == 1) {
509 // Type is available, does it match user properties?
510 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
511 requirements_mask) == requirements_mask) {
512 *typeIndex = i;
513 return true;
514 }
515 }
516 typeBits >>= 1;
517 }
518 // No memory types matched, return failure
519 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600520}
521
Karl Schultze4dc75c2016-02-02 15:37:51 -0700522static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600523 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600524
Tony Barbource7524e2016-07-28 12:01:45 -0600525 // This function could get called twice if the texture uses a staging buffer
526 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528 return;
529
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600530 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600531 assert(!err);
532
Tony Barbource7524e2016-07-28 12:01:45 -0600533 VkFence fence;
534 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
535 .pNext = NULL,
536 .flags = 0};
537 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700538 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700539 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
540 .pNext = NULL,
541 .waitSemaphoreCount = 0,
542 .pWaitSemaphores = NULL,
543 .pWaitDstStageMask = NULL,
544 .commandBufferCount = 1,
545 .pCommandBuffers = cmd_bufs,
546 .signalSemaphoreCount = 0,
547 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600548
Tony Barbource7524e2016-07-28 12:01:45 -0600549 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550 assert(!err);
551
Tony Barbource7524e2016-07-28 12:01:45 -0600552 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553 assert(!err);
554
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600555 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600556 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600557 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600558}
559
Karl Schultze4dc75c2016-02-02 15:37:51 -0700560static void demo_set_image_layout(struct demo *demo, VkImage image,
561 VkImageAspectFlags aspectMask,
562 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700563 VkImageLayout new_image_layout,
564 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600565 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600566
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600567 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800568 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800569 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600570 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800571 .commandPool = demo->cmd_pool,
572 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700573 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600574 };
575
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800576 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600577 assert(!err);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700578 VkCommandBufferBeginInfo cmd_buf_info = {
579 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
580 .pNext = NULL,
581 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700582 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700583 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600584 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600585 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600586 }
587
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600588 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600589 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600590 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700591 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800592 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600593 .oldLayout = old_image_layout,
594 .newLayout = new_image_layout,
595 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700596 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600597
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800598 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600599 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800600 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600601 }
602
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700603 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700604 image_memory_barrier.dstAccessMask =
605 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700606 }
607
608 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700609 image_memory_barrier.dstAccessMask =
610 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700611 }
612
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600613 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600614 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700615 image_memory_barrier.dstAccessMask =
616 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600617 }
618
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600619 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600620
Tony Barbour50bbca42015-06-29 16:20:35 -0600621 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
622 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600623
Karl Schultze4dc75c2016-02-02 15:37:51 -0700624 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
625 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600626}
627
Karl Schultze4dc75c2016-02-02 15:37:51 -0700628static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700629 const VkCommandBufferBeginInfo cmd_buf_info = {
630 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
631 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600632 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700633 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700634 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800635 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700636 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
637 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800638 };
639 const VkRenderPassBeginInfo rp_begin = {
640 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
641 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800642 .renderPass = demo->render_pass,
643 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800644 .renderArea.offset.x = 0,
645 .renderArea.offset.y = 0,
646 .renderArea.extent.width = demo->width,
647 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600648 .clearValueCount = 2,
649 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700650 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800651 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600653 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600654 assert(!err);
655
Tony Barbour5c5b1632016-04-26 11:13:48 -0600656 VkImageMemoryBarrier image_memory_barrier = {
657 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
658 .pNext = NULL,
Tony Barbour8daa6712016-09-29 11:36:49 -0600659 .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour5c5b1632016-04-26 11:13:48 -0600660 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barbour22e06272016-09-07 16:07:56 -0600661 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour5c5b1632016-04-26 11:13:48 -0600662 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
663 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
664 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
665 .image = demo->buffers[demo->current_buffer].image,
666 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
667
Tony Barbource7524e2016-07-28 12:01:45 -0600668 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
669 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
670 NULL, 0, NULL, 1, &image_memory_barrier);
Tony Barbour5c5b1632016-04-26 11:13:48 -0600671
Chia-I Wuf051d262015-10-27 19:25:11 +0800672 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800673
Karl Schultze4dc75c2016-02-02 15:37:51 -0700674 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
675 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
676 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
677 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600678 VkViewport viewport;
679 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700680 viewport.height = (float)demo->height;
681 viewport.width = (float)demo->width;
682 viewport.minDepth = (float)0.0f;
683 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700684 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600685
686 VkRect2D scissor;
687 memset(&scissor, 0, sizeof(scissor));
688 scissor.extent.width = demo->width;
689 scissor.extent.height = demo->height;
690 scissor.offset.x = 0;
691 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700692 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600693 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600694 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600695 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800696 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697
Tony Barbour22e06272016-09-07 16:07:56 -0600698 if (demo->separate_present_queue) {
699 // We have to transfer ownership from the graphics queue family to the
700 // present queue family to be able to present. Note that we don't have
701 // to transfer from present queue family back to graphics queue family at
702 // the start of the next frame because we don't care about the image's
703 // contents at that point.
704 VkImageMemoryBarrier image_ownership_barrier = {
705 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
706 .pNext = NULL,
707 .srcAccessMask = 0,
708 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
709 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
710 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
711 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
712 .dstQueueFamilyIndex = demo->present_queue_family_index,
713 .image = demo->buffers[demo->current_buffer].image,
714 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
715
716 vkCmdPipelineBarrier(cmd_buf,
717 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
718 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0,
719 0, NULL, 0, NULL, 1, &image_ownership_barrier);
720 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600721 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722 assert(!err);
723}
724
Tony Barbour22e06272016-09-07 16:07:56 -0600725void demo_build_image_ownership_cmd(struct demo *demo, int i) {
726 VkResult U_ASSERT_ONLY err;
727
728 const VkCommandBufferBeginInfo cmd_buf_info = {
729 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
730 .pNext = NULL,
731 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
732 .pInheritanceInfo = NULL,
733 };
734 err = vkBeginCommandBuffer(demo->buffers[i].graphics_to_present_cmd,
735 &cmd_buf_info);
736 assert(!err);
737
738 VkImageMemoryBarrier image_ownership_barrier = {
739 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
740 .pNext = NULL,
741 .srcAccessMask = 0,
742 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
743 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
744 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
745 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
746 .dstQueueFamilyIndex = demo->present_queue_family_index,
747 .image = demo->buffers[i].image,
748 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
749
750 vkCmdPipelineBarrier(demo->buffers[i].graphics_to_present_cmd,
751 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
752 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
753 NULL, 0, NULL, 1, &image_ownership_barrier);
754 err = vkEndCommandBuffer(demo->buffers[i].graphics_to_present_cmd);
755 assert(!err);
756}
757
Karl Schultze4dc75c2016-02-02 15:37:51 -0700758void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600759 mat4x4 MVP, Model, VP;
760 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600761 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600762 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600763
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600764 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600765
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600766 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600767 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700768 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
769 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600770 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600771
Karl Schultze4dc75c2016-02-02 15:37:51 -0700772 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
773 demo->uniform_data.mem_alloc.allocationSize, 0,
774 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600775 assert(!err);
776
Karl Schultze4dc75c2016-02-02 15:37:51 -0700777 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600778
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600779 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600780}
781
Karl Schultze4dc75c2016-02-02 15:37:51 -0700782static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600783 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600784
Tony Barbour66a56c32016-09-21 13:10:56 -0600785 if (demo->fencesInited[demo->frame_index])
786 {
787 // Ensure no more than FRAME_LAG presentations are outstanding
788 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
789 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
790 }
791
Ian Elliottaaae5352015-07-06 14:27:58 -0600792 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700793 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour66a56c32016-09-21 13:10:56 -0600794 demo->image_acquired_semaphores[demo->frame_index], demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600795 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600796 demo->fencesInited[demo->frame_index] = true;
797
Ian Elliottcf074d32015-10-16 18:02:43 -0600798 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
799 // demo->swapchain is out of date (e.g. the window was resized) and
800 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600801 demo->frame_index += 1;
802 demo->frame_index %= FRAME_LAG;
803
Ian Elliottcf074d32015-10-16 18:02:43 -0600804 demo_resize(demo);
805 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600806 return;
807 } else if (err == VK_SUBOPTIMAL_KHR) {
808 // demo->swapchain is not as optimal as it could be, but the platform's
809 // presentation engine will still present the image correctly.
810 } else {
811 assert(!err);
812 }
Tony Barbource7524e2016-07-28 12:01:45 -0600813 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600814 // that the image won't be rendered to until the presentation
815 // engine has fully released ownership to the application, and it is
816 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600817 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600818 VkPipelineStageFlags pipe_stage_flags;
819 VkSubmitInfo submit_info;
820 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
821 submit_info.pNext = NULL;
822 submit_info.pWaitDstStageMask = &pipe_stage_flags;
823 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
824 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600825 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600826 submit_info.commandBufferCount = 1;
827 submit_info.pCommandBuffers = &demo->buffers[demo->current_buffer].cmd;
828 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600829 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura2a67812016-07-18 13:21:06 -0600830 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600831 assert(!err);
832
Tony Barbour22e06272016-09-07 16:07:56 -0600833 if (demo->separate_present_queue) {
834 // If we are using separate queues, change image ownership to the
835 // present queue before presenting, waiting for the draw complete
836 // semaphore and signalling the ownership released semaphore when finished
837 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
838 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600839 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600840 submit_info.commandBufferCount = 1;
841 submit_info.pCommandBuffers =
842 &demo->buffers[demo->current_buffer].graphics_to_present_cmd;
843 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600844 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600845 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
846 assert(!err);
847 }
848
849 // If we are using separate queues we have to wait for image ownership,
850 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600851 VkPresentInfoKHR present = {
852 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600853 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600854 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600855 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600856 ? &demo->image_ownership_semaphores[demo->frame_index]
857 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600858 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700859 .pSwapchains = &demo->swapchain,
860 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600861 };
862
Tony Barboura2a67812016-07-18 13:21:06 -0600863 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600864 demo->frame_index += 1;
865 demo->frame_index %= FRAME_LAG;
866
Ian Elliottcf074d32015-10-16 18:02:43 -0600867 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
868 // demo->swapchain is out of date (e.g. the window was resized) and
869 // must be recreated:
870 demo_resize(demo);
871 } else if (err == VK_SUBOPTIMAL_KHR) {
872 // demo->swapchain is not as optimal as it could be, but the platform's
873 // presentation engine will still present the image correctly.
874 } else {
875 assert(!err);
876 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600877}
878
Karl Schultze4dc75c2016-02-02 15:37:51 -0700879static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600881 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600882
Ian Elliottb08e0d92015-11-20 11:55:46 -0700883 // Check the surface capabilities and formats
884 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700885 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
886 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600887 assert(!err);
888
Ian Elliott8528eff2015-08-07 15:56:59 -0600889 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700890 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
891 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600892 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600893 VkPresentModeKHR *presentModes =
894 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600895 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700896 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
897 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600898 assert(!err);
899
Ian Elliotta0781972015-08-21 15:09:33 -0600900 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600901 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
902 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
903 // If the surface size is undefined, the size is set to the size
904 // of the images requested, which must fit within the minimum and
905 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600906 swapchainExtent.width = demo->width;
907 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600908
909 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
910 swapchainExtent.width = surfCapabilities.minImageExtent.width;
911 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
912 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
913 }
914
915 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
916 swapchainExtent.height = surfCapabilities.minImageExtent.height;
917 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
918 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
919 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700920 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600921 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700922 swapchainExtent = surfCapabilities.currentExtent;
923 demo->width = surfCapabilities.currentExtent.width;
924 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600925 }
926
Tony Barbour66a56c32016-09-21 13:10:56 -0600927 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600928 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600929 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottb18fdde2016-10-03 12:11:12 -0600930 // There are times when you may wish to use another present mode. The
931 // following code shows how to select them, and the comments provide some
932 // reasons you may wish to use them.
933 //
934 // It should be noted that Vulkan 1.0 doesn't provide a method for
935 // synchronizing rendering with the presentation engine's display. There
936 // is a method provided for throttling rendering with the display, but
937 // there are some presentation engines for which this method will not work.
938 // If an application doesn't throttle its rendering, and if it renders much
939 // faster than the refresh rate of the display, this can waste power on
940 // mobile devices. That is because power is being spent rendering images
941 // that may never be seen.
942//#define DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR
943//#define DESIRE_VK_PRESENT_MODE_MAILBOX_KHR
944//#define DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR
945#if defined(DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR)
946 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
947 // tearing, or have some way of synchronizing their rendering with the
948 // display.
949 for (size_t i = 0; i < presentModeCount; ++i) {
950 if (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR) {
951 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
952 break;
953 }
954 }
955#elif defined(DESIRE_VK_PRESENT_MODE_MAILBOX_KHR)
956 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
957 // generally render a new presentable image every refresh cycle, but are
958 // occasionally early. In this case, the application wants the new image
959 // to be displayed instead of the previously-queued-for-presentation image
960 // that has not yet been displayed.
961 for (size_t i = 0; i < presentModeCount; ++i) {
962 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
963 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
964 break;
965 }
966 }
967#elif defined(DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR)
968 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
969 // render a new presentable image every refresh cycle, but are occasionally
970 // late. In this case (perhaps because of stuttering/latency concerns),
971 // the application wants the late image to be immediately displayed, even
972 // though that may mean some tearing.
973 for (size_t i = 0; i < presentModeCount; ++i) {
974 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
975 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
976 break;
977 }
978 }
979#endif
Ian Elliottaaae5352015-07-06 14:27:58 -0600980
Ian Elliottcf7f7482016-08-19 03:46:58 -0600981 // Determine the number of VkImage's to use in the swap chain.
982 // Application desires to only acquire 1 image at a time (which is
983 // "surfCapabilities.minImageCount").
984 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
985 // If maxImageCount is 0, we can ask for as many images as we want;
986 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700987 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600988 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600989 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600990 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600991 }
992
Tony Barbour9fd6d352015-09-16 15:01:32 -0600993 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700994 if (surfCapabilities.supportedTransforms &
995 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700996 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600997 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700998 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600999 }
1000
Tony Barboura2a67812016-07-18 13:21:06 -06001001 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001002 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001003 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001004 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001005 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001006 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001007 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001008 .imageExtent =
1009 {
1010 .width = swapchainExtent.width, .height = swapchainExtent.height,
1011 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001012 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001013 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -07001014 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001015 .imageArrayLayers = 1,
1016 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1017 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001018 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001019 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001020 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001021 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001022 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001023 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -06001024 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001025 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001026 assert(!err);
1027
Ian Elliottcf074d32015-10-16 18:02:43 -06001028 // If we just re-created an existing swapchain, we should destroy the old
1029 // swapchain at this point.
1030 // Note: destroying the swapchain also cleans up all its associated
1031 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001032 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001033 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001034 }
1035
1036 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001037 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001038 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001039
Karl Schultze4dc75c2016-02-02 15:37:51 -07001040 VkImage *swapchainImages =
1041 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001042 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001043 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001044 &demo->swapchainImageCount,
1045 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001046 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001047
Karl Schultze4dc75c2016-02-02 15:37:51 -07001048 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
1049 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -06001050 assert(demo->buffers);
1051
Ian Elliotta0781972015-08-21 15:09:33 -06001052 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001053 VkImageViewCreateInfo color_image_view = {
1054 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055 .pNext = NULL,
1056 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001057 .components =
1058 {
1059 .r = VK_COMPONENT_SWIZZLE_R,
1060 .g = VK_COMPONENT_SWIZZLE_G,
1061 .b = VK_COMPONENT_SWIZZLE_B,
1062 .a = VK_COMPONENT_SWIZZLE_A,
1063 },
1064 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1065 .baseMipLevel = 0,
1066 .levelCount = 1,
1067 .baseArrayLayer = 0,
1068 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001069 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1070 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001071 };
1072
Ian Elliotta0781972015-08-21 15:09:33 -06001073 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001074
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001075 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001076
Karl Schultze4dc75c2016-02-02 15:37:51 -07001077 err = vkCreateImageView(demo->device, &color_image_view, NULL,
1078 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001079 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001080
1081 // The draw loop will be expecting the presentable images to be in
1082 // LAYOUT_PRESENT_SRC_KHR
1083 // since that's how they're left at the end of every frame.
1084 demo_set_image_layout(
1085 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1086 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001087 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001088
Mark Young04fd3d82016-01-25 14:43:50 -07001089 if (NULL != presentModes) {
1090 free(presentModes);
1091 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001092}
1093
Karl Schultze4dc75c2016-02-02 15:37:51 -07001094static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001095 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001096 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001097 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001098 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001099 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001100 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001101 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001102 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001103 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001104 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001105 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001106 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001107 .flags = 0,
1108 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001109
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001110 VkImageViewCreateInfo view = {
1111 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001112 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001113 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001114 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001115 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1116 .baseMipLevel = 0,
1117 .levelCount = 1,
1118 .baseArrayLayer = 0,
1119 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001120 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001121 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001122 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001123
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001125 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001126 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001127
1128 demo->depth.format = depth_format;
1129
1130 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001131 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132 assert(!err);
1133
Karl Schultze4dc75c2016-02-02 15:37:51 -07001134 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001135 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001136
Chia-I Wuf48700b2015-11-10 16:21:09 +08001137 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001138 demo->depth.mem_alloc.pNext = NULL;
1139 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1140 demo->depth.mem_alloc.memoryTypeIndex = 0;
1141
Karl Schultze4dc75c2016-02-02 15:37:51 -07001142 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1143 0, /* No requirements */
1144 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001145 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001146
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001147 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001148 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1149 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001150 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001151
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001152 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001153 err =
1154 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001155 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001156
Karl Schultze4dc75c2016-02-02 15:37:51 -07001157 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1158 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001159 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1160 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001161
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162 /* create image view */
1163 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001164 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001165 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001166}
1167
Tony Barbour1a86d032015-09-21 15:17:33 -06001168/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001169bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001170 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001171#ifdef __ANDROID__
1172#include <lunarg.ppm.h>
1173 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001174 cPtr = (char*)lunarg_ppm;
1175 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001176 return false;
1177 }
1178 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001179 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001180 if (rgba_data == NULL) {
1181 return true;
1182 }
1183 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001184 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001185 return false;
1186 }
1187 while(strncmp(cPtr++, "\n", 1));
1188
1189 for (int y = 0; y < *height; y++) {
1190 uint8_t *rowPtr = rgba_data;
1191 for (int x = 0; x < *width; x++) {
1192 memcpy(rowPtr, cPtr, 3);
1193 rowPtr[3] = 255; /* Alpha of 1 */
1194 rowPtr += 4;
1195 cPtr += 3;
1196 }
1197 rgba_data += layout->rowPitch;
1198 }
1199
1200 return true;
1201#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001202 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001203 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001204
Tony Barbour1a86d032015-09-21 15:17:33 -06001205 if (!fPtr)
1206 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001207
Tony Barbour1a86d032015-09-21 15:17:33 -06001208 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001209 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1210 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001211 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001212 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001213
Tony Barbour1a86d032015-09-21 15:17:33 -06001214 do {
1215 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001216 if (cPtr == NULL) {
1217 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001218 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001219 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001220 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001221
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001222 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001223 if (rgba_data == NULL) {
1224 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001225 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001226 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001227 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001228 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001229 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1230 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001231 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001232 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001233
Karl Schultze4dc75c2016-02-02 15:37:51 -07001234 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001235 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001236 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001237 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001238 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001239 rowPtr[3] = 255; /* Alpha of 1 */
1240 rowPtr += 4;
1241 }
1242 rgba_data += layout->rowPitch;
1243 }
1244 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001245 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001246#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001247}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001248
Karl Schultze4dc75c2016-02-02 15:37:51 -07001249static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001250 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001251 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001252 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001253 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001254 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001255 int32_t tex_width;
1256 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001257 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001258 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001259
Karl Schultze4dc75c2016-02-02 15:37:51 -07001260 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001261 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001262 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001263
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001264 tex_obj->tex_width = tex_width;
1265 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001267 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001268 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001269 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001270 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001271 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001272 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001274 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001275 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001276 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001277 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001278 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001279 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001280 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001281
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001282 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001283
Karl Schultze4dc75c2016-02-02 15:37:51 -07001284 err =
1285 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001286 assert(!err);
1287
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001288 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001289
Chia-I Wuf48700b2015-11-10 16:21:09 +08001290 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001291 tex_obj->mem_alloc.pNext = NULL;
1292 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1293 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001294
Karl Schultze4dc75c2016-02-02 15:37:51 -07001295 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1296 required_props,
1297 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001298 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001299
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001300 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001301 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001302 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001303 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001304
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001305 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001306 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001307 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001308
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001309 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001310 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001311 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001312 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001313 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001314 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001315 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001316 void *data;
1317
Karl Schultze4dc75c2016-02-02 15:37:51 -07001318 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1319 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001320
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1322 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001323 assert(!err);
1324
1325 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1326 fprintf(stderr, "Error loading texture: %s\n", filename);
1327 }
1328
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001329 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001330 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001331
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001332 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001333 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001334 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1335 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 /* setting the image layout does not reference the actual memory so no need
1337 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001338}
1339
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340static void demo_destroy_texture_image(struct demo *demo,
1341 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001342 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001343 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1344 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001345}
1346
Karl Schultze4dc75c2016-02-02 15:37:51 -07001347static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001348 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001349 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001350 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001351
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001352 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001353
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001355 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001356
Karl Schultze4dc75c2016-02-02 15:37:51 -07001357 if ((props.linearTilingFeatures &
1358 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1359 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001360 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001361 demo_prepare_texture_image(
1362 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1363 VK_IMAGE_USAGE_SAMPLED_BIT,
1364 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1365 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 } else if (props.optimalTilingFeatures &
1367 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001368 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001369 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001370
1371 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001372 demo_prepare_texture_image(
1373 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1374 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1375 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1376 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001377
Karl Schultze4dc75c2016-02-02 15:37:51 -07001378 demo_prepare_texture_image(
1379 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1380 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1381 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001382
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001383 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001384 VK_IMAGE_ASPECT_COLOR_BIT,
1385 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001386 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1387 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001388
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001389 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001390 VK_IMAGE_ASPECT_COLOR_BIT,
1391 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001392 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1393 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001394
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001395 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001396 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1397 .srcOffset = {0, 0, 0},
1398 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1399 .dstOffset = {0, 0, 0},
1400 .extent = {staging_texture.tex_width,
1401 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001402 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001403 vkCmdCopyImage(
1404 demo->cmd, staging_texture.image,
1405 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1406 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001407
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001408 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001409 VK_IMAGE_ASPECT_COLOR_BIT,
1410 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001411 demo->textures[i].imageLayout,
1412 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001413
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001414 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001415
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001416 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001417 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001418 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1419 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001420 }
1421
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001422 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001423 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001425 .magFilter = VK_FILTER_NEAREST,
1426 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001427 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001428 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1429 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1430 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001431 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001432 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001433 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001434 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 .minLod = 0.0f,
1436 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001437 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001438 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001440
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001441 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001442 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001444 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001445 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001446 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001447 .components =
1448 {
1449 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1450 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1451 },
1452 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001453 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001455
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001457 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001458 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459 assert(!err);
1460
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461 /* create image view */
1462 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001463 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001464 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001465 assert(!err);
1466 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001467}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468
Karl Schultze4dc75c2016-02-02 15:37:51 -07001469void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001470 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001471 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001472 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001473 int i;
1474 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001475 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001476 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001477 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001478
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001479 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001480 mat4x4_mul(MVP, VP, demo->model_matrix);
1481 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001482 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001483
Karl Schultze4dc75c2016-02-02 15:37:51 -07001484 for (i = 0; i < 12 * 3; i++) {
1485 data.position[i][0] = g_vertex_buffer_data[i * 3];
1486 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1487 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001488 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001489 data.attr[i][0] = g_uv_buffer_data[2 * i];
1490 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001491 data.attr[i][2] = 0;
1492 data.attr[i][3] = 0;
1493 }
1494
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001495 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001496 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001497 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001498 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001499 err =
1500 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001501 assert(!err);
1502
Karl Schultze4dc75c2016-02-02 15:37:51 -07001503 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1504 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001505
Chia-I Wuf48700b2015-11-10 16:21:09 +08001506 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001507 demo->uniform_data.mem_alloc.pNext = NULL;
1508 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1509 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1510
Karl Schultze4dc75c2016-02-02 15:37:51 -07001511 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001512 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1513 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001514 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001515 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001516
Karl Schultze4dc75c2016-02-02 15:37:51 -07001517 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1518 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001519 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001520
Karl Schultze4dc75c2016-02-02 15:37:51 -07001521 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1522 demo->uniform_data.mem_alloc.allocationSize, 0,
1523 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001524 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001525
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001526 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001527
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001528 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001529
Karl Schultze4dc75c2016-02-02 15:37:51 -07001530 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1531 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001532 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001533
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001534 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1535 demo->uniform_data.buffer_info.offset = 0;
1536 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001537}
1538
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001540 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001541 [0] =
1542 {
1543 .binding = 0,
1544 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1545 .descriptorCount = 1,
1546 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1547 .pImmutableSamplers = NULL,
1548 },
1549 [1] =
1550 {
1551 .binding = 1,
1552 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1553 .descriptorCount = DEMO_TEXTURE_COUNT,
1554 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1555 .pImmutableSamplers = NULL,
1556 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001557 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001558 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001559 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001560 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001561 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001562 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001563 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001564 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565
Karl Schultze4dc75c2016-02-02 15:37:51 -07001566 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1567 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001568 assert(!err);
1569
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001570 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001571 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1572 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001573 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001574 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001575 };
1576
Karl Schultze4dc75c2016-02-02 15:37:51 -07001577 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001578 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001579 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001580}
1581
Karl Schultze4dc75c2016-02-02 15:37:51 -07001582static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001583 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001584 [0] =
1585 {
1586 .format = demo->format,
1587 .samples = VK_SAMPLE_COUNT_1_BIT,
1588 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1589 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1590 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1591 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1592 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Tony Barbour98390392016-07-28 10:50:13 -06001593 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001594 },
1595 [1] =
1596 {
1597 .format = demo->depth.format,
1598 .samples = VK_SAMPLE_COUNT_1_BIT,
1599 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1600 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1601 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1602 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1603 .initialLayout =
1604 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1605 .finalLayout =
1606 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1607 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001608 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001609 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001610 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001611 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001612 const VkAttachmentReference depth_reference = {
1613 .attachment = 1,
1614 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1615 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001616 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001617 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1618 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001619 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001620 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001621 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001622 .pColorAttachments = &color_reference,
1623 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001624 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001625 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001626 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001627 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001628 const VkRenderPassCreateInfo rp_info = {
1629 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1630 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001631 .attachmentCount = 2,
1632 .pAttachments = attachments,
1633 .subpassCount = 1,
1634 .pSubpasses = &subpass,
1635 .dependencyCount = 0,
1636 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001637 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001638 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001639
Chia-I Wu63636062015-10-26 21:10:41 +08001640 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001641 assert(!err);
1642}
1643
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001644//TODO: Merge shader reading
1645#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001646static VkShaderModule
1647demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001648 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001649 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001650 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001651
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001652 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1653 moduleCreateInfo.pNext = NULL;
1654
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001655 moduleCreateInfo.codeSize = size;
1656 moduleCreateInfo.pCode = code;
1657 moduleCreateInfo.flags = 0;
1658 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1659 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001660
1661 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001662}
1663
Karl Schultze4dc75c2016-02-02 15:37:51 -07001664char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001665 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001666 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001667 void *shader_code;
1668
1669 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001670 if (!fp)
1671 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001672
1673 fseek(fp, 0L, SEEK_END);
1674 size = ftell(fp);
1675
1676 fseek(fp, 0L, SEEK_SET);
1677
1678 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001679 retval = fread(shader_code, size, 1, fp);
1680 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001681
1682 *psize = size;
1683
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001684 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001685 return shader_code;
1686}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001687#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001688
Karl Schultze4dc75c2016-02-02 15:37:51 -07001689static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001690#ifdef __ANDROID__
1691 VkShaderModuleCreateInfo sh_info = {};
1692 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1693
1694#include "cube.vert.h"
1695 sh_info.codeSize = sizeof(cube_vert);
1696 sh_info.pCode = cube_vert;
1697 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1698 assert(!err);
1699#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001700 void *vertShaderCode;
1701 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001702
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001703 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1704
Karl Schultze4dc75c2016-02-02 15:37:51 -07001705 demo->vert_shader_module =
1706 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001707
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001708 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001709#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001710
1711 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001712}
1713
Karl Schultze4dc75c2016-02-02 15:37:51 -07001714static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001715#ifdef __ANDROID__
1716 VkShaderModuleCreateInfo sh_info = {};
1717 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1718
1719#include "cube.frag.h"
1720 sh_info.codeSize = sizeof(cube_frag);
1721 sh_info.pCode = cube_frag;
1722 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1723 assert(!err);
1724#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001725 void *fragShaderCode;
1726 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001727
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001728 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001729
Karl Schultze4dc75c2016-02-02 15:37:51 -07001730 demo->frag_shader_module =
1731 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001732
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001733 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001734#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001735
1736 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001737}
1738
Karl Schultze4dc75c2016-02-02 15:37:51 -07001739static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001740 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001741 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001742 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001743 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001744 VkPipelineRasterizationStateCreateInfo rs;
1745 VkPipelineColorBlendStateCreateInfo cb;
1746 VkPipelineDepthStencilStateCreateInfo ds;
1747 VkPipelineViewportStateCreateInfo vp;
1748 VkPipelineMultisampleStateCreateInfo ms;
1749 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1750 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001751 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752
Piers Daniellba839d12015-09-29 13:01:09 -06001753 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1754 memset(&dynamicState, 0, sizeof dynamicState);
1755 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1756 dynamicState.pDynamicStates = dynamicStateEnables;
1757
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001759 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001760 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001761
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001762 memset(&vi, 0, sizeof(vi));
1763 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1764
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001766 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001767 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001768
1769 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001770 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1771 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001772 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001773 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001774 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001775 rs.rasterizerDiscardEnable = VK_FALSE;
1776 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001777 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001778
1779 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001780 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1781 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001782 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001783 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001784 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001785 cb.attachmentCount = 1;
1786 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001787
Tony Barbour29645d02015-01-16 14:27:35 -07001788 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001789 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001790 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001791 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1792 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001793 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001794 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1795 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001796
1797 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001798 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001799 ds.depthTestEnable = VK_TRUE;
1800 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001801 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001802 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001803 ds.back.failOp = VK_STENCIL_OP_KEEP;
1804 ds.back.passOp = VK_STENCIL_OP_KEEP;
1805 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001806 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001807 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001808
Tony Barbour29645d02015-01-16 14:27:35 -07001809 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001810 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001811 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001812 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001813
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001814 // Two stages: vs and fs
1815 pipeline.stageCount = 2;
1816 VkPipelineShaderStageCreateInfo shaderStages[2];
1817 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1818
Karl Schultze4dc75c2016-02-02 15:37:51 -07001819 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1820 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001821 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001822 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001823
Karl Schultze4dc75c2016-02-02 15:37:51 -07001824 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1825 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001826 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001827 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001828
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001829 memset(&pipelineCache, 0, sizeof(pipelineCache));
1830 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001831
Karl Schultze4dc75c2016-02-02 15:37:51 -07001832 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1833 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001834 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001835
Karl Schultze4dc75c2016-02-02 15:37:51 -07001836 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001837 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001838 pipeline.pRasterizationState = &rs;
1839 pipeline.pColorBlendState = &cb;
1840 pipeline.pMultisampleState = &ms;
1841 pipeline.pViewportState = &vp;
1842 pipeline.pDepthStencilState = &ds;
1843 pipeline.pStages = shaderStages;
1844 pipeline.renderPass = demo->render_pass;
1845 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001846
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001847 pipeline.renderPass = demo->render_pass;
1848
Karl Schultze4dc75c2016-02-02 15:37:51 -07001849 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1850 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851 assert(!err);
1852
Chia-I Wu63636062015-10-26 21:10:41 +08001853 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1854 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001855}
1856
Karl Schultze4dc75c2016-02-02 15:37:51 -07001857static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001858 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001859 [0] =
1860 {
1861 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1862 .descriptorCount = 1,
1863 },
1864 [1] =
1865 {
1866 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1867 .descriptorCount = DEMO_TEXTURE_COUNT,
1868 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001869 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001870 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001871 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001872 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001873 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001874 .poolSizeCount = 2,
1875 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001876 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001877 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001878
Karl Schultze4dc75c2016-02-02 15:37:51 -07001879 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1880 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001881 assert(!err);
1882}
1883
Karl Schultze4dc75c2016-02-02 15:37:51 -07001884static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001885 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001886 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001887 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001888 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001889
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001890 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001891 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001892 .pNext = NULL,
1893 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001894 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001895 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001896 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001897 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001898
Chia-I Wuae721ba2015-05-25 16:27:55 +08001899 memset(&tex_descs, 0, sizeof(tex_descs));
1900 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001901 tex_descs[i].sampler = demo->textures[i].sampler;
1902 tex_descs[i].imageView = demo->textures[i].view;
1903 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001904 }
1905
1906 memset(&writes, 0, sizeof(writes));
1907
1908 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001909 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001910 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001911 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001912 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001913
1914 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001915 writes[1].dstSet = demo->desc_set;
1916 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001917 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001918 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001919 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001920
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001921 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001922}
1923
Karl Schultze4dc75c2016-02-02 15:37:51 -07001924static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001925 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001926 attachments[1] = demo->depth.view;
1927
Chia-I Wuf973e322015-07-08 13:34:24 +08001928 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001929 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1930 .pNext = NULL,
1931 .renderPass = demo->render_pass,
1932 .attachmentCount = 2,
1933 .pAttachments = attachments,
1934 .width = demo->width,
1935 .height = demo->height,
1936 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001937 };
1938 VkResult U_ASSERT_ONLY err;
1939 uint32_t i;
1940
Karl Schultze4dc75c2016-02-02 15:37:51 -07001941 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1942 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001943 assert(demo->framebuffers);
1944
1945 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001946 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001947 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1948 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001949 assert(!err);
1950 }
1951}
1952
Karl Schultze4dc75c2016-02-02 15:37:51 -07001953static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001954 VkResult U_ASSERT_ONLY err;
1955
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001956 const VkCommandPoolCreateInfo cmd_pool_info = {
1957 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001958 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001959 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001960 .flags = 0,
1961 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001962 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1963 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001964 assert(!err);
1965
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001966 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001967 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001969 .commandPool = demo->cmd_pool,
1970 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001971 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001973
1974 demo_prepare_buffers(demo);
1975 demo_prepare_depth(demo);
1976 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001977 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001978
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001979 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001980 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001981 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982
Ian Elliotta0781972015-08-21 15:09:33 -06001983 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001984 err =
1985 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001986 assert(!err);
1987 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001988
Tony Barbour22e06272016-09-07 16:07:56 -06001989 if (demo->separate_present_queue) {
1990 const VkCommandPoolCreateInfo cmd_pool_info = {
1991 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1992 .pNext = NULL,
1993 .queueFamilyIndex = demo->present_queue_family_index,
1994 .flags = 0,
1995 };
1996 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1997 &demo->present_cmd_pool);
1998 assert(!err);
1999 const VkCommandBufferAllocateInfo cmd = {
2000 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2001 .pNext = NULL,
2002 .commandPool = demo->present_cmd_pool,
2003 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2004 .commandBufferCount = 1,
2005 };
2006 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2007 err = vkAllocateCommandBuffers(
2008 demo->device, &cmd, &demo->buffers[i].graphics_to_present_cmd);
2009 assert(!err);
2010 demo_build_image_ownership_cmd(demo, i);
2011 }
2012 }
2013
Chia-I Wu63ea9262015-03-26 13:14:16 +08002014 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002015 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002016
Chia-I Wuf973e322015-07-08 13:34:24 +08002017 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002018
Ian Elliotta0781972015-08-21 15:09:33 -06002019 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002020 demo->current_buffer = i;
2021 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
2022 }
2023
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002024 /*
2025 * Prepare functions above may generate pipeline commands
2026 * that need to be flushed before beginning the render loop.
2027 */
2028 demo_flush_init_cmd(demo);
2029
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002030 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002031 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002032}
2033
Karl Schultze4dc75c2016-02-02 15:37:51 -07002034static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002035 uint32_t i;
2036
2037 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002038 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002039
Tony Barbour66a56c32016-09-21 13:10:56 -06002040 // Wait for fences from present operations
2041 for (i = 0; i < FRAME_LAG; i++) {
2042 if (demo->fencesInited[i]) {
2043 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
2044 }
2045 vkDestroyFence(demo->device, demo->fences[i], NULL);
2046 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2047 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2048 if (demo->separate_present_queue) {
2049 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2050 }
2051 }
2052
Tony Barbourd8e504f2015-10-08 14:26:24 -06002053 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002054 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002055 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06002056 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002057 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002058
Chia-I Wu63636062015-10-26 21:10:41 +08002059 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2060 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2061 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2062 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2063 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002064
2065 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002066 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2067 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2068 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2069 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002070 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002071 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002072
Chia-I Wu63636062015-10-26 21:10:41 +08002073 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2074 vkDestroyImage(demo->device, demo->depth.image, NULL);
2075 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002076
Chia-I Wu63636062015-10-26 21:10:41 +08002077 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2078 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002079
Ian Elliotta0781972015-08-21 15:09:33 -06002080 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002081 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002082 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2083 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06002084 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002085 free(demo->buffers);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002086 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002087 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002088
Tony Barbour22e06272016-09-07 16:07:56 -06002089 if (demo->separate_present_queue) {
2090 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002091 }
Chia-I Wu63636062015-10-26 21:10:41 +08002092 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002093 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002094 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002095 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002096 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002097 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002098
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002099#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002100 if (demo->use_xlib) {
2101 XDestroyWindow(demo->display, demo->xlib_window);
2102 XCloseDisplay(demo->display);
2103 } else {
2104 xcb_destroy_window(demo->connection, demo->xcb_window);
2105 xcb_disconnect(demo->connection);
2106 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002107 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002108#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002109 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002110 xcb_disconnect(demo->connection);
2111 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002112#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2113 wl_shell_surface_destroy(demo->shell_surface);
2114 wl_surface_destroy(demo->window);
2115 wl_shell_destroy(demo->shell);
2116 wl_compositor_destroy(demo->compositor);
2117 wl_registry_destroy(demo->registry);
2118 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002119#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002120}
2121
Karl Schultze4dc75c2016-02-02 15:37:51 -07002122static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002123 uint32_t i;
2124
Mike Stroyanbb60b382015-10-28 09:46:57 -06002125 // Don't react to resize until after first initialization.
2126 if (!demo->prepared) {
2127 return;
2128 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002129 // In order to properly resize the window, we must re-create the swapchain
2130 // AND redo the command buffers, etc.
2131 //
2132 // First, perform part of the demo_cleanup() function:
2133 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002134 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002135
2136 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002137 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002138 }
2139 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002140 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002141
Chia-I Wu63636062015-10-26 21:10:41 +08002142 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2143 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2144 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2145 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2146 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002147
2148 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002149 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2150 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2151 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2152 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002153 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002154
Chia-I Wu63636062015-10-26 21:10:41 +08002155 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2156 vkDestroyImage(demo->device, demo->depth.image, NULL);
2157 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002158
Chia-I Wu63636062015-10-26 21:10:41 +08002159 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2160 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002161
2162 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002163 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002164 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2165 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002166 }
Chia-I Wu63636062015-10-26 21:10:41 +08002167 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002168 if (demo->separate_present_queue) {
2169 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2170 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002171 free(demo->buffers);
2172
Ian Elliottcf074d32015-10-16 18:02:43 -06002173 // Second, re-perform the demo_prepare() function, which will re-create the
2174 // swapchain:
2175 demo_prepare(demo);
2176}
2177
David Pinedo2bb7c932015-06-18 17:03:14 -06002178// On MS-Windows, make this a global, so it's available to WndProc()
2179struct demo demo;
2180
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002181#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002182static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002183 if (!demo->prepared)
2184 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002185
Ian Elliott639ca472015-04-16 15:23:05 -06002186 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002187 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002188 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002189 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002190 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002191 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002192}
Ian Elliott639ca472015-04-16 15:23:05 -06002193
2194// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002195LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2196 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002197 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002198 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002199 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002200 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002201 // The validation callback calls MessageBox which can generate paint
2202 // events - don't make more Vulkan calls if we got here from the
2203 // callback
2204 if (!in_callback) {
2205 demo_run(&demo);
2206 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002207 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002208 case WM_GETMINMAXINFO: // set window's minimum size
2209 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2210 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002211 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002212 // Resize the application to the new window size, except when
2213 // it was minimized. Vulkan doesn't support images or swapchains
2214 // with width=0 and height=0.
2215 if (wParam != SIZE_MINIMIZED) {
2216 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002217 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002218 demo_resize(&demo);
2219 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002220 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002221 default:
2222 break;
2223 }
2224 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2225}
2226
Karl Schultze4dc75c2016-02-02 15:37:51 -07002227static void demo_create_window(struct demo *demo) {
2228 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002229
2230 // Initialize the window class structure:
2231 win_class.cbSize = sizeof(WNDCLASSEX);
2232 win_class.style = CS_HREDRAW | CS_VREDRAW;
2233 win_class.lpfnWndProc = WndProc;
2234 win_class.cbClsExtra = 0;
2235 win_class.cbWndExtra = 0;
2236 win_class.hInstance = demo->connection; // hInstance
2237 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2238 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2239 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2240 win_class.lpszMenuName = NULL;
2241 win_class.lpszClassName = demo->name;
2242 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2243 // Register window class:
2244 if (!RegisterClassEx(&win_class)) {
2245 // It didn't work, so try to give a useful error:
2246 printf("Unexpected error trying to start the application!\n");
2247 fflush(stdout);
2248 exit(1);
2249 }
2250 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002251 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002252 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002253 demo->window = CreateWindowEx(0,
2254 demo->name, // class name
2255 demo->name, // app name
2256 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002257 WS_VISIBLE | WS_SYSMENU,
2258 100, 100, // x/y coords
2259 wr.right - wr.left, // width
2260 wr.bottom - wr.top, // height
2261 NULL, // handle to parent
2262 NULL, // handle to menu
2263 demo->connection, // hInstance
2264 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002265 if (!demo->window) {
2266 // It didn't work, so try to give a useful error:
2267 printf("Cannot create a window in which to draw!\n");
2268 fflush(stdout);
2269 exit(1);
2270 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002271 // Window client area size must be at least 1 pixel high, to prevent crash.
2272 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2273 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002274}
Tony Barbour8295ac42016-08-08 11:04:17 -06002275#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002276static void demo_create_xlib_window(struct demo *demo) {
2277
2278 demo->display = XOpenDisplay(NULL);
2279 long visualMask = VisualScreenMask;
2280 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002281 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002282 vInfoTemplate.screen = DefaultScreen(demo->display);
2283 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2284 &vInfoTemplate, &numberOfVisuals);
2285
2286 Colormap colormap = XCreateColormap(
2287 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2288 visualInfo->visual, AllocNone);
2289
Rene Lindsay11612722016-06-13 17:20:39 -06002290 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002291 windowAttributes.colormap = colormap;
2292 windowAttributes.background_pixel = 0xFFFFFFFF;
2293 windowAttributes.border_pixel = 0;
2294 windowAttributes.event_mask =
2295 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2296
2297 demo->xlib_window = XCreateWindow(
2298 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2299 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2300 visualInfo->visual,
2301 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2302
2303 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2304 XMapWindow(demo->display, demo->xlib_window);
2305 XFlush(demo->display);
2306 demo->xlib_wm_delete_window =
2307 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2308}
2309static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2310 switch(event->type) {
2311 case ClientMessage:
2312 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2313 demo->quit = true;
2314 break;
2315 case KeyPress:
2316 switch (event->xkey.keycode) {
2317 case 0x9: // Escape
2318 demo->quit = true;
2319 break;
2320 case 0x71: // left arrow key
2321 demo->spin_angle += demo->spin_increment;
2322 break;
2323 case 0x72: // right arrow key
2324 demo->spin_angle -= demo->spin_increment;
2325 break;
2326 case 0x41:
2327 demo->pause = !demo->pause;
2328 break;
2329 }
2330 break;
2331 case ConfigureNotify:
2332 if ((demo->width != event->xconfigure.width) ||
2333 (demo->height != event->xconfigure.height)) {
2334 demo->width = event->xconfigure.width;
2335 demo->height = event->xconfigure.height;
2336 demo_resize(demo);
2337 }
2338 break;
2339 default:
2340 break;
2341 }
2342
2343}
2344
2345static void demo_run_xlib(struct demo *demo) {
2346
2347 while (!demo->quit) {
2348 XEvent event;
2349
2350 if (demo->pause) {
2351 XNextEvent(demo->display, &event);
2352 demo_handle_xlib_event(demo, &event);
2353 } else {
2354 while (XPending(demo->display) > 0) {
2355 XNextEvent(demo->display, &event);
2356 demo_handle_xlib_event(demo, &event);
2357 }
2358 }
2359
Tony Barbour2593b182016-04-19 10:57:58 -06002360 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002361 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002362 demo->curFrame++;
2363 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2364 demo->quit = true;
2365 }
2366}
Tony Barbour8295ac42016-08-08 11:04:17 -06002367#endif // VK_USE_PLATFORM_XLIB_KHR
2368#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002369static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002370 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002371 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002372 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002373 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002374 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002375 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002376 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002377 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2378 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002379 demo->quit = true;
2380 }
2381 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002382 case XCB_KEY_RELEASE: {
2383 const xcb_key_release_event_t *key =
2384 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002385
Karl Schultze4dc75c2016-02-02 15:37:51 -07002386 switch (key->detail) {
2387 case 0x9: // Escape
2388 demo->quit = true;
2389 break;
2390 case 0x71: // left arrow key
2391 demo->spin_angle += demo->spin_increment;
2392 break;
2393 case 0x72: // right arrow key
2394 demo->spin_angle -= demo->spin_increment;
2395 break;
2396 case 0x41:
2397 demo->pause = !demo->pause;
2398 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002399 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002400 } break;
2401 case XCB_CONFIGURE_NOTIFY: {
2402 const xcb_configure_notify_event_t *cfg =
2403 (const xcb_configure_notify_event_t *)event;
2404 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002405 demo->width = cfg->width;
2406 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002407 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002408 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002409 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002410 default:
2411 break;
2412 }
2413}
2414
Tony Barbour2593b182016-04-19 10:57:58 -06002415static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002416 xcb_flush(demo->connection);
2417
2418 while (!demo->quit) {
2419 xcb_generic_event_t *event;
2420
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002421 if (demo->pause) {
2422 event = xcb_wait_for_event(demo->connection);
2423 } else {
2424 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002425 while(event) {
2426 demo_handle_xcb_event(demo, event);
2427 free(event);
2428 event = xcb_poll_for_event(demo->connection);
2429 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002430 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002431
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002432 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002433 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002434 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002435 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002436 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002437 }
2438}
2439
Tony Barbour2593b182016-04-19 10:57:58 -06002440static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002441 uint32_t value_mask, value_list[32];
2442
Tony Barbour2593b182016-04-19 10:57:58 -06002443 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002444
2445 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2446 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002447 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002448 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002449
Tony Barbour2593b182016-04-19 10:57:58 -06002450 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002451 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2452 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2453 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002454
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002455 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002456 xcb_intern_atom_cookie_t cookie =
2457 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2458 xcb_intern_atom_reply_t *reply =
2459 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002460
Karl Schultze4dc75c2016-02-02 15:37:51 -07002461 xcb_intern_atom_cookie_t cookie2 =
2462 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2463 demo->atom_wm_delete_window =
2464 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002465
Tony Barbour2593b182016-04-19 10:57:58 -06002466 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002467 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002468 &(*demo->atom_wm_delete_window).atom);
2469 free(reply);
2470
Tony Barbour2593b182016-04-19 10:57:58 -06002471 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002472
Karl Schultze4dc75c2016-02-02 15:37:51 -07002473 // Force the x/y coordinates to 100,100 results are identical in consecutive
2474 // runs
2475 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002476 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002477 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002478}
Tony Barbour6b131662016-08-25 13:14:45 -06002479// VK_USE_PLATFORM_XCB_KHR
2480#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002481static void demo_run(struct demo *demo) {
2482 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002483 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002484 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002485 demo->curFrame++;
2486 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2487 demo->quit = true;
2488 }
2489}
2490
2491static void handle_ping(void *data UNUSED,
2492 struct wl_shell_surface *shell_surface,
2493 uint32_t serial) {
2494 wl_shell_surface_pong(shell_surface, serial);
2495}
2496
2497static void handle_configure(void *data UNUSED,
2498 struct wl_shell_surface *shell_surface UNUSED,
2499 uint32_t edges UNUSED, int32_t width UNUSED,
2500 int32_t height UNUSED) {}
2501
2502static void handle_popup_done(void *data UNUSED,
2503 struct wl_shell_surface *shell_surface UNUSED) {}
2504
2505static const struct wl_shell_surface_listener shell_surface_listener = {
2506 handle_ping, handle_configure, handle_popup_done};
2507
2508static void demo_create_window(struct demo *demo) {
2509 demo->window = wl_compositor_create_surface(demo->compositor);
2510 if (!demo->window) {
2511 printf("Can not create wayland_surface from compositor!\n");
2512 fflush(stdout);
2513 exit(1);
2514 }
2515
2516 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2517 if (!demo->shell_surface) {
2518 printf("Can not get shell_surface from wayland_surface!\n");
2519 fflush(stdout);
2520 exit(1);
2521 }
2522 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2523 demo);
2524 wl_shell_surface_set_toplevel(demo->shell_surface);
2525 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2526}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002527#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2528static void demo_run(struct demo *demo) {
2529 if (!demo->prepared)
2530 return;
2531
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002532 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002533 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002534 demo->curFrame++;
2535}
2536#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002537
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002538/*
2539 * Return 1 (true) if all layer names specified in check_names
2540 * can be found in given layer properties.
2541 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002542static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002543 uint32_t layer_count,
2544 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002545 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002546 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002547 for (uint32_t j = 0; j < layer_count; j++) {
2548 if (!strcmp(check_names[i], layers[j].layerName)) {
2549 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002550 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002551 }
2552 }
2553 if (!found) {
2554 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2555 return 0;
2556 }
2557 }
2558 return 1;
2559}
2560
Karl Schultze4dc75c2016-02-02 15:37:51 -07002561static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002562 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002563 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002564 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002565 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002566 char **instance_validation_layers = NULL;
2567 demo->enabled_extension_count = 0;
2568 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002569
Karl Schultz7c50ad62016-04-01 12:07:03 -06002570 char *instance_validation_layers_alt1[] = {
2571 "VK_LAYER_LUNARG_standard_validation"
2572 };
2573
2574 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002575 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2576 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2577 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2578 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002579 };
2580
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002581 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002582 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002583 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002584
Karl Schultz7c50ad62016-04-01 12:07:03 -06002585 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002586 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002587
Karl Schultz7c50ad62016-04-01 12:07:03 -06002588 instance_validation_layers = instance_validation_layers_alt1;
2589 if (instance_layer_count > 0) {
2590 VkLayerProperties *instance_layers =
2591 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2592 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2593 instance_layers);
2594 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002595
Karl Schultz7c50ad62016-04-01 12:07:03 -06002596
2597 validation_found = demo_check_layers(
2598 ARRAY_SIZE(instance_validation_layers_alt1),
2599 instance_validation_layers, instance_layer_count,
2600 instance_layers);
2601 if (validation_found) {
2602 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002603 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2604 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002605 } else {
2606 // use alternative set of validation layers
2607 instance_validation_layers = instance_validation_layers_alt2;
2608 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2609 validation_found = demo_check_layers(
2610 ARRAY_SIZE(instance_validation_layers_alt2),
2611 instance_validation_layers, instance_layer_count,
2612 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002613 validation_layer_count =
2614 ARRAY_SIZE(instance_validation_layers_alt2);
2615 for (uint32_t i = 0; i < validation_layer_count; i++) {
2616 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002617 }
2618 }
2619 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002620 }
Dustin Graves7c287352016-01-27 13:48:06 -07002621
Karl Schultz7c50ad62016-04-01 12:07:03 -06002622 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002623 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002624 "required validation layer.\n\n"
2625 "Please look at the Getting Started guide for additional "
2626 "information.\n",
2627 "vkCreateInstance Failure");
2628 }
Dustin Graves7c287352016-01-27 13:48:06 -07002629 }
2630
2631 /* Look for instance extensions */
2632 VkBool32 surfaceExtFound = 0;
2633 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002634#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002635 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002636#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002637 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002638
Karl Schultze4dc75c2016-02-02 15:37:51 -07002639 err = vkEnumerateInstanceExtensionProperties(
2640 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002641 assert(!err);
2642
Dustin Graves7c287352016-01-27 13:48:06 -07002643 if (instance_extension_count > 0) {
2644 VkExtensionProperties *instance_extensions =
2645 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2646 err = vkEnumerateInstanceExtensionProperties(
2647 NULL, &instance_extension_count, instance_extensions);
2648 assert(!err);
2649 for (uint32_t i = 0; i < instance_extension_count; i++) {
2650 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2651 instance_extensions[i].extensionName)) {
2652 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002653 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002654 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002655 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002656#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002657 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2658 instance_extensions[i].extensionName)) {
2659 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002660 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002661 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2662 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002663#endif
2664#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002665 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2666 instance_extensions[i].extensionName)) {
2667 platformSurfaceExtFound = 1;
2668 xlibSurfaceExtFound = 1;
2669 demo->extension_names[demo->enabled_extension_count++] =
2670 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2671 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002672#endif
2673#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002674 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2675 instance_extensions[i].extensionName)) {
2676 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002677 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002678 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2679 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002680#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002681#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2682 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2683 instance_extensions[i].extensionName)) {
2684 platformSurfaceExtFound = 1;
2685 demo->extension_names[demo->enabled_extension_count++] =
2686 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2687 }
2688#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002689#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002690 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2691 instance_extensions[i].extensionName)) {
2692 platformSurfaceExtFound = 1;
2693 demo->extension_names[demo->enabled_extension_count++] =
2694 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2695 }
2696#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002697 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2698 instance_extensions[i].extensionName)) {
2699 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002700 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002701 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2702 }
2703 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002704 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002705 }
Dustin Graves7c287352016-01-27 13:48:06 -07002706
2707 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002708 }
Dustin Graves7c287352016-01-27 13:48:06 -07002709
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002710 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002711 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2712 "the " VK_KHR_SURFACE_EXTENSION_NAME
2713 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002714 "Vulkan installable client driver (ICD) installed?\nPlease "
2715 "look at the Getting Started guide for additional "
2716 "information.\n",
2717 "vkCreateInstance Failure");
2718 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002719 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002720#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002721 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2722 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2723 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002724 "Vulkan installable client driver (ICD) installed?\nPlease "
2725 "look at the Getting Started guide for additional "
2726 "information.\n",
2727 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002728#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002729 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2730 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2731 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002732 "Vulkan installable client driver (ICD) installed?\nPlease "
2733 "look at the Getting Started guide for additional "
2734 "information.\n",
2735 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002736#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2737 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2738 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2739 " extension.\n\nDo you have a compatible "
2740 "Vulkan installable client driver (ICD) installed?\nPlease "
2741 "look at the Getting Started guide for additional "
2742 "information.\n",
2743 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002744#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2745 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2746 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2747 " extension.\n\nDo you have a compatible "
2748 "Vulkan installable client driver (ICD) installed?\nPlease "
2749 "look at the Getting Started guide for additional "
2750 "information.\n",
2751 "vkCreateInstance Failure");
2752#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002753 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002754#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002755 if (demo->use_xlib && !xlibSurfaceExtFound) {
2756 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2757 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2758 " extension.\n\nDo you have a compatible "
2759 "Vulkan installable client driver (ICD) installed?\nPlease "
2760 "look at the Getting Started guide for additional "
2761 "information.\n",
2762 "vkCreateInstance Failure");
2763 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002764#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002765 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002766 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002767 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002768 .pApplicationName = APP_SHORT_NAME,
2769 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002770 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002771 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002772 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002773 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002774 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002775 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002776 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002777 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002778 .enabledLayerCount = demo->enabled_layer_count,
2779 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2780 .enabledExtensionCount = demo->enabled_extension_count,
2781 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002782 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002783
2784 /*
2785 * This is info for a temp callback to use during CreateInstance.
2786 * After the instance is created, we use the instance-based
2787 * function to register the final callback.
2788 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002789 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002790 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002791 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2792 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002793 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002794 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002795 dbgCreateInfo.flags =
2796 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2797 inst_info.pNext = &dbgCreateInfo;
2798 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002799
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002800 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002801
Chia-I Wu63636062015-10-26 21:10:41 +08002802 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002803 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2804 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002805 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002806 "additional information.\n",
2807 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002808 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002809 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002810 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002811 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002812 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002813 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2814 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002815 "the Getting Started guide for additional information.\n",
2816 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002817 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002818
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002819 /* Make initial call to query gpu_count, then second call for gpu info*/
2820 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2821 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002822
2823 if (gpu_count > 0) {
2824 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2825 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2826 assert(!err);
2827 /* For cube demo we just grab the first physical device */
2828 demo->gpu = physical_devices[0];
2829 free(physical_devices);
2830 } else {
2831 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2832 "Do you have a compatible Vulkan installable client driver (ICD) "
2833 "installed?\nPlease look at the Getting Started guide for "
2834 "additional information.\n",
2835 "vkEnumeratePhysicalDevices Failure");
2836 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002837
Dustin Graves7c287352016-01-27 13:48:06 -07002838 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002839 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002840 VkBool32 swapchainExtFound = 0;
2841 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002842 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002843
Karl Schultze4dc75c2016-02-02 15:37:51 -07002844 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2845 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002846 assert(!err);
2847
Dustin Graves7c287352016-01-27 13:48:06 -07002848 if (device_extension_count > 0) {
2849 VkExtensionProperties *device_extensions =
2850 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2851 err = vkEnumerateDeviceExtensionProperties(
2852 demo->gpu, NULL, &device_extension_count, device_extensions);
2853 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002854
Dustin Graves7c287352016-01-27 13:48:06 -07002855 for (uint32_t i = 0; i < device_extension_count; i++) {
2856 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2857 device_extensions[i].extensionName)) {
2858 swapchainExtFound = 1;
2859 demo->extension_names[demo->enabled_extension_count++] =
2860 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2861 }
2862 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002863 }
Dustin Graves7c287352016-01-27 13:48:06 -07002864
2865 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002866 }
Dustin Graves7c287352016-01-27 13:48:06 -07002867
Tony Barbourcc69f452015-10-08 13:59:42 -06002868 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002869 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2870 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2871 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002872 "Vulkan installable client driver (ICD) installed?\nPlease "
2873 "look at the Getting Started guide for additional "
2874 "information.\n",
2875 "vkCreateInstance Failure");
2876 }
2877
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002878 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002879 demo->CreateDebugReportCallback =
2880 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2881 demo->inst, "vkCreateDebugReportCallbackEXT");
2882 demo->DestroyDebugReportCallback =
2883 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2884 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002885 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002886 ERR_EXIT(
2887 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2888 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002889 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002890 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002891 ERR_EXIT(
2892 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2893 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002894 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002895 demo->DebugReportMessage =
2896 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2897 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002898 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002899 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002900 "vkGetProcAddr Failure");
2901 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002902
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002903 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002904 PFN_vkDebugReportCallbackEXT callback;
2905 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002906 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002907 dbgCreateInfo.pNext = NULL;
2908 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002909 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002910 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002911 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002912 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2913 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002914 switch (err) {
2915 case VK_SUCCESS:
2916 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002917 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002918 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2919 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002920 break;
2921 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002922 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2923 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002924 break;
2925 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002926 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002927 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002928
2929 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002930 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2931 &demo->queue_family_count, NULL);
2932 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002933
Karl Schultze4dc75c2016-02-02 15:37:51 -07002934 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002935 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2936 vkGetPhysicalDeviceQueueFamilyProperties(
2937 demo->gpu, &demo->queue_family_count, demo->queue_props);
2938
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002939 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002940 // If app has specific feature requirements it should check supported
2941 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002942 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002943 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002944
Tony Barbourda2bad52016-01-22 14:36:40 -07002945 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2946 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2947 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2948 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2949 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2950}
2951
Karl Schultze4dc75c2016-02-02 15:37:51 -07002952static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002953 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002954 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06002955 VkDeviceQueueCreateInfo queues[2];
2956 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2957 queues[0].pNext = NULL;
2958 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
2959 queues[0].queueCount = 1;
2960 queues[0].pQueuePriorities = queue_priorities;
2961 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002962
2963 VkDeviceCreateInfo device = {
2964 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2965 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002966 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06002967 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002968 .enabledLayerCount = 0,
2969 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002970 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002971 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2972 .pEnabledFeatures =
2973 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002974 };
Tony Barbour22e06272016-09-07 16:07:56 -06002975 if (demo->separate_present_queue) {
2976 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2977 queues[1].pNext = NULL;
2978 queues[1].queueFamilyIndex = demo->present_queue_family_index;
2979 queues[1].queueCount = 1;
2980 queues[1].pQueuePriorities = queue_priorities;
2981 queues[1].flags = 0;
2982 device.queueCreateInfoCount = 2;
2983 }
Chia-I Wu63636062015-10-26 21:10:41 +08002984 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002985 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002986}
2987
Karl Schultze4dc75c2016-02-02 15:37:51 -07002988static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002989 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002990 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002991
Karl Schultze4dc75c2016-02-02 15:37:51 -07002992// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002993#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002994 VkWin32SurfaceCreateInfoKHR createInfo;
2995 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2996 createInfo.pNext = NULL;
2997 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002998 createInfo.hinstance = demo->connection;
2999 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003000
Karl Schultze4dc75c2016-02-02 15:37:51 -07003001 err =
3002 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003003#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3004 VkWaylandSurfaceCreateInfoKHR createInfo;
3005 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3006 createInfo.pNext = NULL;
3007 createInfo.flags = 0;
3008 createInfo.display = demo->display;
3009 createInfo.surface = demo->window;
3010
3011 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3012 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003013#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3014 VkAndroidSurfaceCreateInfoKHR createInfo;
3015 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3016 createInfo.pNext = NULL;
3017 createInfo.flags = 0;
3018 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003019
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003020 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3021#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003022 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003023#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003024 VkXlibSurfaceCreateInfoKHR createInfo;
3025 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3026 createInfo.pNext = NULL;
3027 createInfo.flags = 0;
3028 createInfo.dpy = demo->display;
3029 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003030
Tony Barbour2593b182016-04-19 10:57:58 -06003031 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
3032 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003033#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003034 }
3035 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003036#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003037 VkXcbSurfaceCreateInfoKHR createInfo;
3038 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3039 createInfo.pNext = NULL;
3040 createInfo.flags = 0;
3041 createInfo.connection = demo->connection;
3042 createInfo.window = demo->xcb_window;
3043
3044 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003045#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003046 }
Tony Barbour2593b182016-04-19 10:57:58 -06003047 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003048
Tony Barbourcc69f452015-10-08 13:59:42 -06003049 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003050 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003051 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
3052 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003053 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003054 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003055 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003056
3057 // Search for a graphics and a present queue in the array of queue
3058 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003059 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3060 uint32_t presentQueueFamilyIndex = UINT32_MAX;
3061 for (i = 0; i < demo->queue_family_count; i++) {
3062 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3063 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3064 graphicsQueueFamilyIndex = i;
3065 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003066
Tony Barbourab2a0362016-09-06 11:40:12 -06003067 if (supportsPresent[i] == VK_TRUE) {
3068 graphicsQueueFamilyIndex = i;
3069 presentQueueFamilyIndex = i;
3070 break;
3071 }
3072 }
3073 }
3074
3075 if (presentQueueFamilyIndex == UINT32_MAX) {
3076 // If didn't find a queue that supports both graphics and present, then
3077 // find a separate present queue.
3078 for (size_t i = 0; i < demo->queue_family_count; ++i) {
3079 if (supportsPresent[i] == VK_TRUE) {
3080 presentQueueFamilyIndex = i;
3081 break;
3082 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003083 }
3084 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003085
3086 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003087 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3088 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003089 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003090 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003091 }
3092
Tony Barbourab2a0362016-09-06 11:40:12 -06003093 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3094 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003095 demo->separate_present_queue =
3096 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003097 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003098
Tony Barbourda2bad52016-01-22 14:36:40 -07003099 demo_create_device(demo);
3100
3101 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3102 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3103 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3104 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3105 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3106
Tony Barboura2a67812016-07-18 13:21:06 -06003107 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3108 &demo->graphics_queue);
3109
Tony Barbour22e06272016-09-07 16:07:56 -06003110 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003111 demo->present_queue = demo->graphics_queue;
3112 } else {
3113 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3114 &demo->present_queue);
3115 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003116
Ian Elliottaaae5352015-07-06 14:27:58 -06003117 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003118 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003119 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003120 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003121 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003122 VkSurfaceFormatKHR *surfFormats =
3123 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003124 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003125 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003126 assert(!err);
3127 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3128 // the surface has no preferred format. Otherwise, at least one
3129 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003130 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003131 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003132 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003133 assert(formatCount >= 1);
3134 demo->format = surfFormats[0].format;
3135 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003136 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003137
David Pinedo2bb7c932015-06-18 17:03:14 -06003138 demo->quit = false;
3139 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003140
Tony Barbource7524e2016-07-28 12:01:45 -06003141 // Create semaphores to synchronize acquiring presentable buffers before
3142 // rendering and waiting for drawing to be complete before presenting
3143 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3144 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3145 .pNext = NULL,
3146 .flags = 0,
3147 };
Tony Barbource7524e2016-07-28 12:01:45 -06003148
Tony Barbour66a56c32016-09-21 13:10:56 -06003149 // Create fences that we can use to throttle if we get too far
3150 // ahead of the image presents
3151 VkFenceCreateInfo fence_ci = {
3152 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3153 .pNext = NULL,
3154 .flags = 0
3155 };
3156 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3157 vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3158 demo->fencesInited[i] = false;
Tony Barbour22e06272016-09-07 16:07:56 -06003159 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003160 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003161 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003162
3163 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3164 &demo->draw_complete_semaphores[i]);
3165 assert(!err);
3166
3167 if (demo->separate_present_queue) {
3168 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3169 &demo->image_ownership_semaphores[i]);
3170 assert(!err);
3171 }
Tony Barbour22e06272016-09-07 16:07:56 -06003172 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003173 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003174
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003175 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003176 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003177}
3178
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003179#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3180static void registry_handle_global(void *data, struct wl_registry *registry,
3181 uint32_t name, const char *interface,
3182 uint32_t version UNUSED) {
3183 struct demo *demo = data;
3184 if (strcmp(interface, "wl_compositor") == 0) {
3185 demo->compositor =
3186 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3187 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3188 * tp xdg_shell */
3189 } else if (strcmp(interface, "wl_shell") == 0) {
3190 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3191 }
3192}
3193
3194static void registry_handle_global_remove(void *data UNUSED,
3195 struct wl_registry *registry UNUSED,
3196 uint32_t name UNUSED) {}
3197
3198static const struct wl_registry_listener registry_listener = {
3199 registry_handle_global, registry_handle_global_remove};
3200#endif
3201
Karl Schultze4dc75c2016-02-02 15:37:51 -07003202static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003203#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003204 const xcb_setup_t *setup;
3205 xcb_screen_iterator_t iter;
3206 int scr;
3207
3208 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003209 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003210 printf("Cannot find a compatible Vulkan installable client driver "
3211 "(ICD).\nExiting ...\n");
3212 fflush(stdout);
3213 exit(1);
3214 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003215
3216 setup = xcb_get_setup(demo->connection);
3217 iter = xcb_setup_roots_iterator(setup);
3218 while (scr-- > 0)
3219 xcb_screen_next(&iter);
3220
3221 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003222#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3223 demo->display = wl_display_connect(NULL);
3224
3225 if (demo->display == NULL) {
3226 printf("Cannot find a compatible Vulkan installable client driver "
3227 "(ICD).\nExiting ...\n");
3228 fflush(stdout);
3229 exit(1);
3230 }
3231
3232 demo->registry = wl_display_get_registry(demo->display);
3233 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3234 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003235#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003236}
3237
Karl Schultze4dc75c2016-02-02 15:37:51 -07003238static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003239 vec3 eye = {0.0f, 3.0f, 5.0f};
3240 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003241 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003242
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003243 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003244 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003245
Piers Daniell735ee532015-02-23 16:23:13 -07003246 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003247 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003248 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003249 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003250 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003251 if (strcmp(argv[i], "--break") == 0) {
3252 demo->use_break = true;
3253 continue;
3254 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003255 if (strcmp(argv[i], "--validate") == 0) {
3256 demo->validate = true;
3257 continue;
3258 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003259#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003260 if (strcmp(argv[i], "--xlib") == 0) {
3261 demo->use_xlib = true;
3262 continue;
3263 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003264#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003265 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3266 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3267 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003268 i++;
3269 continue;
3270 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003271 if (strcmp(argv[i], "--suppress_popups") == 0) {
3272 demo->suppress_popups = true;
3273 continue;
3274 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003275
Cody Northropaf28a532016-09-27 10:50:57 -06003276#if defined(ANDROID)
3277 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3278#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003279 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003280#if defined(VK_USE_PLATFORM_XLIB_KHR)
3281 "[--xlib] "
3282#endif
lenny-lunargfbe22392016-06-06 11:07:53 -06003283 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003284 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003285 fflush(stderr);
3286 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003287#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003288 }
3289
Tony Barbour2593b182016-04-19 10:57:58 -06003290 if (!demo->use_xlib)
3291 demo_init_connection(demo);
3292
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003293 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003294
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003295 demo->width = 500;
3296 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003297
Tony Barbour66a56c32016-09-21 13:10:56 -06003298 demo->spin_angle = 4.0f;
3299 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003300 demo->pause = false;
3301
Karl Schultze4dc75c2016-02-02 15:37:51 -07003302 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3303 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003304 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3305 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003306
3307 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003308}
3309
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003310#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003311// Include header required for parsing the command line options.
3312#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003313
Karl Schultze4dc75c2016-02-02 15:37:51 -07003314int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3315 int nCmdShow) {
3316 MSG msg; // message
3317 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003318 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003319 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003320
Karl Schultze4dc75c2016-02-02 15:37:51 -07003321 // Use the CommandLine functions to get the command line arguments.
3322 // Unfortunately, Microsoft outputs
3323 // this information as wide characters for Unicode, and we simply want the
3324 // Ascii version to be compatible
3325 // with the non-Windows side. So, we have to convert the information to
3326 // Ascii character strings.
3327 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3328 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003329 argc = 0;
3330 }
3331
Karl Schultze4dc75c2016-02-02 15:37:51 -07003332 if (argc > 0) {
3333 argv = (char **)malloc(sizeof(char *) * argc);
3334 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003335 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003336 } else {
3337 for (int iii = 0; iii < argc; iii++) {
3338 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003339 size_t numConverted = 0;
3340
Karl Schultze4dc75c2016-02-02 15:37:51 -07003341 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3342 if (argv[iii] != NULL) {
3343 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3344 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003345 }
3346 }
3347 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003348 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003349 argv = NULL;
3350 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003351
3352 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003353
3354 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003355 if (argc > 0 && argv != NULL) {
3356 for (int iii = 0; iii < argc; iii++) {
3357 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003358 free(argv[iii]);
3359 }
3360 }
3361 free(argv);
3362 }
3363
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003364 demo.connection = hInstance;
3365 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003366 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003367 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003368
3369 demo_prepare(&demo);
3370
Karl Schultze4dc75c2016-02-02 15:37:51 -07003371 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003372
3373 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003374 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003375 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003376 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003377 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003378 done = true; // if found, quit app
3379 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003380 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003381 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003382 DispatchMessage(&msg);
3383 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003384 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003385 }
3386
3387 demo_cleanup(&demo);
3388
Karl Schultze4dc75c2016-02-02 15:37:51 -07003389 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003390}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003391#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3392#include <android/log.h>
3393#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003394#include "android_util.h"
3395
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003396static bool initialized = false;
3397static bool active = false;
3398struct demo demo;
3399
3400static int32_t processInput(struct android_app* app, AInputEvent* event) {
3401 return 0;
3402}
3403
3404static void processCommand(struct android_app* app, int32_t cmd) {
3405 switch(cmd) {
3406 case APP_CMD_INIT_WINDOW: {
3407 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003408 // We're getting a new window. If the app is starting up, we
3409 // need to initialize. If the app has already been
3410 // initialized, that means that we lost our previous window,
3411 // which means that we have a lot of work to do. At a minimum,
3412 // we need to destroy the swapchain and surface associated with
3413 // the old window, and create a new surface and swapchain.
3414 // However, since there are a lot of other objects/state that
3415 // is tied to the swapchain, it's easiest to simply cleanup and
3416 // start over (i.e. use a brute-force approach of re-starting
3417 // the app)
3418 if (demo.prepared) {
3419 demo_cleanup(&demo);
3420 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003421
3422 // Parse Intents into argc, argv
3423 // Use the following key to send arguments, i.e.
3424 // --es args "--validate"
3425 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003426 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003427 int argc = 0;
3428 char** argv = get_args(app, key, appTag, &argc);
3429
3430 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3431 for (int i = 0; i < argc; i++)
3432 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3433
3434 demo_init(&demo, argc, argv);
3435
3436 // Free the argv malloc'd by get_args
3437 for (int i = 0; i < argc; i++)
3438 free(argv[i]);
3439
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003440 demo.window = (void*)app->window;
3441 demo_init_vk_swapchain(&demo);
3442 demo_prepare(&demo);
3443 initialized = true;
3444 }
3445 break;
3446 }
3447 case APP_CMD_GAINED_FOCUS: {
3448 active = true;
3449 break;
3450 }
3451 case APP_CMD_LOST_FOCUS: {
3452 active = false;
3453 break;
3454 }
3455 }
3456}
3457
3458void android_main(struct android_app *app)
3459{
3460 app_dummy();
3461
Cody Northrop8707a6e2016-04-26 19:59:19 -06003462#ifdef ANDROID
3463 int vulkanSupport = InitVulkan();
3464 if (vulkanSupport == 0)
3465 return;
3466#endif
3467
Ian Elliottf05d5d12016-05-17 12:03:35 -06003468 demo.prepared = false;
3469
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003470 app->onAppCmd = processCommand;
3471 app->onInputEvent = processInput;
3472
3473 while(1) {
3474 int events;
3475 struct android_poll_source* source;
3476 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3477 if (source) {
3478 source->process(app, source);
3479 }
3480
3481 if (app->destroyRequested != 0) {
3482 demo_cleanup(&demo);
3483 return;
3484 }
3485 }
3486 if (initialized && active) {
3487 demo_run(&demo);
3488 }
3489 }
3490
3491}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003492#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003493int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003494 struct demo demo;
3495
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003496 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003497#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003498 if (demo.use_xlib)
3499 demo_create_xlib_window(&demo);
3500 else
3501 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003502#elif defined(VK_USE_PLATFORM_XCB_KHR)
3503 demo_create_xcb_window(&demo);
3504#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3505 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003506#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3507 demo_create_window(&demo);
3508#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003509
Tony Barbourcc69f452015-10-08 13:59:42 -06003510 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003511
3512 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003513
Tony Barbour8295ac42016-08-08 11:04:17 -06003514#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003515 if (demo.use_xlib)
3516 demo_run_xlib(&demo);
3517 else
3518 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003519#elif defined(VK_USE_PLATFORM_XCB_KHR)
3520 demo_run_xcb(&demo);
3521#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3522 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003523#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3524 demo_run(&demo);
3525#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003526
3527 demo_cleanup(&demo);
3528
Karl Schultz0218c002016-03-22 17:06:13 -06003529 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003530}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003531#endif