blob: 5a23657df3580c073cde9158bcf07410c2143c2f [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
2* Copyright (c) 2015-2016 The Khronos Group Inc.
3* Copyright (c) 2015-2016 Valve Corporation
4* Copyright (c) 2015-2016 LunarG, Inc.
5*
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Author: Chia-I Wu <olv@lunarg.com>
19* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20* Author: Ian Elliott <ian@LunarG.com>
21* Author: Jon Ashburn <jon@lunarg.com>
22* Author: Gwan-gyeong Mun <elongbug@gmail.com>
Tony Barbour66a56c32016-09-21 13:10:56 -060023* Author: Tony Barbour <tony@LunarG.com>
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090024*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070025
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060026#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
31#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070032#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090033#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060034#include <X11/Xutil.h>
35#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036
Ian Elliott639ca472015-04-16 15:23:05 -060037#ifdef _WIN32
38#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060039#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060040#endif // _WIN32
41
Cody Northrop8707a6e2016-04-26 19:59:19 -060042#ifdef ANDROID
43#include "vulkan_wrapper.h"
44#else
David Pinedoc5193c12015-11-06 12:54:48 -070045#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060046#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070047
David Pinedo541526f2015-11-24 09:00:24 -070048#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060049#include "linmath.h"
50
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060051#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054
Tony Barbour66a56c32016-09-21 13:10:56 -060055// Allow a maximum of two outstanding presentation operations.
56#define FRAME_LAG 2
57
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060058#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
59
Tony Barbourfdc2d352015-04-22 09:02:32 -060060#if defined(NDEBUG) && defined(__GNUC__)
61#define U_ASSERT_ONLY __attribute__((unused))
62#else
63#define U_ASSERT_ONLY
64#endif
65
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090066#if defined(__GNUC__)
67#define UNUSED __attribute__((unused))
68#else
69#define UNUSED
70#endif
71
Ian Elliott07264132015-04-28 11:35:02 -060072#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060073bool in_callback = false;
Karl Schultze4dc75c2016-02-02 15:37:51 -070074#define ERR_EXIT(err_msg, err_class) \
75 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060076 if (!demo->suppress_popups) \
77 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070078 exit(1); \
79 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060080
Michael Lentinec6cde4b2016-04-18 13:20:45 -050081#elif defined __ANDROID__
82#include <android/log.h>
83#define ERR_EXIT(err_msg, err_class) \
84 do { \
85 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
86 exit(1); \
87 } while (0)
88#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070089#define ERR_EXIT(err_msg, err_class) \
90 do { \
91 printf(err_msg); \
92 fflush(stdout); \
93 exit(1); \
94 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050095#endif
Ian Elliott07264132015-04-28 11:35:02 -060096
Karl Schultze4dc75c2016-02-02 15:37:51 -070097#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
98 { \
99 demo->fp##entrypoint = \
100 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
101 if (demo->fp##entrypoint == NULL) { \
102 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
103 "vkGetInstanceProcAddr Failure"); \
104 } \
105 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600106
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600107static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
108
Karl Schultze4dc75c2016-02-02 15:37:51 -0700109#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
110 { \
111 if (!g_gdpa) \
112 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
113 demo->inst, "vkGetDeviceProcAddr"); \
114 demo->fp##entrypoint = \
115 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
116 if (demo->fp##entrypoint == NULL) { \
117 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
118 "vkGetDeviceProcAddr Failure"); \
119 } \
120 }
Ian Elliott673898b2015-06-22 15:07:49 -0600121
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600122/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700123 * structure to track all objects related to a texture.
124 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600125struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600126 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700127
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600128 VkImage image;
129 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600130
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800131 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500132 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600133 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700134 int32_t tex_width, tex_height;
135};
136
Karl Schultze4dc75c2016-02-02 15:37:51 -0700137static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600138
Karl Schultz0218c002016-03-22 17:06:13 -0600139static int validation_error = 0;
140
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600141struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700143 float mvp[4][4];
144 float position[12 * 3][4];
145 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146};
147
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600148struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700150 float mvp[4][4];
151 float position[12 * 3][4];
152 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600153};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600154
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600155//--------------------------------------------------------------------------------------
156// Mesh and VertexFormat Data
157//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700158// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600159static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 -1.0f,-1.0f, 1.0f,
162 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 -1.0f, 1.0f,-1.0f,
165 -1.0f,-1.0f,-1.0f,
166
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f, 1.0f,-1.0f,
169 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600176 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600179 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 -1.0f, 1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 1.0f, 1.0f, 1.0f,
190 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 1.0f,-1.0f,-1.0f,
193 1.0f, 1.0f,-1.0f,
194
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600197 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 1.0f,-1.0f, 1.0f,
200 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600201};
202
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600203static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700204 0.0f, 1.0f, // -X side
205 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800207 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208 0.0f, 0.0f,
209 0.0f, 1.0f,
210
Rene Lindsay76867e82016-07-29 10:49:11 -0700211 1.0f, 1.0f, // -Z side
212 0.0f, 0.0f,
213 0.0f, 1.0f,
214 1.0f, 1.0f,
215 1.0f, 0.0f,
216 0.0f, 0.0f,
217
218 1.0f, 0.0f, // -Y side
219 1.0f, 1.0f,
220 0.0f, 1.0f,
221 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600222 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224
Rene Lindsay76867e82016-07-29 10:49:11 -0700225 1.0f, 0.0f, // +Y side
226 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231
Rene Lindsay76867e82016-07-29 10:49:11 -0700232 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700234 0.0f, 1.0f,
235 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700238
239 0.0f, 0.0f, // +Z side
240 0.0f, 1.0f,
241 1.0f, 0.0f,
242 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600243 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700244 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600245};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700246// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249 int i;
250
251 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700252 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600253 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
254 }
255 printf("\n");
256 fflush(stdout);
257}
258
Karl Schultze4dc75c2016-02-02 15:37:51 -0700259void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600260 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600262 printf("\n");
263 fflush(stdout);
264}
265
Karl Schultze4dc75c2016-02-02 15:37:51 -0700266VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600267BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
268 uint64_t srcObject, size_t location, int32_t msgCode,
269 const char *pLayerPrefix, const char *pMsg,
270 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700271#ifndef WIN32
272 raise(SIGTRAP);
273#else
274 DebugBreak();
275#endif
276
277 return false;
278}
279
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600280typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600281 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800282 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600283 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600284 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600285} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600286
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600287struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500288#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600289#define APP_NAME_STR_LEN 80
290 HINSTANCE connection; // hInstance - Windows Instance
291 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700292 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600293 POINT minsize; // minimum window size
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600294#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600295 Display* display;
296 Window xlib_window;
297 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600298
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600299 xcb_connection_t *connection;
300 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600301 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800302 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900303#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
304 struct wl_display *display;
305 struct wl_registry *registry;
306 struct wl_compositor *compositor;
307 struct wl_surface *window;
308 struct wl_shell *shell;
309 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500310#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
311 ANativeWindow* window;
312#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700313 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600314 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700315 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600316 bool use_xlib;
Tony Barbour22e06272016-09-07 16:07:56 -0600317 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600320 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600322 VkQueue graphics_queue;
323 VkQueue present_queue;
324 uint32_t graphics_queue_family_index;
325 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600326 VkSemaphore image_acquired_semaphores[FRAME_LAG];
327 VkSemaphore draw_complete_semaphores[FRAME_LAG];
328 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600329 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600330 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600331 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332
Tony Barbourda2bad52016-01-22 14:36:40 -0700333 uint32_t enabled_extension_count;
334 uint32_t enabled_layer_count;
335 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600336 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700337
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600339 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600340 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341
Karl Schultze4dc75c2016-02-02 15:37:51 -0700342 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
343 fpGetPhysicalDeviceSurfaceSupportKHR;
344 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
345 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
346 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
347 fpGetPhysicalDeviceSurfaceFormatsKHR;
348 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
349 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600350 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
351 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
352 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
353 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
354 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600355 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600356 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600357 SwapchainBuffers *buffers;
Tony Barbour66a56c32016-09-21 13:10:56 -0600358 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600359 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800361 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600362 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
364 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600365 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800368 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500369 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600370 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371 } depth;
372
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
375 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800377 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500378 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600379 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600380 } uniform_data;
381
Karl Schultze4dc75c2016-02-02 15:37:51 -0700382 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500383 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600385 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800386 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600389 mat4x4 projection_matrix;
390 mat4x4 view_matrix;
391 mat4x4 model_matrix;
392
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600393 float spin_angle;
394 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600395 bool pause;
396
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600397 VkShaderModule vert_shader_module;
398 VkShaderModule frag_shader_module;
399
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkDescriptorPool desc_pool;
401 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800402
Tony Barbourd8e504f2015-10-08 14:26:24 -0600403 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800404
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600405 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600406 int32_t curFrame;
407 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600408 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600409 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600410 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700411 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
412 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
413 VkDebugReportCallbackEXT msg_callback;
414 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600415
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600416 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600417 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600418};
419
lenny-lunargfbe22392016-06-06 11:07:53 -0600420VKAPI_ATTR VkBool32 VKAPI_CALL
421dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
422 uint64_t srcObject, size_t location, int32_t msgCode,
423 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600424
425 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600426 char *message = (char *)malloc(strlen(pMsg) + 100);
427
428 assert(message);
429
Cody Northropaf28a532016-09-27 10:50:57 -0600430 // We know we're submitting queues without fences, ignore this
431 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer"))
432 return false;
433
434 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
435 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600436 validation_error = 1;
437 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600438 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
439 validation_error = 1;
440 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
441 sprintf(message, "PERFOERMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
442 validation_error = 1;
443 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
444 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
445 validation_error = 1;
446 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
447 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600448 validation_error = 1;
449 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600450 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600451 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600452 }
453
454#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600455
Tony Barbour602ca4f2016-09-12 14:02:43 -0600456 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600457 struct demo *demo = (struct demo*) pUserData;
458 if (!demo->suppress_popups)
459 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600460 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600461
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600462#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600463
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600464 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600465 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600466 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600467 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600468 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600469 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600470 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600471 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600472 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600473 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600474 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600475 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600476 }
Cody Northropaf28a532016-09-27 10:50:57 -0600477
lenny-lunargfbe22392016-06-06 11:07:53 -0600478#else
Cody Northropaf28a532016-09-27 10:50:57 -0600479
lenny-lunargfbe22392016-06-06 11:07:53 -0600480 printf("%s\n", message);
481 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600482
lenny-lunargfbe22392016-06-06 11:07:53 -0600483#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600484
lenny-lunargfbe22392016-06-06 11:07:53 -0600485 free(message);
486
Cody Northropaf28a532016-09-27 10:50:57 -0600487 //clang-format on
488
lenny-lunargfbe22392016-06-06 11:07:53 -0600489 /*
490 * false indicates that layer should not bail-out of an
491 * API call that had validation failures. This may mean that the
492 * app dies inside the driver due to invalid parameter(s).
493 * That's what would happen without validation layers, so we'll
494 * keep that behavior here.
495 */
496 return false;
497}
498
Ian Elliottcf074d32015-10-16 18:02:43 -0600499// Forward declaration:
500static void demo_resize(struct demo *demo);
501
Karl Schultze4dc75c2016-02-02 15:37:51 -0700502static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
503 VkFlags requirements_mask,
504 uint32_t *typeIndex) {
505 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600506 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700507 if ((typeBits & 1) == 1) {
508 // Type is available, does it match user properties?
509 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
510 requirements_mask) == requirements_mask) {
511 *typeIndex = i;
512 return true;
513 }
514 }
515 typeBits >>= 1;
516 }
517 // No memory types matched, return failure
518 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600519}
520
Karl Schultze4dc75c2016-02-02 15:37:51 -0700521static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600522 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600523
Tony Barbource7524e2016-07-28 12:01:45 -0600524 // This function could get called twice if the texture uses a staging buffer
525 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600527 return;
528
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530 assert(!err);
531
Tony Barbource7524e2016-07-28 12:01:45 -0600532 VkFence fence;
533 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
534 .pNext = NULL,
535 .flags = 0};
536 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700537 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700538 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
539 .pNext = NULL,
540 .waitSemaphoreCount = 0,
541 .pWaitSemaphores = NULL,
542 .pWaitDstStageMask = NULL,
543 .commandBufferCount = 1,
544 .pCommandBuffers = cmd_bufs,
545 .signalSemaphoreCount = 0,
546 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547
Tony Barbource7524e2016-07-28 12:01:45 -0600548 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549 assert(!err);
550
Tony Barbource7524e2016-07-28 12:01:45 -0600551 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552 assert(!err);
553
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600554 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600555 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600556 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557}
558
Karl Schultze4dc75c2016-02-02 15:37:51 -0700559static void demo_set_image_layout(struct demo *demo, VkImage image,
560 VkImageAspectFlags aspectMask,
561 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700562 VkImageLayout new_image_layout,
563 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600564 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600565
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600566 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800567 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800568 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600569 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800570 .commandPool = demo->cmd_pool,
571 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700572 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600573 };
574
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800575 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600576 assert(!err);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700577 VkCommandBufferBeginInfo cmd_buf_info = {
578 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
579 .pNext = NULL,
580 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700581 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700582 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600583 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600584 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585 }
586
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600587 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600588 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600589 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700590 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800591 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600592 .oldLayout = old_image_layout,
593 .newLayout = new_image_layout,
594 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700595 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600596
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800597 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600598 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800599 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600600 }
601
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700602 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700603 image_memory_barrier.dstAccessMask =
604 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700605 }
606
607 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 image_memory_barrier.dstAccessMask =
609 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700610 }
611
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600612 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600613 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700614 image_memory_barrier.dstAccessMask =
615 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600616 }
617
Tony Barbourf165b5d2016-10-03 16:01:41 -0600618 if (new_image_layout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
619 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
620 }
621
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600622 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600623
Tony Barbour50bbca42015-06-29 16:20:35 -0600624 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
625 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600626
Karl Schultze4dc75c2016-02-02 15:37:51 -0700627 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
628 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600629}
630
Karl Schultze4dc75c2016-02-02 15:37:51 -0700631static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700632 const VkCommandBufferBeginInfo cmd_buf_info = {
633 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
634 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600635 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700636 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700637 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800638 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700639 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
640 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800641 };
642 const VkRenderPassBeginInfo rp_begin = {
643 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
644 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800645 .renderPass = demo->render_pass,
646 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800647 .renderArea.offset.x = 0,
648 .renderArea.offset.y = 0,
649 .renderArea.extent.width = demo->width,
650 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600651 .clearValueCount = 2,
652 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700653 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800654 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600656 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657 assert(!err);
658
Tony Barbour5c5b1632016-04-26 11:13:48 -0600659 VkImageMemoryBarrier image_memory_barrier = {
660 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
661 .pNext = NULL,
Tony Barbour8daa6712016-09-29 11:36:49 -0600662 .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour5c5b1632016-04-26 11:13:48 -0600663 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barbour22e06272016-09-07 16:07:56 -0600664 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour5c5b1632016-04-26 11:13:48 -0600665 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
666 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
667 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
668 .image = demo->buffers[demo->current_buffer].image,
669 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
670
Tony Barbource7524e2016-07-28 12:01:45 -0600671 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
672 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
673 NULL, 0, NULL, 1, &image_memory_barrier);
Tony Barbour5c5b1632016-04-26 11:13:48 -0600674
Chia-I Wuf051d262015-10-27 19:25:11 +0800675 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800676
Karl Schultze4dc75c2016-02-02 15:37:51 -0700677 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
678 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
679 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
680 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600681 VkViewport viewport;
682 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700683 viewport.height = (float)demo->height;
684 viewport.width = (float)demo->width;
685 viewport.minDepth = (float)0.0f;
686 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700687 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600688
689 VkRect2D scissor;
690 memset(&scissor, 0, sizeof(scissor));
691 scissor.extent.width = demo->width;
692 scissor.extent.height = demo->height;
693 scissor.offset.x = 0;
694 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700695 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600696 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600697 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600698 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800699 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700
Tony Barbour22e06272016-09-07 16:07:56 -0600701 if (demo->separate_present_queue) {
702 // We have to transfer ownership from the graphics queue family to the
703 // present queue family to be able to present. Note that we don't have
704 // to transfer from present queue family back to graphics queue family at
705 // the start of the next frame because we don't care about the image's
706 // contents at that point.
707 VkImageMemoryBarrier image_ownership_barrier = {
708 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
709 .pNext = NULL,
710 .srcAccessMask = 0,
711 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
712 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
713 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
714 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
715 .dstQueueFamilyIndex = demo->present_queue_family_index,
716 .image = demo->buffers[demo->current_buffer].image,
717 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
718
719 vkCmdPipelineBarrier(cmd_buf,
720 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
721 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0,
722 0, NULL, 0, NULL, 1, &image_ownership_barrier);
723 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600724 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600725 assert(!err);
726}
727
Tony Barbour22e06272016-09-07 16:07:56 -0600728void demo_build_image_ownership_cmd(struct demo *demo, int i) {
729 VkResult U_ASSERT_ONLY err;
730
731 const VkCommandBufferBeginInfo cmd_buf_info = {
732 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
733 .pNext = NULL,
734 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
735 .pInheritanceInfo = NULL,
736 };
737 err = vkBeginCommandBuffer(demo->buffers[i].graphics_to_present_cmd,
738 &cmd_buf_info);
739 assert(!err);
740
741 VkImageMemoryBarrier image_ownership_barrier = {
742 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
743 .pNext = NULL,
744 .srcAccessMask = 0,
745 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
746 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
747 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
748 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
749 .dstQueueFamilyIndex = demo->present_queue_family_index,
750 .image = demo->buffers[i].image,
751 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
752
753 vkCmdPipelineBarrier(demo->buffers[i].graphics_to_present_cmd,
754 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
755 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
756 NULL, 0, NULL, 1, &image_ownership_barrier);
757 err = vkEndCommandBuffer(demo->buffers[i].graphics_to_present_cmd);
758 assert(!err);
759}
760
Karl Schultze4dc75c2016-02-02 15:37:51 -0700761void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600762 mat4x4 MVP, Model, VP;
763 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600764 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600765 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600766
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600767 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600768
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600769 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600770 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700771 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
772 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600773 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600774
Karl Schultze4dc75c2016-02-02 15:37:51 -0700775 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
776 demo->uniform_data.mem_alloc.allocationSize, 0,
777 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600778 assert(!err);
779
Karl Schultze4dc75c2016-02-02 15:37:51 -0700780 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600782 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600783}
784
Karl Schultze4dc75c2016-02-02 15:37:51 -0700785static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600786 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600787
szdarkhackd322ff02016-10-08 09:51:22 +0300788 // Ensure no more than FRAME_LAG presentations are outstanding
789 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
790 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600791
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
Ian Elliottcf074d32015-10-16 18:02:43 -0600797 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
798 // demo->swapchain is out of date (e.g. the window was resized) and
799 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600800 demo->frame_index += 1;
801 demo->frame_index %= FRAME_LAG;
802
Ian Elliottcf074d32015-10-16 18:02:43 -0600803 demo_resize(demo);
804 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600805 return;
806 } else if (err == VK_SUBOPTIMAL_KHR) {
807 // demo->swapchain is not as optimal as it could be, but the platform's
808 // presentation engine will still present the image correctly.
809 } else {
810 assert(!err);
811 }
Tony Barbource7524e2016-07-28 12:01:45 -0600812 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600813 // that the image won't be rendered to until the presentation
814 // engine has fully released ownership to the application, and it is
815 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600816 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600817 VkPipelineStageFlags pipe_stage_flags;
818 VkSubmitInfo submit_info;
819 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
820 submit_info.pNext = NULL;
821 submit_info.pWaitDstStageMask = &pipe_stage_flags;
822 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
823 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600824 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600825 submit_info.commandBufferCount = 1;
826 submit_info.pCommandBuffers = &demo->buffers[demo->current_buffer].cmd;
827 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600828 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura2a67812016-07-18 13:21:06 -0600829 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830 assert(!err);
831
Tony Barbour22e06272016-09-07 16:07:56 -0600832 if (demo->separate_present_queue) {
833 // If we are using separate queues, change image ownership to the
834 // present queue before presenting, waiting for the draw complete
835 // semaphore and signalling the ownership released semaphore when finished
836 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
837 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600838 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600839 submit_info.commandBufferCount = 1;
840 submit_info.pCommandBuffers =
841 &demo->buffers[demo->current_buffer].graphics_to_present_cmd;
842 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600843 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600844 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
845 assert(!err);
846 }
847
848 // If we are using separate queues we have to wait for image ownership,
849 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600850 VkPresentInfoKHR present = {
851 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600852 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600853 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600854 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600855 ? &demo->image_ownership_semaphores[demo->frame_index]
856 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600857 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700858 .pSwapchains = &demo->swapchain,
859 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600860 };
861
Tony Barboura2a67812016-07-18 13:21:06 -0600862 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600863 demo->frame_index += 1;
864 demo->frame_index %= FRAME_LAG;
865
Ian Elliottcf074d32015-10-16 18:02:43 -0600866 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
867 // demo->swapchain is out of date (e.g. the window was resized) and
868 // must be recreated:
869 demo_resize(demo);
870 } else if (err == VK_SUBOPTIMAL_KHR) {
871 // demo->swapchain is not as optimal as it could be, but the platform's
872 // presentation engine will still present the image correctly.
873 } else {
874 assert(!err);
875 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600876}
877
Karl Schultze4dc75c2016-02-02 15:37:51 -0700878static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600879 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600880 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600881
Ian Elliottb08e0d92015-11-20 11:55:46 -0700882 // Check the surface capabilities and formats
883 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700884 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
885 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600886 assert(!err);
887
Ian Elliott8528eff2015-08-07 15:56:59 -0600888 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700889 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
890 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600891 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600892 VkPresentModeKHR *presentModes =
893 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600894 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700895 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
896 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600897 assert(!err);
898
Ian Elliotta0781972015-08-21 15:09:33 -0600899 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600900 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
901 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
902 // If the surface size is undefined, the size is set to the size
903 // of the images requested, which must fit within the minimum and
904 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600905 swapchainExtent.width = demo->width;
906 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600907
908 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
909 swapchainExtent.width = surfCapabilities.minImageExtent.width;
910 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
911 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
912 }
913
914 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
915 swapchainExtent.height = surfCapabilities.minImageExtent.height;
916 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
917 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
918 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700919 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600920 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700921 swapchainExtent = surfCapabilities.currentExtent;
922 demo->width = surfCapabilities.currentExtent.width;
923 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600924 }
925
Tony Barbour66a56c32016-09-21 13:10:56 -0600926 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600927 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600928 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottb18fdde2016-10-03 12:11:12 -0600929 // There are times when you may wish to use another present mode. The
930 // following code shows how to select them, and the comments provide some
931 // reasons you may wish to use them.
932 //
933 // It should be noted that Vulkan 1.0 doesn't provide a method for
934 // synchronizing rendering with the presentation engine's display. There
935 // is a method provided for throttling rendering with the display, but
936 // there are some presentation engines for which this method will not work.
937 // If an application doesn't throttle its rendering, and if it renders much
938 // faster than the refresh rate of the display, this can waste power on
939 // mobile devices. That is because power is being spent rendering images
940 // that may never be seen.
941//#define DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR
942//#define DESIRE_VK_PRESENT_MODE_MAILBOX_KHR
943//#define DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR
944#if defined(DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR)
945 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
946 // tearing, or have some way of synchronizing their rendering with the
947 // display.
948 for (size_t i = 0; i < presentModeCount; ++i) {
949 if (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR) {
950 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
951 break;
952 }
953 }
954#elif defined(DESIRE_VK_PRESENT_MODE_MAILBOX_KHR)
955 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
956 // generally render a new presentable image every refresh cycle, but are
957 // occasionally early. In this case, the application wants the new image
958 // to be displayed instead of the previously-queued-for-presentation image
959 // that has not yet been displayed.
960 for (size_t i = 0; i < presentModeCount; ++i) {
961 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
962 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
963 break;
964 }
965 }
966#elif defined(DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR)
967 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
968 // render a new presentable image every refresh cycle, but are occasionally
969 // late. In this case (perhaps because of stuttering/latency concerns),
970 // the application wants the late image to be immediately displayed, even
971 // though that may mean some tearing.
972 for (size_t i = 0; i < presentModeCount; ++i) {
973 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
974 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
975 break;
976 }
977 }
978#endif
Ian Elliottaaae5352015-07-06 14:27:58 -0600979
Ian Elliottcf7f7482016-08-19 03:46:58 -0600980 // Determine the number of VkImage's to use in the swap chain.
981 // Application desires to only acquire 1 image at a time (which is
982 // "surfCapabilities.minImageCount").
983 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
984 // If maxImageCount is 0, we can ask for as many images as we want;
985 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700986 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600987 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600988 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600989 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600990 }
991
Tony Barbour9fd6d352015-09-16 15:01:32 -0600992 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700993 if (surfCapabilities.supportedTransforms &
994 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700995 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600996 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700997 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600998 }
999
Tony Barboura2a67812016-07-18 13:21:06 -06001000 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001001 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001002 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001003 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001004 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001005 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001006 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001007 .imageExtent =
1008 {
1009 .width = swapchainExtent.width, .height = swapchainExtent.height,
1010 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001011 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001012 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -07001013 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001014 .imageArrayLayers = 1,
1015 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1016 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001017 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001018 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001019 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001020 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001021 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001022 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -06001023 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001024 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001025 assert(!err);
1026
Ian Elliottcf074d32015-10-16 18:02:43 -06001027 // If we just re-created an existing swapchain, we should destroy the old
1028 // swapchain at this point.
1029 // Note: destroying the swapchain also cleans up all its associated
1030 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001031 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001032 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001033 }
1034
1035 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001036 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001037 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001038
Karl Schultze4dc75c2016-02-02 15:37:51 -07001039 VkImage *swapchainImages =
1040 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001041 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001042 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001043 &demo->swapchainImageCount,
1044 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001045 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001046
Karl Schultze4dc75c2016-02-02 15:37:51 -07001047 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
1048 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -06001049 assert(demo->buffers);
1050
Ian Elliotta0781972015-08-21 15:09:33 -06001051 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001052 VkImageViewCreateInfo color_image_view = {
1053 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 .pNext = NULL,
1055 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001056 .components =
1057 {
1058 .r = VK_COMPONENT_SWIZZLE_R,
1059 .g = VK_COMPONENT_SWIZZLE_G,
1060 .b = VK_COMPONENT_SWIZZLE_B,
1061 .a = VK_COMPONENT_SWIZZLE_A,
1062 },
1063 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1064 .baseMipLevel = 0,
1065 .levelCount = 1,
1066 .baseArrayLayer = 0,
1067 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001068 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1069 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 };
1071
Ian Elliotta0781972015-08-21 15:09:33 -06001072 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001074 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075
Karl Schultze4dc75c2016-02-02 15:37:51 -07001076 err = vkCreateImageView(demo->device, &color_image_view, NULL,
1077 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001079
1080 // The draw loop will be expecting the presentable images to be in
1081 // LAYOUT_PRESENT_SRC_KHR
1082 // since that's how they're left at the end of every frame.
1083 demo_set_image_layout(
1084 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1085 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001086 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001087
Mark Young04fd3d82016-01-25 14:43:50 -07001088 if (NULL != presentModes) {
1089 free(presentModes);
1090 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001091}
1092
Karl Schultze4dc75c2016-02-02 15:37:51 -07001093static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001094 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001095 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001096 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001097 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001098 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001099 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001100 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001101 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001102 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001103 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001104 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001105 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001106 .flags = 0,
1107 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001108
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001109 VkImageViewCreateInfo view = {
1110 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001112 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001113 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001114 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1115 .baseMipLevel = 0,
1116 .levelCount = 1,
1117 .baseArrayLayer = 0,
1118 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001120 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001121 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001122
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001123 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001124 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001125 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001126
1127 demo->depth.format = depth_format;
1128
1129 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001130 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001131 assert(!err);
1132
Karl Schultze4dc75c2016-02-02 15:37:51 -07001133 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001134 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001135
Chia-I Wuf48700b2015-11-10 16:21:09 +08001136 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001137 demo->depth.mem_alloc.pNext = NULL;
1138 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1139 demo->depth.mem_alloc.memoryTypeIndex = 0;
1140
Karl Schultze4dc75c2016-02-02 15:37:51 -07001141 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1142 0, /* No requirements */
1143 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001144 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001145
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001146 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001147 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1148 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001149 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001150
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001151 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001152 err =
1153 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001154 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001155
Karl Schultze4dc75c2016-02-02 15:37:51 -07001156 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1157 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001158 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1159 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001160
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001161 /* create image view */
1162 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001163 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001164 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001165}
1166
Tony Barbour1a86d032015-09-21 15:17:33 -06001167/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001169 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001170#ifdef __ANDROID__
1171#include <lunarg.ppm.h>
1172 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001173 cPtr = (char*)lunarg_ppm;
1174 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001175 return false;
1176 }
1177 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001178 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001179 if (rgba_data == NULL) {
1180 return true;
1181 }
1182 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001183 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001184 return false;
1185 }
1186 while(strncmp(cPtr++, "\n", 1));
1187
1188 for (int y = 0; y < *height; y++) {
1189 uint8_t *rowPtr = rgba_data;
1190 for (int x = 0; x < *width; x++) {
1191 memcpy(rowPtr, cPtr, 3);
1192 rowPtr[3] = 255; /* Alpha of 1 */
1193 rowPtr += 4;
1194 cPtr += 3;
1195 }
1196 rgba_data += layout->rowPitch;
1197 }
1198
1199 return true;
1200#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001201 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001202 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001203
Tony Barbour1a86d032015-09-21 15:17:33 -06001204 if (!fPtr)
1205 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001206
Tony Barbour1a86d032015-09-21 15:17:33 -06001207 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001208 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1209 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001210 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001211 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001212
Tony Barbour1a86d032015-09-21 15:17:33 -06001213 do {
1214 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001215 if (cPtr == NULL) {
1216 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001217 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001218 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001219 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001220
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001221 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001222 if (rgba_data == NULL) {
1223 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001224 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001225 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001226 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001227 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001228 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1229 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001230 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001231 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001232
Karl Schultze4dc75c2016-02-02 15:37:51 -07001233 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001234 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001235 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001236 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001237 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001238 rowPtr[3] = 255; /* Alpha of 1 */
1239 rowPtr += 4;
1240 }
1241 rgba_data += layout->rowPitch;
1242 }
1243 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001244 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001245#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001246}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001247
Karl Schultze4dc75c2016-02-02 15:37:51 -07001248static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001249 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001250 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001251 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001252 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001253 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001254 int32_t tex_width;
1255 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001256 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001257 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001258
Karl Schultze4dc75c2016-02-02 15:37:51 -07001259 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001260 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001261 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001262
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001263 tex_obj->tex_width = tex_width;
1264 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001265
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001266 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001267 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001268 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001269 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001270 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001271 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001272 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001273 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001274 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001275 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001276 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001277 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001278 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001279 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001280
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001281 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001282
Karl Schultze4dc75c2016-02-02 15:37:51 -07001283 err =
1284 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001285 assert(!err);
1286
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001287 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001288
Chia-I Wuf48700b2015-11-10 16:21:09 +08001289 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001290 tex_obj->mem_alloc.pNext = NULL;
1291 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1292 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001293
Karl Schultze4dc75c2016-02-02 15:37:51 -07001294 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1295 required_props,
1296 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001297 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001298
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001299 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001300 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001301 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001302 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001303
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001304 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001305 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001306 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001307
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001308 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001309 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001310 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001311 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001312 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001313 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001314 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001315 void *data;
1316
Karl Schultze4dc75c2016-02-02 15:37:51 -07001317 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1318 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001319
Karl Schultze4dc75c2016-02-02 15:37:51 -07001320 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1321 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001322 assert(!err);
1323
1324 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1325 fprintf(stderr, "Error loading texture: %s\n", filename);
1326 }
1327
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001328 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001329 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001330
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001331 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001332 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001333 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1334 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001335 /* setting the image layout does not reference the actual memory so no need
1336 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001337}
1338
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339static void demo_destroy_texture_image(struct demo *demo,
1340 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001341 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001342 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1343 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001344}
1345
Karl Schultze4dc75c2016-02-02 15:37:51 -07001346static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001347 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001348 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001349 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001351 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001352
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001354 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001355
Karl Schultze4dc75c2016-02-02 15:37:51 -07001356 if ((props.linearTilingFeatures &
1357 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1358 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001359 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001360 demo_prepare_texture_image(
1361 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1362 VK_IMAGE_USAGE_SAMPLED_BIT,
1363 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1364 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001365 } else if (props.optimalTilingFeatures &
1366 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001367 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001368 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001369
1370 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001371 demo_prepare_texture_image(
1372 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1373 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1374 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1375 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001376
Karl Schultze4dc75c2016-02-02 15:37:51 -07001377 demo_prepare_texture_image(
1378 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1379 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1380 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001381
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001382 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 VK_IMAGE_ASPECT_COLOR_BIT,
1384 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001385 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1386 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001387
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001388 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001389 VK_IMAGE_ASPECT_COLOR_BIT,
1390 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001391 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1392 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001393
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001394 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001395 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1396 .srcOffset = {0, 0, 0},
1397 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1398 .dstOffset = {0, 0, 0},
1399 .extent = {staging_texture.tex_width,
1400 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001401 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001402 vkCmdCopyImage(
1403 demo->cmd, staging_texture.image,
1404 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1405 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001406
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001407 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001408 VK_IMAGE_ASPECT_COLOR_BIT,
1409 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001410 demo->textures[i].imageLayout,
1411 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001412
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001413 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001414
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001415 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001416 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001417 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1418 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001419 }
1420
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001421 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001422 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001424 .magFilter = VK_FILTER_NEAREST,
1425 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001426 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001427 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1428 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1429 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001431 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001432 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001433 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434 .minLod = 0.0f,
1435 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001436 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001437 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001439
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001440 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001441 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001443 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001444 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001445 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001446 .components =
1447 {
1448 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1449 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1450 },
1451 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001452 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001453 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001454
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001455 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001456 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001457 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001458 assert(!err);
1459
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001460 /* create image view */
1461 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001462 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001463 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001464 assert(!err);
1465 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001466}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001467
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001469 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001470 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001471 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001472 int i;
1473 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001474 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001475 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001476 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001477
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001478 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001479 mat4x4_mul(MVP, VP, demo->model_matrix);
1480 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001481 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001482
Karl Schultze4dc75c2016-02-02 15:37:51 -07001483 for (i = 0; i < 12 * 3; i++) {
1484 data.position[i][0] = g_vertex_buffer_data[i * 3];
1485 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1486 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001487 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001488 data.attr[i][0] = g_uv_buffer_data[2 * i];
1489 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001490 data.attr[i][2] = 0;
1491 data.attr[i][3] = 0;
1492 }
1493
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001494 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001495 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001496 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001497 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001498 err =
1499 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001500 assert(!err);
1501
Karl Schultze4dc75c2016-02-02 15:37:51 -07001502 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1503 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001504
Chia-I Wuf48700b2015-11-10 16:21:09 +08001505 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001506 demo->uniform_data.mem_alloc.pNext = NULL;
1507 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1508 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1509
Karl Schultze4dc75c2016-02-02 15:37:51 -07001510 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001511 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1512 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001513 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001514 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001515
Karl Schultze4dc75c2016-02-02 15:37:51 -07001516 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1517 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001518 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001519
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1521 demo->uniform_data.mem_alloc.allocationSize, 0,
1522 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001523 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001524
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001525 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001526
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001527 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001528
Karl Schultze4dc75c2016-02-02 15:37:51 -07001529 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1530 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001531 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001532
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001533 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1534 demo->uniform_data.buffer_info.offset = 0;
1535 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001536}
1537
Karl Schultze4dc75c2016-02-02 15:37:51 -07001538static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001539 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001540 [0] =
1541 {
1542 .binding = 0,
1543 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1544 .descriptorCount = 1,
1545 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1546 .pImmutableSamplers = NULL,
1547 },
1548 [1] =
1549 {
1550 .binding = 1,
1551 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1552 .descriptorCount = DEMO_TEXTURE_COUNT,
1553 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1554 .pImmutableSamplers = NULL,
1555 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001556 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001557 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001558 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001559 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001560 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001561 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001562 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001563 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001564
Karl Schultze4dc75c2016-02-02 15:37:51 -07001565 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1566 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001567 assert(!err);
1568
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001569 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001570 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1571 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001572 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001573 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001574 };
1575
Karl Schultze4dc75c2016-02-02 15:37:51 -07001576 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001577 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001578 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001579}
1580
Karl Schultze4dc75c2016-02-02 15:37:51 -07001581static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001582 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001583 [0] =
1584 {
1585 .format = demo->format,
1586 .samples = VK_SAMPLE_COUNT_1_BIT,
1587 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1588 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1589 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1590 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1591 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Tony Barbour98390392016-07-28 10:50:13 -06001592 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001593 },
1594 [1] =
1595 {
1596 .format = demo->depth.format,
1597 .samples = VK_SAMPLE_COUNT_1_BIT,
1598 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1599 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1600 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1601 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1602 .initialLayout =
1603 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1604 .finalLayout =
1605 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1606 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001607 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001608 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001609 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001610 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001611 const VkAttachmentReference depth_reference = {
1612 .attachment = 1,
1613 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1614 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001615 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001616 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1617 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001618 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001619 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001620 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001621 .pColorAttachments = &color_reference,
1622 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001623 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001624 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001625 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001626 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001627 const VkRenderPassCreateInfo rp_info = {
1628 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1629 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001630 .attachmentCount = 2,
1631 .pAttachments = attachments,
1632 .subpassCount = 1,
1633 .pSubpasses = &subpass,
1634 .dependencyCount = 0,
1635 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001636 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001637 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001638
Chia-I Wu63636062015-10-26 21:10:41 +08001639 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001640 assert(!err);
1641}
1642
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001643//TODO: Merge shader reading
1644#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001645static VkShaderModule
1646demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001647 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001648 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001649 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001650
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001651 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1652 moduleCreateInfo.pNext = NULL;
1653
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001654 moduleCreateInfo.codeSize = size;
1655 moduleCreateInfo.pCode = code;
1656 moduleCreateInfo.flags = 0;
1657 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1658 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001659
1660 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001661}
1662
Karl Schultze4dc75c2016-02-02 15:37:51 -07001663char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001664 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001665 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001666 void *shader_code;
1667
1668 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001669 if (!fp)
1670 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001671
1672 fseek(fp, 0L, SEEK_END);
1673 size = ftell(fp);
1674
1675 fseek(fp, 0L, SEEK_SET);
1676
1677 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001678 retval = fread(shader_code, size, 1, fp);
1679 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001680
1681 *psize = size;
1682
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001683 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001684 return shader_code;
1685}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001686#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001687
Karl Schultze4dc75c2016-02-02 15:37:51 -07001688static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001689#ifdef __ANDROID__
1690 VkShaderModuleCreateInfo sh_info = {};
1691 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1692
1693#include "cube.vert.h"
1694 sh_info.codeSize = sizeof(cube_vert);
1695 sh_info.pCode = cube_vert;
1696 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1697 assert(!err);
1698#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001699 void *vertShaderCode;
1700 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001701
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001702 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1703
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704 demo->vert_shader_module =
1705 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001706
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001707 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001708#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001709
1710 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711}
1712
Karl Schultze4dc75c2016-02-02 15:37:51 -07001713static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001714#ifdef __ANDROID__
1715 VkShaderModuleCreateInfo sh_info = {};
1716 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1717
1718#include "cube.frag.h"
1719 sh_info.codeSize = sizeof(cube_frag);
1720 sh_info.pCode = cube_frag;
1721 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1722 assert(!err);
1723#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001724 void *fragShaderCode;
1725 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001726
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001727 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001728
Karl Schultze4dc75c2016-02-02 15:37:51 -07001729 demo->frag_shader_module =
1730 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001731
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001732 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001733#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001734
1735 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736}
1737
Karl Schultze4dc75c2016-02-02 15:37:51 -07001738static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001739 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001740 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001741 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001742 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001743 VkPipelineRasterizationStateCreateInfo rs;
1744 VkPipelineColorBlendStateCreateInfo cb;
1745 VkPipelineDepthStencilStateCreateInfo ds;
1746 VkPipelineViewportStateCreateInfo vp;
1747 VkPipelineMultisampleStateCreateInfo ms;
1748 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1749 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001750 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751
Piers Daniellba839d12015-09-29 13:01:09 -06001752 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1753 memset(&dynamicState, 0, sizeof dynamicState);
1754 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1755 dynamicState.pDynamicStates = dynamicStateEnables;
1756
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001758 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001759 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001760
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001761 memset(&vi, 0, sizeof(vi));
1762 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1763
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001764 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001765 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001766 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001767
1768 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001769 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1770 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001771 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001772 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001773 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001774 rs.rasterizerDiscardEnable = VK_FALSE;
1775 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001776 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777
1778 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001779 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1780 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001781 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001782 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001783 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001784 cb.attachmentCount = 1;
1785 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786
Tony Barbour29645d02015-01-16 14:27:35 -07001787 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001788 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001789 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001790 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1791 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001792 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001793 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1794 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001795
1796 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001797 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001798 ds.depthTestEnable = VK_TRUE;
1799 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001800 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001801 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001802 ds.back.failOp = VK_STENCIL_OP_KEEP;
1803 ds.back.passOp = VK_STENCIL_OP_KEEP;
1804 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001805 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001806 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001807
Tony Barbour29645d02015-01-16 14:27:35 -07001808 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001809 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001810 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001811 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001812
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001813 // Two stages: vs and fs
1814 pipeline.stageCount = 2;
1815 VkPipelineShaderStageCreateInfo shaderStages[2];
1816 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1817
Karl Schultze4dc75c2016-02-02 15:37:51 -07001818 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1819 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001820 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001821 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001822
Karl Schultze4dc75c2016-02-02 15:37:51 -07001823 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1824 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001825 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001826 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001827
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001828 memset(&pipelineCache, 0, sizeof(pipelineCache));
1829 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830
Karl Schultze4dc75c2016-02-02 15:37:51 -07001831 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1832 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001833 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001834
Karl Schultze4dc75c2016-02-02 15:37:51 -07001835 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001836 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001837 pipeline.pRasterizationState = &rs;
1838 pipeline.pColorBlendState = &cb;
1839 pipeline.pMultisampleState = &ms;
1840 pipeline.pViewportState = &vp;
1841 pipeline.pDepthStencilState = &ds;
1842 pipeline.pStages = shaderStages;
1843 pipeline.renderPass = demo->render_pass;
1844 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001845
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001846 pipeline.renderPass = demo->render_pass;
1847
Karl Schultze4dc75c2016-02-02 15:37:51 -07001848 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1849 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001850 assert(!err);
1851
Chia-I Wu63636062015-10-26 21:10:41 +08001852 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1853 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001854}
1855
Karl Schultze4dc75c2016-02-02 15:37:51 -07001856static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001857 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001858 [0] =
1859 {
1860 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1861 .descriptorCount = 1,
1862 },
1863 [1] =
1864 {
1865 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1866 .descriptorCount = DEMO_TEXTURE_COUNT,
1867 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001868 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001869 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001870 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001871 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001872 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001873 .poolSizeCount = 2,
1874 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001875 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001876 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001877
Karl Schultze4dc75c2016-02-02 15:37:51 -07001878 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1879 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001880 assert(!err);
1881}
1882
Karl Schultze4dc75c2016-02-02 15:37:51 -07001883static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001884 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001885 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001886 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001887 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001888
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001889 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001890 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001891 .pNext = NULL,
1892 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001893 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001894 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001895 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001896 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001897
Chia-I Wuae721ba2015-05-25 16:27:55 +08001898 memset(&tex_descs, 0, sizeof(tex_descs));
1899 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001900 tex_descs[i].sampler = demo->textures[i].sampler;
1901 tex_descs[i].imageView = demo->textures[i].view;
1902 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001903 }
1904
1905 memset(&writes, 0, sizeof(writes));
1906
1907 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001908 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001909 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001910 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001911 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001912
1913 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001914 writes[1].dstSet = demo->desc_set;
1915 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001916 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001917 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001918 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001919
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001920 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001921}
1922
Karl Schultze4dc75c2016-02-02 15:37:51 -07001923static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001924 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001925 attachments[1] = demo->depth.view;
1926
Chia-I Wuf973e322015-07-08 13:34:24 +08001927 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001928 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1929 .pNext = NULL,
1930 .renderPass = demo->render_pass,
1931 .attachmentCount = 2,
1932 .pAttachments = attachments,
1933 .width = demo->width,
1934 .height = demo->height,
1935 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001936 };
1937 VkResult U_ASSERT_ONLY err;
1938 uint32_t i;
1939
Karl Schultze4dc75c2016-02-02 15:37:51 -07001940 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1941 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001942 assert(demo->framebuffers);
1943
1944 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001945 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001946 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1947 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001948 assert(!err);
1949 }
1950}
1951
Karl Schultze4dc75c2016-02-02 15:37:51 -07001952static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001953 VkResult U_ASSERT_ONLY err;
1954
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001955 const VkCommandPoolCreateInfo cmd_pool_info = {
1956 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001957 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001958 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001959 .flags = 0,
1960 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001961 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1962 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001963 assert(!err);
1964
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001965 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001966 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001967 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001968 .commandPool = demo->cmd_pool,
1969 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001970 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972
1973 demo_prepare_buffers(demo);
1974 demo_prepare_depth(demo);
1975 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001976 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001977
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001978 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001979 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001980 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001981
Ian Elliotta0781972015-08-21 15:09:33 -06001982 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001983 err =
1984 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001985 assert(!err);
1986 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001987
Tony Barbour22e06272016-09-07 16:07:56 -06001988 if (demo->separate_present_queue) {
1989 const VkCommandPoolCreateInfo cmd_pool_info = {
1990 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1991 .pNext = NULL,
1992 .queueFamilyIndex = demo->present_queue_family_index,
1993 .flags = 0,
1994 };
1995 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1996 &demo->present_cmd_pool);
1997 assert(!err);
1998 const VkCommandBufferAllocateInfo cmd = {
1999 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2000 .pNext = NULL,
2001 .commandPool = demo->present_cmd_pool,
2002 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2003 .commandBufferCount = 1,
2004 };
2005 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2006 err = vkAllocateCommandBuffers(
2007 demo->device, &cmd, &demo->buffers[i].graphics_to_present_cmd);
2008 assert(!err);
2009 demo_build_image_ownership_cmd(demo, i);
2010 }
2011 }
2012
Chia-I Wu63ea9262015-03-26 13:14:16 +08002013 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002014 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002015
Chia-I Wuf973e322015-07-08 13:34:24 +08002016 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002017
Ian Elliotta0781972015-08-21 15:09:33 -06002018 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002019 demo->current_buffer = i;
2020 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
2021 }
2022
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002023 /*
2024 * Prepare functions above may generate pipeline commands
2025 * that need to be flushed before beginning the render loop.
2026 */
2027 demo_flush_init_cmd(demo);
2028
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002029 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002030 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031}
2032
Karl Schultze4dc75c2016-02-02 15:37:51 -07002033static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002034 uint32_t i;
2035
2036 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002037 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002038
Tony Barbour66a56c32016-09-21 13:10:56 -06002039 // Wait for fences from present operations
2040 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002041 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002042 vkDestroyFence(demo->device, demo->fences[i], NULL);
2043 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2044 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2045 if (demo->separate_present_queue) {
2046 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2047 }
2048 }
2049
Tony Barbourd8e504f2015-10-08 14:26:24 -06002050 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002051 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002052 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06002053 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002054 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002055
Chia-I Wu63636062015-10-26 21:10:41 +08002056 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2057 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2058 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2059 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2060 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002061
2062 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002063 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2064 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2065 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2066 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002067 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002068 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002069
Chia-I Wu63636062015-10-26 21:10:41 +08002070 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2071 vkDestroyImage(demo->device, demo->depth.image, NULL);
2072 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002073
Chia-I Wu63636062015-10-26 21:10:41 +08002074 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2075 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002076
Ian Elliotta0781972015-08-21 15:09:33 -06002077 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002078 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002079 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2080 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06002081 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002082 free(demo->buffers);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002083 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002084 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002085
Tony Barbour22e06272016-09-07 16:07:56 -06002086 if (demo->separate_present_queue) {
2087 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002088 }
Chia-I Wu63636062015-10-26 21:10:41 +08002089 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002090 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002091 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002092 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002093 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002094 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002095
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002096#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002097 if (demo->use_xlib) {
2098 XDestroyWindow(demo->display, demo->xlib_window);
2099 XCloseDisplay(demo->display);
2100 } else {
2101 xcb_destroy_window(demo->connection, demo->xcb_window);
2102 xcb_disconnect(demo->connection);
2103 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002104 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002105#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002106 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002107 xcb_disconnect(demo->connection);
2108 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002109#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2110 wl_shell_surface_destroy(demo->shell_surface);
2111 wl_surface_destroy(demo->window);
2112 wl_shell_destroy(demo->shell);
2113 wl_compositor_destroy(demo->compositor);
2114 wl_registry_destroy(demo->registry);
2115 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002116#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002117}
2118
Karl Schultze4dc75c2016-02-02 15:37:51 -07002119static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002120 uint32_t i;
2121
Mike Stroyanbb60b382015-10-28 09:46:57 -06002122 // Don't react to resize until after first initialization.
2123 if (!demo->prepared) {
2124 return;
2125 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002126 // In order to properly resize the window, we must re-create the swapchain
2127 // AND redo the command buffers, etc.
2128 //
2129 // First, perform part of the demo_cleanup() function:
2130 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002131 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002132
2133 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002134 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002135 }
2136 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002137 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002138
Chia-I Wu63636062015-10-26 21:10:41 +08002139 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2140 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2141 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2142 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2143 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002144
2145 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002146 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2147 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2148 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2149 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002150 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002151
Chia-I Wu63636062015-10-26 21:10:41 +08002152 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2153 vkDestroyImage(demo->device, demo->depth.image, NULL);
2154 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002155
Chia-I Wu63636062015-10-26 21:10:41 +08002156 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2157 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002158
2159 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002160 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002161 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2162 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002163 }
Chia-I Wu63636062015-10-26 21:10:41 +08002164 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002165 if (demo->separate_present_queue) {
2166 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2167 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002168 free(demo->buffers);
2169
Ian Elliottcf074d32015-10-16 18:02:43 -06002170 // Second, re-perform the demo_prepare() function, which will re-create the
2171 // swapchain:
2172 demo_prepare(demo);
2173}
2174
David Pinedo2bb7c932015-06-18 17:03:14 -06002175// On MS-Windows, make this a global, so it's available to WndProc()
2176struct demo demo;
2177
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002178#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002179static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002180 if (!demo->prepared)
2181 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002182
Ian Elliott639ca472015-04-16 15:23:05 -06002183 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002184 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002185 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002187 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002188 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002189}
Ian Elliott639ca472015-04-16 15:23:05 -06002190
2191// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002192LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2193 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002194 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002195 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002196 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002197 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002198 // The validation callback calls MessageBox which can generate paint
2199 // events - don't make more Vulkan calls if we got here from the
2200 // callback
2201 if (!in_callback) {
2202 demo_run(&demo);
2203 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002204 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002205 case WM_GETMINMAXINFO: // set window's minimum size
2206 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2207 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002208 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002209 // Resize the application to the new window size, except when
2210 // it was minimized. Vulkan doesn't support images or swapchains
2211 // with width=0 and height=0.
2212 if (wParam != SIZE_MINIMIZED) {
2213 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002214 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002215 demo_resize(&demo);
2216 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002217 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002218 default:
2219 break;
2220 }
2221 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2222}
2223
Karl Schultze4dc75c2016-02-02 15:37:51 -07002224static void demo_create_window(struct demo *demo) {
2225 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002226
2227 // Initialize the window class structure:
2228 win_class.cbSize = sizeof(WNDCLASSEX);
2229 win_class.style = CS_HREDRAW | CS_VREDRAW;
2230 win_class.lpfnWndProc = WndProc;
2231 win_class.cbClsExtra = 0;
2232 win_class.cbWndExtra = 0;
2233 win_class.hInstance = demo->connection; // hInstance
2234 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2235 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2236 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2237 win_class.lpszMenuName = NULL;
2238 win_class.lpszClassName = demo->name;
2239 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2240 // Register window class:
2241 if (!RegisterClassEx(&win_class)) {
2242 // It didn't work, so try to give a useful error:
2243 printf("Unexpected error trying to start the application!\n");
2244 fflush(stdout);
2245 exit(1);
2246 }
2247 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002248 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002249 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002250 demo->window = CreateWindowEx(0,
2251 demo->name, // class name
2252 demo->name, // app name
2253 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002254 WS_VISIBLE | WS_SYSMENU,
2255 100, 100, // x/y coords
2256 wr.right - wr.left, // width
2257 wr.bottom - wr.top, // height
2258 NULL, // handle to parent
2259 NULL, // handle to menu
2260 demo->connection, // hInstance
2261 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002262 if (!demo->window) {
2263 // It didn't work, so try to give a useful error:
2264 printf("Cannot create a window in which to draw!\n");
2265 fflush(stdout);
2266 exit(1);
2267 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002268 // Window client area size must be at least 1 pixel high, to prevent crash.
2269 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2270 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002271}
Tony Barbour8295ac42016-08-08 11:04:17 -06002272#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002273static void demo_create_xlib_window(struct demo *demo) {
2274
2275 demo->display = XOpenDisplay(NULL);
2276 long visualMask = VisualScreenMask;
2277 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002278 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002279 vInfoTemplate.screen = DefaultScreen(demo->display);
2280 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2281 &vInfoTemplate, &numberOfVisuals);
2282
2283 Colormap colormap = XCreateColormap(
2284 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2285 visualInfo->visual, AllocNone);
2286
Rene Lindsay11612722016-06-13 17:20:39 -06002287 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002288 windowAttributes.colormap = colormap;
2289 windowAttributes.background_pixel = 0xFFFFFFFF;
2290 windowAttributes.border_pixel = 0;
2291 windowAttributes.event_mask =
2292 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2293
2294 demo->xlib_window = XCreateWindow(
2295 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2296 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2297 visualInfo->visual,
2298 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2299
2300 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2301 XMapWindow(demo->display, demo->xlib_window);
2302 XFlush(demo->display);
2303 demo->xlib_wm_delete_window =
2304 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2305}
2306static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2307 switch(event->type) {
2308 case ClientMessage:
2309 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2310 demo->quit = true;
2311 break;
2312 case KeyPress:
2313 switch (event->xkey.keycode) {
2314 case 0x9: // Escape
2315 demo->quit = true;
2316 break;
2317 case 0x71: // left arrow key
2318 demo->spin_angle += demo->spin_increment;
2319 break;
2320 case 0x72: // right arrow key
2321 demo->spin_angle -= demo->spin_increment;
2322 break;
2323 case 0x41:
2324 demo->pause = !demo->pause;
2325 break;
2326 }
2327 break;
2328 case ConfigureNotify:
2329 if ((demo->width != event->xconfigure.width) ||
2330 (demo->height != event->xconfigure.height)) {
2331 demo->width = event->xconfigure.width;
2332 demo->height = event->xconfigure.height;
2333 demo_resize(demo);
2334 }
2335 break;
2336 default:
2337 break;
2338 }
2339
2340}
2341
2342static void demo_run_xlib(struct demo *demo) {
2343
2344 while (!demo->quit) {
2345 XEvent event;
2346
2347 if (demo->pause) {
2348 XNextEvent(demo->display, &event);
2349 demo_handle_xlib_event(demo, &event);
2350 } else {
2351 while (XPending(demo->display) > 0) {
2352 XNextEvent(demo->display, &event);
2353 demo_handle_xlib_event(demo, &event);
2354 }
2355 }
2356
Tony Barbour2593b182016-04-19 10:57:58 -06002357 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002358 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002359 demo->curFrame++;
2360 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2361 demo->quit = true;
2362 }
2363}
Tony Barbour8295ac42016-08-08 11:04:17 -06002364#endif // VK_USE_PLATFORM_XLIB_KHR
2365#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002366static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002367 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002368 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002369 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002370 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002371 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002372 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002373 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002374 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2375 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002376 demo->quit = true;
2377 }
2378 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002379 case XCB_KEY_RELEASE: {
2380 const xcb_key_release_event_t *key =
2381 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002382
Karl Schultze4dc75c2016-02-02 15:37:51 -07002383 switch (key->detail) {
2384 case 0x9: // Escape
2385 demo->quit = true;
2386 break;
2387 case 0x71: // left arrow key
2388 demo->spin_angle += demo->spin_increment;
2389 break;
2390 case 0x72: // right arrow key
2391 demo->spin_angle -= demo->spin_increment;
2392 break;
2393 case 0x41:
2394 demo->pause = !demo->pause;
2395 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002396 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002397 } break;
2398 case XCB_CONFIGURE_NOTIFY: {
2399 const xcb_configure_notify_event_t *cfg =
2400 (const xcb_configure_notify_event_t *)event;
2401 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002402 demo->width = cfg->width;
2403 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002404 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002405 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002406 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002407 default:
2408 break;
2409 }
2410}
2411
Tony Barbour2593b182016-04-19 10:57:58 -06002412static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002413 xcb_flush(demo->connection);
2414
2415 while (!demo->quit) {
2416 xcb_generic_event_t *event;
2417
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002418 if (demo->pause) {
2419 event = xcb_wait_for_event(demo->connection);
2420 } else {
2421 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002422 while(event) {
2423 demo_handle_xcb_event(demo, event);
2424 free(event);
2425 event = xcb_poll_for_event(demo->connection);
2426 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002427 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002428
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002429 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002430 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002431 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002432 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002433 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002434 }
2435}
2436
Tony Barbour2593b182016-04-19 10:57:58 -06002437static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002438 uint32_t value_mask, value_list[32];
2439
Tony Barbour2593b182016-04-19 10:57:58 -06002440 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002441
2442 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2443 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002444 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002445 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002446
Tony Barbour2593b182016-04-19 10:57:58 -06002447 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002448 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2449 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2450 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002451
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002452 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002453 xcb_intern_atom_cookie_t cookie =
2454 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2455 xcb_intern_atom_reply_t *reply =
2456 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002457
Karl Schultze4dc75c2016-02-02 15:37:51 -07002458 xcb_intern_atom_cookie_t cookie2 =
2459 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2460 demo->atom_wm_delete_window =
2461 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002462
Tony Barbour2593b182016-04-19 10:57:58 -06002463 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002464 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002465 &(*demo->atom_wm_delete_window).atom);
2466 free(reply);
2467
Tony Barbour2593b182016-04-19 10:57:58 -06002468 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002469
Karl Schultze4dc75c2016-02-02 15:37:51 -07002470 // Force the x/y coordinates to 100,100 results are identical in consecutive
2471 // runs
2472 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002473 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002474 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002475}
Tony Barbour6b131662016-08-25 13:14:45 -06002476// VK_USE_PLATFORM_XCB_KHR
2477#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002478static void demo_run(struct demo *demo) {
2479 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002480 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002481 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002482 demo->curFrame++;
2483 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2484 demo->quit = true;
2485 }
2486}
2487
2488static void handle_ping(void *data UNUSED,
2489 struct wl_shell_surface *shell_surface,
2490 uint32_t serial) {
2491 wl_shell_surface_pong(shell_surface, serial);
2492}
2493
2494static void handle_configure(void *data UNUSED,
2495 struct wl_shell_surface *shell_surface UNUSED,
2496 uint32_t edges UNUSED, int32_t width UNUSED,
2497 int32_t height UNUSED) {}
2498
2499static void handle_popup_done(void *data UNUSED,
2500 struct wl_shell_surface *shell_surface UNUSED) {}
2501
2502static const struct wl_shell_surface_listener shell_surface_listener = {
2503 handle_ping, handle_configure, handle_popup_done};
2504
2505static void demo_create_window(struct demo *demo) {
2506 demo->window = wl_compositor_create_surface(demo->compositor);
2507 if (!demo->window) {
2508 printf("Can not create wayland_surface from compositor!\n");
2509 fflush(stdout);
2510 exit(1);
2511 }
2512
2513 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2514 if (!demo->shell_surface) {
2515 printf("Can not get shell_surface from wayland_surface!\n");
2516 fflush(stdout);
2517 exit(1);
2518 }
2519 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2520 demo);
2521 wl_shell_surface_set_toplevel(demo->shell_surface);
2522 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2523}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002524#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2525static void demo_run(struct demo *demo) {
2526 if (!demo->prepared)
2527 return;
2528
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002529 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002530 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002531 demo->curFrame++;
2532}
2533#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002534
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002535/*
2536 * Return 1 (true) if all layer names specified in check_names
2537 * can be found in given layer properties.
2538 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002539static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002540 uint32_t layer_count,
2541 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002542 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002543 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002544 for (uint32_t j = 0; j < layer_count; j++) {
2545 if (!strcmp(check_names[i], layers[j].layerName)) {
2546 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002547 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002548 }
2549 }
2550 if (!found) {
2551 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2552 return 0;
2553 }
2554 }
2555 return 1;
2556}
2557
Karl Schultze4dc75c2016-02-02 15:37:51 -07002558static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002559 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002560 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002561 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002562 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002563 char **instance_validation_layers = NULL;
2564 demo->enabled_extension_count = 0;
2565 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002566
Karl Schultz7c50ad62016-04-01 12:07:03 -06002567 char *instance_validation_layers_alt1[] = {
2568 "VK_LAYER_LUNARG_standard_validation"
2569 };
2570
2571 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002572 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2573 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2574 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2575 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002576 };
2577
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002578 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002579 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002580 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002581
Karl Schultz7c50ad62016-04-01 12:07:03 -06002582 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002583 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002584
Karl Schultz7c50ad62016-04-01 12:07:03 -06002585 instance_validation_layers = instance_validation_layers_alt1;
2586 if (instance_layer_count > 0) {
2587 VkLayerProperties *instance_layers =
2588 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2589 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2590 instance_layers);
2591 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002592
Karl Schultz7c50ad62016-04-01 12:07:03 -06002593
2594 validation_found = demo_check_layers(
2595 ARRAY_SIZE(instance_validation_layers_alt1),
2596 instance_validation_layers, instance_layer_count,
2597 instance_layers);
2598 if (validation_found) {
2599 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002600 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2601 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002602 } else {
2603 // use alternative set of validation layers
2604 instance_validation_layers = instance_validation_layers_alt2;
2605 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2606 validation_found = demo_check_layers(
2607 ARRAY_SIZE(instance_validation_layers_alt2),
2608 instance_validation_layers, instance_layer_count,
2609 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002610 validation_layer_count =
2611 ARRAY_SIZE(instance_validation_layers_alt2);
2612 for (uint32_t i = 0; i < validation_layer_count; i++) {
2613 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002614 }
2615 }
2616 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002617 }
Dustin Graves7c287352016-01-27 13:48:06 -07002618
Karl Schultz7c50ad62016-04-01 12:07:03 -06002619 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002620 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002621 "required validation layer.\n\n"
2622 "Please look at the Getting Started guide for additional "
2623 "information.\n",
2624 "vkCreateInstance Failure");
2625 }
Dustin Graves7c287352016-01-27 13:48:06 -07002626 }
2627
2628 /* Look for instance extensions */
2629 VkBool32 surfaceExtFound = 0;
2630 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002631#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002632 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002633#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002634 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002635
Karl Schultze4dc75c2016-02-02 15:37:51 -07002636 err = vkEnumerateInstanceExtensionProperties(
2637 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002638 assert(!err);
2639
Dustin Graves7c287352016-01-27 13:48:06 -07002640 if (instance_extension_count > 0) {
2641 VkExtensionProperties *instance_extensions =
2642 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2643 err = vkEnumerateInstanceExtensionProperties(
2644 NULL, &instance_extension_count, instance_extensions);
2645 assert(!err);
2646 for (uint32_t i = 0; i < instance_extension_count; i++) {
2647 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2648 instance_extensions[i].extensionName)) {
2649 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002650 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002651 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002652 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002653#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002654 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2655 instance_extensions[i].extensionName)) {
2656 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002657 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002658 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2659 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002660#endif
2661#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002662 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2663 instance_extensions[i].extensionName)) {
2664 platformSurfaceExtFound = 1;
2665 xlibSurfaceExtFound = 1;
2666 demo->extension_names[demo->enabled_extension_count++] =
2667 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2668 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002669#endif
2670#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002671 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2672 instance_extensions[i].extensionName)) {
2673 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002674 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002675 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2676 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002677#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002678#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2679 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2680 instance_extensions[i].extensionName)) {
2681 platformSurfaceExtFound = 1;
2682 demo->extension_names[demo->enabled_extension_count++] =
2683 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2684 }
2685#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002686#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002687 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2688 instance_extensions[i].extensionName)) {
2689 platformSurfaceExtFound = 1;
2690 demo->extension_names[demo->enabled_extension_count++] =
2691 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2692 }
2693#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002694 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2695 instance_extensions[i].extensionName)) {
2696 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002697 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002698 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2699 }
2700 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002701 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002702 }
Dustin Graves7c287352016-01-27 13:48:06 -07002703
2704 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002705 }
Dustin Graves7c287352016-01-27 13:48:06 -07002706
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002707 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002708 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2709 "the " VK_KHR_SURFACE_EXTENSION_NAME
2710 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002711 "Vulkan installable client driver (ICD) installed?\nPlease "
2712 "look at the Getting Started guide for additional "
2713 "information.\n",
2714 "vkCreateInstance Failure");
2715 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002716 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002717#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002718 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2719 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2720 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002721 "Vulkan installable client driver (ICD) installed?\nPlease "
2722 "look at the Getting Started guide for additional "
2723 "information.\n",
2724 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002725#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002726 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2727 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2728 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002729 "Vulkan installable client driver (ICD) installed?\nPlease "
2730 "look at the Getting Started guide for additional "
2731 "information.\n",
2732 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002733#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2734 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2735 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2736 " extension.\n\nDo you have a compatible "
2737 "Vulkan installable client driver (ICD) installed?\nPlease "
2738 "look at the Getting Started guide for additional "
2739 "information.\n",
2740 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002741#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2742 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2743 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2744 " extension.\n\nDo you have a compatible "
2745 "Vulkan installable client driver (ICD) installed?\nPlease "
2746 "look at the Getting Started guide for additional "
2747 "information.\n",
2748 "vkCreateInstance Failure");
2749#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002750 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002751#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002752 if (demo->use_xlib && !xlibSurfaceExtFound) {
2753 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2754 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2755 " extension.\n\nDo you have a compatible "
2756 "Vulkan installable client driver (ICD) installed?\nPlease "
2757 "look at the Getting Started guide for additional "
2758 "information.\n",
2759 "vkCreateInstance Failure");
2760 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002761#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002762 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002763 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002764 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002765 .pApplicationName = APP_SHORT_NAME,
2766 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002767 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002768 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002769 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002770 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002771 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002772 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002773 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002774 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002775 .enabledLayerCount = demo->enabled_layer_count,
2776 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2777 .enabledExtensionCount = demo->enabled_extension_count,
2778 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002779 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002780
2781 /*
2782 * This is info for a temp callback to use during CreateInstance.
2783 * After the instance is created, we use the instance-based
2784 * function to register the final callback.
2785 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002786 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002787 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002788 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2789 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002790 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002791 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002792 dbgCreateInfo.flags =
2793 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2794 inst_info.pNext = &dbgCreateInfo;
2795 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002796
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002797 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002798
Chia-I Wu63636062015-10-26 21:10:41 +08002799 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002800 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2801 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002802 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002803 "additional information.\n",
2804 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002805 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002806 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002807 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002808 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002809 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002810 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2811 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002812 "the Getting Started guide for additional information.\n",
2813 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002814 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002815
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002816 /* Make initial call to query gpu_count, then second call for gpu info*/
2817 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2818 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002819
2820 if (gpu_count > 0) {
2821 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2822 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2823 assert(!err);
2824 /* For cube demo we just grab the first physical device */
2825 demo->gpu = physical_devices[0];
2826 free(physical_devices);
2827 } else {
2828 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2829 "Do you have a compatible Vulkan installable client driver (ICD) "
2830 "installed?\nPlease look at the Getting Started guide for "
2831 "additional information.\n",
2832 "vkEnumeratePhysicalDevices Failure");
2833 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002834
Dustin Graves7c287352016-01-27 13:48:06 -07002835 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002836 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002837 VkBool32 swapchainExtFound = 0;
2838 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002839 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002840
Karl Schultze4dc75c2016-02-02 15:37:51 -07002841 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2842 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002843 assert(!err);
2844
Dustin Graves7c287352016-01-27 13:48:06 -07002845 if (device_extension_count > 0) {
2846 VkExtensionProperties *device_extensions =
2847 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2848 err = vkEnumerateDeviceExtensionProperties(
2849 demo->gpu, NULL, &device_extension_count, device_extensions);
2850 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002851
Dustin Graves7c287352016-01-27 13:48:06 -07002852 for (uint32_t i = 0; i < device_extension_count; i++) {
2853 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2854 device_extensions[i].extensionName)) {
2855 swapchainExtFound = 1;
2856 demo->extension_names[demo->enabled_extension_count++] =
2857 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2858 }
2859 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002860 }
Dustin Graves7c287352016-01-27 13:48:06 -07002861
2862 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002863 }
Dustin Graves7c287352016-01-27 13:48:06 -07002864
Tony Barbourcc69f452015-10-08 13:59:42 -06002865 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002866 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2867 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2868 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002869 "Vulkan installable client driver (ICD) installed?\nPlease "
2870 "look at the Getting Started guide for additional "
2871 "information.\n",
2872 "vkCreateInstance Failure");
2873 }
2874
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002875 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002876 demo->CreateDebugReportCallback =
2877 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2878 demo->inst, "vkCreateDebugReportCallbackEXT");
2879 demo->DestroyDebugReportCallback =
2880 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2881 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002882 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002883 ERR_EXIT(
2884 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2885 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002886 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002887 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002888 ERR_EXIT(
2889 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2890 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002891 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002892 demo->DebugReportMessage =
2893 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2894 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002895 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002896 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002897 "vkGetProcAddr Failure");
2898 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002899
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002900 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002901 PFN_vkDebugReportCallbackEXT callback;
2902 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002903 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002904 dbgCreateInfo.pNext = NULL;
2905 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002906 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002907 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002908 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002909 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2910 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002911 switch (err) {
2912 case VK_SUCCESS:
2913 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002914 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002915 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2916 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002917 break;
2918 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002919 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2920 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002921 break;
2922 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002923 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002924 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002925
2926 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002927 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2928 &demo->queue_family_count, NULL);
2929 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002930
Karl Schultze4dc75c2016-02-02 15:37:51 -07002931 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002932 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2933 vkGetPhysicalDeviceQueueFamilyProperties(
2934 demo->gpu, &demo->queue_family_count, demo->queue_props);
2935
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002936 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002937 // If app has specific feature requirements it should check supported
2938 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002939 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002940 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002941
Tony Barbourda2bad52016-01-22 14:36:40 -07002942 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2943 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2944 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2945 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2946 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2947}
2948
Karl Schultze4dc75c2016-02-02 15:37:51 -07002949static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002950 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002951 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06002952 VkDeviceQueueCreateInfo queues[2];
2953 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2954 queues[0].pNext = NULL;
2955 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
2956 queues[0].queueCount = 1;
2957 queues[0].pQueuePriorities = queue_priorities;
2958 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002959
2960 VkDeviceCreateInfo device = {
2961 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2962 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002963 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06002964 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002965 .enabledLayerCount = 0,
2966 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002967 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002968 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2969 .pEnabledFeatures =
2970 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002971 };
Tony Barbour22e06272016-09-07 16:07:56 -06002972 if (demo->separate_present_queue) {
2973 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2974 queues[1].pNext = NULL;
2975 queues[1].queueFamilyIndex = demo->present_queue_family_index;
2976 queues[1].queueCount = 1;
2977 queues[1].pQueuePriorities = queue_priorities;
2978 queues[1].flags = 0;
2979 device.queueCreateInfoCount = 2;
2980 }
Chia-I Wu63636062015-10-26 21:10:41 +08002981 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002982 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002983}
2984
Karl Schultze4dc75c2016-02-02 15:37:51 -07002985static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002986 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002987 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002988
Karl Schultze4dc75c2016-02-02 15:37:51 -07002989// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002990#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002991 VkWin32SurfaceCreateInfoKHR createInfo;
2992 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2993 createInfo.pNext = NULL;
2994 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002995 createInfo.hinstance = demo->connection;
2996 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002997
Karl Schultze4dc75c2016-02-02 15:37:51 -07002998 err =
2999 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003000#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3001 VkWaylandSurfaceCreateInfoKHR createInfo;
3002 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3003 createInfo.pNext = NULL;
3004 createInfo.flags = 0;
3005 createInfo.display = demo->display;
3006 createInfo.surface = demo->window;
3007
3008 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3009 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003010#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3011 VkAndroidSurfaceCreateInfoKHR createInfo;
3012 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3013 createInfo.pNext = NULL;
3014 createInfo.flags = 0;
3015 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003016
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003017 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3018#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003019 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003020#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003021 VkXlibSurfaceCreateInfoKHR createInfo;
3022 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3023 createInfo.pNext = NULL;
3024 createInfo.flags = 0;
3025 createInfo.dpy = demo->display;
3026 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003027
Tony Barbour2593b182016-04-19 10:57:58 -06003028 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
3029 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003030#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003031 }
3032 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003033#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003034 VkXcbSurfaceCreateInfoKHR createInfo;
3035 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3036 createInfo.pNext = NULL;
3037 createInfo.flags = 0;
3038 createInfo.connection = demo->connection;
3039 createInfo.window = demo->xcb_window;
3040
3041 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003042#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003043 }
Tony Barbour2593b182016-04-19 10:57:58 -06003044 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003045
Tony Barbourcc69f452015-10-08 13:59:42 -06003046 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003047 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003048 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
3049 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003050 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003051 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003052 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003053
3054 // Search for a graphics and a present queue in the array of queue
3055 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003056 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3057 uint32_t presentQueueFamilyIndex = UINT32_MAX;
3058 for (i = 0; i < demo->queue_family_count; i++) {
3059 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3060 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3061 graphicsQueueFamilyIndex = i;
3062 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003063
Tony Barbourab2a0362016-09-06 11:40:12 -06003064 if (supportsPresent[i] == VK_TRUE) {
3065 graphicsQueueFamilyIndex = i;
3066 presentQueueFamilyIndex = i;
3067 break;
3068 }
3069 }
3070 }
3071
3072 if (presentQueueFamilyIndex == UINT32_MAX) {
3073 // If didn't find a queue that supports both graphics and present, then
3074 // find a separate present queue.
Mark Lobodzinski5abdfcc2016-10-03 16:00:40 -06003075 for (i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003076 if (supportsPresent[i] == VK_TRUE) {
3077 presentQueueFamilyIndex = i;
3078 break;
3079 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003080 }
3081 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003082
3083 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003084 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3085 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003086 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003087 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003088 }
3089
Tony Barbourab2a0362016-09-06 11:40:12 -06003090 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3091 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003092 demo->separate_present_queue =
3093 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003094 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003095
Tony Barbourda2bad52016-01-22 14:36:40 -07003096 demo_create_device(demo);
3097
3098 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3099 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3100 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3101 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3102 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3103
Tony Barboura2a67812016-07-18 13:21:06 -06003104 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3105 &demo->graphics_queue);
3106
Tony Barbour22e06272016-09-07 16:07:56 -06003107 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003108 demo->present_queue = demo->graphics_queue;
3109 } else {
3110 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3111 &demo->present_queue);
3112 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003113
Ian Elliottaaae5352015-07-06 14:27:58 -06003114 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003115 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003116 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003117 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003118 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003119 VkSurfaceFormatKHR *surfFormats =
3120 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003121 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003122 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003123 assert(!err);
3124 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3125 // the surface has no preferred format. Otherwise, at least one
3126 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003127 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003128 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003129 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003130 assert(formatCount >= 1);
3131 demo->format = surfFormats[0].format;
3132 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003133 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003134
David Pinedo2bb7c932015-06-18 17:03:14 -06003135 demo->quit = false;
3136 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003137
Tony Barbource7524e2016-07-28 12:01:45 -06003138 // Create semaphores to synchronize acquiring presentable buffers before
3139 // rendering and waiting for drawing to be complete before presenting
3140 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3141 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3142 .pNext = NULL,
3143 .flags = 0,
3144 };
Tony Barbource7524e2016-07-28 12:01:45 -06003145
Tony Barbour66a56c32016-09-21 13:10:56 -06003146 // Create fences that we can use to throttle if we get too far
3147 // ahead of the image presents
3148 VkFenceCreateInfo fence_ci = {
3149 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3150 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003151 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003152 };
3153 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3154 vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003155 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003156 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003157 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003158
3159 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3160 &demo->draw_complete_semaphores[i]);
3161 assert(!err);
3162
3163 if (demo->separate_present_queue) {
3164 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3165 &demo->image_ownership_semaphores[i]);
3166 assert(!err);
3167 }
Tony Barbour22e06272016-09-07 16:07:56 -06003168 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003169 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003170
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003171 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003172 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003173}
3174
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003175#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3176static void registry_handle_global(void *data, struct wl_registry *registry,
3177 uint32_t name, const char *interface,
3178 uint32_t version UNUSED) {
3179 struct demo *demo = data;
3180 if (strcmp(interface, "wl_compositor") == 0) {
3181 demo->compositor =
3182 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3183 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3184 * tp xdg_shell */
3185 } else if (strcmp(interface, "wl_shell") == 0) {
3186 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3187 }
3188}
3189
3190static void registry_handle_global_remove(void *data UNUSED,
3191 struct wl_registry *registry UNUSED,
3192 uint32_t name UNUSED) {}
3193
3194static const struct wl_registry_listener registry_listener = {
3195 registry_handle_global, registry_handle_global_remove};
3196#endif
3197
Karl Schultze4dc75c2016-02-02 15:37:51 -07003198static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003199#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003200 const xcb_setup_t *setup;
3201 xcb_screen_iterator_t iter;
3202 int scr;
3203
3204 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003205 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003206 printf("Cannot find a compatible Vulkan installable client driver "
3207 "(ICD).\nExiting ...\n");
3208 fflush(stdout);
3209 exit(1);
3210 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003211
3212 setup = xcb_get_setup(demo->connection);
3213 iter = xcb_setup_roots_iterator(setup);
3214 while (scr-- > 0)
3215 xcb_screen_next(&iter);
3216
3217 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003218#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3219 demo->display = wl_display_connect(NULL);
3220
3221 if (demo->display == NULL) {
3222 printf("Cannot find a compatible Vulkan installable client driver "
3223 "(ICD).\nExiting ...\n");
3224 fflush(stdout);
3225 exit(1);
3226 }
3227
3228 demo->registry = wl_display_get_registry(demo->display);
3229 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3230 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003231#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003232}
3233
Karl Schultze4dc75c2016-02-02 15:37:51 -07003234static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003235 vec3 eye = {0.0f, 3.0f, 5.0f};
3236 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003237 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003238
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003239 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003240 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003241
Piers Daniell735ee532015-02-23 16:23:13 -07003242 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003243 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003244 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003245 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003246 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003247 if (strcmp(argv[i], "--break") == 0) {
3248 demo->use_break = true;
3249 continue;
3250 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003251 if (strcmp(argv[i], "--validate") == 0) {
3252 demo->validate = true;
3253 continue;
3254 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003255#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003256 if (strcmp(argv[i], "--xlib") == 0) {
3257 demo->use_xlib = true;
3258 continue;
3259 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003260#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003261 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3262 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3263 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003264 i++;
3265 continue;
3266 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003267 if (strcmp(argv[i], "--suppress_popups") == 0) {
3268 demo->suppress_popups = true;
3269 continue;
3270 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003271
Cody Northropaf28a532016-09-27 10:50:57 -06003272#if defined(ANDROID)
3273 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3274#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003275 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003276#if defined(VK_USE_PLATFORM_XLIB_KHR)
3277 "[--xlib] "
3278#endif
lenny-lunargfbe22392016-06-06 11:07:53 -06003279 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003280 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003281 fflush(stderr);
3282 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003283#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003284 }
3285
Tony Barbour2593b182016-04-19 10:57:58 -06003286 if (!demo->use_xlib)
3287 demo_init_connection(demo);
3288
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003289 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003290
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003291 demo->width = 500;
3292 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003293
Tony Barbour66a56c32016-09-21 13:10:56 -06003294 demo->spin_angle = 4.0f;
3295 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003296 demo->pause = false;
3297
Karl Schultze4dc75c2016-02-02 15:37:51 -07003298 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3299 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003300 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3301 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003302
3303 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003304}
3305
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003306#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003307// Include header required for parsing the command line options.
3308#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003309
Karl Schultze4dc75c2016-02-02 15:37:51 -07003310int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3311 int nCmdShow) {
3312 MSG msg; // message
3313 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003314 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003315 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003316
Karl Schultze4dc75c2016-02-02 15:37:51 -07003317 // Use the CommandLine functions to get the command line arguments.
3318 // Unfortunately, Microsoft outputs
3319 // this information as wide characters for Unicode, and we simply want the
3320 // Ascii version to be compatible
3321 // with the non-Windows side. So, we have to convert the information to
3322 // Ascii character strings.
3323 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3324 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003325 argc = 0;
3326 }
3327
Karl Schultze4dc75c2016-02-02 15:37:51 -07003328 if (argc > 0) {
3329 argv = (char **)malloc(sizeof(char *) * argc);
3330 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003331 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003332 } else {
3333 for (int iii = 0; iii < argc; iii++) {
3334 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003335 size_t numConverted = 0;
3336
Karl Schultze4dc75c2016-02-02 15:37:51 -07003337 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3338 if (argv[iii] != NULL) {
3339 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3340 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003341 }
3342 }
3343 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003344 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003345 argv = NULL;
3346 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003347
3348 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003349
3350 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003351 if (argc > 0 && argv != NULL) {
3352 for (int iii = 0; iii < argc; iii++) {
3353 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003354 free(argv[iii]);
3355 }
3356 }
3357 free(argv);
3358 }
3359
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003360 demo.connection = hInstance;
3361 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003362 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003363 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003364
3365 demo_prepare(&demo);
3366
Karl Schultze4dc75c2016-02-02 15:37:51 -07003367 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003368
3369 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003370 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003371 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003372 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003373 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003374 done = true; // if found, quit app
3375 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003376 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003377 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003378 DispatchMessage(&msg);
3379 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003380 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003381 }
3382
3383 demo_cleanup(&demo);
3384
Karl Schultze4dc75c2016-02-02 15:37:51 -07003385 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003386}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003387#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3388#include <android/log.h>
3389#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003390#include "android_util.h"
3391
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003392static bool initialized = false;
3393static bool active = false;
3394struct demo demo;
3395
3396static int32_t processInput(struct android_app* app, AInputEvent* event) {
3397 return 0;
3398}
3399
3400static void processCommand(struct android_app* app, int32_t cmd) {
3401 switch(cmd) {
3402 case APP_CMD_INIT_WINDOW: {
3403 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003404 // We're getting a new window. If the app is starting up, we
3405 // need to initialize. If the app has already been
3406 // initialized, that means that we lost our previous window,
3407 // which means that we have a lot of work to do. At a minimum,
3408 // we need to destroy the swapchain and surface associated with
3409 // the old window, and create a new surface and swapchain.
3410 // However, since there are a lot of other objects/state that
3411 // is tied to the swapchain, it's easiest to simply cleanup and
3412 // start over (i.e. use a brute-force approach of re-starting
3413 // the app)
3414 if (demo.prepared) {
3415 demo_cleanup(&demo);
3416 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003417
3418 // Parse Intents into argc, argv
3419 // Use the following key to send arguments, i.e.
3420 // --es args "--validate"
3421 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003422 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003423 int argc = 0;
3424 char** argv = get_args(app, key, appTag, &argc);
3425
3426 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3427 for (int i = 0; i < argc; i++)
3428 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3429
3430 demo_init(&demo, argc, argv);
3431
3432 // Free the argv malloc'd by get_args
3433 for (int i = 0; i < argc; i++)
3434 free(argv[i]);
3435
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003436 demo.window = (void*)app->window;
3437 demo_init_vk_swapchain(&demo);
3438 demo_prepare(&demo);
3439 initialized = true;
3440 }
3441 break;
3442 }
3443 case APP_CMD_GAINED_FOCUS: {
3444 active = true;
3445 break;
3446 }
3447 case APP_CMD_LOST_FOCUS: {
3448 active = false;
3449 break;
3450 }
3451 }
3452}
3453
3454void android_main(struct android_app *app)
3455{
3456 app_dummy();
3457
Cody Northrop8707a6e2016-04-26 19:59:19 -06003458#ifdef ANDROID
3459 int vulkanSupport = InitVulkan();
3460 if (vulkanSupport == 0)
3461 return;
3462#endif
3463
Ian Elliottf05d5d12016-05-17 12:03:35 -06003464 demo.prepared = false;
3465
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003466 app->onAppCmd = processCommand;
3467 app->onInputEvent = processInput;
3468
3469 while(1) {
3470 int events;
3471 struct android_poll_source* source;
3472 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3473 if (source) {
3474 source->process(app, source);
3475 }
3476
3477 if (app->destroyRequested != 0) {
3478 demo_cleanup(&demo);
3479 return;
3480 }
3481 }
3482 if (initialized && active) {
3483 demo_run(&demo);
3484 }
3485 }
3486
3487}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003488#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003489int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003490 struct demo demo;
3491
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003492 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003493#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003494 if (demo.use_xlib)
3495 demo_create_xlib_window(&demo);
3496 else
3497 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003498#elif defined(VK_USE_PLATFORM_XCB_KHR)
3499 demo_create_xcb_window(&demo);
3500#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3501 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003502#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3503 demo_create_window(&demo);
3504#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003505
Tony Barbourcc69f452015-10-08 13:59:42 -06003506 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003507
3508 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003509
Tony Barbour8295ac42016-08-08 11:04:17 -06003510#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003511 if (demo.use_xlib)
3512 demo_run_xlib(&demo);
3513 else
3514 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003515#elif defined(VK_USE_PLATFORM_XCB_KHR)
3516 demo_run_xcb(&demo);
3517#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3518 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003519#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3520 demo_run(&demo);
3521#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003522
3523 demo_cleanup(&demo);
3524
Karl Schultz0218c002016-03-22 17:06:13 -06003525 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003526}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003527#endif