blob: 42a6f73d1ddd844388bcbe2a211d1a6cf4c459ea [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
2* Copyright (c) 2015-2016 The Khronos Group Inc.
3* Copyright (c) 2015-2016 Valve Corporation
4* Copyright (c) 2015-2016 LunarG, Inc.
5*
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Author: Chia-I Wu <olv@lunarg.com>
19* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20* Author: Ian Elliott <ian@LunarG.com>
21* Author: Jon Ashburn <jon@lunarg.com>
22* Author: Gwan-gyeong Mun <elongbug@gmail.com>
Tony Barbour66a56c32016-09-21 13:10:56 -060023* Author: Tony Barbour <tony@LunarG.com>
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090024*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070025
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060026#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
31#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070032#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090033#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060034#include <X11/Xutil.h>
35#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036
Ian Elliott639ca472015-04-16 15:23:05 -060037#ifdef _WIN32
38#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060039#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060040#endif // _WIN32
41
Cody Northrop8707a6e2016-04-26 19:59:19 -060042#ifdef ANDROID
43#include "vulkan_wrapper.h"
44#else
David Pinedoc5193c12015-11-06 12:54:48 -070045#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060046#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070047
David Pinedo541526f2015-11-24 09:00:24 -070048#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060049#include "linmath.h"
50
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060051#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054
Tony Barbour66a56c32016-09-21 13:10:56 -060055// Allow a maximum of two outstanding presentation operations.
56#define FRAME_LAG 2
57
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060058#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
59
Tony Barbourfdc2d352015-04-22 09:02:32 -060060#if defined(NDEBUG) && defined(__GNUC__)
61#define U_ASSERT_ONLY __attribute__((unused))
62#else
63#define U_ASSERT_ONLY
64#endif
65
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090066#if defined(__GNUC__)
67#define UNUSED __attribute__((unused))
68#else
69#define UNUSED
70#endif
71
Ian Elliott07264132015-04-28 11:35:02 -060072#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060073bool in_callback = false;
Karl Schultze4dc75c2016-02-02 15:37:51 -070074#define ERR_EXIT(err_msg, err_class) \
75 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060076 if (!demo->suppress_popups) \
77 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070078 exit(1); \
79 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060080
Michael Lentinec6cde4b2016-04-18 13:20:45 -050081#elif defined __ANDROID__
82#include <android/log.h>
83#define ERR_EXIT(err_msg, err_class) \
84 do { \
85 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
86 exit(1); \
87 } while (0)
88#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070089#define ERR_EXIT(err_msg, err_class) \
90 do { \
91 printf(err_msg); \
92 fflush(stdout); \
93 exit(1); \
94 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050095#endif
Ian Elliott07264132015-04-28 11:35:02 -060096
Karl Schultze4dc75c2016-02-02 15:37:51 -070097#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
98 { \
99 demo->fp##entrypoint = \
100 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
101 if (demo->fp##entrypoint == NULL) { \
102 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
103 "vkGetInstanceProcAddr Failure"); \
104 } \
105 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600106
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600107static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
108
Karl Schultze4dc75c2016-02-02 15:37:51 -0700109#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
110 { \
111 if (!g_gdpa) \
112 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
113 demo->inst, "vkGetDeviceProcAddr"); \
114 demo->fp##entrypoint = \
115 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
116 if (demo->fp##entrypoint == NULL) { \
117 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
118 "vkGetDeviceProcAddr Failure"); \
119 } \
120 }
Ian Elliott673898b2015-06-22 15:07:49 -0600121
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600122/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700123 * structure to track all objects related to a texture.
124 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600125struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600126 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700127
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600128 VkImage image;
129 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600130
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800131 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500132 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600133 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700134 int32_t tex_width, tex_height;
135};
136
Karl Schultze4dc75c2016-02-02 15:37:51 -0700137static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600138
Karl Schultz0218c002016-03-22 17:06:13 -0600139static int validation_error = 0;
140
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600141struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700143 float mvp[4][4];
144 float position[12 * 3][4];
145 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146};
147
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600148struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700150 float mvp[4][4];
151 float position[12 * 3][4];
152 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600153};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600154
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600155//--------------------------------------------------------------------------------------
156// Mesh and VertexFormat Data
157//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700158// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600159static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 -1.0f,-1.0f, 1.0f,
162 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 -1.0f, 1.0f,-1.0f,
165 -1.0f,-1.0f,-1.0f,
166
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f, 1.0f,-1.0f,
169 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600176 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600179 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 -1.0f, 1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 1.0f, 1.0f, 1.0f,
190 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 1.0f,-1.0f,-1.0f,
193 1.0f, 1.0f,-1.0f,
194
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600197 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 1.0f,-1.0f, 1.0f,
200 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600201};
202
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600203static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700204 0.0f, 1.0f, // -X side
205 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800207 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208 0.0f, 0.0f,
209 0.0f, 1.0f,
210
Rene Lindsay76867e82016-07-29 10:49:11 -0700211 1.0f, 1.0f, // -Z side
212 0.0f, 0.0f,
213 0.0f, 1.0f,
214 1.0f, 1.0f,
215 1.0f, 0.0f,
216 0.0f, 0.0f,
217
218 1.0f, 0.0f, // -Y side
219 1.0f, 1.0f,
220 0.0f, 1.0f,
221 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600222 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224
Rene Lindsay76867e82016-07-29 10:49:11 -0700225 1.0f, 0.0f, // +Y side
226 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231
Rene Lindsay76867e82016-07-29 10:49:11 -0700232 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700234 0.0f, 1.0f,
235 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700238
239 0.0f, 0.0f, // +Z side
240 0.0f, 1.0f,
241 1.0f, 0.0f,
242 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600243 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700244 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600245};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700246// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249 int i;
250
251 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700252 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600253 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
254 }
255 printf("\n");
256 fflush(stdout);
257}
258
Karl Schultze4dc75c2016-02-02 15:37:51 -0700259void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600260 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600262 printf("\n");
263 fflush(stdout);
264}
265
Karl Schultze4dc75c2016-02-02 15:37:51 -0700266VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600267BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
268 uint64_t srcObject, size_t location, int32_t msgCode,
269 const char *pLayerPrefix, const char *pMsg,
270 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700271#ifndef WIN32
272 raise(SIGTRAP);
273#else
274 DebugBreak();
275#endif
276
277 return false;
278}
279
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600280typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600281 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800282 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600283 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600284 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600285} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600286
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600287struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500288#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600289#define APP_NAME_STR_LEN 80
290 HINSTANCE connection; // hInstance - Windows Instance
291 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700292 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600293 POINT minsize; // minimum window size
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600294#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600295 Display* display;
296 Window xlib_window;
297 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600298
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600299 xcb_connection_t *connection;
300 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600301 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800302 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900303#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
304 struct wl_display *display;
305 struct wl_registry *registry;
306 struct wl_compositor *compositor;
307 struct wl_surface *window;
308 struct wl_shell *shell;
309 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500310#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
311 ANativeWindow* window;
312#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700313 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600314 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700315 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600316 bool use_xlib;
Tony Barbour22e06272016-09-07 16:07:56 -0600317 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600320 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600322 VkQueue graphics_queue;
323 VkQueue present_queue;
324 uint32_t graphics_queue_family_index;
325 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600326 VkSemaphore image_acquired_semaphores[FRAME_LAG];
327 VkSemaphore draw_complete_semaphores[FRAME_LAG];
328 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600329 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600330 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600331 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332
Tony Barbourda2bad52016-01-22 14:36:40 -0700333 uint32_t enabled_extension_count;
334 uint32_t enabled_layer_count;
335 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600336 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700337
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600339 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600340 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341
Karl Schultze4dc75c2016-02-02 15:37:51 -0700342 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
343 fpGetPhysicalDeviceSurfaceSupportKHR;
344 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
345 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
346 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
347 fpGetPhysicalDeviceSurfaceFormatsKHR;
348 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
349 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600350 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
351 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
352 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
353 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
354 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600355 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600356 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600357 SwapchainBuffers *buffers;
Tony Barbour66a56c32016-09-21 13:10:56 -0600358 VkFence fences[FRAME_LAG];
359 bool fencesInited[FRAME_LAG];
360 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800362 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600363 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
365 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600368 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800369 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500370 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600371 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372 } depth;
373
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600374 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
376 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800378 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500379 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600380 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600381 } uniform_data;
382
Karl Schultze4dc75c2016-02-02 15:37:51 -0700383 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500384 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600386 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800387 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600389
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600390 mat4x4 projection_matrix;
391 mat4x4 view_matrix;
392 mat4x4 model_matrix;
393
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600394 float spin_angle;
395 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600396 bool pause;
397
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600398 VkShaderModule vert_shader_module;
399 VkShaderModule frag_shader_module;
400
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 VkDescriptorPool desc_pool;
402 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800403
Tony Barbourd8e504f2015-10-08 14:26:24 -0600404 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800405
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600406 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600407 int32_t curFrame;
408 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600409 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600410 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600411 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700412 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
413 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
414 VkDebugReportCallbackEXT msg_callback;
415 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600416
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600417 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600418 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600419};
420
lenny-lunargfbe22392016-06-06 11:07:53 -0600421VKAPI_ATTR VkBool32 VKAPI_CALL
422dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
423 uint64_t srcObject, size_t location, int32_t msgCode,
424 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
425 char *message = (char *)malloc(strlen(pMsg) + 100);
426
427 assert(message);
428
429 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
430 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
431 pMsg);
432 validation_error = 1;
433 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
434 // We know that we're submitting queues without fences, ignore this
435 // warning
436 if (strstr(pMsg,
437 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
438 return false;
439 }
440 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
441 pMsg);
442 validation_error = 1;
443 } else {
444 validation_error = 1;
445 return false;
446 }
447
448#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -0600449 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600450 struct demo *demo = (struct demo*) pUserData;
451 if (!demo->suppress_popups)
452 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600453 in_callback = false;
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600454#elif defined(ANDROID)
455 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
456 __android_log_print(ANDROID_LOG_INFO, "Cube", "%s", message);
457 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
458 __android_log_print(ANDROID_LOG_WARN, "Cube", "%s", message);
459 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
460 __android_log_print(ANDROID_LOG_WARN, "Cube", "%s", message);
461 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
462 __android_log_print(ANDROID_LOG_ERROR, "Cube", "%s", message);
463 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
464 __android_log_print(ANDROID_LOG_DEBUG, "Cube", "%s", message);
465 } else {
466 __android_log_print(ANDROID_LOG_INFO, "Cube", "%s", message);
467 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600468#else
469 printf("%s\n", message);
470 fflush(stdout);
471#endif
472 free(message);
473
474 /*
475 * false indicates that layer should not bail-out of an
476 * API call that had validation failures. This may mean that the
477 * app dies inside the driver due to invalid parameter(s).
478 * That's what would happen without validation layers, so we'll
479 * keep that behavior here.
480 */
481 return false;
482}
483
Ian Elliottcf074d32015-10-16 18:02:43 -0600484// Forward declaration:
485static void demo_resize(struct demo *demo);
486
Karl Schultze4dc75c2016-02-02 15:37:51 -0700487static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
488 VkFlags requirements_mask,
489 uint32_t *typeIndex) {
490 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600491 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700492 if ((typeBits & 1) == 1) {
493 // Type is available, does it match user properties?
494 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
495 requirements_mask) == requirements_mask) {
496 *typeIndex = i;
497 return true;
498 }
499 }
500 typeBits >>= 1;
501 }
502 // No memory types matched, return failure
503 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600504}
505
Karl Schultze4dc75c2016-02-02 15:37:51 -0700506static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600507 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600508
Tony Barbource7524e2016-07-28 12:01:45 -0600509 // This function could get called twice if the texture uses a staging buffer
510 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600511 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600512 return;
513
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600514 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515 assert(!err);
516
Tony Barbource7524e2016-07-28 12:01:45 -0600517 VkFence fence;
518 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
519 .pNext = NULL,
520 .flags = 0};
521 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700522 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700523 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
524 .pNext = NULL,
525 .waitSemaphoreCount = 0,
526 .pWaitSemaphores = NULL,
527 .pWaitDstStageMask = NULL,
528 .commandBufferCount = 1,
529 .pCommandBuffers = cmd_bufs,
530 .signalSemaphoreCount = 0,
531 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532
Tony Barbource7524e2016-07-28 12:01:45 -0600533 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600534 assert(!err);
535
Tony Barbource7524e2016-07-28 12:01:45 -0600536 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600537 assert(!err);
538
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600539 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600540 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600542}
543
Karl Schultze4dc75c2016-02-02 15:37:51 -0700544static void demo_set_image_layout(struct demo *demo, VkImage image,
545 VkImageAspectFlags aspectMask,
546 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700547 VkImageLayout new_image_layout,
548 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600549 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600551 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800552 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800553 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600554 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800555 .commandPool = demo->cmd_pool,
556 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700557 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600558 };
559
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800560 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600561 assert(!err);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700562 VkCommandBufferBeginInfo cmd_buf_info = {
563 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
564 .pNext = NULL,
565 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700566 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700567 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600568 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600569 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600570 }
571
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600572 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600573 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600574 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700575 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800576 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600577 .oldLayout = old_image_layout,
578 .newLayout = new_image_layout,
579 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700580 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600581
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800582 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600583 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800584 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585 }
586
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700587 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700588 image_memory_barrier.dstAccessMask =
589 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700590 }
591
592 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700593 image_memory_barrier.dstAccessMask =
594 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700595 }
596
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600597 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600598 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700599 image_memory_barrier.dstAccessMask =
600 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600601 }
602
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600603 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600604
Tony Barbour50bbca42015-06-29 16:20:35 -0600605 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
606 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600607
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
609 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600610}
611
Karl Schultze4dc75c2016-02-02 15:37:51 -0700612static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700613 const VkCommandBufferBeginInfo cmd_buf_info = {
614 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
615 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600616 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700617 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700618 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800619 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700620 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
621 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800622 };
623 const VkRenderPassBeginInfo rp_begin = {
624 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
625 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800626 .renderPass = demo->render_pass,
627 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800628 .renderArea.offset.x = 0,
629 .renderArea.offset.y = 0,
630 .renderArea.extent.width = demo->width,
631 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600632 .clearValueCount = 2,
633 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700634 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800635 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600636
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600637 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638 assert(!err);
639
Tony Barbour5c5b1632016-04-26 11:13:48 -0600640 VkImageMemoryBarrier image_memory_barrier = {
641 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
642 .pNext = NULL,
643 .srcAccessMask = 0,
644 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barbour22e06272016-09-07 16:07:56 -0600645 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour5c5b1632016-04-26 11:13:48 -0600646 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
647 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
648 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
649 .image = demo->buffers[demo->current_buffer].image,
650 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
651
Tony Barbource7524e2016-07-28 12:01:45 -0600652 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
653 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
654 NULL, 0, NULL, 1, &image_memory_barrier);
Tony Barbour5c5b1632016-04-26 11:13:48 -0600655
Chia-I Wuf051d262015-10-27 19:25:11 +0800656 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800657
Karl Schultze4dc75c2016-02-02 15:37:51 -0700658 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
659 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
660 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
661 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600662 VkViewport viewport;
663 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700664 viewport.height = (float)demo->height;
665 viewport.width = (float)demo->width;
666 viewport.minDepth = (float)0.0f;
667 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700668 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600669
670 VkRect2D scissor;
671 memset(&scissor, 0, sizeof(scissor));
672 scissor.extent.width = demo->width;
673 scissor.extent.height = demo->height;
674 scissor.offset.x = 0;
675 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700676 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600677 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600678 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600679 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800680 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600681
Tony Barbour22e06272016-09-07 16:07:56 -0600682 if (demo->separate_present_queue) {
683 // We have to transfer ownership from the graphics queue family to the
684 // present queue family to be able to present. Note that we don't have
685 // to transfer from present queue family back to graphics queue family at
686 // the start of the next frame because we don't care about the image's
687 // contents at that point.
688 VkImageMemoryBarrier image_ownership_barrier = {
689 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
690 .pNext = NULL,
691 .srcAccessMask = 0,
692 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
693 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
694 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
695 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
696 .dstQueueFamilyIndex = demo->present_queue_family_index,
697 .image = demo->buffers[demo->current_buffer].image,
698 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
699
700 vkCmdPipelineBarrier(cmd_buf,
701 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
702 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0,
703 0, NULL, 0, NULL, 1, &image_ownership_barrier);
704 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600705 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600706 assert(!err);
707}
708
Tony Barbour22e06272016-09-07 16:07:56 -0600709void demo_build_image_ownership_cmd(struct demo *demo, int i) {
710 VkResult U_ASSERT_ONLY err;
711
712 const VkCommandBufferBeginInfo cmd_buf_info = {
713 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
714 .pNext = NULL,
715 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
716 .pInheritanceInfo = NULL,
717 };
718 err = vkBeginCommandBuffer(demo->buffers[i].graphics_to_present_cmd,
719 &cmd_buf_info);
720 assert(!err);
721
722 VkImageMemoryBarrier image_ownership_barrier = {
723 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
724 .pNext = NULL,
725 .srcAccessMask = 0,
726 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
727 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
728 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
729 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
730 .dstQueueFamilyIndex = demo->present_queue_family_index,
731 .image = demo->buffers[i].image,
732 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
733
734 vkCmdPipelineBarrier(demo->buffers[i].graphics_to_present_cmd,
735 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
736 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
737 NULL, 0, NULL, 1, &image_ownership_barrier);
738 err = vkEndCommandBuffer(demo->buffers[i].graphics_to_present_cmd);
739 assert(!err);
740}
741
Karl Schultze4dc75c2016-02-02 15:37:51 -0700742void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600743 mat4x4 MVP, Model, VP;
744 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600745 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600746 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600747
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600748 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600749
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600750 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600751 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700752 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
753 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600754 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600755
Karl Schultze4dc75c2016-02-02 15:37:51 -0700756 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
757 demo->uniform_data.mem_alloc.allocationSize, 0,
758 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600759 assert(!err);
760
Karl Schultze4dc75c2016-02-02 15:37:51 -0700761 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600762
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600763 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600764}
765
Karl Schultze4dc75c2016-02-02 15:37:51 -0700766static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600767 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600768
Tony Barbour66a56c32016-09-21 13:10:56 -0600769 if (demo->fencesInited[demo->frame_index])
770 {
771 // Ensure no more than FRAME_LAG presentations are outstanding
772 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
773 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
774 }
775
Ian Elliottaaae5352015-07-06 14:27:58 -0600776 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700777 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour66a56c32016-09-21 13:10:56 -0600778 demo->image_acquired_semaphores[demo->frame_index], demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600779 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600780 demo->fencesInited[demo->frame_index] = true;
781
Ian Elliottcf074d32015-10-16 18:02:43 -0600782 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
783 // demo->swapchain is out of date (e.g. the window was resized) and
784 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600785 demo->frame_index += 1;
786 demo->frame_index %= FRAME_LAG;
787
Ian Elliottcf074d32015-10-16 18:02:43 -0600788 demo_resize(demo);
789 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600790 return;
791 } else if (err == VK_SUBOPTIMAL_KHR) {
792 // demo->swapchain is not as optimal as it could be, but the platform's
793 // presentation engine will still present the image correctly.
794 } else {
795 assert(!err);
796 }
Tony Barbource7524e2016-07-28 12:01:45 -0600797 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600798 // that the image won't be rendered to until the presentation
799 // engine has fully released ownership to the application, and it is
800 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600801 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600802 VkPipelineStageFlags pipe_stage_flags;
803 VkSubmitInfo submit_info;
804 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
805 submit_info.pNext = NULL;
806 submit_info.pWaitDstStageMask = &pipe_stage_flags;
807 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
808 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600809 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600810 submit_info.commandBufferCount = 1;
811 submit_info.pCommandBuffers = &demo->buffers[demo->current_buffer].cmd;
812 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600813 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura2a67812016-07-18 13:21:06 -0600814 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600815 assert(!err);
816
Tony Barbour22e06272016-09-07 16:07:56 -0600817 if (demo->separate_present_queue) {
818 // If we are using separate queues, change image ownership to the
819 // present queue before presenting, waiting for the draw complete
820 // semaphore and signalling the ownership released semaphore when finished
821 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
822 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600823 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600824 submit_info.commandBufferCount = 1;
825 submit_info.pCommandBuffers =
826 &demo->buffers[demo->current_buffer].graphics_to_present_cmd;
827 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600828 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600829 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
830 assert(!err);
831 }
832
833 // If we are using separate queues we have to wait for image ownership,
834 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600835 VkPresentInfoKHR present = {
836 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600837 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600838 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600839 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600840 ? &demo->image_ownership_semaphores[demo->frame_index]
841 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600842 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700843 .pSwapchains = &demo->swapchain,
844 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600845 };
846
Tony Barboura2a67812016-07-18 13:21:06 -0600847 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600848 demo->frame_index += 1;
849 demo->frame_index %= FRAME_LAG;
850
Ian Elliottcf074d32015-10-16 18:02:43 -0600851 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
852 // demo->swapchain is out of date (e.g. the window was resized) and
853 // must be recreated:
854 demo_resize(demo);
855 } else if (err == VK_SUBOPTIMAL_KHR) {
856 // demo->swapchain is not as optimal as it could be, but the platform's
857 // presentation engine will still present the image correctly.
858 } else {
859 assert(!err);
860 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600861}
862
Karl Schultze4dc75c2016-02-02 15:37:51 -0700863static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600864 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600865 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600866
Ian Elliottb08e0d92015-11-20 11:55:46 -0700867 // Check the surface capabilities and formats
868 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700869 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
870 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600871 assert(!err);
872
Ian Elliott8528eff2015-08-07 15:56:59 -0600873 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700874 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
875 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600876 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600877 VkPresentModeKHR *presentModes =
878 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600879 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700880 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
881 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600882 assert(!err);
883
Ian Elliotta0781972015-08-21 15:09:33 -0600884 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600885 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
886 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
887 // If the surface size is undefined, the size is set to the size
888 // of the images requested, which must fit within the minimum and
889 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600890 swapchainExtent.width = demo->width;
891 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600892
893 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
894 swapchainExtent.width = surfCapabilities.minImageExtent.width;
895 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
896 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
897 }
898
899 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
900 swapchainExtent.height = surfCapabilities.minImageExtent.height;
901 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
902 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
903 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700904 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600905 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700906 swapchainExtent = surfCapabilities.currentExtent;
907 demo->width = surfCapabilities.currentExtent.width;
908 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600909 }
910
Tony Barbour66a56c32016-09-21 13:10:56 -0600911 // The FIFO present mode is guaranteed by the spec to be supported
912 // and to have no tearing. MAILBOX is another option, but for fast
913 // rendering apps we could use a lot of gpu power rendering frames
914 // that are discarded and never displayed
Ian Elliotta0781972015-08-21 15:09:33 -0600915 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600916
Ian Elliottcf7f7482016-08-19 03:46:58 -0600917 // Determine the number of VkImage's to use in the swap chain.
918 // Application desires to only acquire 1 image at a time (which is
919 // "surfCapabilities.minImageCount").
920 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
921 // If maxImageCount is 0, we can ask for as many images as we want;
922 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700923 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600924 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600925 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600926 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600927 }
928
Tony Barbour9fd6d352015-09-16 15:01:32 -0600929 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700930 if (surfCapabilities.supportedTransforms &
931 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700932 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600933 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700934 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600935 }
936
Tony Barboura2a67812016-07-18 13:21:06 -0600937 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600938 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800939 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700940 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600941 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800942 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600943 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700944 .imageExtent =
945 {
946 .width = swapchainExtent.width, .height = swapchainExtent.height,
947 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700948 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600949 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700950 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700951 .imageArrayLayers = 1,
952 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
953 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600954 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600955 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600956 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600957 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600958 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600959 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600960 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700961 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800962 assert(!err);
963
Ian Elliottcf074d32015-10-16 18:02:43 -0600964 // If we just re-created an existing swapchain, we should destroy the old
965 // swapchain at this point.
966 // Note: destroying the swapchain also cleans up all its associated
967 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800968 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700969 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600970 }
971
972 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600973 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600974 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800975
Karl Schultze4dc75c2016-02-02 15:37:51 -0700976 VkImage *swapchainImages =
977 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600978 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600979 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600980 &demo->swapchainImageCount,
981 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600982 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600983
Karl Schultze4dc75c2016-02-02 15:37:51 -0700984 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
985 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600986 assert(demo->buffers);
987
Ian Elliotta0781972015-08-21 15:09:33 -0600988 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600989 VkImageViewCreateInfo color_image_view = {
990 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991 .pNext = NULL,
992 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700993 .components =
994 {
995 .r = VK_COMPONENT_SWIZZLE_R,
996 .g = VK_COMPONENT_SWIZZLE_G,
997 .b = VK_COMPONENT_SWIZZLE_B,
998 .a = VK_COMPONENT_SWIZZLE_A,
999 },
1000 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1001 .baseMipLevel = 0,
1002 .levelCount = 1,
1003 .baseArrayLayer = 0,
1004 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001005 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1006 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001007 };
1008
Ian Elliotta0781972015-08-21 15:09:33 -06001009 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001010
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001011 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001012
Karl Schultze4dc75c2016-02-02 15:37:51 -07001013 err = vkCreateImageView(demo->device, &color_image_view, NULL,
1014 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001015 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001016
1017 // The draw loop will be expecting the presentable images to be in
1018 // LAYOUT_PRESENT_SRC_KHR
1019 // since that's how they're left at the end of every frame.
1020 demo_set_image_layout(
1021 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1022 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001023 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001024
Mark Young04fd3d82016-01-25 14:43:50 -07001025 if (NULL != presentModes) {
1026 free(presentModes);
1027 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028}
1029
Karl Schultze4dc75c2016-02-02 15:37:51 -07001030static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001031 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001032 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001033 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001035 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001036 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001037 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001039 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001040 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001041 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001042 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001043 .flags = 0,
1044 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001045
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001046 VkImageViewCreateInfo view = {
1047 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001048 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001049 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001050 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001051 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1052 .baseMipLevel = 0,
1053 .levelCount = 1,
1054 .baseArrayLayer = 0,
1055 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001057 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001058 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001059
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001060 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001061 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001062 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001063
1064 demo->depth.format = depth_format;
1065
1066 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001067 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068 assert(!err);
1069
Karl Schultze4dc75c2016-02-02 15:37:51 -07001070 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001071 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001072
Chia-I Wuf48700b2015-11-10 16:21:09 +08001073 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001074 demo->depth.mem_alloc.pNext = NULL;
1075 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1076 demo->depth.mem_alloc.memoryTypeIndex = 0;
1077
Karl Schultze4dc75c2016-02-02 15:37:51 -07001078 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1079 0, /* No requirements */
1080 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001081 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001082
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001083 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001084 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1085 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001086 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001087
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001088 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001089 err =
1090 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001091 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001092
Karl Schultze4dc75c2016-02-02 15:37:51 -07001093 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1094 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001095 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1096 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001097
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001098 /* create image view */
1099 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001100 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001101 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001102}
1103
Tony Barbour1a86d032015-09-21 15:17:33 -06001104/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001105bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001106 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001107#ifdef __ANDROID__
1108#include <lunarg.ppm.h>
1109 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001110 cPtr = (char*)lunarg_ppm;
1111 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001112 return false;
1113 }
1114 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001115 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001116 if (rgba_data == NULL) {
1117 return true;
1118 }
1119 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001120 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001121 return false;
1122 }
1123 while(strncmp(cPtr++, "\n", 1));
1124
1125 for (int y = 0; y < *height; y++) {
1126 uint8_t *rowPtr = rgba_data;
1127 for (int x = 0; x < *width; x++) {
1128 memcpy(rowPtr, cPtr, 3);
1129 rowPtr[3] = 255; /* Alpha of 1 */
1130 rowPtr += 4;
1131 cPtr += 3;
1132 }
1133 rgba_data += layout->rowPitch;
1134 }
1135
1136 return true;
1137#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001138 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001139 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001140
Tony Barbour1a86d032015-09-21 15:17:33 -06001141 if (!fPtr)
1142 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001143
Tony Barbour1a86d032015-09-21 15:17:33 -06001144 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001145 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1146 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001147 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001148 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001149
Tony Barbour1a86d032015-09-21 15:17:33 -06001150 do {
1151 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001152 if (cPtr == NULL) {
1153 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001154 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001155 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001156 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001157
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001158 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001159 if (rgba_data == NULL) {
1160 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001161 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001162 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001163 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001164 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001165 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1166 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001167 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001168 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001169
Karl Schultze4dc75c2016-02-02 15:37:51 -07001170 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001171 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001172 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001173 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001175 rowPtr[3] = 255; /* Alpha of 1 */
1176 rowPtr += 4;
1177 }
1178 rgba_data += layout->rowPitch;
1179 }
1180 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001181 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001182#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001183}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001184
Karl Schultze4dc75c2016-02-02 15:37:51 -07001185static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001186 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001187 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001188 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001189 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001190 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001191 int32_t tex_width;
1192 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001193 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001194 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195
Karl Schultze4dc75c2016-02-02 15:37:51 -07001196 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001197 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001198 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001200 tex_obj->tex_width = tex_width;
1201 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001202
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001203 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001204 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001205 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001206 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001207 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001208 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001209 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001210 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001211 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001212 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001213 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001214 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001215 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001218 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219
Karl Schultze4dc75c2016-02-02 15:37:51 -07001220 err =
1221 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001222 assert(!err);
1223
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001224 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001225
Chia-I Wuf48700b2015-11-10 16:21:09 +08001226 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001227 tex_obj->mem_alloc.pNext = NULL;
1228 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1229 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001230
Karl Schultze4dc75c2016-02-02 15:37:51 -07001231 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1232 required_props,
1233 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001234 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001235
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001236 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001237 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001238 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001239 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001241 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001242 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001243 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001244
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001245 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001246 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001247 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001248 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001249 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001250 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001251 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252 void *data;
1253
Karl Schultze4dc75c2016-02-02 15:37:51 -07001254 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1255 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001256
Karl Schultze4dc75c2016-02-02 15:37:51 -07001257 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1258 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001259 assert(!err);
1260
1261 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1262 fprintf(stderr, "Error loading texture: %s\n", filename);
1263 }
1264
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001265 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001267
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001268 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001269 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001270 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1271 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001272 /* setting the image layout does not reference the actual memory so no need
1273 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001274}
1275
Karl Schultze4dc75c2016-02-02 15:37:51 -07001276static void demo_destroy_texture_image(struct demo *demo,
1277 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001278 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001279 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1280 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001281}
1282
Karl Schultze4dc75c2016-02-02 15:37:51 -07001283static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001284 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001285 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001286 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001287
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001288 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001289
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001291 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001292
Karl Schultze4dc75c2016-02-02 15:37:51 -07001293 if ((props.linearTilingFeatures &
1294 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1295 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001296 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001297 demo_prepare_texture_image(
1298 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1299 VK_IMAGE_USAGE_SAMPLED_BIT,
1300 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1301 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001302 } else if (props.optimalTilingFeatures &
1303 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001304 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001305 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001306
1307 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001308 demo_prepare_texture_image(
1309 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1310 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1311 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1312 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001313
Karl Schultze4dc75c2016-02-02 15:37:51 -07001314 demo_prepare_texture_image(
1315 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1316 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1317 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001318
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001319 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001320 VK_IMAGE_ASPECT_COLOR_BIT,
1321 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001322 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1323 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001324
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001325 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001326 VK_IMAGE_ASPECT_COLOR_BIT,
1327 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001328 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1329 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001330
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001331 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001332 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1333 .srcOffset = {0, 0, 0},
1334 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1335 .dstOffset = {0, 0, 0},
1336 .extent = {staging_texture.tex_width,
1337 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001338 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339 vkCmdCopyImage(
1340 demo->cmd, staging_texture.image,
1341 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1342 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001343
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001344 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001345 VK_IMAGE_ASPECT_COLOR_BIT,
1346 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001347 demo->textures[i].imageLayout,
1348 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001349
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001350 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001351
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001352 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001353 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001354 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1355 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001356 }
1357
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001358 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001359 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001360 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001361 .magFilter = VK_FILTER_NEAREST,
1362 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001363 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001364 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1365 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1366 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001367 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001368 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001369 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001370 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371 .minLod = 0.0f,
1372 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001373 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001374 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001375 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001376
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001377 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001378 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001380 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001381 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001382 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 .components =
1384 {
1385 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1386 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1387 },
1388 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001389 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001391
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001393 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001394 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395 assert(!err);
1396
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397 /* create image view */
1398 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001399 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001400 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001401 assert(!err);
1402 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404
Karl Schultze4dc75c2016-02-02 15:37:51 -07001405void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001406 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001407 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001408 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001409 int i;
1410 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001411 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001412 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001413 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001414
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001415 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001416 mat4x4_mul(MVP, VP, demo->model_matrix);
1417 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001418 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001419
Karl Schultze4dc75c2016-02-02 15:37:51 -07001420 for (i = 0; i < 12 * 3; i++) {
1421 data.position[i][0] = g_vertex_buffer_data[i * 3];
1422 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1423 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001424 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001425 data.attr[i][0] = g_uv_buffer_data[2 * i];
1426 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001427 data.attr[i][2] = 0;
1428 data.attr[i][3] = 0;
1429 }
1430
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001431 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001432 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001433 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001434 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001435 err =
1436 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001437 assert(!err);
1438
Karl Schultze4dc75c2016-02-02 15:37:51 -07001439 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1440 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001441
Chia-I Wuf48700b2015-11-10 16:21:09 +08001442 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001443 demo->uniform_data.mem_alloc.pNext = NULL;
1444 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1445 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1446
Karl Schultze4dc75c2016-02-02 15:37:51 -07001447 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001448 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1449 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001450 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001451 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001452
Karl Schultze4dc75c2016-02-02 15:37:51 -07001453 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1454 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001455 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001456
Karl Schultze4dc75c2016-02-02 15:37:51 -07001457 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1458 demo->uniform_data.mem_alloc.allocationSize, 0,
1459 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001460 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001461
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001462 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001463
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001464 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001465
Karl Schultze4dc75c2016-02-02 15:37:51 -07001466 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1467 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001468 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001469
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001470 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1471 demo->uniform_data.buffer_info.offset = 0;
1472 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001473}
1474
Karl Schultze4dc75c2016-02-02 15:37:51 -07001475static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001476 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001477 [0] =
1478 {
1479 .binding = 0,
1480 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1481 .descriptorCount = 1,
1482 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1483 .pImmutableSamplers = NULL,
1484 },
1485 [1] =
1486 {
1487 .binding = 1,
1488 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1489 .descriptorCount = DEMO_TEXTURE_COUNT,
1490 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1491 .pImmutableSamplers = NULL,
1492 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001493 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001494 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001495 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001496 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001497 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001498 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001499 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001500 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501
Karl Schultze4dc75c2016-02-02 15:37:51 -07001502 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1503 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001504 assert(!err);
1505
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001506 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001507 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1508 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001509 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001510 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001511 };
1512
Karl Schultze4dc75c2016-02-02 15:37:51 -07001513 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001514 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001515 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001516}
1517
Karl Schultze4dc75c2016-02-02 15:37:51 -07001518static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001519 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520 [0] =
1521 {
1522 .format = demo->format,
1523 .samples = VK_SAMPLE_COUNT_1_BIT,
1524 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1525 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1526 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1527 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1528 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Tony Barbour98390392016-07-28 10:50:13 -06001529 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001530 },
1531 [1] =
1532 {
1533 .format = demo->depth.format,
1534 .samples = VK_SAMPLE_COUNT_1_BIT,
1535 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1536 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1537 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1538 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1539 .initialLayout =
1540 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1541 .finalLayout =
1542 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1543 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001544 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001545 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001546 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001547 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001548 const VkAttachmentReference depth_reference = {
1549 .attachment = 1,
1550 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1551 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001552 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001553 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1554 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001555 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001556 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001557 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001558 .pColorAttachments = &color_reference,
1559 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001560 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001561 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001562 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001563 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001564 const VkRenderPassCreateInfo rp_info = {
1565 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1566 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001567 .attachmentCount = 2,
1568 .pAttachments = attachments,
1569 .subpassCount = 1,
1570 .pSubpasses = &subpass,
1571 .dependencyCount = 0,
1572 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001573 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001574 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001575
Chia-I Wu63636062015-10-26 21:10:41 +08001576 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001577 assert(!err);
1578}
1579
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001580//TODO: Merge shader reading
1581#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001582static VkShaderModule
1583demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001584 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001585 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001586 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001588 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1589 moduleCreateInfo.pNext = NULL;
1590
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001591 moduleCreateInfo.codeSize = size;
1592 moduleCreateInfo.pCode = code;
1593 moduleCreateInfo.flags = 0;
1594 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1595 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001596
1597 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001598}
1599
Karl Schultze4dc75c2016-02-02 15:37:51 -07001600char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001601 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001602 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001603 void *shader_code;
1604
1605 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001606 if (!fp)
1607 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001608
1609 fseek(fp, 0L, SEEK_END);
1610 size = ftell(fp);
1611
1612 fseek(fp, 0L, SEEK_SET);
1613
1614 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001615 retval = fread(shader_code, size, 1, fp);
1616 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001617
1618 *psize = size;
1619
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001620 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001621 return shader_code;
1622}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001623#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001624
Karl Schultze4dc75c2016-02-02 15:37:51 -07001625static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001626#ifdef __ANDROID__
1627 VkShaderModuleCreateInfo sh_info = {};
1628 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1629
1630#include "cube.vert.h"
1631 sh_info.codeSize = sizeof(cube_vert);
1632 sh_info.pCode = cube_vert;
1633 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1634 assert(!err);
1635#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001636 void *vertShaderCode;
1637 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001638
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001639 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1640
Karl Schultze4dc75c2016-02-02 15:37:51 -07001641 demo->vert_shader_module =
1642 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001643
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001644 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001645#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001646
1647 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001648}
1649
Karl Schultze4dc75c2016-02-02 15:37:51 -07001650static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001651#ifdef __ANDROID__
1652 VkShaderModuleCreateInfo sh_info = {};
1653 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1654
1655#include "cube.frag.h"
1656 sh_info.codeSize = sizeof(cube_frag);
1657 sh_info.pCode = cube_frag;
1658 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1659 assert(!err);
1660#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001661 void *fragShaderCode;
1662 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001663
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001664 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001665
Karl Schultze4dc75c2016-02-02 15:37:51 -07001666 demo->frag_shader_module =
1667 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001668
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001669 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001670#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001671
1672 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001673}
1674
Karl Schultze4dc75c2016-02-02 15:37:51 -07001675static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001676 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001677 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001678 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001679 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001680 VkPipelineRasterizationStateCreateInfo rs;
1681 VkPipelineColorBlendStateCreateInfo cb;
1682 VkPipelineDepthStencilStateCreateInfo ds;
1683 VkPipelineViewportStateCreateInfo vp;
1684 VkPipelineMultisampleStateCreateInfo ms;
1685 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1686 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001687 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688
Piers Daniellba839d12015-09-29 13:01:09 -06001689 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1690 memset(&dynamicState, 0, sizeof dynamicState);
1691 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1692 dynamicState.pDynamicStates = dynamicStateEnables;
1693
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001695 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001696 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001697
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001698 memset(&vi, 0, sizeof(vi));
1699 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1700
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001701 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001702 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001703 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001704
1705 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001706 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1707 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001708 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001709 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001710 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001711 rs.rasterizerDiscardEnable = VK_FALSE;
1712 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001713 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714
1715 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001716 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1717 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001718 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001719 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001720 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001721 cb.attachmentCount = 1;
1722 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723
Tony Barbour29645d02015-01-16 14:27:35 -07001724 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001725 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001726 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001727 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1728 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001729 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001730 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1731 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001732
1733 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001734 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001735 ds.depthTestEnable = VK_TRUE;
1736 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001737 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001738 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001739 ds.back.failOp = VK_STENCIL_OP_KEEP;
1740 ds.back.passOp = VK_STENCIL_OP_KEEP;
1741 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001742 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001743 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744
Tony Barbour29645d02015-01-16 14:27:35 -07001745 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001746 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001747 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001748 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001750 // Two stages: vs and fs
1751 pipeline.stageCount = 2;
1752 VkPipelineShaderStageCreateInfo shaderStages[2];
1753 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1754
Karl Schultze4dc75c2016-02-02 15:37:51 -07001755 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1756 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001757 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001758 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001759
Karl Schultze4dc75c2016-02-02 15:37:51 -07001760 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1761 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001762 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001763 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001764
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001765 memset(&pipelineCache, 0, sizeof(pipelineCache));
1766 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001767
Karl Schultze4dc75c2016-02-02 15:37:51 -07001768 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1769 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001770 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001771
Karl Schultze4dc75c2016-02-02 15:37:51 -07001772 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001773 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001774 pipeline.pRasterizationState = &rs;
1775 pipeline.pColorBlendState = &cb;
1776 pipeline.pMultisampleState = &ms;
1777 pipeline.pViewportState = &vp;
1778 pipeline.pDepthStencilState = &ds;
1779 pipeline.pStages = shaderStages;
1780 pipeline.renderPass = demo->render_pass;
1781 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001782
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001783 pipeline.renderPass = demo->render_pass;
1784
Karl Schultze4dc75c2016-02-02 15:37:51 -07001785 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1786 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001787 assert(!err);
1788
Chia-I Wu63636062015-10-26 21:10:41 +08001789 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1790 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791}
1792
Karl Schultze4dc75c2016-02-02 15:37:51 -07001793static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001794 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001795 [0] =
1796 {
1797 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1798 .descriptorCount = 1,
1799 },
1800 [1] =
1801 {
1802 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1803 .descriptorCount = DEMO_TEXTURE_COUNT,
1804 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001805 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001806 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001807 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001808 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001809 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001810 .poolSizeCount = 2,
1811 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001812 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001813 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001814
Karl Schultze4dc75c2016-02-02 15:37:51 -07001815 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1816 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001817 assert(!err);
1818}
1819
Karl Schultze4dc75c2016-02-02 15:37:51 -07001820static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001821 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001822 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001823 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001824 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001825
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001826 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001827 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001828 .pNext = NULL,
1829 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001830 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001831 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001832 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001833 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001834
Chia-I Wuae721ba2015-05-25 16:27:55 +08001835 memset(&tex_descs, 0, sizeof(tex_descs));
1836 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001837 tex_descs[i].sampler = demo->textures[i].sampler;
1838 tex_descs[i].imageView = demo->textures[i].view;
1839 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001840 }
1841
1842 memset(&writes, 0, sizeof(writes));
1843
1844 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001845 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001846 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001847 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001848 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001849
1850 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001851 writes[1].dstSet = demo->desc_set;
1852 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001853 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001854 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001855 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001856
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001857 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001858}
1859
Karl Schultze4dc75c2016-02-02 15:37:51 -07001860static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001861 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001862 attachments[1] = demo->depth.view;
1863
Chia-I Wuf973e322015-07-08 13:34:24 +08001864 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001865 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1866 .pNext = NULL,
1867 .renderPass = demo->render_pass,
1868 .attachmentCount = 2,
1869 .pAttachments = attachments,
1870 .width = demo->width,
1871 .height = demo->height,
1872 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001873 };
1874 VkResult U_ASSERT_ONLY err;
1875 uint32_t i;
1876
Karl Schultze4dc75c2016-02-02 15:37:51 -07001877 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1878 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001879 assert(demo->framebuffers);
1880
1881 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001882 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001883 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1884 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001885 assert(!err);
1886 }
1887}
1888
Karl Schultze4dc75c2016-02-02 15:37:51 -07001889static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001890 VkResult U_ASSERT_ONLY err;
1891
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001892 const VkCommandPoolCreateInfo cmd_pool_info = {
1893 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001894 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001895 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001896 .flags = 0,
1897 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001898 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1899 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001900 assert(!err);
1901
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001902 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001903 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001904 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001905 .commandPool = demo->cmd_pool,
1906 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001907 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001908 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001909
1910 demo_prepare_buffers(demo);
1911 demo_prepare_depth(demo);
1912 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001913 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001914
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001915 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001916 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001917 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001918
Ian Elliotta0781972015-08-21 15:09:33 -06001919 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001920 err =
1921 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001922 assert(!err);
1923 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001924
Tony Barbour22e06272016-09-07 16:07:56 -06001925 if (demo->separate_present_queue) {
1926 const VkCommandPoolCreateInfo cmd_pool_info = {
1927 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1928 .pNext = NULL,
1929 .queueFamilyIndex = demo->present_queue_family_index,
1930 .flags = 0,
1931 };
1932 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1933 &demo->present_cmd_pool);
1934 assert(!err);
1935 const VkCommandBufferAllocateInfo cmd = {
1936 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1937 .pNext = NULL,
1938 .commandPool = demo->present_cmd_pool,
1939 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1940 .commandBufferCount = 1,
1941 };
1942 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1943 err = vkAllocateCommandBuffers(
1944 demo->device, &cmd, &demo->buffers[i].graphics_to_present_cmd);
1945 assert(!err);
1946 demo_build_image_ownership_cmd(demo, i);
1947 }
1948 }
1949
Chia-I Wu63ea9262015-03-26 13:14:16 +08001950 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001951 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001952
Chia-I Wuf973e322015-07-08 13:34:24 +08001953 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001954
Ian Elliotta0781972015-08-21 15:09:33 -06001955 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001956 demo->current_buffer = i;
1957 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1958 }
1959
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001960 /*
1961 * Prepare functions above may generate pipeline commands
1962 * that need to be flushed before beginning the render loop.
1963 */
1964 demo_flush_init_cmd(demo);
1965
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001966 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001967 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968}
1969
Karl Schultze4dc75c2016-02-02 15:37:51 -07001970static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001971 uint32_t i;
1972
1973 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06001974 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001975
Tony Barbour66a56c32016-09-21 13:10:56 -06001976 // Wait for fences from present operations
1977 for (i = 0; i < FRAME_LAG; i++) {
1978 if (demo->fencesInited[i]) {
1979 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
1980 }
1981 vkDestroyFence(demo->device, demo->fences[i], NULL);
1982 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
1983 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
1984 if (demo->separate_present_queue) {
1985 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
1986 }
1987 }
1988
Tony Barbourd8e504f2015-10-08 14:26:24 -06001989 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001990 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001991 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001992 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001993 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001994
Chia-I Wu63636062015-10-26 21:10:41 +08001995 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1996 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1997 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1998 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1999 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002000
2001 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002002 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2003 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2004 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2005 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002006 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002007 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002008
Chia-I Wu63636062015-10-26 21:10:41 +08002009 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2010 vkDestroyImage(demo->device, demo->depth.image, NULL);
2011 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002012
Chia-I Wu63636062015-10-26 21:10:41 +08002013 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2014 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002015
Ian Elliotta0781972015-08-21 15:09:33 -06002016 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002017 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002018 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2019 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06002020 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002021 free(demo->buffers);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002022 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002023 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002024
Tony Barbour22e06272016-09-07 16:07:56 -06002025 if (demo->separate_present_queue) {
2026 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002027 }
Chia-I Wu63636062015-10-26 21:10:41 +08002028 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002029 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002030 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002031 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002032 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002033 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002034
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002035#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002036 if (demo->use_xlib) {
2037 XDestroyWindow(demo->display, demo->xlib_window);
2038 XCloseDisplay(demo->display);
2039 } else {
2040 xcb_destroy_window(demo->connection, demo->xcb_window);
2041 xcb_disconnect(demo->connection);
2042 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002043 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002044#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002045 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002046 xcb_disconnect(demo->connection);
2047 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002048#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2049 wl_shell_surface_destroy(demo->shell_surface);
2050 wl_surface_destroy(demo->window);
2051 wl_shell_destroy(demo->shell);
2052 wl_compositor_destroy(demo->compositor);
2053 wl_registry_destroy(demo->registry);
2054 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002055#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002056}
2057
Karl Schultze4dc75c2016-02-02 15:37:51 -07002058static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002059 uint32_t i;
2060
Mike Stroyanbb60b382015-10-28 09:46:57 -06002061 // Don't react to resize until after first initialization.
2062 if (!demo->prepared) {
2063 return;
2064 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002065 // In order to properly resize the window, we must re-create the swapchain
2066 // AND redo the command buffers, etc.
2067 //
2068 // First, perform part of the demo_cleanup() function:
2069 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002070 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002071
2072 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002073 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002074 }
2075 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002076 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002077
Chia-I Wu63636062015-10-26 21:10:41 +08002078 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2079 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2080 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2081 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2082 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002083
2084 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002085 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2086 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2087 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2088 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002089 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002090
Chia-I Wu63636062015-10-26 21:10:41 +08002091 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2092 vkDestroyImage(demo->device, demo->depth.image, NULL);
2093 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002094
Chia-I Wu63636062015-10-26 21:10:41 +08002095 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2096 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002097
2098 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002099 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002100 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2101 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002102 }
Chia-I Wu63636062015-10-26 21:10:41 +08002103 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002104 if (demo->separate_present_queue) {
2105 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2106 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002107 free(demo->buffers);
2108
Ian Elliottcf074d32015-10-16 18:02:43 -06002109 // Second, re-perform the demo_prepare() function, which will re-create the
2110 // swapchain:
2111 demo_prepare(demo);
2112}
2113
David Pinedo2bb7c932015-06-18 17:03:14 -06002114// On MS-Windows, make this a global, so it's available to WndProc()
2115struct demo demo;
2116
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002117#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002118static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002119 if (!demo->prepared)
2120 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002121
Ian Elliott639ca472015-04-16 15:23:05 -06002122 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002123 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002124 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002125 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002126 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002127 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002128}
Ian Elliott639ca472015-04-16 15:23:05 -06002129
2130// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002131LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2132 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002133 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002134 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002135 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002136 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002137 // The validation callback calls MessageBox which can generate paint
2138 // events - don't make more Vulkan calls if we got here from the
2139 // callback
2140 if (!in_callback) {
2141 demo_run(&demo);
2142 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002143 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002144 case WM_GETMINMAXINFO: // set window's minimum size
2145 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2146 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002147 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002148 // Resize the application to the new window size, except when
2149 // it was minimized. Vulkan doesn't support images or swapchains
2150 // with width=0 and height=0.
2151 if (wParam != SIZE_MINIMIZED) {
2152 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002153 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002154 demo_resize(&demo);
2155 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002156 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002157 default:
2158 break;
2159 }
2160 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2161}
2162
Karl Schultze4dc75c2016-02-02 15:37:51 -07002163static void demo_create_window(struct demo *demo) {
2164 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002165
2166 // Initialize the window class structure:
2167 win_class.cbSize = sizeof(WNDCLASSEX);
2168 win_class.style = CS_HREDRAW | CS_VREDRAW;
2169 win_class.lpfnWndProc = WndProc;
2170 win_class.cbClsExtra = 0;
2171 win_class.cbWndExtra = 0;
2172 win_class.hInstance = demo->connection; // hInstance
2173 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2174 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2175 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2176 win_class.lpszMenuName = NULL;
2177 win_class.lpszClassName = demo->name;
2178 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2179 // Register window class:
2180 if (!RegisterClassEx(&win_class)) {
2181 // It didn't work, so try to give a useful error:
2182 printf("Unexpected error trying to start the application!\n");
2183 fflush(stdout);
2184 exit(1);
2185 }
2186 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002187 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002188 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002189 demo->window = CreateWindowEx(0,
2190 demo->name, // class name
2191 demo->name, // app name
2192 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002193 WS_VISIBLE | WS_SYSMENU,
2194 100, 100, // x/y coords
2195 wr.right - wr.left, // width
2196 wr.bottom - wr.top, // height
2197 NULL, // handle to parent
2198 NULL, // handle to menu
2199 demo->connection, // hInstance
2200 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002201 if (!demo->window) {
2202 // It didn't work, so try to give a useful error:
2203 printf("Cannot create a window in which to draw!\n");
2204 fflush(stdout);
2205 exit(1);
2206 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002207 // Window client area size must be at least 1 pixel high, to prevent crash.
2208 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2209 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002210}
Tony Barbour8295ac42016-08-08 11:04:17 -06002211#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002212static void demo_create_xlib_window(struct demo *demo) {
2213
2214 demo->display = XOpenDisplay(NULL);
2215 long visualMask = VisualScreenMask;
2216 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002217 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002218 vInfoTemplate.screen = DefaultScreen(demo->display);
2219 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2220 &vInfoTemplate, &numberOfVisuals);
2221
2222 Colormap colormap = XCreateColormap(
2223 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2224 visualInfo->visual, AllocNone);
2225
Rene Lindsay11612722016-06-13 17:20:39 -06002226 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002227 windowAttributes.colormap = colormap;
2228 windowAttributes.background_pixel = 0xFFFFFFFF;
2229 windowAttributes.border_pixel = 0;
2230 windowAttributes.event_mask =
2231 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2232
2233 demo->xlib_window = XCreateWindow(
2234 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2235 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2236 visualInfo->visual,
2237 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2238
2239 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2240 XMapWindow(demo->display, demo->xlib_window);
2241 XFlush(demo->display);
2242 demo->xlib_wm_delete_window =
2243 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2244}
2245static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2246 switch(event->type) {
2247 case ClientMessage:
2248 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2249 demo->quit = true;
2250 break;
2251 case KeyPress:
2252 switch (event->xkey.keycode) {
2253 case 0x9: // Escape
2254 demo->quit = true;
2255 break;
2256 case 0x71: // left arrow key
2257 demo->spin_angle += demo->spin_increment;
2258 break;
2259 case 0x72: // right arrow key
2260 demo->spin_angle -= demo->spin_increment;
2261 break;
2262 case 0x41:
2263 demo->pause = !demo->pause;
2264 break;
2265 }
2266 break;
2267 case ConfigureNotify:
2268 if ((demo->width != event->xconfigure.width) ||
2269 (demo->height != event->xconfigure.height)) {
2270 demo->width = event->xconfigure.width;
2271 demo->height = event->xconfigure.height;
2272 demo_resize(demo);
2273 }
2274 break;
2275 default:
2276 break;
2277 }
2278
2279}
2280
2281static void demo_run_xlib(struct demo *demo) {
2282
2283 while (!demo->quit) {
2284 XEvent event;
2285
2286 if (demo->pause) {
2287 XNextEvent(demo->display, &event);
2288 demo_handle_xlib_event(demo, &event);
2289 } else {
2290 while (XPending(demo->display) > 0) {
2291 XNextEvent(demo->display, &event);
2292 demo_handle_xlib_event(demo, &event);
2293 }
2294 }
2295
Tony Barbour2593b182016-04-19 10:57:58 -06002296 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002297 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002298 demo->curFrame++;
2299 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2300 demo->quit = true;
2301 }
2302}
Tony Barbour8295ac42016-08-08 11:04:17 -06002303#endif // VK_USE_PLATFORM_XLIB_KHR
2304#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002305static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002306 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002307 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002308 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002309 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002310 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002311 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002312 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002313 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2314 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002315 demo->quit = true;
2316 }
2317 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002318 case XCB_KEY_RELEASE: {
2319 const xcb_key_release_event_t *key =
2320 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002321
Karl Schultze4dc75c2016-02-02 15:37:51 -07002322 switch (key->detail) {
2323 case 0x9: // Escape
2324 demo->quit = true;
2325 break;
2326 case 0x71: // left arrow key
2327 demo->spin_angle += demo->spin_increment;
2328 break;
2329 case 0x72: // right arrow key
2330 demo->spin_angle -= demo->spin_increment;
2331 break;
2332 case 0x41:
2333 demo->pause = !demo->pause;
2334 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002335 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002336 } break;
2337 case XCB_CONFIGURE_NOTIFY: {
2338 const xcb_configure_notify_event_t *cfg =
2339 (const xcb_configure_notify_event_t *)event;
2340 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002341 demo->width = cfg->width;
2342 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002343 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002344 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002345 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002346 default:
2347 break;
2348 }
2349}
2350
Tony Barbour2593b182016-04-19 10:57:58 -06002351static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002352 xcb_flush(demo->connection);
2353
2354 while (!demo->quit) {
2355 xcb_generic_event_t *event;
2356
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002357 if (demo->pause) {
2358 event = xcb_wait_for_event(demo->connection);
2359 } else {
2360 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002361 while(event) {
2362 demo_handle_xcb_event(demo, event);
2363 free(event);
2364 event = xcb_poll_for_event(demo->connection);
2365 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002366 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002367
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002368 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002369 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002370 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002371 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002372 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002373 }
2374}
2375
Tony Barbour2593b182016-04-19 10:57:58 -06002376static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002377 uint32_t value_mask, value_list[32];
2378
Tony Barbour2593b182016-04-19 10:57:58 -06002379 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002380
2381 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2382 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002383 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002384 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002385
Tony Barbour2593b182016-04-19 10:57:58 -06002386 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002387 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2388 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2389 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002390
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002391 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002392 xcb_intern_atom_cookie_t cookie =
2393 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2394 xcb_intern_atom_reply_t *reply =
2395 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002396
Karl Schultze4dc75c2016-02-02 15:37:51 -07002397 xcb_intern_atom_cookie_t cookie2 =
2398 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2399 demo->atom_wm_delete_window =
2400 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002401
Tony Barbour2593b182016-04-19 10:57:58 -06002402 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002403 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002404 &(*demo->atom_wm_delete_window).atom);
2405 free(reply);
2406
Tony Barbour2593b182016-04-19 10:57:58 -06002407 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002408
Karl Schultze4dc75c2016-02-02 15:37:51 -07002409 // Force the x/y coordinates to 100,100 results are identical in consecutive
2410 // runs
2411 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002412 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002413 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002414}
Tony Barbour6b131662016-08-25 13:14:45 -06002415// VK_USE_PLATFORM_XCB_KHR
2416#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002417static void demo_run(struct demo *demo) {
2418 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002419 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002420 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002421 demo->curFrame++;
2422 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2423 demo->quit = true;
2424 }
2425}
2426
2427static void handle_ping(void *data UNUSED,
2428 struct wl_shell_surface *shell_surface,
2429 uint32_t serial) {
2430 wl_shell_surface_pong(shell_surface, serial);
2431}
2432
2433static void handle_configure(void *data UNUSED,
2434 struct wl_shell_surface *shell_surface UNUSED,
2435 uint32_t edges UNUSED, int32_t width UNUSED,
2436 int32_t height UNUSED) {}
2437
2438static void handle_popup_done(void *data UNUSED,
2439 struct wl_shell_surface *shell_surface UNUSED) {}
2440
2441static const struct wl_shell_surface_listener shell_surface_listener = {
2442 handle_ping, handle_configure, handle_popup_done};
2443
2444static void demo_create_window(struct demo *demo) {
2445 demo->window = wl_compositor_create_surface(demo->compositor);
2446 if (!demo->window) {
2447 printf("Can not create wayland_surface from compositor!\n");
2448 fflush(stdout);
2449 exit(1);
2450 }
2451
2452 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2453 if (!demo->shell_surface) {
2454 printf("Can not get shell_surface from wayland_surface!\n");
2455 fflush(stdout);
2456 exit(1);
2457 }
2458 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2459 demo);
2460 wl_shell_surface_set_toplevel(demo->shell_surface);
2461 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2462}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002463#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2464static void demo_run(struct demo *demo) {
2465 if (!demo->prepared)
2466 return;
2467
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002468 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002469 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002470 demo->curFrame++;
2471}
2472#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002473
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002474/*
2475 * Return 1 (true) if all layer names specified in check_names
2476 * can be found in given layer properties.
2477 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002478static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002479 uint32_t layer_count,
2480 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002481 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002482 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002483 for (uint32_t j = 0; j < layer_count; j++) {
2484 if (!strcmp(check_names[i], layers[j].layerName)) {
2485 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002486 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002487 }
2488 }
2489 if (!found) {
2490 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2491 return 0;
2492 }
2493 }
2494 return 1;
2495}
2496
Karl Schultze4dc75c2016-02-02 15:37:51 -07002497static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002498 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002499 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002500 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002501 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002502 char **instance_validation_layers = NULL;
2503 demo->enabled_extension_count = 0;
2504 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002505
Karl Schultz7c50ad62016-04-01 12:07:03 -06002506 char *instance_validation_layers_alt1[] = {
2507 "VK_LAYER_LUNARG_standard_validation"
2508 };
2509
2510 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002511 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2512 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2513 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2514 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002515 };
2516
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002517 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002518 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002519 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002520
Karl Schultz7c50ad62016-04-01 12:07:03 -06002521 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002522 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002523
Karl Schultz7c50ad62016-04-01 12:07:03 -06002524 instance_validation_layers = instance_validation_layers_alt1;
2525 if (instance_layer_count > 0) {
2526 VkLayerProperties *instance_layers =
2527 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2528 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2529 instance_layers);
2530 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002531
Karl Schultz7c50ad62016-04-01 12:07:03 -06002532
2533 validation_found = demo_check_layers(
2534 ARRAY_SIZE(instance_validation_layers_alt1),
2535 instance_validation_layers, instance_layer_count,
2536 instance_layers);
2537 if (validation_found) {
2538 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002539 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2540 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002541 } else {
2542 // use alternative set of validation layers
2543 instance_validation_layers = instance_validation_layers_alt2;
2544 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2545 validation_found = demo_check_layers(
2546 ARRAY_SIZE(instance_validation_layers_alt2),
2547 instance_validation_layers, instance_layer_count,
2548 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002549 validation_layer_count =
2550 ARRAY_SIZE(instance_validation_layers_alt2);
2551 for (uint32_t i = 0; i < validation_layer_count; i++) {
2552 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002553 }
2554 }
2555 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002556 }
Dustin Graves7c287352016-01-27 13:48:06 -07002557
Karl Schultz7c50ad62016-04-01 12:07:03 -06002558 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002559 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002560 "required validation layer.\n\n"
2561 "Please look at the Getting Started guide for additional "
2562 "information.\n",
2563 "vkCreateInstance Failure");
2564 }
Dustin Graves7c287352016-01-27 13:48:06 -07002565 }
2566
2567 /* Look for instance extensions */
2568 VkBool32 surfaceExtFound = 0;
2569 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002570#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002571 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002572#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002573 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002574
Karl Schultze4dc75c2016-02-02 15:37:51 -07002575 err = vkEnumerateInstanceExtensionProperties(
2576 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002577 assert(!err);
2578
Dustin Graves7c287352016-01-27 13:48:06 -07002579 if (instance_extension_count > 0) {
2580 VkExtensionProperties *instance_extensions =
2581 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2582 err = vkEnumerateInstanceExtensionProperties(
2583 NULL, &instance_extension_count, instance_extensions);
2584 assert(!err);
2585 for (uint32_t i = 0; i < instance_extension_count; i++) {
2586 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2587 instance_extensions[i].extensionName)) {
2588 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002589 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002590 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002591 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002592#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002593 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2594 instance_extensions[i].extensionName)) {
2595 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002596 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002597 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2598 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002599#endif
2600#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002601 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2602 instance_extensions[i].extensionName)) {
2603 platformSurfaceExtFound = 1;
2604 xlibSurfaceExtFound = 1;
2605 demo->extension_names[demo->enabled_extension_count++] =
2606 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2607 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002608#endif
2609#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002610 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2611 instance_extensions[i].extensionName)) {
2612 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002613 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002614 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2615 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002616#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002617#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2618 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2619 instance_extensions[i].extensionName)) {
2620 platformSurfaceExtFound = 1;
2621 demo->extension_names[demo->enabled_extension_count++] =
2622 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2623 }
2624#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002625#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002626 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2627 instance_extensions[i].extensionName)) {
2628 platformSurfaceExtFound = 1;
2629 demo->extension_names[demo->enabled_extension_count++] =
2630 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2631 }
2632#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002633 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2634 instance_extensions[i].extensionName)) {
2635 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002636 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002637 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2638 }
2639 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002640 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002641 }
Dustin Graves7c287352016-01-27 13:48:06 -07002642
2643 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002644 }
Dustin Graves7c287352016-01-27 13:48:06 -07002645
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002646 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002647 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2648 "the " VK_KHR_SURFACE_EXTENSION_NAME
2649 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002650 "Vulkan installable client driver (ICD) installed?\nPlease "
2651 "look at the Getting Started guide for additional "
2652 "information.\n",
2653 "vkCreateInstance Failure");
2654 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002655 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002656#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002657 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2658 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2659 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002660 "Vulkan installable client driver (ICD) installed?\nPlease "
2661 "look at the Getting Started guide for additional "
2662 "information.\n",
2663 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002664#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002665 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2666 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2667 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002668 "Vulkan installable client driver (ICD) installed?\nPlease "
2669 "look at the Getting Started guide for additional "
2670 "information.\n",
2671 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002672#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2673 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2674 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2675 " extension.\n\nDo you have a compatible "
2676 "Vulkan installable client driver (ICD) installed?\nPlease "
2677 "look at the Getting Started guide for additional "
2678 "information.\n",
2679 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002680#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2681 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2682 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2683 " extension.\n\nDo you have a compatible "
2684 "Vulkan installable client driver (ICD) installed?\nPlease "
2685 "look at the Getting Started guide for additional "
2686 "information.\n",
2687 "vkCreateInstance Failure");
2688#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002689 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002690#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002691 if (demo->use_xlib && !xlibSurfaceExtFound) {
2692 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2693 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2694 " extension.\n\nDo you have a compatible "
2695 "Vulkan installable client driver (ICD) installed?\nPlease "
2696 "look at the Getting Started guide for additional "
2697 "information.\n",
2698 "vkCreateInstance Failure");
2699 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002700#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002701 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002702 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002703 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002704 .pApplicationName = APP_SHORT_NAME,
2705 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002706 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002707 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002708 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002709 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002710 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002711 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002712 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002713 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002714 .enabledLayerCount = demo->enabled_layer_count,
2715 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2716 .enabledExtensionCount = demo->enabled_extension_count,
2717 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002718 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002719
2720 /*
2721 * This is info for a temp callback to use during CreateInstance.
2722 * After the instance is created, we use the instance-based
2723 * function to register the final callback.
2724 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002725 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002726 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002727 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2728 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002729 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002730 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002731 dbgCreateInfo.flags =
2732 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2733 inst_info.pNext = &dbgCreateInfo;
2734 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002735
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002736 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002737
Chia-I Wu63636062015-10-26 21:10:41 +08002738 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002739 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2740 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002741 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002742 "additional information.\n",
2743 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002744 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002745 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002746 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002747 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002748 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002749 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2750 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002751 "the Getting Started guide for additional information.\n",
2752 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002753 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002754
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002755 /* Make initial call to query gpu_count, then second call for gpu info*/
2756 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2757 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002758
2759 if (gpu_count > 0) {
2760 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2761 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2762 assert(!err);
2763 /* For cube demo we just grab the first physical device */
2764 demo->gpu = physical_devices[0];
2765 free(physical_devices);
2766 } else {
2767 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2768 "Do you have a compatible Vulkan installable client driver (ICD) "
2769 "installed?\nPlease look at the Getting Started guide for "
2770 "additional information.\n",
2771 "vkEnumeratePhysicalDevices Failure");
2772 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002773
Dustin Graves7c287352016-01-27 13:48:06 -07002774 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002775 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002776 VkBool32 swapchainExtFound = 0;
2777 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002778 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002779
Karl Schultze4dc75c2016-02-02 15:37:51 -07002780 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2781 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002782 assert(!err);
2783
Dustin Graves7c287352016-01-27 13:48:06 -07002784 if (device_extension_count > 0) {
2785 VkExtensionProperties *device_extensions =
2786 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2787 err = vkEnumerateDeviceExtensionProperties(
2788 demo->gpu, NULL, &device_extension_count, device_extensions);
2789 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002790
Dustin Graves7c287352016-01-27 13:48:06 -07002791 for (uint32_t i = 0; i < device_extension_count; i++) {
2792 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2793 device_extensions[i].extensionName)) {
2794 swapchainExtFound = 1;
2795 demo->extension_names[demo->enabled_extension_count++] =
2796 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2797 }
2798 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002799 }
Dustin Graves7c287352016-01-27 13:48:06 -07002800
2801 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002802 }
Dustin Graves7c287352016-01-27 13:48:06 -07002803
Tony Barbourcc69f452015-10-08 13:59:42 -06002804 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002805 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2806 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2807 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002808 "Vulkan installable client driver (ICD) installed?\nPlease "
2809 "look at the Getting Started guide for additional "
2810 "information.\n",
2811 "vkCreateInstance Failure");
2812 }
2813
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002814 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002815 demo->CreateDebugReportCallback =
2816 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2817 demo->inst, "vkCreateDebugReportCallbackEXT");
2818 demo->DestroyDebugReportCallback =
2819 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2820 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002821 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002822 ERR_EXIT(
2823 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2824 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002825 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002826 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002827 ERR_EXIT(
2828 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2829 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002830 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002831 demo->DebugReportMessage =
2832 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2833 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002834 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002835 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002836 "vkGetProcAddr Failure");
2837 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002838
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002839 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002840 PFN_vkDebugReportCallbackEXT callback;
2841 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002842 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002843 dbgCreateInfo.pNext = NULL;
2844 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002845 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002846 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002847 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002848 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2849 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002850 switch (err) {
2851 case VK_SUCCESS:
2852 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002853 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002854 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2855 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002856 break;
2857 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002858 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2859 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002860 break;
2861 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002862 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002863 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002864
2865 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002866 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2867 &demo->queue_family_count, NULL);
2868 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002869
Karl Schultze4dc75c2016-02-02 15:37:51 -07002870 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002871 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2872 vkGetPhysicalDeviceQueueFamilyProperties(
2873 demo->gpu, &demo->queue_family_count, demo->queue_props);
2874
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002875 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002876 // If app has specific feature requirements it should check supported
2877 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002878 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002879 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002880
Tony Barbourda2bad52016-01-22 14:36:40 -07002881 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2882 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2883 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2884 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2885 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2886}
2887
Karl Schultze4dc75c2016-02-02 15:37:51 -07002888static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002889 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002890 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06002891 VkDeviceQueueCreateInfo queues[2];
2892 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2893 queues[0].pNext = NULL;
2894 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
2895 queues[0].queueCount = 1;
2896 queues[0].pQueuePriorities = queue_priorities;
2897 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002898
2899 VkDeviceCreateInfo device = {
2900 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2901 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002902 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06002903 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002904 .enabledLayerCount = 0,
2905 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002906 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002907 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2908 .pEnabledFeatures =
2909 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002910 };
Tony Barbour22e06272016-09-07 16:07:56 -06002911 if (demo->separate_present_queue) {
2912 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2913 queues[1].pNext = NULL;
2914 queues[1].queueFamilyIndex = demo->present_queue_family_index;
2915 queues[1].queueCount = 1;
2916 queues[1].pQueuePriorities = queue_priorities;
2917 queues[1].flags = 0;
2918 device.queueCreateInfoCount = 2;
2919 }
Chia-I Wu63636062015-10-26 21:10:41 +08002920 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002921 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002922}
2923
Karl Schultze4dc75c2016-02-02 15:37:51 -07002924static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002925 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002926 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002927
Karl Schultze4dc75c2016-02-02 15:37:51 -07002928// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002929#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002930 VkWin32SurfaceCreateInfoKHR createInfo;
2931 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2932 createInfo.pNext = NULL;
2933 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002934 createInfo.hinstance = demo->connection;
2935 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002936
Karl Schultze4dc75c2016-02-02 15:37:51 -07002937 err =
2938 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002939#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2940 VkWaylandSurfaceCreateInfoKHR createInfo;
2941 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2942 createInfo.pNext = NULL;
2943 createInfo.flags = 0;
2944 createInfo.display = demo->display;
2945 createInfo.surface = demo->window;
2946
2947 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2948 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002949#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2950 VkAndroidSurfaceCreateInfoKHR createInfo;
2951 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2952 createInfo.pNext = NULL;
2953 createInfo.flags = 0;
2954 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002955
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002956 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2957#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002958 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002959#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002960 VkXlibSurfaceCreateInfoKHR createInfo;
2961 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2962 createInfo.pNext = NULL;
2963 createInfo.flags = 0;
2964 createInfo.dpy = demo->display;
2965 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002966
Tony Barbour2593b182016-04-19 10:57:58 -06002967 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2968 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002969#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002970 }
2971 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002972#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002973 VkXcbSurfaceCreateInfoKHR createInfo;
2974 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2975 createInfo.pNext = NULL;
2976 createInfo.flags = 0;
2977 createInfo.connection = demo->connection;
2978 createInfo.window = demo->xcb_window;
2979
2980 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002981#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002982 }
Tony Barbour2593b182016-04-19 10:57:58 -06002983 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002984
Tony Barbourcc69f452015-10-08 13:59:42 -06002985 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002986 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06002987 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
2988 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002989 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002990 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002991 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002992
2993 // Search for a graphics and a present queue in the array of queue
2994 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06002995 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
2996 uint32_t presentQueueFamilyIndex = UINT32_MAX;
2997 for (i = 0; i < demo->queue_family_count; i++) {
2998 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2999 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3000 graphicsQueueFamilyIndex = i;
3001 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003002
Tony Barbourab2a0362016-09-06 11:40:12 -06003003 if (supportsPresent[i] == VK_TRUE) {
3004 graphicsQueueFamilyIndex = i;
3005 presentQueueFamilyIndex = i;
3006 break;
3007 }
3008 }
3009 }
3010
3011 if (presentQueueFamilyIndex == UINT32_MAX) {
3012 // If didn't find a queue that supports both graphics and present, then
3013 // find a separate present queue.
3014 for (size_t i = 0; i < demo->queue_family_count; ++i) {
3015 if (supportsPresent[i] == VK_TRUE) {
3016 presentQueueFamilyIndex = i;
3017 break;
3018 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003019 }
3020 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003021
3022 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003023 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3024 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003025 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003026 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003027 }
3028
Tony Barbourab2a0362016-09-06 11:40:12 -06003029 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3030 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003031 demo->separate_present_queue =
3032 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003033 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003034
Tony Barbourda2bad52016-01-22 14:36:40 -07003035 demo_create_device(demo);
3036
3037 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3038 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3039 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3040 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3041 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3042
Tony Barboura2a67812016-07-18 13:21:06 -06003043 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3044 &demo->graphics_queue);
3045
Tony Barbour22e06272016-09-07 16:07:56 -06003046 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003047 demo->present_queue = demo->graphics_queue;
3048 } else {
3049 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3050 &demo->present_queue);
3051 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003052
Ian Elliottaaae5352015-07-06 14:27:58 -06003053 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003054 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003055 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003056 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003057 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003058 VkSurfaceFormatKHR *surfFormats =
3059 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003060 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003061 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003062 assert(!err);
3063 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3064 // the surface has no preferred format. Otherwise, at least one
3065 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003066 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003067 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003068 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003069 assert(formatCount >= 1);
3070 demo->format = surfFormats[0].format;
3071 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003072 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003073
David Pinedo2bb7c932015-06-18 17:03:14 -06003074 demo->quit = false;
3075 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003076
Tony Barbource7524e2016-07-28 12:01:45 -06003077 // Create semaphores to synchronize acquiring presentable buffers before
3078 // rendering and waiting for drawing to be complete before presenting
3079 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3080 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3081 .pNext = NULL,
3082 .flags = 0,
3083 };
Tony Barbource7524e2016-07-28 12:01:45 -06003084
Tony Barbour66a56c32016-09-21 13:10:56 -06003085 // Create fences that we can use to throttle if we get too far
3086 // ahead of the image presents
3087 VkFenceCreateInfo fence_ci = {
3088 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3089 .pNext = NULL,
3090 .flags = 0
3091 };
3092 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3093 vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3094 demo->fencesInited[i] = false;
Tony Barbour22e06272016-09-07 16:07:56 -06003095 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003096 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003097 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003098
3099 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3100 &demo->draw_complete_semaphores[i]);
3101 assert(!err);
3102
3103 if (demo->separate_present_queue) {
3104 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3105 &demo->image_ownership_semaphores[i]);
3106 assert(!err);
3107 }
Tony Barbour22e06272016-09-07 16:07:56 -06003108 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003109 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003110
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003111 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003112 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003113}
3114
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003115#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3116static void registry_handle_global(void *data, struct wl_registry *registry,
3117 uint32_t name, const char *interface,
3118 uint32_t version UNUSED) {
3119 struct demo *demo = data;
3120 if (strcmp(interface, "wl_compositor") == 0) {
3121 demo->compositor =
3122 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3123 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3124 * tp xdg_shell */
3125 } else if (strcmp(interface, "wl_shell") == 0) {
3126 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3127 }
3128}
3129
3130static void registry_handle_global_remove(void *data UNUSED,
3131 struct wl_registry *registry UNUSED,
3132 uint32_t name UNUSED) {}
3133
3134static const struct wl_registry_listener registry_listener = {
3135 registry_handle_global, registry_handle_global_remove};
3136#endif
3137
Karl Schultze4dc75c2016-02-02 15:37:51 -07003138static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003139#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003140 const xcb_setup_t *setup;
3141 xcb_screen_iterator_t iter;
3142 int scr;
3143
3144 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003145 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003146 printf("Cannot find a compatible Vulkan installable client driver "
3147 "(ICD).\nExiting ...\n");
3148 fflush(stdout);
3149 exit(1);
3150 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003151
3152 setup = xcb_get_setup(demo->connection);
3153 iter = xcb_setup_roots_iterator(setup);
3154 while (scr-- > 0)
3155 xcb_screen_next(&iter);
3156
3157 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003158#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3159 demo->display = wl_display_connect(NULL);
3160
3161 if (demo->display == NULL) {
3162 printf("Cannot find a compatible Vulkan installable client driver "
3163 "(ICD).\nExiting ...\n");
3164 fflush(stdout);
3165 exit(1);
3166 }
3167
3168 demo->registry = wl_display_get_registry(demo->display);
3169 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3170 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003171#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003172}
3173
Karl Schultze4dc75c2016-02-02 15:37:51 -07003174static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003175 vec3 eye = {0.0f, 3.0f, 5.0f};
3176 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003177 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003178
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003179 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003180 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003181
Piers Daniell735ee532015-02-23 16:23:13 -07003182 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003183 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003184 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003185 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003186 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003187 if (strcmp(argv[i], "--break") == 0) {
3188 demo->use_break = true;
3189 continue;
3190 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003191 if (strcmp(argv[i], "--validate") == 0) {
3192 demo->validate = true;
3193 continue;
3194 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003195#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003196 if (strcmp(argv[i], "--xlib") == 0) {
3197 demo->use_xlib = true;
3198 continue;
3199 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003200#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003201 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3202 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3203 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003204 i++;
3205 continue;
3206 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003207 if (strcmp(argv[i], "--suppress_popups") == 0) {
3208 demo->suppress_popups = true;
3209 continue;
3210 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003211
Karl Schultze4dc75c2016-02-02 15:37:51 -07003212 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003213#if defined(VK_USE_PLATFORM_XLIB_KHR)
3214 "[--xlib] "
3215#endif
lenny-lunargfbe22392016-06-06 11:07:53 -06003216 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003217 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003218 fflush(stderr);
3219 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003220 }
3221
Tony Barbour2593b182016-04-19 10:57:58 -06003222 if (!demo->use_xlib)
3223 demo_init_connection(demo);
3224
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003225 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003226
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003227 demo->width = 500;
3228 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003229
Tony Barbour66a56c32016-09-21 13:10:56 -06003230 demo->spin_angle = 4.0f;
3231 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003232 demo->pause = false;
3233
Karl Schultze4dc75c2016-02-02 15:37:51 -07003234 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3235 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003236 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3237 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003238
3239 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003240}
3241
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003242#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003243// Include header required for parsing the command line options.
3244#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003245
Karl Schultze4dc75c2016-02-02 15:37:51 -07003246int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3247 int nCmdShow) {
3248 MSG msg; // message
3249 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003250 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003251 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003252
Karl Schultze4dc75c2016-02-02 15:37:51 -07003253 // Use the CommandLine functions to get the command line arguments.
3254 // Unfortunately, Microsoft outputs
3255 // this information as wide characters for Unicode, and we simply want the
3256 // Ascii version to be compatible
3257 // with the non-Windows side. So, we have to convert the information to
3258 // Ascii character strings.
3259 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3260 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003261 argc = 0;
3262 }
3263
Karl Schultze4dc75c2016-02-02 15:37:51 -07003264 if (argc > 0) {
3265 argv = (char **)malloc(sizeof(char *) * argc);
3266 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003267 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003268 } else {
3269 for (int iii = 0; iii < argc; iii++) {
3270 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003271 size_t numConverted = 0;
3272
Karl Schultze4dc75c2016-02-02 15:37:51 -07003273 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3274 if (argv[iii] != NULL) {
3275 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3276 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003277 }
3278 }
3279 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003280 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003281 argv = NULL;
3282 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003283
3284 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003285
3286 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003287 if (argc > 0 && argv != NULL) {
3288 for (int iii = 0; iii < argc; iii++) {
3289 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003290 free(argv[iii]);
3291 }
3292 }
3293 free(argv);
3294 }
3295
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003296 demo.connection = hInstance;
3297 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003298 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003299 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003300
3301 demo_prepare(&demo);
3302
Karl Schultze4dc75c2016-02-02 15:37:51 -07003303 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003304
3305 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003306 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003307 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003308 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003309 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003310 done = true; // if found, quit app
3311 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003312 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003313 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003314 DispatchMessage(&msg);
3315 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003316 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003317 }
3318
3319 demo_cleanup(&demo);
3320
Karl Schultze4dc75c2016-02-02 15:37:51 -07003321 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003322}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003323#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3324#include <android/log.h>
3325#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003326#include "android_util.h"
3327
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003328static bool initialized = false;
3329static bool active = false;
3330struct demo demo;
3331
3332static int32_t processInput(struct android_app* app, AInputEvent* event) {
3333 return 0;
3334}
3335
3336static void processCommand(struct android_app* app, int32_t cmd) {
3337 switch(cmd) {
3338 case APP_CMD_INIT_WINDOW: {
3339 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003340 // We're getting a new window. If the app is starting up, we
3341 // need to initialize. If the app has already been
3342 // initialized, that means that we lost our previous window,
3343 // which means that we have a lot of work to do. At a minimum,
3344 // we need to destroy the swapchain and surface associated with
3345 // the old window, and create a new surface and swapchain.
3346 // However, since there are a lot of other objects/state that
3347 // is tied to the swapchain, it's easiest to simply cleanup and
3348 // start over (i.e. use a brute-force approach of re-starting
3349 // the app)
3350 if (demo.prepared) {
3351 demo_cleanup(&demo);
3352 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003353
3354 // Parse Intents into argc, argv
3355 // Use the following key to send arguments, i.e.
3356 // --es args "--validate"
3357 const char key[] = "args";
3358 char* appTag = (char*) "Cube";
3359 int argc = 0;
3360 char** argv = get_args(app, key, appTag, &argc);
3361
3362 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3363 for (int i = 0; i < argc; i++)
3364 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3365
3366 demo_init(&demo, argc, argv);
3367
3368 // Free the argv malloc'd by get_args
3369 for (int i = 0; i < argc; i++)
3370 free(argv[i]);
3371
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003372 demo.window = (void*)app->window;
3373 demo_init_vk_swapchain(&demo);
3374 demo_prepare(&demo);
3375 initialized = true;
3376 }
3377 break;
3378 }
3379 case APP_CMD_GAINED_FOCUS: {
3380 active = true;
3381 break;
3382 }
3383 case APP_CMD_LOST_FOCUS: {
3384 active = false;
3385 break;
3386 }
3387 }
3388}
3389
3390void android_main(struct android_app *app)
3391{
3392 app_dummy();
3393
Cody Northrop8707a6e2016-04-26 19:59:19 -06003394#ifdef ANDROID
3395 int vulkanSupport = InitVulkan();
3396 if (vulkanSupport == 0)
3397 return;
3398#endif
3399
Ian Elliottf05d5d12016-05-17 12:03:35 -06003400 demo.prepared = false;
3401
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003402 app->onAppCmd = processCommand;
3403 app->onInputEvent = processInput;
3404
3405 while(1) {
3406 int events;
3407 struct android_poll_source* source;
3408 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3409 if (source) {
3410 source->process(app, source);
3411 }
3412
3413 if (app->destroyRequested != 0) {
3414 demo_cleanup(&demo);
3415 return;
3416 }
3417 }
3418 if (initialized && active) {
3419 demo_run(&demo);
3420 }
3421 }
3422
3423}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003424#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003425int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003426 struct demo demo;
3427
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003428 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003429#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003430 if (demo.use_xlib)
3431 demo_create_xlib_window(&demo);
3432 else
3433 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003434#elif defined(VK_USE_PLATFORM_XCB_KHR)
3435 demo_create_xcb_window(&demo);
3436#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3437 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003438#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3439 demo_create_window(&demo);
3440#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003441
Tony Barbourcc69f452015-10-08 13:59:42 -06003442 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003443
3444 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003445
Tony Barbour8295ac42016-08-08 11:04:17 -06003446#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003447 if (demo.use_xlib)
3448 demo_run_xlib(&demo);
3449 else
3450 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003451#elif defined(VK_USE_PLATFORM_XCB_KHR)
3452 demo_run_xcb(&demo);
3453#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3454 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003455#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3456 demo_run(&demo);
3457#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003458
3459 demo_cleanup(&demo);
3460
Karl Schultz0218c002016-03-22 17:06:13 -06003461 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003462}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003463#endif