blob: 381df5ff21ac82cca358463b4512ee3b726f7e21 [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;
lenny-lunargfbe22392016-06-06 11:07:53 -0600454#else
455 printf("%s\n", message);
456 fflush(stdout);
457#endif
458 free(message);
459
460 /*
461 * false indicates that layer should not bail-out of an
462 * API call that had validation failures. This may mean that the
463 * app dies inside the driver due to invalid parameter(s).
464 * That's what would happen without validation layers, so we'll
465 * keep that behavior here.
466 */
467 return false;
468}
469
Ian Elliottcf074d32015-10-16 18:02:43 -0600470// Forward declaration:
471static void demo_resize(struct demo *demo);
472
Karl Schultze4dc75c2016-02-02 15:37:51 -0700473static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
474 VkFlags requirements_mask,
475 uint32_t *typeIndex) {
476 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600477 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700478 if ((typeBits & 1) == 1) {
479 // Type is available, does it match user properties?
480 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
481 requirements_mask) == requirements_mask) {
482 *typeIndex = i;
483 return true;
484 }
485 }
486 typeBits >>= 1;
487 }
488 // No memory types matched, return failure
489 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600490}
491
Karl Schultze4dc75c2016-02-02 15:37:51 -0700492static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600493 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494
Tony Barbource7524e2016-07-28 12:01:45 -0600495 // This function could get called twice if the texture uses a staging buffer
496 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 return;
499
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600500 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600501 assert(!err);
502
Tony Barbource7524e2016-07-28 12:01:45 -0600503 VkFence fence;
504 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
505 .pNext = NULL,
506 .flags = 0};
507 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700508 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700509 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
510 .pNext = NULL,
511 .waitSemaphoreCount = 0,
512 .pWaitSemaphores = NULL,
513 .pWaitDstStageMask = NULL,
514 .commandBufferCount = 1,
515 .pCommandBuffers = cmd_bufs,
516 .signalSemaphoreCount = 0,
517 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518
Tony Barbource7524e2016-07-28 12:01:45 -0600519 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600520 assert(!err);
521
Tony Barbource7524e2016-07-28 12:01:45 -0600522 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600523 assert(!err);
524
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600525 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600526 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528}
529
Karl Schultze4dc75c2016-02-02 15:37:51 -0700530static void demo_set_image_layout(struct demo *demo, VkImage image,
531 VkImageAspectFlags aspectMask,
532 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700533 VkImageLayout new_image_layout,
534 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600535 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600536
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800538 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800539 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600540 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800541 .commandPool = demo->cmd_pool,
542 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700543 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600544 };
545
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800546 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547 assert(!err);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700548 VkCommandBufferBeginInfo cmd_buf_info = {
549 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
550 .pNext = NULL,
551 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700552 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700553 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600554 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600555 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600556 }
557
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600558 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600559 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600560 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700561 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800562 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600563 .oldLayout = old_image_layout,
564 .newLayout = new_image_layout,
565 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700566 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600567
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800568 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600569 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800570 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600571 }
572
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700573 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700574 image_memory_barrier.dstAccessMask =
575 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700576 }
577
578 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700579 image_memory_barrier.dstAccessMask =
580 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700581 }
582
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600583 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600584 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700585 image_memory_barrier.dstAccessMask =
586 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600587 }
588
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600589 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600590
Tony Barbour50bbca42015-06-29 16:20:35 -0600591 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
592 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600593
Karl Schultze4dc75c2016-02-02 15:37:51 -0700594 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
595 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600596}
597
Karl Schultze4dc75c2016-02-02 15:37:51 -0700598static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700599 const VkCommandBufferBeginInfo cmd_buf_info = {
600 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
601 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600602 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700603 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700604 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800605 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700606 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
607 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800608 };
609 const VkRenderPassBeginInfo rp_begin = {
610 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
611 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800612 .renderPass = demo->render_pass,
613 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800614 .renderArea.offset.x = 0,
615 .renderArea.offset.y = 0,
616 .renderArea.extent.width = demo->width,
617 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600618 .clearValueCount = 2,
619 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700620 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800621 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600623 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624 assert(!err);
625
Tony Barbour5c5b1632016-04-26 11:13:48 -0600626 VkImageMemoryBarrier image_memory_barrier = {
627 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
628 .pNext = NULL,
629 .srcAccessMask = 0,
630 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barbour22e06272016-09-07 16:07:56 -0600631 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour5c5b1632016-04-26 11:13:48 -0600632 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
633 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
634 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
635 .image = demo->buffers[demo->current_buffer].image,
636 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
637
Tony Barbource7524e2016-07-28 12:01:45 -0600638 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
639 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
640 NULL, 0, NULL, 1, &image_memory_barrier);
Tony Barbour5c5b1632016-04-26 11:13:48 -0600641
Chia-I Wuf051d262015-10-27 19:25:11 +0800642 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800643
Karl Schultze4dc75c2016-02-02 15:37:51 -0700644 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
645 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
646 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
647 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600648 VkViewport viewport;
649 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700650 viewport.height = (float)demo->height;
651 viewport.width = (float)demo->width;
652 viewport.minDepth = (float)0.0f;
653 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700654 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600655
656 VkRect2D scissor;
657 memset(&scissor, 0, sizeof(scissor));
658 scissor.extent.width = demo->width;
659 scissor.extent.height = demo->height;
660 scissor.offset.x = 0;
661 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700662 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600663 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600664 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600665 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800666 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667
Tony Barbour22e06272016-09-07 16:07:56 -0600668 if (demo->separate_present_queue) {
669 // We have to transfer ownership from the graphics queue family to the
670 // present queue family to be able to present. Note that we don't have
671 // to transfer from present queue family back to graphics queue family at
672 // the start of the next frame because we don't care about the image's
673 // contents at that point.
674 VkImageMemoryBarrier image_ownership_barrier = {
675 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
676 .pNext = NULL,
677 .srcAccessMask = 0,
678 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
679 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
680 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
681 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
682 .dstQueueFamilyIndex = demo->present_queue_family_index,
683 .image = demo->buffers[demo->current_buffer].image,
684 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
685
686 vkCmdPipelineBarrier(cmd_buf,
687 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
688 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0,
689 0, NULL, 0, NULL, 1, &image_ownership_barrier);
690 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600691 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692 assert(!err);
693}
694
Tony Barbour22e06272016-09-07 16:07:56 -0600695void demo_build_image_ownership_cmd(struct demo *demo, int i) {
696 VkResult U_ASSERT_ONLY err;
697
698 const VkCommandBufferBeginInfo cmd_buf_info = {
699 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
700 .pNext = NULL,
701 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
702 .pInheritanceInfo = NULL,
703 };
704 err = vkBeginCommandBuffer(demo->buffers[i].graphics_to_present_cmd,
705 &cmd_buf_info);
706 assert(!err);
707
708 VkImageMemoryBarrier image_ownership_barrier = {
709 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
710 .pNext = NULL,
711 .srcAccessMask = 0,
712 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
713 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
714 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
715 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
716 .dstQueueFamilyIndex = demo->present_queue_family_index,
717 .image = demo->buffers[i].image,
718 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
719
720 vkCmdPipelineBarrier(demo->buffers[i].graphics_to_present_cmd,
721 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
722 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
723 NULL, 0, NULL, 1, &image_ownership_barrier);
724 err = vkEndCommandBuffer(demo->buffers[i].graphics_to_present_cmd);
725 assert(!err);
726}
727
Karl Schultze4dc75c2016-02-02 15:37:51 -0700728void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600729 mat4x4 MVP, Model, VP;
730 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600731 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600732 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600734 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600735
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600736 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600737 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700738 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
739 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600740 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600741
Karl Schultze4dc75c2016-02-02 15:37:51 -0700742 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
743 demo->uniform_data.mem_alloc.allocationSize, 0,
744 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600745 assert(!err);
746
Karl Schultze4dc75c2016-02-02 15:37:51 -0700747 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600748
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600749 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600750}
751
Karl Schultze4dc75c2016-02-02 15:37:51 -0700752static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600753 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600754
Tony Barbour66a56c32016-09-21 13:10:56 -0600755 if (demo->fencesInited[demo->frame_index])
756 {
757 // Ensure no more than FRAME_LAG presentations are outstanding
758 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
759 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
760 }
761
Ian Elliottaaae5352015-07-06 14:27:58 -0600762 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700763 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour66a56c32016-09-21 13:10:56 -0600764 demo->image_acquired_semaphores[demo->frame_index], demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600765 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600766 demo->fencesInited[demo->frame_index] = true;
767
Ian Elliottcf074d32015-10-16 18:02:43 -0600768 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
769 // demo->swapchain is out of date (e.g. the window was resized) and
770 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600771 demo->frame_index += 1;
772 demo->frame_index %= FRAME_LAG;
773
Ian Elliottcf074d32015-10-16 18:02:43 -0600774 demo_resize(demo);
775 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600776 return;
777 } else if (err == VK_SUBOPTIMAL_KHR) {
778 // demo->swapchain is not as optimal as it could be, but the platform's
779 // presentation engine will still present the image correctly.
780 } else {
781 assert(!err);
782 }
Tony Barbource7524e2016-07-28 12:01:45 -0600783 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600784 // that the image won't be rendered to until the presentation
785 // engine has fully released ownership to the application, and it is
786 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600787 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600788 VkPipelineStageFlags pipe_stage_flags;
789 VkSubmitInfo submit_info;
790 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
791 submit_info.pNext = NULL;
792 submit_info.pWaitDstStageMask = &pipe_stage_flags;
793 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
794 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600795 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600796 submit_info.commandBufferCount = 1;
797 submit_info.pCommandBuffers = &demo->buffers[demo->current_buffer].cmd;
798 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600799 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura2a67812016-07-18 13:21:06 -0600800 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801 assert(!err);
802
Tony Barbour22e06272016-09-07 16:07:56 -0600803 if (demo->separate_present_queue) {
804 // If we are using separate queues, change image ownership to the
805 // present queue before presenting, waiting for the draw complete
806 // semaphore and signalling the ownership released semaphore when finished
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->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600810 submit_info.commandBufferCount = 1;
811 submit_info.pCommandBuffers =
812 &demo->buffers[demo->current_buffer].graphics_to_present_cmd;
813 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600814 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600815 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
816 assert(!err);
817 }
818
819 // If we are using separate queues we have to wait for image ownership,
820 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600821 VkPresentInfoKHR present = {
822 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600823 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600824 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600825 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600826 ? &demo->image_ownership_semaphores[demo->frame_index]
827 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600828 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700829 .pSwapchains = &demo->swapchain,
830 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600831 };
832
Tony Barboura2a67812016-07-18 13:21:06 -0600833 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600834 demo->frame_index += 1;
835 demo->frame_index %= FRAME_LAG;
836
Ian Elliottcf074d32015-10-16 18:02:43 -0600837 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
838 // demo->swapchain is out of date (e.g. the window was resized) and
839 // must be recreated:
840 demo_resize(demo);
841 } else if (err == VK_SUBOPTIMAL_KHR) {
842 // demo->swapchain is not as optimal as it could be, but the platform's
843 // presentation engine will still present the image correctly.
844 } else {
845 assert(!err);
846 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847}
848
Karl Schultze4dc75c2016-02-02 15:37:51 -0700849static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600850 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600851 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600852
Ian Elliottb08e0d92015-11-20 11:55:46 -0700853 // Check the surface capabilities and formats
854 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700855 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
856 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600857 assert(!err);
858
Ian Elliott8528eff2015-08-07 15:56:59 -0600859 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700860 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
861 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600862 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600863 VkPresentModeKHR *presentModes =
864 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600865 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700866 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
867 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600868 assert(!err);
869
Ian Elliotta0781972015-08-21 15:09:33 -0600870 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600871 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
872 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
873 // If the surface size is undefined, the size is set to the size
874 // of the images requested, which must fit within the minimum and
875 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600876 swapchainExtent.width = demo->width;
877 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600878
879 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
880 swapchainExtent.width = surfCapabilities.minImageExtent.width;
881 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
882 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
883 }
884
885 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
886 swapchainExtent.height = surfCapabilities.minImageExtent.height;
887 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
888 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
889 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700890 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600891 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700892 swapchainExtent = surfCapabilities.currentExtent;
893 demo->width = surfCapabilities.currentExtent.width;
894 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600895 }
896
Tony Barbour66a56c32016-09-21 13:10:56 -0600897 // The FIFO present mode is guaranteed by the spec to be supported
898 // and to have no tearing. MAILBOX is another option, but for fast
899 // rendering apps we could use a lot of gpu power rendering frames
900 // that are discarded and never displayed
Ian Elliotta0781972015-08-21 15:09:33 -0600901 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600902
Ian Elliottcf7f7482016-08-19 03:46:58 -0600903 // Determine the number of VkImage's to use in the swap chain.
904 // Application desires to only acquire 1 image at a time (which is
905 // "surfCapabilities.minImageCount").
906 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
907 // If maxImageCount is 0, we can ask for as many images as we want;
908 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700909 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600910 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600911 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600912 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600913 }
914
Tony Barbour9fd6d352015-09-16 15:01:32 -0600915 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700916 if (surfCapabilities.supportedTransforms &
917 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700918 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600919 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700920 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600921 }
922
Tony Barboura2a67812016-07-18 13:21:06 -0600923 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600924 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800925 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700926 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600927 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800928 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600929 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700930 .imageExtent =
931 {
932 .width = swapchainExtent.width, .height = swapchainExtent.height,
933 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700934 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600935 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700936 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700937 .imageArrayLayers = 1,
938 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
939 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600940 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600941 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600942 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600943 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600945 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600946 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700947 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800948 assert(!err);
949
Ian Elliottcf074d32015-10-16 18:02:43 -0600950 // If we just re-created an existing swapchain, we should destroy the old
951 // swapchain at this point.
952 // Note: destroying the swapchain also cleans up all its associated
953 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800954 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700955 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600956 }
957
958 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600959 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600960 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800961
Karl Schultze4dc75c2016-02-02 15:37:51 -0700962 VkImage *swapchainImages =
963 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600964 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600965 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600966 &demo->swapchainImageCount,
967 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600968 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600969
Karl Schultze4dc75c2016-02-02 15:37:51 -0700970 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
971 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600972 assert(demo->buffers);
973
Ian Elliotta0781972015-08-21 15:09:33 -0600974 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600975 VkImageViewCreateInfo color_image_view = {
976 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977 .pNext = NULL,
978 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700979 .components =
980 {
981 .r = VK_COMPONENT_SWIZZLE_R,
982 .g = VK_COMPONENT_SWIZZLE_G,
983 .b = VK_COMPONENT_SWIZZLE_B,
984 .a = VK_COMPONENT_SWIZZLE_A,
985 },
986 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
987 .baseMipLevel = 0,
988 .levelCount = 1,
989 .baseArrayLayer = 0,
990 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600991 .viewType = VK_IMAGE_VIEW_TYPE_2D,
992 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600993 };
994
Ian Elliotta0781972015-08-21 15:09:33 -0600995 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600996
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600997 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998
Karl Schultze4dc75c2016-02-02 15:37:51 -0700999 err = vkCreateImageView(demo->device, &color_image_view, NULL,
1000 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001001 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001002
1003 // The draw loop will be expecting the presentable images to be in
1004 // LAYOUT_PRESENT_SRC_KHR
1005 // since that's how they're left at the end of every frame.
1006 demo_set_image_layout(
1007 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1008 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001009 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001010
Mark Young04fd3d82016-01-25 14:43:50 -07001011 if (NULL != presentModes) {
1012 free(presentModes);
1013 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014}
1015
Karl Schultze4dc75c2016-02-02 15:37:51 -07001016static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001017 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001018 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001019 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001020 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001021 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001022 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001023 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001024 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001025 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001026 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001027 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001028 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001029 .flags = 0,
1030 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001031
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001032 VkImageViewCreateInfo view = {
1033 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001035 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001036 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001037 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1038 .baseMipLevel = 0,
1039 .levelCount = 1,
1040 .baseArrayLayer = 0,
1041 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001042 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001043 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001045
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001046 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001047 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001048 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001049
1050 demo->depth.format = depth_format;
1051
1052 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001053 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 assert(!err);
1055
Karl Schultze4dc75c2016-02-02 15:37:51 -07001056 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001057 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001058
Chia-I Wuf48700b2015-11-10 16:21:09 +08001059 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001060 demo->depth.mem_alloc.pNext = NULL;
1061 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1062 demo->depth.mem_alloc.memoryTypeIndex = 0;
1063
Karl Schultze4dc75c2016-02-02 15:37:51 -07001064 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1065 0, /* No requirements */
1066 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001067 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001069 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001070 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1071 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001072 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001074 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001075 err =
1076 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001077 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078
Karl Schultze4dc75c2016-02-02 15:37:51 -07001079 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1080 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001081 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1082 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001083
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084 /* create image view */
1085 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001086 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001087 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001088}
1089
Tony Barbour1a86d032015-09-21 15:17:33 -06001090/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001091bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001092 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001093#ifdef __ANDROID__
1094#include <lunarg.ppm.h>
1095 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001096 cPtr = (char*)lunarg_ppm;
1097 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001098 return false;
1099 }
1100 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001101 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001102 if (rgba_data == NULL) {
1103 return true;
1104 }
1105 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001106 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001107 return false;
1108 }
1109 while(strncmp(cPtr++, "\n", 1));
1110
1111 for (int y = 0; y < *height; y++) {
1112 uint8_t *rowPtr = rgba_data;
1113 for (int x = 0; x < *width; x++) {
1114 memcpy(rowPtr, cPtr, 3);
1115 rowPtr[3] = 255; /* Alpha of 1 */
1116 rowPtr += 4;
1117 cPtr += 3;
1118 }
1119 rgba_data += layout->rowPitch;
1120 }
1121
1122 return true;
1123#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001124 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001125 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001126
Tony Barbour1a86d032015-09-21 15:17:33 -06001127 if (!fPtr)
1128 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001129
Tony Barbour1a86d032015-09-21 15:17:33 -06001130 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001131 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1132 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001133 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001134 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001135
Tony Barbour1a86d032015-09-21 15:17:33 -06001136 do {
1137 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001138 if (cPtr == NULL) {
1139 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001140 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001141 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001142 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001143
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001144 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001145 if (rgba_data == NULL) {
1146 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001147 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001148 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001149 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001150 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001151 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1152 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001153 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001154 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001155
Karl Schultze4dc75c2016-02-02 15:37:51 -07001156 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001157 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001158 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001159 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001160 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001161 rowPtr[3] = 255; /* Alpha of 1 */
1162 rowPtr += 4;
1163 }
1164 rgba_data += layout->rowPitch;
1165 }
1166 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001167 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001168#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001169}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001170
Karl Schultze4dc75c2016-02-02 15:37:51 -07001171static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001172 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001173 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001174 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001175 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001176 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001177 int32_t tex_width;
1178 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001179 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001180 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001181
Karl Schultze4dc75c2016-02-02 15:37:51 -07001182 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001183 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001184 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001185
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001186 tex_obj->tex_width = tex_width;
1187 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001188
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001189 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001190 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001191 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001192 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001193 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001194 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001196 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001197 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001198 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001199 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001200 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001201 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001202 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001203
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001204 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001205
Karl Schultze4dc75c2016-02-02 15:37:51 -07001206 err =
1207 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208 assert(!err);
1209
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001210 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001211
Chia-I Wuf48700b2015-11-10 16:21:09 +08001212 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001213 tex_obj->mem_alloc.pNext = NULL;
1214 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1215 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216
Karl Schultze4dc75c2016-02-02 15:37:51 -07001217 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1218 required_props,
1219 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001220 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001221
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001222 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001223 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001224 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001225 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001227 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001228 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001229 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001230
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001231 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001232 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001233 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001235 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001236 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001237 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238 void *data;
1239
Karl Schultze4dc75c2016-02-02 15:37:51 -07001240 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1241 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001242
Karl Schultze4dc75c2016-02-02 15:37:51 -07001243 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1244 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001245 assert(!err);
1246
1247 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1248 fprintf(stderr, "Error loading texture: %s\n", filename);
1249 }
1250
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001251 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001253
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001254 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001255 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001256 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1257 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001258 /* setting the image layout does not reference the actual memory so no need
1259 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001260}
1261
Karl Schultze4dc75c2016-02-02 15:37:51 -07001262static void demo_destroy_texture_image(struct demo *demo,
1263 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001264 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001265 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1266 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001267}
1268
Karl Schultze4dc75c2016-02-02 15:37:51 -07001269static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001270 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001271 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001272 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001273
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001274 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001275
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001277 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001278
Karl Schultze4dc75c2016-02-02 15:37:51 -07001279 if ((props.linearTilingFeatures &
1280 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1281 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001282 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001283 demo_prepare_texture_image(
1284 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1285 VK_IMAGE_USAGE_SAMPLED_BIT,
1286 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1287 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001288 } else if (props.optimalTilingFeatures &
1289 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001290 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001291 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001292
1293 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001294 demo_prepare_texture_image(
1295 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1296 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1297 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1298 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001299
Karl Schultze4dc75c2016-02-02 15:37:51 -07001300 demo_prepare_texture_image(
1301 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1302 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1303 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001304
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001305 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001306 VK_IMAGE_ASPECT_COLOR_BIT,
1307 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001308 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1309 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001310
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001311 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001312 VK_IMAGE_ASPECT_COLOR_BIT,
1313 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001314 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1315 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001316
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001317 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001318 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1319 .srcOffset = {0, 0, 0},
1320 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1321 .dstOffset = {0, 0, 0},
1322 .extent = {staging_texture.tex_width,
1323 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001324 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001325 vkCmdCopyImage(
1326 demo->cmd, staging_texture.image,
1327 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1328 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001329
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001330 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001331 VK_IMAGE_ASPECT_COLOR_BIT,
1332 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001333 demo->textures[i].imageLayout,
1334 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001335
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001336 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001337
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001338 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001339 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001340 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1341 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001342 }
1343
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001344 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001345 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001347 .magFilter = VK_FILTER_NEAREST,
1348 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001349 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001350 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1351 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1352 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001354 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001355 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001356 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001357 .minLod = 0.0f,
1358 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001359 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001360 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001361 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001362
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001363 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001364 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001366 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001367 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001368 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001369 .components =
1370 {
1371 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1372 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1373 },
1374 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001375 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001376 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001377
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001379 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001380 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381 assert(!err);
1382
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001383 /* create image view */
1384 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001385 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001386 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387 assert(!err);
1388 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390
Karl Schultze4dc75c2016-02-02 15:37:51 -07001391void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001392 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001393 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001394 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001395 int i;
1396 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001397 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001398 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001399 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001400
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001401 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001402 mat4x4_mul(MVP, VP, demo->model_matrix);
1403 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001404 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001405
Karl Schultze4dc75c2016-02-02 15:37:51 -07001406 for (i = 0; i < 12 * 3; i++) {
1407 data.position[i][0] = g_vertex_buffer_data[i * 3];
1408 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1409 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001410 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001411 data.attr[i][0] = g_uv_buffer_data[2 * i];
1412 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001413 data.attr[i][2] = 0;
1414 data.attr[i][3] = 0;
1415 }
1416
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001417 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001418 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001419 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001420 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001421 err =
1422 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001423 assert(!err);
1424
Karl Schultze4dc75c2016-02-02 15:37:51 -07001425 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1426 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001427
Chia-I Wuf48700b2015-11-10 16:21:09 +08001428 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001429 demo->uniform_data.mem_alloc.pNext = NULL;
1430 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1431 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1432
Karl Schultze4dc75c2016-02-02 15:37:51 -07001433 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001434 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1435 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001436 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001437 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001438
Karl Schultze4dc75c2016-02-02 15:37:51 -07001439 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1440 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001441 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001442
Karl Schultze4dc75c2016-02-02 15:37:51 -07001443 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1444 demo->uniform_data.mem_alloc.allocationSize, 0,
1445 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001446 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001447
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001448 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001449
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001450 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001451
Karl Schultze4dc75c2016-02-02 15:37:51 -07001452 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1453 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001454 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001455
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001456 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1457 demo->uniform_data.buffer_info.offset = 0;
1458 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001459}
1460
Karl Schultze4dc75c2016-02-02 15:37:51 -07001461static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001462 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001463 [0] =
1464 {
1465 .binding = 0,
1466 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1467 .descriptorCount = 1,
1468 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1469 .pImmutableSamplers = NULL,
1470 },
1471 [1] =
1472 {
1473 .binding = 1,
1474 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1475 .descriptorCount = DEMO_TEXTURE_COUNT,
1476 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1477 .pImmutableSamplers = NULL,
1478 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001479 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001480 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001481 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001482 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001483 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001484 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001485 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001486 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001487
Karl Schultze4dc75c2016-02-02 15:37:51 -07001488 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1489 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001490 assert(!err);
1491
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001492 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001493 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1494 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001495 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001496 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001497 };
1498
Karl Schultze4dc75c2016-02-02 15:37:51 -07001499 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001500 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001502}
1503
Karl Schultze4dc75c2016-02-02 15:37:51 -07001504static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001505 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001506 [0] =
1507 {
1508 .format = demo->format,
1509 .samples = VK_SAMPLE_COUNT_1_BIT,
1510 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1511 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1512 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1513 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1514 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Tony Barbour98390392016-07-28 10:50:13 -06001515 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001516 },
1517 [1] =
1518 {
1519 .format = demo->depth.format,
1520 .samples = VK_SAMPLE_COUNT_1_BIT,
1521 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1522 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1523 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1524 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1525 .initialLayout =
1526 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1527 .finalLayout =
1528 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1529 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001530 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001531 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001532 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001533 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001534 const VkAttachmentReference depth_reference = {
1535 .attachment = 1,
1536 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1537 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001538 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001539 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1540 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001541 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001542 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001543 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001544 .pColorAttachments = &color_reference,
1545 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001546 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001547 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001548 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001549 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001550 const VkRenderPassCreateInfo rp_info = {
1551 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1552 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001553 .attachmentCount = 2,
1554 .pAttachments = attachments,
1555 .subpassCount = 1,
1556 .pSubpasses = &subpass,
1557 .dependencyCount = 0,
1558 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001559 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001560 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001561
Chia-I Wu63636062015-10-26 21:10:41 +08001562 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001563 assert(!err);
1564}
1565
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001566//TODO: Merge shader reading
1567#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001568static VkShaderModule
1569demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001570 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001571 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001572 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001573
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001574 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1575 moduleCreateInfo.pNext = NULL;
1576
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001577 moduleCreateInfo.codeSize = size;
1578 moduleCreateInfo.pCode = code;
1579 moduleCreateInfo.flags = 0;
1580 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1581 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001582
1583 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001584}
1585
Karl Schultze4dc75c2016-02-02 15:37:51 -07001586char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001587 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001588 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001589 void *shader_code;
1590
1591 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001592 if (!fp)
1593 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001594
1595 fseek(fp, 0L, SEEK_END);
1596 size = ftell(fp);
1597
1598 fseek(fp, 0L, SEEK_SET);
1599
1600 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001601 retval = fread(shader_code, size, 1, fp);
1602 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001603
1604 *psize = size;
1605
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001606 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001607 return shader_code;
1608}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001609#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001610
Karl Schultze4dc75c2016-02-02 15:37:51 -07001611static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001612#ifdef __ANDROID__
1613 VkShaderModuleCreateInfo sh_info = {};
1614 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1615
1616#include "cube.vert.h"
1617 sh_info.codeSize = sizeof(cube_vert);
1618 sh_info.pCode = cube_vert;
1619 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1620 assert(!err);
1621#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001622 void *vertShaderCode;
1623 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001624
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001625 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1626
Karl Schultze4dc75c2016-02-02 15:37:51 -07001627 demo->vert_shader_module =
1628 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001629
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001630 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001631#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001632
1633 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001634}
1635
Karl Schultze4dc75c2016-02-02 15:37:51 -07001636static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001637#ifdef __ANDROID__
1638 VkShaderModuleCreateInfo sh_info = {};
1639 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1640
1641#include "cube.frag.h"
1642 sh_info.codeSize = sizeof(cube_frag);
1643 sh_info.pCode = cube_frag;
1644 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1645 assert(!err);
1646#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001647 void *fragShaderCode;
1648 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001649
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001650 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001651
Karl Schultze4dc75c2016-02-02 15:37:51 -07001652 demo->frag_shader_module =
1653 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001654
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001655 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001656#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001657
1658 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001659}
1660
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001662 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001663 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001664 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001665 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001666 VkPipelineRasterizationStateCreateInfo rs;
1667 VkPipelineColorBlendStateCreateInfo cb;
1668 VkPipelineDepthStencilStateCreateInfo ds;
1669 VkPipelineViewportStateCreateInfo vp;
1670 VkPipelineMultisampleStateCreateInfo ms;
1671 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1672 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001673 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001674
Piers Daniellba839d12015-09-29 13:01:09 -06001675 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1676 memset(&dynamicState, 0, sizeof dynamicState);
1677 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1678 dynamicState.pDynamicStates = dynamicStateEnables;
1679
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001680 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001681 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001682 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001683
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001684 memset(&vi, 0, sizeof(vi));
1685 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1686
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001688 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001689 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690
1691 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001692 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1693 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001694 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001695 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001696 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001697 rs.rasterizerDiscardEnable = VK_FALSE;
1698 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001699 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700
1701 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001702 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1703 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001704 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001705 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001706 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001707 cb.attachmentCount = 1;
1708 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709
Tony Barbour29645d02015-01-16 14:27:35 -07001710 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001711 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001712 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001713 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1714 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001715 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001716 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1717 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001718
1719 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001720 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001721 ds.depthTestEnable = VK_TRUE;
1722 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001723 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001724 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001725 ds.back.failOp = VK_STENCIL_OP_KEEP;
1726 ds.back.passOp = VK_STENCIL_OP_KEEP;
1727 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001728 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001729 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730
Tony Barbour29645d02015-01-16 14:27:35 -07001731 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001732 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001733 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001734 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001735
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001736 // Two stages: vs and fs
1737 pipeline.stageCount = 2;
1738 VkPipelineShaderStageCreateInfo shaderStages[2];
1739 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1740
Karl Schultze4dc75c2016-02-02 15:37:51 -07001741 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1742 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001743 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001744 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001745
Karl Schultze4dc75c2016-02-02 15:37:51 -07001746 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1747 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001748 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001749 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001750
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001751 memset(&pipelineCache, 0, sizeof(pipelineCache));
1752 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753
Karl Schultze4dc75c2016-02-02 15:37:51 -07001754 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1755 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001756 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001757
Karl Schultze4dc75c2016-02-02 15:37:51 -07001758 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001759 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001760 pipeline.pRasterizationState = &rs;
1761 pipeline.pColorBlendState = &cb;
1762 pipeline.pMultisampleState = &ms;
1763 pipeline.pViewportState = &vp;
1764 pipeline.pDepthStencilState = &ds;
1765 pipeline.pStages = shaderStages;
1766 pipeline.renderPass = demo->render_pass;
1767 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001768
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001769 pipeline.renderPass = demo->render_pass;
1770
Karl Schultze4dc75c2016-02-02 15:37:51 -07001771 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1772 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001773 assert(!err);
1774
Chia-I Wu63636062015-10-26 21:10:41 +08001775 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1776 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777}
1778
Karl Schultze4dc75c2016-02-02 15:37:51 -07001779static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001780 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001781 [0] =
1782 {
1783 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1784 .descriptorCount = 1,
1785 },
1786 [1] =
1787 {
1788 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1789 .descriptorCount = DEMO_TEXTURE_COUNT,
1790 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001791 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001792 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001793 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001794 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001795 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001796 .poolSizeCount = 2,
1797 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001798 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001799 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001800
Karl Schultze4dc75c2016-02-02 15:37:51 -07001801 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1802 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001803 assert(!err);
1804}
1805
Karl Schultze4dc75c2016-02-02 15:37:51 -07001806static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001807 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001808 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001809 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001810 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001811
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001812 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001813 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001814 .pNext = NULL,
1815 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001816 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001817 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001818 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001819 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001820
Chia-I Wuae721ba2015-05-25 16:27:55 +08001821 memset(&tex_descs, 0, sizeof(tex_descs));
1822 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001823 tex_descs[i].sampler = demo->textures[i].sampler;
1824 tex_descs[i].imageView = demo->textures[i].view;
1825 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001826 }
1827
1828 memset(&writes, 0, sizeof(writes));
1829
1830 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001831 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001832 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001833 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001834 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001835
1836 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001837 writes[1].dstSet = demo->desc_set;
1838 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001839 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001840 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001841 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001842
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001843 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001844}
1845
Karl Schultze4dc75c2016-02-02 15:37:51 -07001846static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001847 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001848 attachments[1] = demo->depth.view;
1849
Chia-I Wuf973e322015-07-08 13:34:24 +08001850 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001851 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1852 .pNext = NULL,
1853 .renderPass = demo->render_pass,
1854 .attachmentCount = 2,
1855 .pAttachments = attachments,
1856 .width = demo->width,
1857 .height = demo->height,
1858 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001859 };
1860 VkResult U_ASSERT_ONLY err;
1861 uint32_t i;
1862
Karl Schultze4dc75c2016-02-02 15:37:51 -07001863 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1864 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001865 assert(demo->framebuffers);
1866
1867 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001868 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001869 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1870 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001871 assert(!err);
1872 }
1873}
1874
Karl Schultze4dc75c2016-02-02 15:37:51 -07001875static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001876 VkResult U_ASSERT_ONLY err;
1877
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001878 const VkCommandPoolCreateInfo cmd_pool_info = {
1879 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001880 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001881 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001882 .flags = 0,
1883 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001884 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1885 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001886 assert(!err);
1887
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001888 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001889 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001890 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001891 .commandPool = demo->cmd_pool,
1892 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001893 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001894 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001895
1896 demo_prepare_buffers(demo);
1897 demo_prepare_depth(demo);
1898 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001899 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001900
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001901 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001902 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001903 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001904
Ian Elliotta0781972015-08-21 15:09:33 -06001905 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001906 err =
1907 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001908 assert(!err);
1909 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001910
Tony Barbour22e06272016-09-07 16:07:56 -06001911 if (demo->separate_present_queue) {
1912 const VkCommandPoolCreateInfo cmd_pool_info = {
1913 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1914 .pNext = NULL,
1915 .queueFamilyIndex = demo->present_queue_family_index,
1916 .flags = 0,
1917 };
1918 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1919 &demo->present_cmd_pool);
1920 assert(!err);
1921 const VkCommandBufferAllocateInfo cmd = {
1922 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1923 .pNext = NULL,
1924 .commandPool = demo->present_cmd_pool,
1925 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1926 .commandBufferCount = 1,
1927 };
1928 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1929 err = vkAllocateCommandBuffers(
1930 demo->device, &cmd, &demo->buffers[i].graphics_to_present_cmd);
1931 assert(!err);
1932 demo_build_image_ownership_cmd(demo, i);
1933 }
1934 }
1935
Chia-I Wu63ea9262015-03-26 13:14:16 +08001936 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001937 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001938
Chia-I Wuf973e322015-07-08 13:34:24 +08001939 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001940
Ian Elliotta0781972015-08-21 15:09:33 -06001941 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001942 demo->current_buffer = i;
1943 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1944 }
1945
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001946 /*
1947 * Prepare functions above may generate pipeline commands
1948 * that need to be flushed before beginning the render loop.
1949 */
1950 demo_flush_init_cmd(demo);
1951
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001952 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001953 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001954}
1955
Karl Schultze4dc75c2016-02-02 15:37:51 -07001956static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001957 uint32_t i;
1958
1959 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06001960 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001961
Tony Barbour66a56c32016-09-21 13:10:56 -06001962 // Wait for fences from present operations
1963 for (i = 0; i < FRAME_LAG; i++) {
1964 if (demo->fencesInited[i]) {
1965 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
1966 }
1967 vkDestroyFence(demo->device, demo->fences[i], NULL);
1968 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
1969 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
1970 if (demo->separate_present_queue) {
1971 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
1972 }
1973 }
1974
Tony Barbourd8e504f2015-10-08 14:26:24 -06001975 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001976 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001977 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001978 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001979 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001980
Chia-I Wu63636062015-10-26 21:10:41 +08001981 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1982 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1983 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1984 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1985 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001986
1987 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001988 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1989 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1990 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1991 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001992 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001993 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001994
Chia-I Wu63636062015-10-26 21:10:41 +08001995 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1996 vkDestroyImage(demo->device, demo->depth.image, NULL);
1997 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001998
Chia-I Wu63636062015-10-26 21:10:41 +08001999 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2000 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002001
Ian Elliotta0781972015-08-21 15:09:33 -06002002 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002003 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002004 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2005 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06002006 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002007 free(demo->buffers);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002008 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002009 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002010
Tony Barbour22e06272016-09-07 16:07:56 -06002011 if (demo->separate_present_queue) {
2012 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002013 }
Chia-I Wu63636062015-10-26 21:10:41 +08002014 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002015 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002016 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002017 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002018 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002019 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002020
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002021#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002022 if (demo->use_xlib) {
2023 XDestroyWindow(demo->display, demo->xlib_window);
2024 XCloseDisplay(demo->display);
2025 } else {
2026 xcb_destroy_window(demo->connection, demo->xcb_window);
2027 xcb_disconnect(demo->connection);
2028 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002029 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002030#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002031 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002032 xcb_disconnect(demo->connection);
2033 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002034#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2035 wl_shell_surface_destroy(demo->shell_surface);
2036 wl_surface_destroy(demo->window);
2037 wl_shell_destroy(demo->shell);
2038 wl_compositor_destroy(demo->compositor);
2039 wl_registry_destroy(demo->registry);
2040 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002041#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002042}
2043
Karl Schultze4dc75c2016-02-02 15:37:51 -07002044static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002045 uint32_t i;
2046
Mike Stroyanbb60b382015-10-28 09:46:57 -06002047 // Don't react to resize until after first initialization.
2048 if (!demo->prepared) {
2049 return;
2050 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002051 // In order to properly resize the window, we must re-create the swapchain
2052 // AND redo the command buffers, etc.
2053 //
2054 // First, perform part of the demo_cleanup() function:
2055 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002056 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002057
2058 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002059 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002060 }
2061 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08002062 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002063
Chia-I Wu63636062015-10-26 21:10:41 +08002064 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2065 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2066 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2067 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2068 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002069
2070 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002071 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2072 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2073 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2074 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002075 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002076
Chia-I Wu63636062015-10-26 21:10:41 +08002077 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2078 vkDestroyImage(demo->device, demo->depth.image, NULL);
2079 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002080
Chia-I Wu63636062015-10-26 21:10:41 +08002081 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
2082 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002083
2084 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002085 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002086 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2087 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002088 }
Chia-I Wu63636062015-10-26 21:10:41 +08002089 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002090 if (demo->separate_present_queue) {
2091 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2092 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002093 free(demo->buffers);
2094
Ian Elliottcf074d32015-10-16 18:02:43 -06002095 // Second, re-perform the demo_prepare() function, which will re-create the
2096 // swapchain:
2097 demo_prepare(demo);
2098}
2099
David Pinedo2bb7c932015-06-18 17:03:14 -06002100// On MS-Windows, make this a global, so it's available to WndProc()
2101struct demo demo;
2102
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002103#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002104static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002105 if (!demo->prepared)
2106 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002107
Ian Elliott639ca472015-04-16 15:23:05 -06002108 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002109 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002110 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002111 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002112 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002113 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002114}
Ian Elliott639ca472015-04-16 15:23:05 -06002115
2116// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002117LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2118 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002119 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002120 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002121 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002122 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002123 // The validation callback calls MessageBox which can generate paint
2124 // events - don't make more Vulkan calls if we got here from the
2125 // callback
2126 if (!in_callback) {
2127 demo_run(&demo);
2128 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002129 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002130 case WM_GETMINMAXINFO: // set window's minimum size
2131 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2132 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002133 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002134 // Resize the application to the new window size, except when
2135 // it was minimized. Vulkan doesn't support images or swapchains
2136 // with width=0 and height=0.
2137 if (wParam != SIZE_MINIMIZED) {
2138 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002139 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002140 demo_resize(&demo);
2141 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002142 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002143 default:
2144 break;
2145 }
2146 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2147}
2148
Karl Schultze4dc75c2016-02-02 15:37:51 -07002149static void demo_create_window(struct demo *demo) {
2150 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002151
2152 // Initialize the window class structure:
2153 win_class.cbSize = sizeof(WNDCLASSEX);
2154 win_class.style = CS_HREDRAW | CS_VREDRAW;
2155 win_class.lpfnWndProc = WndProc;
2156 win_class.cbClsExtra = 0;
2157 win_class.cbWndExtra = 0;
2158 win_class.hInstance = demo->connection; // hInstance
2159 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2160 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2161 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2162 win_class.lpszMenuName = NULL;
2163 win_class.lpszClassName = demo->name;
2164 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2165 // Register window class:
2166 if (!RegisterClassEx(&win_class)) {
2167 // It didn't work, so try to give a useful error:
2168 printf("Unexpected error trying to start the application!\n");
2169 fflush(stdout);
2170 exit(1);
2171 }
2172 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002173 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002174 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002175 demo->window = CreateWindowEx(0,
2176 demo->name, // class name
2177 demo->name, // app name
2178 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002179 WS_VISIBLE | WS_SYSMENU,
2180 100, 100, // x/y coords
2181 wr.right - wr.left, // width
2182 wr.bottom - wr.top, // height
2183 NULL, // handle to parent
2184 NULL, // handle to menu
2185 demo->connection, // hInstance
2186 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002187 if (!demo->window) {
2188 // It didn't work, so try to give a useful error:
2189 printf("Cannot create a window in which to draw!\n");
2190 fflush(stdout);
2191 exit(1);
2192 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002193 // Window client area size must be at least 1 pixel high, to prevent crash.
2194 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2195 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002196}
Tony Barbour8295ac42016-08-08 11:04:17 -06002197#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002198static void demo_create_xlib_window(struct demo *demo) {
2199
2200 demo->display = XOpenDisplay(NULL);
2201 long visualMask = VisualScreenMask;
2202 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002203 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002204 vInfoTemplate.screen = DefaultScreen(demo->display);
2205 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2206 &vInfoTemplate, &numberOfVisuals);
2207
2208 Colormap colormap = XCreateColormap(
2209 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2210 visualInfo->visual, AllocNone);
2211
Rene Lindsay11612722016-06-13 17:20:39 -06002212 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002213 windowAttributes.colormap = colormap;
2214 windowAttributes.background_pixel = 0xFFFFFFFF;
2215 windowAttributes.border_pixel = 0;
2216 windowAttributes.event_mask =
2217 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2218
2219 demo->xlib_window = XCreateWindow(
2220 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2221 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2222 visualInfo->visual,
2223 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2224
2225 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2226 XMapWindow(demo->display, demo->xlib_window);
2227 XFlush(demo->display);
2228 demo->xlib_wm_delete_window =
2229 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2230}
2231static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2232 switch(event->type) {
2233 case ClientMessage:
2234 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2235 demo->quit = true;
2236 break;
2237 case KeyPress:
2238 switch (event->xkey.keycode) {
2239 case 0x9: // Escape
2240 demo->quit = true;
2241 break;
2242 case 0x71: // left arrow key
2243 demo->spin_angle += demo->spin_increment;
2244 break;
2245 case 0x72: // right arrow key
2246 demo->spin_angle -= demo->spin_increment;
2247 break;
2248 case 0x41:
2249 demo->pause = !demo->pause;
2250 break;
2251 }
2252 break;
2253 case ConfigureNotify:
2254 if ((demo->width != event->xconfigure.width) ||
2255 (demo->height != event->xconfigure.height)) {
2256 demo->width = event->xconfigure.width;
2257 demo->height = event->xconfigure.height;
2258 demo_resize(demo);
2259 }
2260 break;
2261 default:
2262 break;
2263 }
2264
2265}
2266
2267static void demo_run_xlib(struct demo *demo) {
2268
2269 while (!demo->quit) {
2270 XEvent event;
2271
2272 if (demo->pause) {
2273 XNextEvent(demo->display, &event);
2274 demo_handle_xlib_event(demo, &event);
2275 } else {
2276 while (XPending(demo->display) > 0) {
2277 XNextEvent(demo->display, &event);
2278 demo_handle_xlib_event(demo, &event);
2279 }
2280 }
2281
Tony Barbour2593b182016-04-19 10:57:58 -06002282 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002283 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002284 demo->curFrame++;
2285 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2286 demo->quit = true;
2287 }
2288}
Tony Barbour8295ac42016-08-08 11:04:17 -06002289#endif // VK_USE_PLATFORM_XLIB_KHR
2290#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002291static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002292 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002293 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002294 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002295 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002296 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002297 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002298 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002299 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2300 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002301 demo->quit = true;
2302 }
2303 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002304 case XCB_KEY_RELEASE: {
2305 const xcb_key_release_event_t *key =
2306 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002307
Karl Schultze4dc75c2016-02-02 15:37:51 -07002308 switch (key->detail) {
2309 case 0x9: // Escape
2310 demo->quit = true;
2311 break;
2312 case 0x71: // left arrow key
2313 demo->spin_angle += demo->spin_increment;
2314 break;
2315 case 0x72: // right arrow key
2316 demo->spin_angle -= demo->spin_increment;
2317 break;
2318 case 0x41:
2319 demo->pause = !demo->pause;
2320 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002321 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002322 } break;
2323 case XCB_CONFIGURE_NOTIFY: {
2324 const xcb_configure_notify_event_t *cfg =
2325 (const xcb_configure_notify_event_t *)event;
2326 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002327 demo->width = cfg->width;
2328 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002329 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002330 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002331 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002332 default:
2333 break;
2334 }
2335}
2336
Tony Barbour2593b182016-04-19 10:57:58 -06002337static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002338 xcb_flush(demo->connection);
2339
2340 while (!demo->quit) {
2341 xcb_generic_event_t *event;
2342
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002343 if (demo->pause) {
2344 event = xcb_wait_for_event(demo->connection);
2345 } else {
2346 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002347 while(event) {
2348 demo_handle_xcb_event(demo, event);
2349 free(event);
2350 event = xcb_poll_for_event(demo->connection);
2351 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002352 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002353
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002354 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002355 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002356 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002357 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002358 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002359 }
2360}
2361
Tony Barbour2593b182016-04-19 10:57:58 -06002362static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002363 uint32_t value_mask, value_list[32];
2364
Tony Barbour2593b182016-04-19 10:57:58 -06002365 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002366
2367 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2368 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002369 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002370 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002371
Tony Barbour2593b182016-04-19 10:57:58 -06002372 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002373 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2374 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2375 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002376
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002377 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002378 xcb_intern_atom_cookie_t cookie =
2379 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2380 xcb_intern_atom_reply_t *reply =
2381 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002382
Karl Schultze4dc75c2016-02-02 15:37:51 -07002383 xcb_intern_atom_cookie_t cookie2 =
2384 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2385 demo->atom_wm_delete_window =
2386 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002387
Tony Barbour2593b182016-04-19 10:57:58 -06002388 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002389 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002390 &(*demo->atom_wm_delete_window).atom);
2391 free(reply);
2392
Tony Barbour2593b182016-04-19 10:57:58 -06002393 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002394
Karl Schultze4dc75c2016-02-02 15:37:51 -07002395 // Force the x/y coordinates to 100,100 results are identical in consecutive
2396 // runs
2397 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002398 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002399 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002400}
Tony Barbour6b131662016-08-25 13:14:45 -06002401// VK_USE_PLATFORM_XCB_KHR
2402#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002403static void demo_run(struct demo *demo) {
2404 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002405 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002406 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002407 demo->curFrame++;
2408 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2409 demo->quit = true;
2410 }
2411}
2412
2413static void handle_ping(void *data UNUSED,
2414 struct wl_shell_surface *shell_surface,
2415 uint32_t serial) {
2416 wl_shell_surface_pong(shell_surface, serial);
2417}
2418
2419static void handle_configure(void *data UNUSED,
2420 struct wl_shell_surface *shell_surface UNUSED,
2421 uint32_t edges UNUSED, int32_t width UNUSED,
2422 int32_t height UNUSED) {}
2423
2424static void handle_popup_done(void *data UNUSED,
2425 struct wl_shell_surface *shell_surface UNUSED) {}
2426
2427static const struct wl_shell_surface_listener shell_surface_listener = {
2428 handle_ping, handle_configure, handle_popup_done};
2429
2430static void demo_create_window(struct demo *demo) {
2431 demo->window = wl_compositor_create_surface(demo->compositor);
2432 if (!demo->window) {
2433 printf("Can not create wayland_surface from compositor!\n");
2434 fflush(stdout);
2435 exit(1);
2436 }
2437
2438 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2439 if (!demo->shell_surface) {
2440 printf("Can not get shell_surface from wayland_surface!\n");
2441 fflush(stdout);
2442 exit(1);
2443 }
2444 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2445 demo);
2446 wl_shell_surface_set_toplevel(demo->shell_surface);
2447 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2448}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002449#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2450static void demo_run(struct demo *demo) {
2451 if (!demo->prepared)
2452 return;
2453
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002454 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002455 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002456 demo->curFrame++;
2457}
2458#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002459
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002460/*
2461 * Return 1 (true) if all layer names specified in check_names
2462 * can be found in given layer properties.
2463 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002464static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002465 uint32_t layer_count,
2466 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002467 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002468 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002469 for (uint32_t j = 0; j < layer_count; j++) {
2470 if (!strcmp(check_names[i], layers[j].layerName)) {
2471 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002472 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002473 }
2474 }
2475 if (!found) {
2476 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2477 return 0;
2478 }
2479 }
2480 return 1;
2481}
2482
Karl Schultze4dc75c2016-02-02 15:37:51 -07002483static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002484 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002485 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002486 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002487 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002488 char **instance_validation_layers = NULL;
2489 demo->enabled_extension_count = 0;
2490 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002491
Karl Schultz7c50ad62016-04-01 12:07:03 -06002492 char *instance_validation_layers_alt1[] = {
2493 "VK_LAYER_LUNARG_standard_validation"
2494 };
2495
2496 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002497 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2498 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2499 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2500 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002501 };
2502
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002503 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002504 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002505 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002506
Karl Schultz7c50ad62016-04-01 12:07:03 -06002507 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002508 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002509
Karl Schultz7c50ad62016-04-01 12:07:03 -06002510 instance_validation_layers = instance_validation_layers_alt1;
2511 if (instance_layer_count > 0) {
2512 VkLayerProperties *instance_layers =
2513 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2514 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2515 instance_layers);
2516 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002517
Karl Schultz7c50ad62016-04-01 12:07:03 -06002518
2519 validation_found = demo_check_layers(
2520 ARRAY_SIZE(instance_validation_layers_alt1),
2521 instance_validation_layers, instance_layer_count,
2522 instance_layers);
2523 if (validation_found) {
2524 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002525 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2526 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002527 } else {
2528 // use alternative set of validation layers
2529 instance_validation_layers = instance_validation_layers_alt2;
2530 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2531 validation_found = demo_check_layers(
2532 ARRAY_SIZE(instance_validation_layers_alt2),
2533 instance_validation_layers, instance_layer_count,
2534 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002535 validation_layer_count =
2536 ARRAY_SIZE(instance_validation_layers_alt2);
2537 for (uint32_t i = 0; i < validation_layer_count; i++) {
2538 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002539 }
2540 }
2541 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002542 }
Dustin Graves7c287352016-01-27 13:48:06 -07002543
Karl Schultz7c50ad62016-04-01 12:07:03 -06002544 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002545 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002546 "required validation layer.\n\n"
2547 "Please look at the Getting Started guide for additional "
2548 "information.\n",
2549 "vkCreateInstance Failure");
2550 }
Dustin Graves7c287352016-01-27 13:48:06 -07002551 }
2552
2553 /* Look for instance extensions */
2554 VkBool32 surfaceExtFound = 0;
2555 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002556#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002557 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002558#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002559 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002560
Karl Schultze4dc75c2016-02-02 15:37:51 -07002561 err = vkEnumerateInstanceExtensionProperties(
2562 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002563 assert(!err);
2564
Dustin Graves7c287352016-01-27 13:48:06 -07002565 if (instance_extension_count > 0) {
2566 VkExtensionProperties *instance_extensions =
2567 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2568 err = vkEnumerateInstanceExtensionProperties(
2569 NULL, &instance_extension_count, instance_extensions);
2570 assert(!err);
2571 for (uint32_t i = 0; i < instance_extension_count; i++) {
2572 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2573 instance_extensions[i].extensionName)) {
2574 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002575 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002576 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002577 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002578#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002579 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2580 instance_extensions[i].extensionName)) {
2581 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002582 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002583 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2584 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002585#endif
2586#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002587 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2588 instance_extensions[i].extensionName)) {
2589 platformSurfaceExtFound = 1;
2590 xlibSurfaceExtFound = 1;
2591 demo->extension_names[demo->enabled_extension_count++] =
2592 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2593 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002594#endif
2595#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002596 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2597 instance_extensions[i].extensionName)) {
2598 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002599 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002600 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2601 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002602#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002603#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2604 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2605 instance_extensions[i].extensionName)) {
2606 platformSurfaceExtFound = 1;
2607 demo->extension_names[demo->enabled_extension_count++] =
2608 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2609 }
2610#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002611#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002612 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2613 instance_extensions[i].extensionName)) {
2614 platformSurfaceExtFound = 1;
2615 demo->extension_names[demo->enabled_extension_count++] =
2616 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2617 }
2618#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002619 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2620 instance_extensions[i].extensionName)) {
2621 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002622 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002623 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2624 }
2625 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002626 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002627 }
Dustin Graves7c287352016-01-27 13:48:06 -07002628
2629 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002630 }
Dustin Graves7c287352016-01-27 13:48:06 -07002631
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002632 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002633 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2634 "the " VK_KHR_SURFACE_EXTENSION_NAME
2635 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002636 "Vulkan installable client driver (ICD) installed?\nPlease "
2637 "look at the Getting Started guide for additional "
2638 "information.\n",
2639 "vkCreateInstance Failure");
2640 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002641 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002642#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002643 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2644 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2645 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002646 "Vulkan installable client driver (ICD) installed?\nPlease "
2647 "look at the Getting Started guide for additional "
2648 "information.\n",
2649 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002650#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002651 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2652 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2653 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002654 "Vulkan installable client driver (ICD) installed?\nPlease "
2655 "look at the Getting Started guide for additional "
2656 "information.\n",
2657 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002658#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2659 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2660 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2661 " extension.\n\nDo you have a compatible "
2662 "Vulkan installable client driver (ICD) installed?\nPlease "
2663 "look at the Getting Started guide for additional "
2664 "information.\n",
2665 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002666#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2667 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2668 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2669 " extension.\n\nDo you have a compatible "
2670 "Vulkan installable client driver (ICD) installed?\nPlease "
2671 "look at the Getting Started guide for additional "
2672 "information.\n",
2673 "vkCreateInstance Failure");
2674#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002675 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002676#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002677 if (demo->use_xlib && !xlibSurfaceExtFound) {
2678 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2679 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2680 " extension.\n\nDo you have a compatible "
2681 "Vulkan installable client driver (ICD) installed?\nPlease "
2682 "look at the Getting Started guide for additional "
2683 "information.\n",
2684 "vkCreateInstance Failure");
2685 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002686#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002687 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002688 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002689 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002690 .pApplicationName = APP_SHORT_NAME,
2691 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002692 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002693 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002694 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002695 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002696 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002697 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002698 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002699 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002700 .enabledLayerCount = demo->enabled_layer_count,
2701 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2702 .enabledExtensionCount = demo->enabled_extension_count,
2703 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002704 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002705
2706 /*
2707 * This is info for a temp callback to use during CreateInstance.
2708 * After the instance is created, we use the instance-based
2709 * function to register the final callback.
2710 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002711 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002712 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002713 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2714 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002715 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002716 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002717 dbgCreateInfo.flags =
2718 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2719 inst_info.pNext = &dbgCreateInfo;
2720 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002721
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002722 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002723
Chia-I Wu63636062015-10-26 21:10:41 +08002724 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002725 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2726 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002727 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002728 "additional information.\n",
2729 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002730 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002731 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002732 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002733 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002734 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002735 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2736 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002737 "the Getting Started guide for additional information.\n",
2738 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002739 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002740
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002741 /* Make initial call to query gpu_count, then second call for gpu info*/
2742 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2743 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002744
2745 if (gpu_count > 0) {
2746 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2747 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2748 assert(!err);
2749 /* For cube demo we just grab the first physical device */
2750 demo->gpu = physical_devices[0];
2751 free(physical_devices);
2752 } else {
2753 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2754 "Do you have a compatible Vulkan installable client driver (ICD) "
2755 "installed?\nPlease look at the Getting Started guide for "
2756 "additional information.\n",
2757 "vkEnumeratePhysicalDevices Failure");
2758 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002759
Dustin Graves7c287352016-01-27 13:48:06 -07002760 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002761 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002762 VkBool32 swapchainExtFound = 0;
2763 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002764 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002765
Karl Schultze4dc75c2016-02-02 15:37:51 -07002766 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2767 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002768 assert(!err);
2769
Dustin Graves7c287352016-01-27 13:48:06 -07002770 if (device_extension_count > 0) {
2771 VkExtensionProperties *device_extensions =
2772 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2773 err = vkEnumerateDeviceExtensionProperties(
2774 demo->gpu, NULL, &device_extension_count, device_extensions);
2775 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002776
Dustin Graves7c287352016-01-27 13:48:06 -07002777 for (uint32_t i = 0; i < device_extension_count; i++) {
2778 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2779 device_extensions[i].extensionName)) {
2780 swapchainExtFound = 1;
2781 demo->extension_names[demo->enabled_extension_count++] =
2782 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2783 }
2784 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002785 }
Dustin Graves7c287352016-01-27 13:48:06 -07002786
2787 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002788 }
Dustin Graves7c287352016-01-27 13:48:06 -07002789
Tony Barbourcc69f452015-10-08 13:59:42 -06002790 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002791 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2792 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2793 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002794 "Vulkan installable client driver (ICD) installed?\nPlease "
2795 "look at the Getting Started guide for additional "
2796 "information.\n",
2797 "vkCreateInstance Failure");
2798 }
2799
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002800 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002801 demo->CreateDebugReportCallback =
2802 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2803 demo->inst, "vkCreateDebugReportCallbackEXT");
2804 demo->DestroyDebugReportCallback =
2805 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2806 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002807 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002808 ERR_EXIT(
2809 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2810 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002811 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002812 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002813 ERR_EXIT(
2814 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2815 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002816 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002817 demo->DebugReportMessage =
2818 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2819 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002820 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002821 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002822 "vkGetProcAddr Failure");
2823 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002824
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002825 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002826 PFN_vkDebugReportCallbackEXT callback;
2827 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002828 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002829 dbgCreateInfo.pNext = NULL;
2830 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002831 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002832 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002833 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002834 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2835 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002836 switch (err) {
2837 case VK_SUCCESS:
2838 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002839 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002840 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2841 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002842 break;
2843 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002844 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2845 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002846 break;
2847 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002848 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002849 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002850
2851 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002852 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2853 &demo->queue_family_count, NULL);
2854 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002855
Karl Schultze4dc75c2016-02-02 15:37:51 -07002856 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002857 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2858 vkGetPhysicalDeviceQueueFamilyProperties(
2859 demo->gpu, &demo->queue_family_count, demo->queue_props);
2860
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002861 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002862 // If app has specific feature requirements it should check supported
2863 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002864 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002865 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002866
Tony Barbourda2bad52016-01-22 14:36:40 -07002867 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2868 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2869 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2870 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2871 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2872}
2873
Karl Schultze4dc75c2016-02-02 15:37:51 -07002874static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002875 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002876 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06002877 VkDeviceQueueCreateInfo queues[2];
2878 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2879 queues[0].pNext = NULL;
2880 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
2881 queues[0].queueCount = 1;
2882 queues[0].pQueuePriorities = queue_priorities;
2883 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002884
2885 VkDeviceCreateInfo device = {
2886 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2887 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002888 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06002889 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002890 .enabledLayerCount = 0,
2891 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002892 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002893 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2894 .pEnabledFeatures =
2895 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002896 };
Tony Barbour22e06272016-09-07 16:07:56 -06002897 if (demo->separate_present_queue) {
2898 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2899 queues[1].pNext = NULL;
2900 queues[1].queueFamilyIndex = demo->present_queue_family_index;
2901 queues[1].queueCount = 1;
2902 queues[1].pQueuePriorities = queue_priorities;
2903 queues[1].flags = 0;
2904 device.queueCreateInfoCount = 2;
2905 }
Chia-I Wu63636062015-10-26 21:10:41 +08002906 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002907 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002908}
2909
Karl Schultze4dc75c2016-02-02 15:37:51 -07002910static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002911 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002912 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002913
Karl Schultze4dc75c2016-02-02 15:37:51 -07002914// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002915#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002916 VkWin32SurfaceCreateInfoKHR createInfo;
2917 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2918 createInfo.pNext = NULL;
2919 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002920 createInfo.hinstance = demo->connection;
2921 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002922
Karl Schultze4dc75c2016-02-02 15:37:51 -07002923 err =
2924 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002925#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2926 VkWaylandSurfaceCreateInfoKHR createInfo;
2927 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2928 createInfo.pNext = NULL;
2929 createInfo.flags = 0;
2930 createInfo.display = demo->display;
2931 createInfo.surface = demo->window;
2932
2933 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2934 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002935#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2936 VkAndroidSurfaceCreateInfoKHR createInfo;
2937 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2938 createInfo.pNext = NULL;
2939 createInfo.flags = 0;
2940 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002941
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002942 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2943#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002944 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002945#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002946 VkXlibSurfaceCreateInfoKHR createInfo;
2947 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2948 createInfo.pNext = NULL;
2949 createInfo.flags = 0;
2950 createInfo.dpy = demo->display;
2951 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002952
Tony Barbour2593b182016-04-19 10:57:58 -06002953 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2954 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002955#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002956 }
2957 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002958#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002959 VkXcbSurfaceCreateInfoKHR createInfo;
2960 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2961 createInfo.pNext = NULL;
2962 createInfo.flags = 0;
2963 createInfo.connection = demo->connection;
2964 createInfo.window = demo->xcb_window;
2965
2966 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002967#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002968 }
Tony Barbour2593b182016-04-19 10:57:58 -06002969 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002970
Tony Barbourcc69f452015-10-08 13:59:42 -06002971 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002972 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06002973 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
2974 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002975 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002976 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002977 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002978
2979 // Search for a graphics and a present queue in the array of queue
2980 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06002981 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
2982 uint32_t presentQueueFamilyIndex = UINT32_MAX;
2983 for (i = 0; i < demo->queue_family_count; i++) {
2984 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2985 if (graphicsQueueFamilyIndex == UINT32_MAX) {
2986 graphicsQueueFamilyIndex = i;
2987 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002988
Tony Barbourab2a0362016-09-06 11:40:12 -06002989 if (supportsPresent[i] == VK_TRUE) {
2990 graphicsQueueFamilyIndex = i;
2991 presentQueueFamilyIndex = i;
2992 break;
2993 }
2994 }
2995 }
2996
2997 if (presentQueueFamilyIndex == UINT32_MAX) {
2998 // If didn't find a queue that supports both graphics and present, then
2999 // find a separate present queue.
3000 for (size_t i = 0; i < demo->queue_family_count; ++i) {
3001 if (supportsPresent[i] == VK_TRUE) {
3002 presentQueueFamilyIndex = i;
3003 break;
3004 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003005 }
3006 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003007
3008 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003009 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3010 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003011 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003012 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003013 }
3014
Tony Barbourab2a0362016-09-06 11:40:12 -06003015 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3016 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003017 demo->separate_present_queue =
3018 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003019 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003020
Tony Barbourda2bad52016-01-22 14:36:40 -07003021 demo_create_device(demo);
3022
3023 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3024 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3025 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3026 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3027 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3028
Tony Barboura2a67812016-07-18 13:21:06 -06003029 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3030 &demo->graphics_queue);
3031
Tony Barbour22e06272016-09-07 16:07:56 -06003032 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003033 demo->present_queue = demo->graphics_queue;
3034 } else {
3035 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3036 &demo->present_queue);
3037 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003038
Ian Elliottaaae5352015-07-06 14:27:58 -06003039 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003040 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003041 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003042 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003043 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003044 VkSurfaceFormatKHR *surfFormats =
3045 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003046 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003047 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003048 assert(!err);
3049 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3050 // the surface has no preferred format. Otherwise, at least one
3051 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003052 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003053 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003054 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003055 assert(formatCount >= 1);
3056 demo->format = surfFormats[0].format;
3057 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003058 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003059
David Pinedo2bb7c932015-06-18 17:03:14 -06003060 demo->quit = false;
3061 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003062
Tony Barbource7524e2016-07-28 12:01:45 -06003063 // Create semaphores to synchronize acquiring presentable buffers before
3064 // rendering and waiting for drawing to be complete before presenting
3065 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3066 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3067 .pNext = NULL,
3068 .flags = 0,
3069 };
Tony Barbource7524e2016-07-28 12:01:45 -06003070
Tony Barbour66a56c32016-09-21 13:10:56 -06003071 // Create fences that we can use to throttle if we get too far
3072 // ahead of the image presents
3073 VkFenceCreateInfo fence_ci = {
3074 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3075 .pNext = NULL,
3076 .flags = 0
3077 };
3078 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3079 vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3080 demo->fencesInited[i] = false;
Tony Barbour22e06272016-09-07 16:07:56 -06003081 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003082 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003083 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003084
3085 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3086 &demo->draw_complete_semaphores[i]);
3087 assert(!err);
3088
3089 if (demo->separate_present_queue) {
3090 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3091 &demo->image_ownership_semaphores[i]);
3092 assert(!err);
3093 }
Tony Barbour22e06272016-09-07 16:07:56 -06003094 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003095 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003096
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003097 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003098 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003099}
3100
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003101#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3102static void registry_handle_global(void *data, struct wl_registry *registry,
3103 uint32_t name, const char *interface,
3104 uint32_t version UNUSED) {
3105 struct demo *demo = data;
3106 if (strcmp(interface, "wl_compositor") == 0) {
3107 demo->compositor =
3108 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3109 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3110 * tp xdg_shell */
3111 } else if (strcmp(interface, "wl_shell") == 0) {
3112 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3113 }
3114}
3115
3116static void registry_handle_global_remove(void *data UNUSED,
3117 struct wl_registry *registry UNUSED,
3118 uint32_t name UNUSED) {}
3119
3120static const struct wl_registry_listener registry_listener = {
3121 registry_handle_global, registry_handle_global_remove};
3122#endif
3123
Karl Schultze4dc75c2016-02-02 15:37:51 -07003124static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003125#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003126 const xcb_setup_t *setup;
3127 xcb_screen_iterator_t iter;
3128 int scr;
3129
3130 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003131 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003132 printf("Cannot find a compatible Vulkan installable client driver "
3133 "(ICD).\nExiting ...\n");
3134 fflush(stdout);
3135 exit(1);
3136 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003137
3138 setup = xcb_get_setup(demo->connection);
3139 iter = xcb_setup_roots_iterator(setup);
3140 while (scr-- > 0)
3141 xcb_screen_next(&iter);
3142
3143 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003144#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3145 demo->display = wl_display_connect(NULL);
3146
3147 if (demo->display == NULL) {
3148 printf("Cannot find a compatible Vulkan installable client driver "
3149 "(ICD).\nExiting ...\n");
3150 fflush(stdout);
3151 exit(1);
3152 }
3153
3154 demo->registry = wl_display_get_registry(demo->display);
3155 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3156 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003157#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003158}
3159
Karl Schultze4dc75c2016-02-02 15:37:51 -07003160static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003161 vec3 eye = {0.0f, 3.0f, 5.0f};
3162 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003163 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003164
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003165 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003166 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003167
Piers Daniell735ee532015-02-23 16:23:13 -07003168 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003169 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003170 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003171 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003172 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003173 if (strcmp(argv[i], "--break") == 0) {
3174 demo->use_break = true;
3175 continue;
3176 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003177 if (strcmp(argv[i], "--validate") == 0) {
3178 demo->validate = true;
3179 continue;
3180 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003181#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003182 if (strcmp(argv[i], "--xlib") == 0) {
3183 demo->use_xlib = true;
3184 continue;
3185 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003186#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003187 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3188 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3189 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003190 i++;
3191 continue;
3192 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003193 if (strcmp(argv[i], "--suppress_popups") == 0) {
3194 demo->suppress_popups = true;
3195 continue;
3196 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003197
Karl Schultze4dc75c2016-02-02 15:37:51 -07003198 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003199#if defined(VK_USE_PLATFORM_XLIB_KHR)
3200 "[--xlib] "
3201#endif
lenny-lunargfbe22392016-06-06 11:07:53 -06003202 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003203 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003204 fflush(stderr);
3205 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003206 }
3207
Tony Barbour2593b182016-04-19 10:57:58 -06003208 if (!demo->use_xlib)
3209 demo_init_connection(demo);
3210
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003211 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003212
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003213 demo->width = 500;
3214 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003215
Tony Barbour66a56c32016-09-21 13:10:56 -06003216 demo->spin_angle = 4.0f;
3217 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003218 demo->pause = false;
3219
Karl Schultze4dc75c2016-02-02 15:37:51 -07003220 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3221 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003222 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3223 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003224
3225 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003226}
3227
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003228#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003229// Include header required for parsing the command line options.
3230#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003231
Karl Schultze4dc75c2016-02-02 15:37:51 -07003232int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3233 int nCmdShow) {
3234 MSG msg; // message
3235 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003236 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003237 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003238
Karl Schultze4dc75c2016-02-02 15:37:51 -07003239 // Use the CommandLine functions to get the command line arguments.
3240 // Unfortunately, Microsoft outputs
3241 // this information as wide characters for Unicode, and we simply want the
3242 // Ascii version to be compatible
3243 // with the non-Windows side. So, we have to convert the information to
3244 // Ascii character strings.
3245 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3246 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003247 argc = 0;
3248 }
3249
Karl Schultze4dc75c2016-02-02 15:37:51 -07003250 if (argc > 0) {
3251 argv = (char **)malloc(sizeof(char *) * argc);
3252 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003253 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003254 } else {
3255 for (int iii = 0; iii < argc; iii++) {
3256 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003257 size_t numConverted = 0;
3258
Karl Schultze4dc75c2016-02-02 15:37:51 -07003259 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3260 if (argv[iii] != NULL) {
3261 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3262 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003263 }
3264 }
3265 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003266 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003267 argv = NULL;
3268 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003269
3270 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003271
3272 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003273 if (argc > 0 && argv != NULL) {
3274 for (int iii = 0; iii < argc; iii++) {
3275 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003276 free(argv[iii]);
3277 }
3278 }
3279 free(argv);
3280 }
3281
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003282 demo.connection = hInstance;
3283 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003284 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003285 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003286
3287 demo_prepare(&demo);
3288
Karl Schultze4dc75c2016-02-02 15:37:51 -07003289 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003290
3291 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003292 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003293 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003294 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003295 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003296 done = true; // if found, quit app
3297 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003298 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003299 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003300 DispatchMessage(&msg);
3301 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003302 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003303 }
3304
3305 demo_cleanup(&demo);
3306
Karl Schultze4dc75c2016-02-02 15:37:51 -07003307 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003308}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003309#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3310#include <android/log.h>
3311#include <android_native_app_glue.h>
3312static bool initialized = false;
3313static bool active = false;
3314struct demo demo;
3315
3316static int32_t processInput(struct android_app* app, AInputEvent* event) {
3317 return 0;
3318}
3319
3320static void processCommand(struct android_app* app, int32_t cmd) {
3321 switch(cmd) {
3322 case APP_CMD_INIT_WINDOW: {
3323 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003324 // We're getting a new window. If the app is starting up, we
3325 // need to initialize. If the app has already been
3326 // initialized, that means that we lost our previous window,
3327 // which means that we have a lot of work to do. At a minimum,
3328 // we need to destroy the swapchain and surface associated with
3329 // the old window, and create a new surface and swapchain.
3330 // However, since there are a lot of other objects/state that
3331 // is tied to the swapchain, it's easiest to simply cleanup and
3332 // start over (i.e. use a brute-force approach of re-starting
3333 // the app)
3334 if (demo.prepared) {
3335 demo_cleanup(&demo);
3336 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003337 demo_init(&demo, 0, NULL);
3338 demo.window = (void*)app->window;
3339 demo_init_vk_swapchain(&demo);
3340 demo_prepare(&demo);
3341 initialized = true;
3342 }
3343 break;
3344 }
3345 case APP_CMD_GAINED_FOCUS: {
3346 active = true;
3347 break;
3348 }
3349 case APP_CMD_LOST_FOCUS: {
3350 active = false;
3351 break;
3352 }
3353 }
3354}
3355
3356void android_main(struct android_app *app)
3357{
3358 app_dummy();
3359
Cody Northrop8707a6e2016-04-26 19:59:19 -06003360#ifdef ANDROID
3361 int vulkanSupport = InitVulkan();
3362 if (vulkanSupport == 0)
3363 return;
3364#endif
3365
Ian Elliottf05d5d12016-05-17 12:03:35 -06003366 demo.prepared = false;
3367
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003368 app->onAppCmd = processCommand;
3369 app->onInputEvent = processInput;
3370
3371 while(1) {
3372 int events;
3373 struct android_poll_source* source;
3374 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3375 if (source) {
3376 source->process(app, source);
3377 }
3378
3379 if (app->destroyRequested != 0) {
3380 demo_cleanup(&demo);
3381 return;
3382 }
3383 }
3384 if (initialized && active) {
3385 demo_run(&demo);
3386 }
3387 }
3388
3389}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003390#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003391int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003392 struct demo demo;
3393
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003394 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003395#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003396 if (demo.use_xlib)
3397 demo_create_xlib_window(&demo);
3398 else
3399 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003400#elif defined(VK_USE_PLATFORM_XCB_KHR)
3401 demo_create_xcb_window(&demo);
3402#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3403 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003404#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3405 demo_create_window(&demo);
3406#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003407
Tony Barbourcc69f452015-10-08 13:59:42 -06003408 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003409
3410 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003411
Tony Barbour8295ac42016-08-08 11:04:17 -06003412#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003413 if (demo.use_xlib)
3414 demo_run_xlib(&demo);
3415 else
3416 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003417#elif defined(VK_USE_PLATFORM_XCB_KHR)
3418 demo_run_xcb(&demo);
3419#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3420 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003421#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3422 demo_run(&demo);
3423#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003424
3425 demo_cleanup(&demo);
3426
Karl Schultz0218c002016-03-22 17:06:13 -06003427 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003428}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003429#endif