blob: 4b84ae64fae2469fc973a732adf66efdb7bdc6fe [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
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070040#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060041
Tony Barbourefd0c5a2016-12-07 14:45:12 -070042#if defined(VK_USE_PLATFORM_MIR_KHR)
43#warning "Cube does not have code for Mir at this time"
44#endif
45
Cody Northrop8707a6e2016-04-26 19:59:19 -060046#ifdef ANDROID
47#include "vulkan_wrapper.h"
48#else
David Pinedoc5193c12015-11-06 12:54:48 -070049#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060050#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070051
David Pinedo541526f2015-11-24 09:00:24 -070052#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060053#include "linmath.h"
54
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060055#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060056#define APP_SHORT_NAME "cube"
57#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060058
Tony Barbour66a56c32016-09-21 13:10:56 -060059// Allow a maximum of two outstanding presentation operations.
60#define FRAME_LAG 2
61
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060062#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
63
Tony Barbourfdc2d352015-04-22 09:02:32 -060064#if defined(NDEBUG) && defined(__GNUC__)
65#define U_ASSERT_ONLY __attribute__((unused))
66#else
67#define U_ASSERT_ONLY
68#endif
69
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090070#if defined(__GNUC__)
71#define UNUSED __attribute__((unused))
72#else
73#define UNUSED
74#endif
75
Ian Elliott07264132015-04-28 11:35:02 -060076#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060077bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070078#define ERR_EXIT(err_msg, err_class) \
79 do { \
80 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
81 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070082 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060083
Michael Lentinec6cde4b2016-04-18 13:20:45 -050084#elif defined __ANDROID__
85#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070086#define ERR_EXIT(err_msg, err_class) \
87 do { \
88 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
89 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -050090 } while (0)
91#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070092#define ERR_EXIT(err_msg, err_class) \
93 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -080094 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070095 fflush(stdout); \
96 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070097 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050098#endif
Ian Elliott07264132015-04-28 11:35:02 -060099
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700100#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
101 { \
102 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
103 if (demo->fp##entrypoint == NULL) { \
104 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
105 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700106 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600107
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600108static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
109
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700110#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
111 { \
112 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
113 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
114 if (demo->fp##entrypoint == NULL) { \
115 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
116 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700117 }
Ian Elliott673898b2015-06-22 15:07:49 -0600118
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600119/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700120 * structure to track all objects related to a texture.
121 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600122struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600123 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700124
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600125 VkImage image;
126 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600127
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800128 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500129 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600130 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700131 int32_t tex_width, tex_height;
132};
133
Karl Schultze4dc75c2016-02-02 15:37:51 -0700134static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600135
Karl Schultz0218c002016-03-22 17:06:13 -0600136static int validation_error = 0;
137
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600138struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600139 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700140 float mvp[4][4];
141 float position[12 * 3][4];
142 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600143};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600144
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600145//--------------------------------------------------------------------------------------
146// Mesh and VertexFormat Data
147//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700148// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600149static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800150 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600151 -1.0f,-1.0f, 1.0f,
152 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800153 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 -1.0f, 1.0f,-1.0f,
155 -1.0f,-1.0f,-1.0f,
156
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800157 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 1.0f, 1.0f,-1.0f,
159 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600162 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800164 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600166 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600169 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800171 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 -1.0f, 1.0f, 1.0f,
173 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f, 1.0f,-1.0f,
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,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800178 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 1.0f, 1.0f, 1.0f,
180 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 1.0f,-1.0f,-1.0f,
183 1.0f, 1.0f,-1.0f,
184
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600186 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600187 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 1.0f,-1.0f, 1.0f,
190 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600191};
192
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600193static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700194 0.0f, 1.0f, // -X side
195 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800196 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198 0.0f, 0.0f,
199 0.0f, 1.0f,
200
Rene Lindsay76867e82016-07-29 10:49:11 -0700201 1.0f, 1.0f, // -Z side
202 0.0f, 0.0f,
203 0.0f, 1.0f,
204 1.0f, 1.0f,
205 1.0f, 0.0f,
206 0.0f, 0.0f,
207
208 1.0f, 0.0f, // -Y side
209 1.0f, 1.0f,
210 0.0f, 1.0f,
211 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600212 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214
Rene Lindsay76867e82016-07-29 10:49:11 -0700215 1.0f, 0.0f, // +Y side
216 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800217 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600218 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700219 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221
Rene Lindsay76867e82016-07-29 10:49:11 -0700222 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700224 0.0f, 1.0f,
225 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700228
229 0.0f, 0.0f, // +Z side
230 0.0f, 1.0f,
231 1.0f, 0.0f,
232 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700234 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600235};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700236// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600237
Karl Schultze4dc75c2016-02-02 15:37:51 -0700238void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600239 int i;
240
241 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700242 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600243 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
244 }
245 printf("\n");
246 fflush(stdout);
247}
248
Karl Schultze4dc75c2016-02-02 15:37:51 -0700249void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600250 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700251 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600252 printf("\n");
253 fflush(stdout);
254}
255
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700256VKAPI_ATTR VkBool32 VKAPI_CALL BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
257 size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg,
258 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700259#ifndef WIN32
260 raise(SIGTRAP);
261#else
262 DebugBreak();
263#endif
264
265 return false;
266}
267
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600268typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600269 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800270 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600271 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600272 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700273 VkBuffer uniform_buffer;
274 VkDeviceMemory uniform_memory;
275 VkFramebuffer framebuffer;
276 VkDescriptorSet descriptor_set;
277 VkFence fence;
278} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600279
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600280struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500281#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600282#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700283 HINSTANCE connection; // hInstance - Windows Instance
284 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
285 HWND window; // hWnd - window handle
286 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700287#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700288 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600289 Window xlib_window;
290 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700291#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700292 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600293 xcb_connection_t *connection;
294 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600295 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800296 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900297#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
298 struct wl_display *display;
299 struct wl_registry *registry;
300 struct wl_compositor *compositor;
301 struct wl_surface *window;
302 struct wl_shell *shell;
303 struct wl_shell_surface *shell_surface;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700304#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500305#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700306 ANativeWindow *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500307#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700308 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600309 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700310 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600311 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600313 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600314 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600316 VkQueue graphics_queue;
317 VkQueue present_queue;
318 uint32_t graphics_queue_family_index;
319 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600320 VkSemaphore image_acquired_semaphores[FRAME_LAG];
321 VkSemaphore draw_complete_semaphores[FRAME_LAG];
322 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600323 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600324 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600325 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Tony Barbourda2bad52016-01-22 14:36:40 -0700327 uint32_t enabled_extension_count;
328 uint32_t enabled_layer_count;
329 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600330 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700331
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600333 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600334 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700336 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
337 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
338 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
339 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600340 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
341 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
342 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
343 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
344 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600345 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600346 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700347 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600348 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600349 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600350 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800352 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600353 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
355 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800359 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500360 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600361 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600362 } depth;
363
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600364 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600365 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700367 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500368 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600370 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800371 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600372 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600374 mat4x4 projection_matrix;
375 mat4x4 view_matrix;
376 mat4x4 model_matrix;
377
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600378 float spin_angle;
379 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600380 bool pause;
381
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600382 VkShaderModule vert_shader_module;
383 VkShaderModule frag_shader_module;
384
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800386
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600388 int32_t curFrame;
389 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600390 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600391 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600392 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700393 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
394 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
395 VkDebugReportCallbackEXT msg_callback;
396 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600397
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600398 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600399 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400};
401
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700402VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
403 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600404 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600405 char *message = (char *)malloc(strlen(pMsg) + 100);
406
407 assert(message);
408
Cody Northropaf28a532016-09-27 10:50:57 -0600409 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
410 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600411 validation_error = 1;
412 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600413 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
414 validation_error = 1;
415 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600416 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600417 validation_error = 1;
418 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
419 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
420 validation_error = 1;
421 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
422 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600423 validation_error = 1;
424 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600425 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600426 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600427 }
428
429#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600430
Tony Barbour602ca4f2016-09-12 14:02:43 -0600431 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600432 struct demo *demo = (struct demo*) pUserData;
433 if (!demo->suppress_popups)
434 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600435 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600436
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600437#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600438
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600439 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600440 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600441 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600442 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600443 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600444 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600445 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600446 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600447 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600448 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600449 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600450 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600451 }
Cody Northropaf28a532016-09-27 10:50:57 -0600452
lenny-lunargfbe22392016-06-06 11:07:53 -0600453#else
Cody Northropaf28a532016-09-27 10:50:57 -0600454
lenny-lunargfbe22392016-06-06 11:07:53 -0600455 printf("%s\n", message);
456 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600457
lenny-lunargfbe22392016-06-06 11:07:53 -0600458#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600459
lenny-lunargfbe22392016-06-06 11:07:53 -0600460 free(message);
461
Cody Northropaf28a532016-09-27 10:50:57 -0600462 //clang-format on
463
lenny-lunargfbe22392016-06-06 11:07:53 -0600464 /*
465 * false indicates that layer should not bail-out of an
466 * API call that had validation failures. This may mean that the
467 * app dies inside the driver due to invalid parameter(s).
468 * That's what would happen without validation layers, so we'll
469 * keep that behavior here.
470 */
471 return false;
472}
473
Ian Elliottcf074d32015-10-16 18:02:43 -0600474// Forward declaration:
475static void demo_resize(struct demo *demo);
476
Karl Schultze4dc75c2016-02-02 15:37:51 -0700477static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
478 VkFlags requirements_mask,
479 uint32_t *typeIndex) {
480 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600481 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700482 if ((typeBits & 1) == 1) {
483 // Type is available, does it match user properties?
484 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
485 requirements_mask) == requirements_mask) {
486 *typeIndex = i;
487 return true;
488 }
489 }
490 typeBits >>= 1;
491 }
492 // No memory types matched, return failure
493 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600494}
495
Karl Schultze4dc75c2016-02-02 15:37:51 -0700496static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600497 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498
Tony Barbource7524e2016-07-28 12:01:45 -0600499 // This function could get called twice if the texture uses a staging buffer
500 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 return;
503
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505 assert(!err);
506
Tony Barbource7524e2016-07-28 12:01:45 -0600507 VkFence fence;
508 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
509 .pNext = NULL,
510 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700511 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
512 assert(!err);
513
Karl Schultze4dc75c2016-02-02 15:37:51 -0700514 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700515 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
516 .pNext = NULL,
517 .waitSemaphoreCount = 0,
518 .pWaitSemaphores = NULL,
519 .pWaitDstStageMask = NULL,
520 .commandBufferCount = 1,
521 .pCommandBuffers = cmd_bufs,
522 .signalSemaphoreCount = 0,
523 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600524
Tony Barbource7524e2016-07-28 12:01:45 -0600525 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526 assert(!err);
527
Tony Barbource7524e2016-07-28 12:01:45 -0600528 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 assert(!err);
530
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600531 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600532 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600533 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600534}
535
Karl Schultze4dc75c2016-02-02 15:37:51 -0700536static void demo_set_image_layout(struct demo *demo, VkImage image,
537 VkImageAspectFlags aspectMask,
538 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700539 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600540 VkAccessFlagBits srcAccessMask,
541 VkPipelineStageFlags src_stages,
542 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600543 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600544
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600545 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600546 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700548 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800549 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550 .oldLayout = old_image_layout,
551 .newLayout = new_image_layout,
552 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700553 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600554
Tony Barboureb5077b2016-10-19 15:23:36 -0600555 switch (new_image_layout) {
556 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600558 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
559 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600560
Tony Barboureb5077b2016-10-19 15:23:36 -0600561 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700562 image_memory_barrier.dstAccessMask =
563 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600564 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700565
Tony Barboureb5077b2016-10-19 15:23:36 -0600566 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700567 image_memory_barrier.dstAccessMask =
568 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600569 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700570
Tony Barboureb5077b2016-10-19 15:23:36 -0600571 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700572 image_memory_barrier.dstAccessMask =
573 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600574 break;
575
576 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
577 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
578 break;
579
580 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
581 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
582 break;
583
584 default:
585 image_memory_barrier.dstAccessMask = 0;
586 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600587 }
588
Tony Barbourf165b5d2016-10-03 16:01:41 -0600589
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600590 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600591
Karl Schultze4dc75c2016-02-02 15:37:51 -0700592 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
593 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600594}
595
Karl Schultze4dc75c2016-02-02 15:37:51 -0700596static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700597 const VkCommandBufferBeginInfo cmd_buf_info = {
598 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
599 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600600 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700601 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700602 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800603 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700604 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
605 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800606 };
607 const VkRenderPassBeginInfo rp_begin = {
608 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
609 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800610 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700611 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800612 .renderArea.offset.x = 0,
613 .renderArea.offset.y = 0,
614 .renderArea.extent.width = demo->width,
615 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600616 .clearValueCount = 2,
617 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700618 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800619 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600620
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600621 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800623 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700624 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
625 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700626 demo->pipeline_layout, 0, 1,
627 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
628 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600629 VkViewport viewport;
630 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700631 viewport.height = (float)demo->height;
632 viewport.width = (float)demo->width;
633 viewport.minDepth = (float)0.0f;
634 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700635 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600636
637 VkRect2D scissor;
638 memset(&scissor, 0, sizeof(scissor));
639 scissor.extent.width = demo->width;
640 scissor.extent.height = demo->height;
641 scissor.offset.x = 0;
642 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700643 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600644 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600645 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600646 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800647 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600648
Tony Barbour22e06272016-09-07 16:07:56 -0600649 if (demo->separate_present_queue) {
650 // We have to transfer ownership from the graphics queue family to the
651 // present queue family to be able to present. Note that we don't have
652 // to transfer from present queue family back to graphics queue family at
653 // the start of the next frame because we don't care about the image's
654 // contents at that point.
655 VkImageMemoryBarrier image_ownership_barrier = {
656 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
657 .pNext = NULL,
658 .srcAccessMask = 0,
659 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
660 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
661 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
662 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
663 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700664 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600665 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
666
667 vkCmdPipelineBarrier(cmd_buf,
668 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600669 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600670 0, NULL, 0, NULL, 1, &image_ownership_barrier);
671 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600672 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600673 assert(!err);
674}
675
Tony Barbour22e06272016-09-07 16:07:56 -0600676void demo_build_image_ownership_cmd(struct demo *demo, int i) {
677 VkResult U_ASSERT_ONLY err;
678
679 const VkCommandBufferBeginInfo cmd_buf_info = {
680 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
681 .pNext = NULL,
682 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
683 .pInheritanceInfo = NULL,
684 };
Tony Barboura72d9492017-01-11 13:35:09 -0700685 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600686 &cmd_buf_info);
687 assert(!err);
688
689 VkImageMemoryBarrier image_ownership_barrier = {
690 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
691 .pNext = NULL,
692 .srcAccessMask = 0,
693 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
694 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
695 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
696 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
697 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700698 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600699 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
700
Tony Barboura72d9492017-01-11 13:35:09 -0700701 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600702 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
703 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
704 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700705 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600706 assert(!err);
707}
708
Karl Schultze4dc75c2016-02-02 15:37:51 -0700709void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600710 mat4x4 MVP, Model, VP;
711 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600712 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600713 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600714
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600715 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700717 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600718 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700719 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
720 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600721 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600722
Tony Barboura72d9492017-01-11 13:35:09 -0700723 vkWaitForFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence,
724 VK_TRUE, UINT64_MAX);
725
726 err = vkMapMemory(demo->device,
727 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
728 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600729 assert(!err);
730
Karl Schultze4dc75c2016-02-02 15:37:51 -0700731 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600732
Tony Barboura72d9492017-01-11 13:35:09 -0700733 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600734}
735
Karl Schultze4dc75c2016-02-02 15:37:51 -0700736static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600737 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600738
szdarkhackd322ff02016-10-08 09:51:22 +0300739 // Ensure no more than FRAME_LAG presentations are outstanding
740 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
741 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600742
Ian Elliottaaae5352015-07-06 14:27:58 -0600743 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700744 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -0700745 demo->image_acquired_semaphores[demo->frame_index],
746 demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600747 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600748
Tony Barboura72d9492017-01-11 13:35:09 -0700749 demo_update_data_buffer(demo);
750
Ian Elliottcf074d32015-10-16 18:02:43 -0600751 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
752 // demo->swapchain is out of date (e.g. the window was resized) and
753 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600754 demo->frame_index += 1;
755 demo->frame_index %= FRAME_LAG;
756
Ian Elliottcf074d32015-10-16 18:02:43 -0600757 demo_resize(demo);
758 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600759 return;
760 } else if (err == VK_SUBOPTIMAL_KHR) {
761 // demo->swapchain is not as optimal as it could be, but the platform's
762 // presentation engine will still present the image correctly.
763 } else {
764 assert(!err);
765 }
Tony Barbource7524e2016-07-28 12:01:45 -0600766 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600767 // that the image won't be rendered to until the presentation
768 // engine has fully released ownership to the application, and it is
769 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -0600770 VkPipelineStageFlags pipe_stage_flags;
771 VkSubmitInfo submit_info;
772 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
773 submit_info.pNext = NULL;
774 submit_info.pWaitDstStageMask = &pipe_stage_flags;
775 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
776 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600777 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600778 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -0700779 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600780 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600781 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -0700782 vkResetFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence);
783 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
784 demo->swapchain_image_resources[demo->current_buffer].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600785 assert(!err);
786
Tony Barbour22e06272016-09-07 16:07:56 -0600787 if (demo->separate_present_queue) {
788 // If we are using separate queues, change image ownership to the
789 // present queue before presenting, waiting for the draw complete
790 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -0700791 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600792 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
793 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600794 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600795 submit_info.commandBufferCount = 1;
796 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -0700797 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600798 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600799 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600800 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
801 assert(!err);
802 }
803
804 // If we are using separate queues we have to wait for image ownership,
805 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600806 VkPresentInfoKHR present = {
807 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600808 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600809 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600810 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600811 ? &demo->image_ownership_semaphores[demo->frame_index]
812 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600813 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700814 .pSwapchains = &demo->swapchain,
815 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600816 };
817
Tony Barboura2a67812016-07-18 13:21:06 -0600818 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600819 demo->frame_index += 1;
820 demo->frame_index %= FRAME_LAG;
821
Ian Elliottcf074d32015-10-16 18:02:43 -0600822 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
823 // demo->swapchain is out of date (e.g. the window was resized) and
824 // must be recreated:
825 demo_resize(demo);
826 } else if (err == VK_SUBOPTIMAL_KHR) {
827 // demo->swapchain is not as optimal as it could be, but the platform's
828 // presentation engine will still present the image correctly.
829 } else {
830 assert(!err);
831 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600832}
833
Karl Schultze4dc75c2016-02-02 15:37:51 -0700834static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600835 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600836 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600837
Ian Elliottb08e0d92015-11-20 11:55:46 -0700838 // Check the surface capabilities and formats
839 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700840 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
841 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600842 assert(!err);
843
Ian Elliott8528eff2015-08-07 15:56:59 -0600844 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700845 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
846 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600847 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600848 VkPresentModeKHR *presentModes =
849 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600850 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700851 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
852 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600853 assert(!err);
854
Ian Elliotta0781972015-08-21 15:09:33 -0600855 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600856 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
857 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
858 // If the surface size is undefined, the size is set to the size
859 // of the images requested, which must fit within the minimum and
860 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600861 swapchainExtent.width = demo->width;
862 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600863
864 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
865 swapchainExtent.width = surfCapabilities.minImageExtent.width;
866 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
867 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
868 }
869
870 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
871 swapchainExtent.height = surfCapabilities.minImageExtent.height;
872 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
873 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
874 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700875 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600876 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700877 swapchainExtent = surfCapabilities.currentExtent;
878 demo->width = surfCapabilities.currentExtent.width;
879 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 }
881
Tony Barbour66a56c32016-09-21 13:10:56 -0600882 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600883 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600884 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -0600885
Ian Elliottb18fdde2016-10-03 12:11:12 -0600886 // There are times when you may wish to use another present mode. The
887 // following code shows how to select them, and the comments provide some
888 // reasons you may wish to use them.
889 //
890 // It should be noted that Vulkan 1.0 doesn't provide a method for
891 // synchronizing rendering with the presentation engine's display. There
892 // is a method provided for throttling rendering with the display, but
893 // there are some presentation engines for which this method will not work.
894 // If an application doesn't throttle its rendering, and if it renders much
895 // faster than the refresh rate of the display, this can waste power on
896 // mobile devices. That is because power is being spent rendering images
897 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -0600898
Ian Elliottb18fdde2016-10-03 12:11:12 -0600899 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
900 // tearing, or have some way of synchronizing their rendering with the
901 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600902 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
903 // generally render a new presentable image every refresh cycle, but are
904 // occasionally early. In this case, the application wants the new image
905 // to be displayed instead of the previously-queued-for-presentation image
906 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600907 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
908 // render a new presentable image every refresh cycle, but are occasionally
909 // late. In this case (perhaps because of stuttering/latency concerns),
910 // the application wants the late image to be immediately displayed, even
911 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -0600912
913 if (demo->presentMode != swapchainPresentMode) {
914
915 for (size_t i = 0; i < presentModeCount; ++i) {
916 if (presentModes[i] == demo->presentMode) {
917 swapchainPresentMode = demo->presentMode;
918 break;
919 }
Ian Elliottb18fdde2016-10-03 12:11:12 -0600920 }
921 }
Tony Barbour291d57b2016-10-21 14:47:07 -0600922 if (swapchainPresentMode != demo->presentMode) {
923 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
924 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600925
Tony Barbour84376fe2017-01-06 15:54:22 -0700926 // Determine the number of VkImages to use in the swap chain.
927 // Application desires to acquire 3 images at a time for triple
928 // buffering
929 uint32_t desiredNumOfSwapchainImages = 3;
930 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
931 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
932 }
Ian Elliottcf7f7482016-08-19 03:46:58 -0600933 // If maxImageCount is 0, we can ask for as many images as we want;
934 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700935 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600936 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600937 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600938 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600939 }
940
Tony Barbour9fd6d352015-09-16 15:01:32 -0600941 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700942 if (surfCapabilities.supportedTransforms &
943 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700944 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600945 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700946 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600947 }
948
Tony Barboura2a67812016-07-18 13:21:06 -0600949 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600950 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800951 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700952 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600953 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800954 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600955 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700956 .imageExtent =
957 {
958 .width = swapchainExtent.width, .height = swapchainExtent.height,
959 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700960 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600961 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700962 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700963 .imageArrayLayers = 1,
964 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
965 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600966 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600967 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600968 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600969 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600970 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600971 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600972 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800974 assert(!err);
975
Ian Elliottcf074d32015-10-16 18:02:43 -0600976 // If we just re-created an existing swapchain, we should destroy the old
977 // swapchain at this point.
978 // Note: destroying the swapchain also cleans up all its associated
979 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800980 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700981 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600982 }
983
984 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600985 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600986 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800987
Karl Schultze4dc75c2016-02-02 15:37:51 -0700988 VkImage *swapchainImages =
989 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600990 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600991 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600992 &demo->swapchainImageCount,
993 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600994 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600995
Tony Barboura72d9492017-01-11 13:35:09 -0700996 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -0700997 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -0700998 assert(demo->swapchain_image_resources);
999
1000 VkFenceCreateInfo fence_ci = {
1001 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1002 .pNext = NULL,
1003 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1004 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001005
Ian Elliotta0781972015-08-21 15:09:33 -06001006 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001007 VkImageViewCreateInfo color_image_view = {
1008 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001009 .pNext = NULL,
1010 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001011 .components =
1012 {
1013 .r = VK_COMPONENT_SWIZZLE_R,
1014 .g = VK_COMPONENT_SWIZZLE_G,
1015 .b = VK_COMPONENT_SWIZZLE_B,
1016 .a = VK_COMPONENT_SWIZZLE_A,
1017 },
1018 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1019 .baseMipLevel = 0,
1020 .levelCount = 1,
1021 .baseArrayLayer = 0,
1022 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001023 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1024 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001025 };
1026
Tony Barboura72d9492017-01-11 13:35:09 -07001027 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028
Tony Barboura72d9492017-01-11 13:35:09 -07001029 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001030
Karl Schultze4dc75c2016-02-02 15:37:51 -07001031 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001032 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001033 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001034
Tony Barboura72d9492017-01-11 13:35:09 -07001035 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1036 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001037 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001038
Mark Young04fd3d82016-01-25 14:43:50 -07001039 if (NULL != presentModes) {
1040 free(presentModes);
1041 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001042}
1043
Karl Schultze4dc75c2016-02-02 15:37:51 -07001044static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001045 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001046 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001047 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001048 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001049 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001050 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001051 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001053 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001054 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001055 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001056 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 .flags = 0,
1058 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001059
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001060 VkImageViewCreateInfo view = {
1061 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001062 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001063 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001064 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001065 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1066 .baseMipLevel = 0,
1067 .levelCount = 1,
1068 .baseArrayLayer = 0,
1069 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001071 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001073
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001074 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001075 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001076 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001077
1078 demo->depth.format = depth_format;
1079
1080 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001081 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001082 assert(!err);
1083
Karl Schultze4dc75c2016-02-02 15:37:51 -07001084 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001085 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001086
Chia-I Wuf48700b2015-11-10 16:21:09 +08001087 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001088 demo->depth.mem_alloc.pNext = NULL;
1089 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1090 demo->depth.mem_alloc.memoryTypeIndex = 0;
1091
Karl Schultze4dc75c2016-02-02 15:37:51 -07001092 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1093 0, /* No requirements */
1094 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001095 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001096
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001097 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001098 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1099 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001100 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001101
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001103 err =
1104 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001105 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001106
1107 /* create image view */
1108 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001109 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001110 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111}
1112
Tony Barbour1a86d032015-09-21 15:17:33 -06001113/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001114bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001115 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001116#ifdef __ANDROID__
1117#include <lunarg.ppm.h>
1118 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001119 cPtr = (char*)lunarg_ppm;
1120 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001121 return false;
1122 }
1123 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001124 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001125 if (rgba_data == NULL) {
1126 return true;
1127 }
1128 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001129 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001130 return false;
1131 }
1132 while(strncmp(cPtr++, "\n", 1));
1133
1134 for (int y = 0; y < *height; y++) {
1135 uint8_t *rowPtr = rgba_data;
1136 for (int x = 0; x < *width; x++) {
1137 memcpy(rowPtr, cPtr, 3);
1138 rowPtr[3] = 255; /* Alpha of 1 */
1139 rowPtr += 4;
1140 cPtr += 3;
1141 }
1142 rgba_data += layout->rowPitch;
1143 }
1144
1145 return true;
1146#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001147 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001148 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001149
Tony Barbour1a86d032015-09-21 15:17:33 -06001150 if (!fPtr)
1151 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001152
Tony Barbour1a86d032015-09-21 15:17:33 -06001153 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001154 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1155 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001156 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001157 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001158
Tony Barbour1a86d032015-09-21 15:17:33 -06001159 do {
1160 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001161 if (cPtr == NULL) {
1162 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001163 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001164 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001165 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001166
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001167 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001168 if (rgba_data == NULL) {
1169 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001170 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001171 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001172 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001173 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001174 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1175 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001176 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001177 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001178
Karl Schultze4dc75c2016-02-02 15:37:51 -07001179 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001180 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001181 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001182 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001183 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001184 rowPtr[3] = 255; /* Alpha of 1 */
1185 rowPtr += 4;
1186 }
1187 rgba_data += layout->rowPitch;
1188 }
1189 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001190 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001191#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001192}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001193
Karl Schultze4dc75c2016-02-02 15:37:51 -07001194static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001195 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001196 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001197 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001198 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001199 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001200 int32_t tex_width;
1201 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001202 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001203 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001204
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001206 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001207 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001209 tex_obj->tex_width = tex_width;
1210 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001211
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001212 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001213 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001214 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001215 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001217 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001218 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001219 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001220 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001221 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001222 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001223 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001224 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001225 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001227 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001228
Karl Schultze4dc75c2016-02-02 15:37:51 -07001229 err =
1230 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231 assert(!err);
1232
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001233 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001234
Chia-I Wuf48700b2015-11-10 16:21:09 +08001235 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001236 tex_obj->mem_alloc.pNext = NULL;
1237 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1238 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001239
Karl Schultze4dc75c2016-02-02 15:37:51 -07001240 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1241 required_props,
1242 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001243 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001244
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001245 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001246 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001247 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001248 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001249
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001250 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001251 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001252 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001253
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001254 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001255 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001256 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001257 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001258 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001259 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001260 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001261 void *data;
1262
Karl Schultze4dc75c2016-02-02 15:37:51 -07001263 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1264 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001265
Karl Schultze4dc75c2016-02-02 15:37:51 -07001266 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1267 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001268 assert(!err);
1269
1270 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1271 fprintf(stderr, "Error loading texture: %s\n", filename);
1272 }
1273
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001274 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001275 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001276
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001277 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001278}
1279
Karl Schultze4dc75c2016-02-02 15:37:51 -07001280static void demo_destroy_texture_image(struct demo *demo,
1281 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001282 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001283 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1284 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001285}
1286
Karl Schultze4dc75c2016-02-02 15:37:51 -07001287static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001288 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001289 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001290 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001291
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001292 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001293
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001294 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001295 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001296
Karl Schultze4dc75c2016-02-02 15:37:51 -07001297 if ((props.linearTilingFeatures &
1298 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1299 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001300 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001301 demo_prepare_texture_image(
1302 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1303 VK_IMAGE_USAGE_SAMPLED_BIT,
1304 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1305 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001306 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1307 // shader to run until layout transition completes
1308 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1309 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1310 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1311 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001312 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001313 } else if (props.optimalTilingFeatures &
1314 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001315 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001316
Tony Barbour16156cb2016-10-19 14:15:49 -06001317 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001318 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001319 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001320 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1321 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1322 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001323
Karl Schultze4dc75c2016-02-02 15:37:51 -07001324 demo_prepare_texture_image(
1325 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1326 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1327 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001328
Tony Barbour16156cb2016-10-19 14:15:49 -06001329 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001330 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001331 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001332 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001333 VK_ACCESS_HOST_WRITE_BIT,
1334 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1335 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001336
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001337 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001338 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001339 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001340 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001341 VK_ACCESS_HOST_WRITE_BIT,
1342 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1343 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001344
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001345 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001346 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1347 .srcOffset = {0, 0, 0},
1348 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1349 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001350 .extent = {demo->staging_texture.tex_width,
1351 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001352 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001353 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001354 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001355 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1356 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001357
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001358 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001359 VK_IMAGE_ASPECT_COLOR_BIT,
1360 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001361 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001362 VK_ACCESS_TRANSFER_WRITE_BIT,
1363 VK_PIPELINE_STAGE_TRANSFER_BIT,
1364 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001365
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001366 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001367 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1368 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001369 }
1370
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001371 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001372 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001374 .magFilter = VK_FILTER_NEAREST,
1375 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001376 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001377 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1378 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1379 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001381 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001382 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001383 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384 .minLod = 0.0f,
1385 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001386 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001387 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001389
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001390 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001391 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001393 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001394 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001395 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001396 .components =
1397 {
1398 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1399 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1400 },
1401 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001402 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001404
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001405 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001406 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001407 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408 assert(!err);
1409
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410 /* create image view */
1411 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001412 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001413 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414 assert(!err);
1415 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417
Tony Barboura72d9492017-01-11 13:35:09 -07001418void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001419 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001420 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001421 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001422 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001423 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001424 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001425 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001426 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001427
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001428 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001429 mat4x4_mul(MVP, VP, demo->model_matrix);
1430 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001431 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001432
Karl Schultza2615462017-01-20 13:19:20 -07001433 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001434 data.position[i][0] = g_vertex_buffer_data[i * 3];
1435 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1436 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001437 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001438 data.attr[i][0] = g_uv_buffer_data[2 * i];
1439 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001440 data.attr[i][2] = 0;
1441 data.attr[i][3] = 0;
1442 }
1443
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001444 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001445 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001446 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001447 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001448
Tony Barboura72d9492017-01-11 13:35:09 -07001449 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1450 err =
1451 vkCreateBuffer(demo->device, &buf_info, NULL,
1452 &demo->swapchain_image_resources[i].uniform_buffer);
1453 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001454
Tony Barboura72d9492017-01-11 13:35:09 -07001455 vkGetBufferMemoryRequirements(demo->device,
1456 demo->swapchain_image_resources[i].uniform_buffer,
1457 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001458
Tony Barboura72d9492017-01-11 13:35:09 -07001459 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1460 mem_alloc.pNext = NULL;
1461 mem_alloc.allocationSize = mem_reqs.size;
1462 mem_alloc.memoryTypeIndex = 0;
1463
1464 pass = memory_type_from_properties(
1465 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001466 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001467 &mem_alloc.memoryTypeIndex);
1468 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001469
Tony Barboura72d9492017-01-11 13:35:09 -07001470 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1471 &demo->swapchain_image_resources[i].uniform_memory);
1472 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001473
Tony Barboura72d9492017-01-11 13:35:09 -07001474 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1475 VK_WHOLE_SIZE, 0, (void **)&pData);
1476 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001477
Tony Barboura72d9492017-01-11 13:35:09 -07001478 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001479
Tony Barboura72d9492017-01-11 13:35:09 -07001480 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001481
Tony Barboura72d9492017-01-11 13:35:09 -07001482 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1483 demo->swapchain_image_resources[i].uniform_memory, 0);
1484 assert(!err);
1485 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001486}
1487
Karl Schultze4dc75c2016-02-02 15:37:51 -07001488static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001489 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001490 [0] =
1491 {
1492 .binding = 0,
1493 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1494 .descriptorCount = 1,
1495 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1496 .pImmutableSamplers = NULL,
1497 },
1498 [1] =
1499 {
1500 .binding = 1,
1501 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1502 .descriptorCount = DEMO_TEXTURE_COUNT,
1503 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1504 .pImmutableSamplers = NULL,
1505 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001506 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001507 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001508 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001509 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001510 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001511 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001512 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001513 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001514
Karl Schultze4dc75c2016-02-02 15:37:51 -07001515 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1516 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001517 assert(!err);
1518
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001519 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1521 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001522 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001523 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001524 };
1525
Karl Schultze4dc75c2016-02-02 15:37:51 -07001526 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001527 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001529}
1530
Karl Schultze4dc75c2016-02-02 15:37:51 -07001531static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001532 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1533 // because at the start of the renderpass, we don't care about their contents.
1534 // At the start of the subpass, the color attachment's layout will be transitioned
1535 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1536 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1537 // the renderpass, the color attachment's layout will be transitioned to
1538 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1539 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001540 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001541 [0] =
1542 {
1543 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001544 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001545 .samples = VK_SAMPLE_COUNT_1_BIT,
1546 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1547 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1548 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1549 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001550 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001551 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001552 },
1553 [1] =
1554 {
1555 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001556 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001557 .samples = VK_SAMPLE_COUNT_1_BIT,
1558 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1559 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1560 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1561 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1562 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001563 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001564 .finalLayout =
1565 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1566 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001567 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001568 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001570 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001571 const VkAttachmentReference depth_reference = {
1572 .attachment = 1,
1573 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1574 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001575 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001576 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1577 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001578 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001579 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001580 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001581 .pColorAttachments = &color_reference,
1582 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001583 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001584 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001585 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001586 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001587 const VkRenderPassCreateInfo rp_info = {
1588 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1589 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001590 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001591 .attachmentCount = 2,
1592 .pAttachments = attachments,
1593 .subpassCount = 1,
1594 .pSubpasses = &subpass,
1595 .dependencyCount = 0,
1596 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001597 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001598 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001599
Chia-I Wu63636062015-10-26 21:10:41 +08001600 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001601 assert(!err);
1602}
1603
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001604//TODO: Merge shader reading
1605#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001606static VkShaderModule
1607demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001608 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001609 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001610 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001611
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001612 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1613 moduleCreateInfo.pNext = NULL;
1614
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001615 moduleCreateInfo.codeSize = size;
1616 moduleCreateInfo.pCode = code;
1617 moduleCreateInfo.flags = 0;
1618 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1619 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001620
1621 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001622}
1623
Karl Schultze4dc75c2016-02-02 15:37:51 -07001624char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001625 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001626 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001627 void *shader_code;
1628
1629 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001630 if (!fp)
1631 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001632
1633 fseek(fp, 0L, SEEK_END);
1634 size = ftell(fp);
1635
1636 fseek(fp, 0L, SEEK_SET);
1637
1638 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001639 retval = fread(shader_code, size, 1, fp);
1640 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001641
1642 *psize = size;
1643
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001644 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001645 return shader_code;
1646}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001647#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001648
Karl Schultze4dc75c2016-02-02 15:37:51 -07001649static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001650#ifdef __ANDROID__
1651 VkShaderModuleCreateInfo sh_info = {};
1652 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1653
1654#include "cube.vert.h"
1655 sh_info.codeSize = sizeof(cube_vert);
1656 sh_info.pCode = cube_vert;
1657 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1658 assert(!err);
1659#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001660 void *vertShaderCode;
1661 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001662
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001663 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001664 if (!vertShaderCode) {
1665 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1666 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001667
Karl Schultze4dc75c2016-02-02 15:37:51 -07001668 demo->vert_shader_module =
1669 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001670
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001671 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001672#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001673
1674 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001675}
1676
Karl Schultze4dc75c2016-02-02 15:37:51 -07001677static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001678#ifdef __ANDROID__
1679 VkShaderModuleCreateInfo sh_info = {};
1680 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1681
1682#include "cube.frag.h"
1683 sh_info.codeSize = sizeof(cube_frag);
1684 sh_info.pCode = cube_frag;
1685 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1686 assert(!err);
1687#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001688 void *fragShaderCode;
1689 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001690
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001691 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001692 if (!fragShaderCode) {
1693 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
1694 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001695
Karl Schultze4dc75c2016-02-02 15:37:51 -07001696 demo->frag_shader_module =
1697 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001698
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001699 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001700#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001701
1702 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703}
1704
Karl Schultze4dc75c2016-02-02 15:37:51 -07001705static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001706 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001707 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001708 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001709 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001710 VkPipelineRasterizationStateCreateInfo rs;
1711 VkPipelineColorBlendStateCreateInfo cb;
1712 VkPipelineDepthStencilStateCreateInfo ds;
1713 VkPipelineViewportStateCreateInfo vp;
1714 VkPipelineMultisampleStateCreateInfo ms;
1715 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1716 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001717 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001718
Piers Daniellba839d12015-09-29 13:01:09 -06001719 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1720 memset(&dynamicState, 0, sizeof dynamicState);
1721 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1722 dynamicState.pDynamicStates = dynamicStateEnables;
1723
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001724 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001725 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001726 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001728 memset(&vi, 0, sizeof(vi));
1729 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1730
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001731 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001732 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001733 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734
1735 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001736 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1737 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001738 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001739 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001740 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001741 rs.rasterizerDiscardEnable = VK_FALSE;
1742 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001743 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744
1745 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001746 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1747 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001748 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001749 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001750 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001751 cb.attachmentCount = 1;
1752 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753
Tony Barbour29645d02015-01-16 14:27:35 -07001754 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001755 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001756 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001757 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1758 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001759 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001760 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1761 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001762
1763 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001764 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001765 ds.depthTestEnable = VK_TRUE;
1766 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001767 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001768 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001769 ds.back.failOp = VK_STENCIL_OP_KEEP;
1770 ds.back.passOp = VK_STENCIL_OP_KEEP;
1771 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001772 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001773 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001774
Tony Barbour29645d02015-01-16 14:27:35 -07001775 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001776 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001777 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001778 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001779
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001780 // Two stages: vs and fs
1781 pipeline.stageCount = 2;
1782 VkPipelineShaderStageCreateInfo shaderStages[2];
1783 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1784
Karl Schultze4dc75c2016-02-02 15:37:51 -07001785 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1786 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001787 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001788 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001789
Karl Schultze4dc75c2016-02-02 15:37:51 -07001790 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1791 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001792 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001793 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001794
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001795 memset(&pipelineCache, 0, sizeof(pipelineCache));
1796 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001797
Karl Schultze4dc75c2016-02-02 15:37:51 -07001798 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1799 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001800 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001801
Karl Schultze4dc75c2016-02-02 15:37:51 -07001802 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001803 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001804 pipeline.pRasterizationState = &rs;
1805 pipeline.pColorBlendState = &cb;
1806 pipeline.pMultisampleState = &ms;
1807 pipeline.pViewportState = &vp;
1808 pipeline.pDepthStencilState = &ds;
1809 pipeline.pStages = shaderStages;
1810 pipeline.renderPass = demo->render_pass;
1811 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001812
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001813 pipeline.renderPass = demo->render_pass;
1814
Karl Schultze4dc75c2016-02-02 15:37:51 -07001815 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1816 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817 assert(!err);
1818
Chia-I Wu63636062015-10-26 21:10:41 +08001819 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1820 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001821}
1822
Karl Schultze4dc75c2016-02-02 15:37:51 -07001823static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001824 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001825 [0] =
1826 {
1827 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07001828 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001829 },
1830 [1] =
1831 {
1832 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07001833 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001834 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001835 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001836 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001837 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001838 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001839 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08001840 .poolSizeCount = 2,
1841 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001842 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001843 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001844
Karl Schultze4dc75c2016-02-02 15:37:51 -07001845 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1846 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001847 assert(!err);
1848}
1849
Karl Schultze4dc75c2016-02-02 15:37:51 -07001850static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001851 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001852 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001853 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001854
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001855 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001856 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001857 .pNext = NULL,
1858 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001859 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001860 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07001861
1862 VkDescriptorBufferInfo buffer_info;
1863 buffer_info.offset = 0;
1864 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001865
Chia-I Wuae721ba2015-05-25 16:27:55 +08001866 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07001867 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001868 tex_descs[i].sampler = demo->textures[i].sampler;
1869 tex_descs[i].imageView = demo->textures[i].view;
1870 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001871 }
1872
1873 memset(&writes, 0, sizeof(writes));
1874
1875 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001876 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001877 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07001878 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001879
1880 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001881 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001882 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001883 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001884 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001885
Tony Barboura72d9492017-01-11 13:35:09 -07001886 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1887 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
1888 assert(!err);
1889 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
1890 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1891 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1892 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1893 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001894}
1895
Karl Schultze4dc75c2016-02-02 15:37:51 -07001896static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001897 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001898 attachments[1] = demo->depth.view;
1899
Chia-I Wuf973e322015-07-08 13:34:24 +08001900 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001901 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1902 .pNext = NULL,
1903 .renderPass = demo->render_pass,
1904 .attachmentCount = 2,
1905 .pAttachments = attachments,
1906 .width = demo->width,
1907 .height = demo->height,
1908 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001909 };
1910 VkResult U_ASSERT_ONLY err;
1911 uint32_t i;
1912
Tony Barbourd8e504f2015-10-08 14:26:24 -06001913 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07001914 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001915 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001916 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08001917 assert(!err);
1918 }
1919}
1920
Karl Schultze4dc75c2016-02-02 15:37:51 -07001921static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001922 VkResult U_ASSERT_ONLY err;
1923
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001924 const VkCommandPoolCreateInfo cmd_pool_info = {
1925 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001926 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001927 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001928 .flags = 0,
1929 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001930 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1931 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001932 assert(!err);
1933
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001934 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001935 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001936 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001937 .commandPool = demo->cmd_pool,
1938 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001939 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001940 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001941 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1942 assert(!err);
1943 VkCommandBufferBeginInfo cmd_buf_info = {
1944 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1945 .pNext = NULL,
1946 .flags = 0,
1947 .pInheritanceInfo = NULL,
1948 };
1949 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1950 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001951
1952 demo_prepare_buffers(demo);
1953 demo_prepare_depth(demo);
1954 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07001955 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001957 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001958 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001959 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001960
Ian Elliotta0781972015-08-21 15:09:33 -06001961 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001962 err =
Tony Barboura72d9492017-01-11 13:35:09 -07001963 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001964 assert(!err);
1965 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001966
Tony Barbour22e06272016-09-07 16:07:56 -06001967 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07001968 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06001969 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1970 .pNext = NULL,
1971 .queueFamilyIndex = demo->present_queue_family_index,
1972 .flags = 0,
1973 };
Karl Schultza2615462017-01-20 13:19:20 -07001974 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06001975 &demo->present_cmd_pool);
1976 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07001977 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06001978 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1979 .pNext = NULL,
1980 .commandPool = demo->present_cmd_pool,
1981 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1982 .commandBufferCount = 1,
1983 };
1984 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1985 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07001986 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06001987 assert(!err);
1988 demo_build_image_ownership_cmd(demo, i);
1989 }
1990 }
1991
Chia-I Wu63ea9262015-03-26 13:14:16 +08001992 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001993 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001994
Chia-I Wuf973e322015-07-08 13:34:24 +08001995 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001996
Ian Elliotta0781972015-08-21 15:09:33 -06001997 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001998 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07001999 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002000 }
2001
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002002 /*
2003 * Prepare functions above may generate pipeline commands
2004 * that need to be flushed before beginning the render loop.
2005 */
2006 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002007 if (demo->staging_texture.image) {
2008 demo_destroy_texture_image(demo, &demo->staging_texture);
2009 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002010
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002011 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002012 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002013}
2014
Karl Schultze4dc75c2016-02-02 15:37:51 -07002015static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002016 uint32_t i;
2017
2018 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002019 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002020
Tony Barbour66a56c32016-09-21 13:10:56 -06002021 // Wait for fences from present operations
2022 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002023 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002024 vkDestroyFence(demo->device, demo->fences[i], NULL);
2025 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2026 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2027 if (demo->separate_present_queue) {
2028 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2029 }
2030 }
2031
Tony Barbourd8e504f2015-10-08 14:26:24 -06002032 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002033 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002034 }
Chia-I Wu63636062015-10-26 21:10:41 +08002035 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002036
Chia-I Wu63636062015-10-26 21:10:41 +08002037 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2038 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2039 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2040 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2041 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002042
2043 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002044 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2045 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2046 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2047 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002048 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002049 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002050
Chia-I Wu63636062015-10-26 21:10:41 +08002051 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2052 vkDestroyImage(demo->device, demo->depth.image, NULL);
2053 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002054
Ian Elliotta0781972015-08-21 15:09:33 -06002055 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002056 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002057 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002058 &demo->swapchain_image_resources[i].cmd);
2059 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2060 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2061 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002062 }
Tony Barboura72d9492017-01-11 13:35:09 -07002063 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002064 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002065 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002066
Tony Barbour22e06272016-09-07 16:07:56 -06002067 if (demo->separate_present_queue) {
2068 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002069 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002070 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002071 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002072 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002073 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002074 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002075 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002076 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002077
Tony Barbour153cb062016-12-07 13:43:36 -07002078#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002079 XDestroyWindow(demo->display, demo->xlib_window);
2080 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002081#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002082 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002083 xcb_disconnect(demo->connection);
2084 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002085#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2086 wl_shell_surface_destroy(demo->shell_surface);
2087 wl_surface_destroy(demo->window);
2088 wl_shell_destroy(demo->shell);
2089 wl_compositor_destroy(demo->compositor);
2090 wl_registry_destroy(demo->registry);
2091 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002092#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002093#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002094}
2095
Karl Schultze4dc75c2016-02-02 15:37:51 -07002096static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002097 uint32_t i;
2098
Mike Stroyanbb60b382015-10-28 09:46:57 -06002099 // Don't react to resize until after first initialization.
2100 if (!demo->prepared) {
2101 return;
2102 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002103 // In order to properly resize the window, we must re-create the swapchain
2104 // AND redo the command buffers, etc.
2105 //
2106 // First, perform part of the demo_cleanup() function:
2107 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002108 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002109
2110 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002111 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002112 }
Chia-I Wu63636062015-10-26 21:10:41 +08002113 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002114
Chia-I Wu63636062015-10-26 21:10:41 +08002115 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2116 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2117 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2118 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2119 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002120
2121 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002122 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2123 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2124 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2125 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002126 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002127
Chia-I Wu63636062015-10-26 21:10:41 +08002128 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2129 vkDestroyImage(demo->device, demo->depth.image, NULL);
2130 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002131
Ian Elliottcf074d32015-10-16 18:02:43 -06002132 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002133 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002134 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002135 &demo->swapchain_image_resources[i].cmd);
2136 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2137 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2138 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002139 }
Chia-I Wu63636062015-10-26 21:10:41 +08002140 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002141 if (demo->separate_present_queue) {
2142 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2143 }
Tony Barboura72d9492017-01-11 13:35:09 -07002144 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002145
Ian Elliottcf074d32015-10-16 18:02:43 -06002146 // Second, re-perform the demo_prepare() function, which will re-create the
2147 // swapchain:
2148 demo_prepare(demo);
2149}
2150
David Pinedo2bb7c932015-06-18 17:03:14 -06002151// On MS-Windows, make this a global, so it's available to WndProc()
2152struct demo demo;
2153
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002154#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002155static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002156 if (!demo->prepared)
2157 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002158
Ian Elliott639ca472015-04-16 15:23:05 -06002159 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002160 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002161 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002162 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002163 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002164}
Ian Elliott639ca472015-04-16 15:23:05 -06002165
2166// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002167LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2168 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002169 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002170 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002171 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002172 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002173 // The validation callback calls MessageBox which can generate paint
2174 // events - don't make more Vulkan calls if we got here from the
2175 // callback
2176 if (!in_callback) {
2177 demo_run(&demo);
2178 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002179 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002180 case WM_GETMINMAXINFO: // set window's minimum size
2181 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2182 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002183 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002184 // Resize the application to the new window size, except when
2185 // it was minimized. Vulkan doesn't support images or swapchains
2186 // with width=0 and height=0.
2187 if (wParam != SIZE_MINIMIZED) {
2188 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002189 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002190 demo_resize(&demo);
2191 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002192 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002193 default:
2194 break;
2195 }
2196 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2197}
2198
Karl Schultze4dc75c2016-02-02 15:37:51 -07002199static void demo_create_window(struct demo *demo) {
2200 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002201
2202 // Initialize the window class structure:
2203 win_class.cbSize = sizeof(WNDCLASSEX);
2204 win_class.style = CS_HREDRAW | CS_VREDRAW;
2205 win_class.lpfnWndProc = WndProc;
2206 win_class.cbClsExtra = 0;
2207 win_class.cbWndExtra = 0;
2208 win_class.hInstance = demo->connection; // hInstance
2209 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2210 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2211 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2212 win_class.lpszMenuName = NULL;
2213 win_class.lpszClassName = demo->name;
2214 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2215 // Register window class:
2216 if (!RegisterClassEx(&win_class)) {
2217 // It didn't work, so try to give a useful error:
2218 printf("Unexpected error trying to start the application!\n");
2219 fflush(stdout);
2220 exit(1);
2221 }
2222 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002223 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002224 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002225 demo->window = CreateWindowEx(0,
2226 demo->name, // class name
2227 demo->name, // app name
2228 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002229 WS_VISIBLE | WS_SYSMENU,
2230 100, 100, // x/y coords
2231 wr.right - wr.left, // width
2232 wr.bottom - wr.top, // height
2233 NULL, // handle to parent
2234 NULL, // handle to menu
2235 demo->connection, // hInstance
2236 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002237 if (!demo->window) {
2238 // It didn't work, so try to give a useful error:
2239 printf("Cannot create a window in which to draw!\n");
2240 fflush(stdout);
2241 exit(1);
2242 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002243 // Window client area size must be at least 1 pixel high, to prevent crash.
2244 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2245 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002246}
Tony Barbour8295ac42016-08-08 11:04:17 -06002247#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002248static void demo_create_xlib_window(struct demo *demo) {
2249
Tony Barbouree9cd372017-01-31 16:09:58 -07002250 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002251 demo->display = XOpenDisplay(NULL);
2252 long visualMask = VisualScreenMask;
2253 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002254 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002255 vInfoTemplate.screen = DefaultScreen(demo->display);
2256 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2257 &vInfoTemplate, &numberOfVisuals);
2258
2259 Colormap colormap = XCreateColormap(
2260 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2261 visualInfo->visual, AllocNone);
2262
Rene Lindsay11612722016-06-13 17:20:39 -06002263 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002264 windowAttributes.colormap = colormap;
2265 windowAttributes.background_pixel = 0xFFFFFFFF;
2266 windowAttributes.border_pixel = 0;
2267 windowAttributes.event_mask =
2268 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2269
2270 demo->xlib_window = XCreateWindow(
2271 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2272 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2273 visualInfo->visual,
2274 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2275
2276 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2277 XMapWindow(demo->display, demo->xlib_window);
2278 XFlush(demo->display);
2279 demo->xlib_wm_delete_window =
2280 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2281}
2282static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2283 switch(event->type) {
2284 case ClientMessage:
2285 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2286 demo->quit = true;
2287 break;
2288 case KeyPress:
2289 switch (event->xkey.keycode) {
2290 case 0x9: // Escape
2291 demo->quit = true;
2292 break;
2293 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002294 demo->spin_angle -= demo->spin_increment;
2295 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002296 case 0x72: // right arrow key
2297 demo->spin_angle += demo->spin_increment;
2298 break;
2299 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002300 demo->pause = !demo->pause;
2301 break;
2302 }
2303 break;
2304 case ConfigureNotify:
2305 if ((demo->width != event->xconfigure.width) ||
2306 (demo->height != event->xconfigure.height)) {
2307 demo->width = event->xconfigure.width;
2308 demo->height = event->xconfigure.height;
2309 demo_resize(demo);
2310 }
2311 break;
2312 default:
2313 break;
2314 }
2315
2316}
2317
2318static void demo_run_xlib(struct demo *demo) {
2319
2320 while (!demo->quit) {
2321 XEvent event;
2322
2323 if (demo->pause) {
2324 XNextEvent(demo->display, &event);
2325 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002326 }
2327 while (XPending(demo->display) > 0) {
2328 XNextEvent(demo->display, &event);
2329 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002330 }
2331
Tony Barbour2593b182016-04-19 10:57:58 -06002332 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002333 demo->curFrame++;
2334 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2335 demo->quit = true;
2336 }
2337}
Tony Barbour153cb062016-12-07 13:43:36 -07002338#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002339static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002340 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002341 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002342 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002343 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002344 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002345 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002346 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002347 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2348 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002349 demo->quit = true;
2350 }
2351 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002352 case XCB_KEY_RELEASE: {
2353 const xcb_key_release_event_t *key =
2354 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002355
Karl Schultze4dc75c2016-02-02 15:37:51 -07002356 switch (key->detail) {
2357 case 0x9: // Escape
2358 demo->quit = true;
2359 break;
2360 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002361 demo->spin_angle -= demo->spin_increment;
2362 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002363 case 0x72: // right arrow key
2364 demo->spin_angle += demo->spin_increment;
2365 break;
2366 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002367 demo->pause = !demo->pause;
2368 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002369 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002370 } break;
2371 case XCB_CONFIGURE_NOTIFY: {
2372 const xcb_configure_notify_event_t *cfg =
2373 (const xcb_configure_notify_event_t *)event;
2374 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002375 demo->width = cfg->width;
2376 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002377 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002378 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002379 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002380 default:
2381 break;
2382 }
2383}
2384
Tony Barbour2593b182016-04-19 10:57:58 -06002385static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002386 xcb_flush(demo->connection);
2387
2388 while (!demo->quit) {
2389 xcb_generic_event_t *event;
2390
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002391 if (demo->pause) {
2392 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002393 }
2394 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002395 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002396 }
2397 while (event) {
2398 demo_handle_xcb_event(demo, event);
2399 free(event);
2400 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002401 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002402
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002403 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002404 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002405 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002406 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002407 }
2408}
2409
Tony Barbour2593b182016-04-19 10:57:58 -06002410static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002411 uint32_t value_mask, value_list[32];
2412
Tony Barbour2593b182016-04-19 10:57:58 -06002413 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002414
2415 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2416 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002417 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002418 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002419
Tony Barbour2593b182016-04-19 10:57:58 -06002420 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002421 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2422 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2423 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002424
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002425 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002426 xcb_intern_atom_cookie_t cookie =
2427 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2428 xcb_intern_atom_reply_t *reply =
2429 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002430
Karl Schultze4dc75c2016-02-02 15:37:51 -07002431 xcb_intern_atom_cookie_t cookie2 =
2432 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2433 demo->atom_wm_delete_window =
2434 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002435
Tony Barbour2593b182016-04-19 10:57:58 -06002436 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002437 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002438 &(*demo->atom_wm_delete_window).atom);
2439 free(reply);
2440
Tony Barbour2593b182016-04-19 10:57:58 -06002441 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002442
Karl Schultze4dc75c2016-02-02 15:37:51 -07002443 // Force the x/y coordinates to 100,100 results are identical in consecutive
2444 // runs
2445 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002446 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002447 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002448}
Tony Barbour6b131662016-08-25 13:14:45 -06002449// VK_USE_PLATFORM_XCB_KHR
2450#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002451static void demo_run(struct demo *demo) {
2452 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002453 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002454 demo->curFrame++;
2455 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2456 demo->quit = true;
2457 }
2458}
2459
2460static void handle_ping(void *data UNUSED,
2461 struct wl_shell_surface *shell_surface,
2462 uint32_t serial) {
2463 wl_shell_surface_pong(shell_surface, serial);
2464}
2465
2466static void handle_configure(void *data UNUSED,
2467 struct wl_shell_surface *shell_surface UNUSED,
2468 uint32_t edges UNUSED, int32_t width UNUSED,
2469 int32_t height UNUSED) {}
2470
2471static void handle_popup_done(void *data UNUSED,
2472 struct wl_shell_surface *shell_surface UNUSED) {}
2473
2474static const struct wl_shell_surface_listener shell_surface_listener = {
2475 handle_ping, handle_configure, handle_popup_done};
2476
2477static void demo_create_window(struct demo *demo) {
2478 demo->window = wl_compositor_create_surface(demo->compositor);
2479 if (!demo->window) {
2480 printf("Can not create wayland_surface from compositor!\n");
2481 fflush(stdout);
2482 exit(1);
2483 }
2484
2485 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2486 if (!demo->shell_surface) {
2487 printf("Can not get shell_surface from wayland_surface!\n");
2488 fflush(stdout);
2489 exit(1);
2490 }
2491 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2492 demo);
2493 wl_shell_surface_set_toplevel(demo->shell_surface);
2494 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2495}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002496#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2497static void demo_run(struct demo *demo) {
2498 if (!demo->prepared)
2499 return;
2500
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002501 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002502 demo->curFrame++;
2503}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002504#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002505#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2506static VkResult demo_create_display_surface(struct demo *demo) {
2507 VkResult U_ASSERT_ONLY err;
2508 uint32_t display_count;
2509 uint32_t mode_count;
2510 uint32_t plane_count;
2511 VkDisplayPropertiesKHR display_props;
2512 VkDisplayKHR display;
2513 VkDisplayModePropertiesKHR mode_props;
2514 VkDisplayPlanePropertiesKHR *plane_props;
2515 VkBool32 found_plane = VK_FALSE;
2516 uint32_t plane_index;
2517 VkExtent2D image_extent;
2518 VkDisplaySurfaceCreateInfoKHR create_info;
2519
2520 // Get the first display
2521 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2522 assert(!err);
2523
2524 if (display_count == 0) {
2525 printf("Cannot find any display!\n");
2526 fflush(stdout);
2527 exit(1);
2528 }
2529
2530 display_count = 1;
2531 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2532 assert(!err || (err == VK_INCOMPLETE));
2533
2534 display = display_props.display;
2535
2536 // Get the first mode of the display
2537 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2538 assert(!err);
2539
2540 if (mode_count == 0) {
2541 printf("Cannot find any mode for the display!\n");
2542 fflush(stdout);
2543 exit(1);
2544 }
2545
2546 mode_count = 1;
2547 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2548 assert(!err || (err == VK_INCOMPLETE));
2549
2550 // Get the list of planes
2551 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2552 assert(!err);
2553
2554 if (plane_count == 0) {
2555 printf("Cannot find any plane!\n");
2556 fflush(stdout);
2557 exit(1);
2558 }
2559
2560 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2561 assert(plane_props);
2562
2563 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2564 assert(!err);
2565
2566 // Find a plane compatible with the display
2567 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2568 uint32_t supported_count;
2569 VkDisplayKHR *supported_displays;
2570
2571 // Disqualify planes that are bound to a different display
2572 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2573 (plane_props[plane_index].currentDisplay != display)) {
2574 continue;
2575 }
2576
2577 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2578 assert(!err);
2579
2580 if (supported_count == 0) {
2581 continue;
2582 }
2583
2584 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2585 assert(supported_displays);
2586
2587 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2588 assert(!err);
2589
2590 for (uint32_t i = 0; i < supported_count; i++) {
2591 if (supported_displays[i] == display) {
2592 found_plane = VK_TRUE;
2593 break;
2594 }
2595 }
2596
2597 free(supported_displays);
2598
2599 if (found_plane) {
2600 break;
2601 }
2602 }
2603
2604 if (!found_plane) {
2605 printf("Cannot find a plane compatible with the display!\n");
2606 fflush(stdout);
2607 exit(1);
2608 }
2609
2610 free(plane_props);
2611
2612 image_extent.width = mode_props.parameters.visibleRegion.width;
2613 image_extent.height = mode_props.parameters.visibleRegion.height;
2614
2615 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2616 create_info.pNext = NULL;
2617 create_info.flags = 0;
2618 create_info.displayMode = mode_props.displayMode;
2619 create_info.planeIndex = plane_index;
2620 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2621 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
2622 create_info.alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2623 create_info.globalAlpha = 1.0f;
2624 create_info.imageExtent = image_extent;
2625
2626 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2627}
2628
2629static void demo_run_display(struct demo *demo)
2630{
2631 while (!demo->quit) {
2632 demo_draw(demo);
2633 demo->curFrame++;
2634
2635 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2636 demo->quit = true;
2637 }
2638 }
2639}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002640#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002641
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002642/*
2643 * Return 1 (true) if all layer names specified in check_names
2644 * can be found in given layer properties.
2645 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002646static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002647 uint32_t layer_count,
2648 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002649 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002650 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002651 for (uint32_t j = 0; j < layer_count; j++) {
2652 if (!strcmp(check_names[i], layers[j].layerName)) {
2653 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002654 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002655 }
2656 }
2657 if (!found) {
2658 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2659 return 0;
2660 }
2661 }
2662 return 1;
2663}
2664
Karl Schultze4dc75c2016-02-02 15:37:51 -07002665static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002666 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002667 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002668 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002669 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002670 char **instance_validation_layers = NULL;
2671 demo->enabled_extension_count = 0;
2672 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002673
Karl Schultz7c50ad62016-04-01 12:07:03 -06002674 char *instance_validation_layers_alt1[] = {
2675 "VK_LAYER_LUNARG_standard_validation"
2676 };
2677
2678 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002679 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2680 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2681 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2682 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002683 };
2684
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002685 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002686 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002687 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002688
Karl Schultz7c50ad62016-04-01 12:07:03 -06002689 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002690 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002691
Karl Schultz7c50ad62016-04-01 12:07:03 -06002692 instance_validation_layers = instance_validation_layers_alt1;
2693 if (instance_layer_count > 0) {
2694 VkLayerProperties *instance_layers =
2695 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2696 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2697 instance_layers);
2698 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002699
Karl Schultz7c50ad62016-04-01 12:07:03 -06002700
2701 validation_found = demo_check_layers(
2702 ARRAY_SIZE(instance_validation_layers_alt1),
2703 instance_validation_layers, instance_layer_count,
2704 instance_layers);
2705 if (validation_found) {
2706 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002707 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2708 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002709 } else {
2710 // use alternative set of validation layers
2711 instance_validation_layers = instance_validation_layers_alt2;
2712 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2713 validation_found = demo_check_layers(
2714 ARRAY_SIZE(instance_validation_layers_alt2),
2715 instance_validation_layers, instance_layer_count,
2716 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002717 validation_layer_count =
2718 ARRAY_SIZE(instance_validation_layers_alt2);
2719 for (uint32_t i = 0; i < validation_layer_count; i++) {
2720 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002721 }
2722 }
2723 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002724 }
Dustin Graves7c287352016-01-27 13:48:06 -07002725
Karl Schultz7c50ad62016-04-01 12:07:03 -06002726 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002727 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002728 "required validation layer.\n\n"
2729 "Please look at the Getting Started guide for additional "
2730 "information.\n",
2731 "vkCreateInstance Failure");
2732 }
Dustin Graves7c287352016-01-27 13:48:06 -07002733 }
2734
2735 /* Look for instance extensions */
2736 VkBool32 surfaceExtFound = 0;
2737 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002738 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002739
Karl Schultze4dc75c2016-02-02 15:37:51 -07002740 err = vkEnumerateInstanceExtensionProperties(
2741 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002742 assert(!err);
2743
Dustin Graves7c287352016-01-27 13:48:06 -07002744 if (instance_extension_count > 0) {
2745 VkExtensionProperties *instance_extensions =
2746 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2747 err = vkEnumerateInstanceExtensionProperties(
2748 NULL, &instance_extension_count, instance_extensions);
2749 assert(!err);
2750 for (uint32_t i = 0; i < instance_extension_count; i++) {
2751 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2752 instance_extensions[i].extensionName)) {
2753 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002754 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002755 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002756 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002757#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002758 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2759 instance_extensions[i].extensionName)) {
2760 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002761 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002762 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2763 }
Tony Barbour153cb062016-12-07 13:43:36 -07002764#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002765 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2766 instance_extensions[i].extensionName)) {
2767 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06002768 demo->extension_names[demo->enabled_extension_count++] =
2769 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2770 }
Tony Barbour153cb062016-12-07 13:43:36 -07002771#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002772 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2773 instance_extensions[i].extensionName)) {
2774 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002775 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002776 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2777 }
Tony Barbour153cb062016-12-07 13:43:36 -07002778#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002779 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2780 instance_extensions[i].extensionName)) {
2781 platformSurfaceExtFound = 1;
2782 demo->extension_names[demo->enabled_extension_count++] =
2783 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2784 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002785#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002786#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2787 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
2788 instance_extensions[i].extensionName)) {
2789 platformSurfaceExtFound = 1;
2790 demo->extension_names[demo->enabled_extension_count++] =
2791 VK_KHR_DISPLAY_EXTENSION_NAME;
2792 }
Tony Barbour153cb062016-12-07 13:43:36 -07002793#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002794 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2795 instance_extensions[i].extensionName)) {
2796 platformSurfaceExtFound = 1;
2797 demo->extension_names[demo->enabled_extension_count++] =
2798 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2799 }
2800#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002801 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2802 instance_extensions[i].extensionName)) {
2803 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002804 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002805 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2806 }
2807 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002808 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002809 }
Dustin Graves7c287352016-01-27 13:48:06 -07002810
2811 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002812 }
Dustin Graves7c287352016-01-27 13:48:06 -07002813
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002814 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002815 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2816 "the " VK_KHR_SURFACE_EXTENSION_NAME
2817 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002818 "Vulkan installable client driver (ICD) installed?\nPlease "
2819 "look at the Getting Started guide for additional "
2820 "information.\n",
2821 "vkCreateInstance Failure");
2822 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002823 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002824#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002825 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2826 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2827 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002828 "Vulkan installable client driver (ICD) installed?\nPlease "
2829 "look at the Getting Started guide for additional "
2830 "information.\n",
2831 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002832#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002833 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2834 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2835 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002836 "Vulkan installable client driver (ICD) installed?\nPlease "
2837 "look at the Getting Started guide for additional "
2838 "information.\n",
2839 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002840#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2841 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2842 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2843 " extension.\n\nDo you have a compatible "
2844 "Vulkan installable client driver (ICD) installed?\nPlease "
2845 "look at the Getting Started guide for additional "
2846 "information.\n",
2847 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002848#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002849#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2850 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2851 "the " VK_KHR_DISPLAY_EXTENSION_NAME
2852 " extension.\n\nDo you have a compatible "
2853 "Vulkan installable client driver (ICD) installed?\nPlease "
2854 "look at the Getting Started guide for additional "
2855 "information.\n",
2856 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002857#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2858 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2859 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2860 " extension.\n\nDo you have a compatible "
2861 "Vulkan installable client driver (ICD) installed?\nPlease "
2862 "look at the Getting Started guide for additional "
2863 "information.\n",
2864 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002865#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002866 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2867 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2868 " extension.\n\nDo you have a compatible "
2869 "Vulkan installable client driver (ICD) installed?\nPlease "
2870 "look at the Getting Started guide for additional "
2871 "information.\n",
2872 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002873#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002874 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002875 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002876 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002877 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002878 .pApplicationName = APP_SHORT_NAME,
2879 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002880 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002881 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002882 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002883 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002884 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002885 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002886 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002887 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002888 .enabledLayerCount = demo->enabled_layer_count,
2889 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2890 .enabledExtensionCount = demo->enabled_extension_count,
2891 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002892 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002893
2894 /*
2895 * This is info for a temp callback to use during CreateInstance.
2896 * After the instance is created, we use the instance-based
2897 * function to register the final callback.
2898 */
Karl Schultza2615462017-01-20 13:19:20 -07002899 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002900 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07002901 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2902 dbgCreateInfoTemp.pNext = NULL;
2903 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
2904 dbgCreateInfoTemp.pUserData = demo;
2905 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06002906 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultza2615462017-01-20 13:19:20 -07002907 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002908 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002909
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002910 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002911
Chia-I Wu63636062015-10-26 21:10:41 +08002912 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002913 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2914 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002915 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002916 "additional information.\n",
2917 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002918 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002919 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002920 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002921 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002922 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002923 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2924 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002925 "the Getting Started guide for additional information.\n",
2926 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002927 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002928
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002929 /* Make initial call to query gpu_count, then second call for gpu info*/
2930 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2931 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002932
2933 if (gpu_count > 0) {
2934 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2935 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2936 assert(!err);
2937 /* For cube demo we just grab the first physical device */
2938 demo->gpu = physical_devices[0];
2939 free(physical_devices);
2940 } else {
2941 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2942 "Do you have a compatible Vulkan installable client driver (ICD) "
2943 "installed?\nPlease look at the Getting Started guide for "
2944 "additional information.\n",
2945 "vkEnumeratePhysicalDevices Failure");
2946 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002947
Dustin Graves7c287352016-01-27 13:48:06 -07002948 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002949 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002950 VkBool32 swapchainExtFound = 0;
2951 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002952 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002953
Karl Schultze4dc75c2016-02-02 15:37:51 -07002954 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2955 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002956 assert(!err);
2957
Dustin Graves7c287352016-01-27 13:48:06 -07002958 if (device_extension_count > 0) {
2959 VkExtensionProperties *device_extensions =
2960 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2961 err = vkEnumerateDeviceExtensionProperties(
2962 demo->gpu, NULL, &device_extension_count, device_extensions);
2963 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002964
Dustin Graves7c287352016-01-27 13:48:06 -07002965 for (uint32_t i = 0; i < device_extension_count; i++) {
2966 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2967 device_extensions[i].extensionName)) {
2968 swapchainExtFound = 1;
2969 demo->extension_names[demo->enabled_extension_count++] =
2970 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2971 }
2972 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002973 }
Dustin Graves7c287352016-01-27 13:48:06 -07002974
2975 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002976 }
Dustin Graves7c287352016-01-27 13:48:06 -07002977
Tony Barbourcc69f452015-10-08 13:59:42 -06002978 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002979 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2980 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2981 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002982 "Vulkan installable client driver (ICD) installed?\nPlease "
2983 "look at the Getting Started guide for additional "
2984 "information.\n",
2985 "vkCreateInstance Failure");
2986 }
2987
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002988 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002989 demo->CreateDebugReportCallback =
2990 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2991 demo->inst, "vkCreateDebugReportCallbackEXT");
2992 demo->DestroyDebugReportCallback =
2993 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2994 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002995 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002996 ERR_EXIT(
2997 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2998 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002999 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003000 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003001 ERR_EXIT(
3002 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3003 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003004 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003005 demo->DebugReportMessage =
3006 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3007 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003008 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003009 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003010 "vkGetProcAddr Failure");
3011 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003012
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003013 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003014 PFN_vkDebugReportCallbackEXT callback;
3015 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003016 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003017 dbgCreateInfo.pNext = NULL;
3018 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003019 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003020 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003021 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003022 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3023 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003024 switch (err) {
3025 case VK_SUCCESS:
3026 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003027 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003028 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3029 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003030 break;
3031 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003032 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3033 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003034 break;
3035 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003036 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003037 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003038
3039 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003040 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3041 &demo->queue_family_count, NULL);
3042 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003043
Karl Schultze4dc75c2016-02-02 15:37:51 -07003044 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003045 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3046 vkGetPhysicalDeviceQueueFamilyProperties(
3047 demo->gpu, &demo->queue_family_count, demo->queue_props);
3048
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003049 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003050 // If app has specific feature requirements it should check supported
3051 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003052 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003053 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003054
Tony Barbourda2bad52016-01-22 14:36:40 -07003055 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3056 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3057 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3058 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3059 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3060}
3061
Karl Schultze4dc75c2016-02-02 15:37:51 -07003062static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003063 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003064 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003065 VkDeviceQueueCreateInfo queues[2];
3066 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3067 queues[0].pNext = NULL;
3068 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3069 queues[0].queueCount = 1;
3070 queues[0].pQueuePriorities = queue_priorities;
3071 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003072
3073 VkDeviceCreateInfo device = {
3074 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3075 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003076 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003077 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003078 .enabledLayerCount = 0,
3079 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003080 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003081 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3082 .pEnabledFeatures =
3083 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003084 };
Tony Barbour22e06272016-09-07 16:07:56 -06003085 if (demo->separate_present_queue) {
3086 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3087 queues[1].pNext = NULL;
3088 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3089 queues[1].queueCount = 1;
3090 queues[1].pQueuePriorities = queue_priorities;
3091 queues[1].flags = 0;
3092 device.queueCreateInfoCount = 2;
3093 }
Chia-I Wu63636062015-10-26 21:10:41 +08003094 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003095 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003096}
3097
Karl Schultze4dc75c2016-02-02 15:37:51 -07003098static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003099 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003100
Karl Schultze4dc75c2016-02-02 15:37:51 -07003101// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003102#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003103 VkWin32SurfaceCreateInfoKHR createInfo;
3104 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3105 createInfo.pNext = NULL;
3106 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003107 createInfo.hinstance = demo->connection;
3108 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003109
Karl Schultze4dc75c2016-02-02 15:37:51 -07003110 err =
3111 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003112#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003113 VkWaylandSurfaceCreateInfoKHR createInfo;
3114 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3115 createInfo.pNext = NULL;
3116 createInfo.flags = 0;
3117 createInfo.display = demo->display;
3118 createInfo.surface = demo->window;
3119
3120 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3121 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003122#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003123#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3124 VkAndroidSurfaceCreateInfoKHR createInfo;
3125 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3126 createInfo.pNext = NULL;
3127 createInfo.flags = 0;
3128 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003129
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003130 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003131#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3132 VkXlibSurfaceCreateInfoKHR createInfo;
3133 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3134 createInfo.pNext = NULL;
3135 createInfo.flags = 0;
3136 createInfo.dpy = demo->display;
3137 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003138
Tony Barbour153cb062016-12-07 13:43:36 -07003139 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003140 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003141#elif defined(VK_USE_PLATFORM_XCB_KHR)
3142 VkXcbSurfaceCreateInfoKHR createInfo;
3143 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3144 createInfo.pNext = NULL;
3145 createInfo.flags = 0;
3146 createInfo.connection = demo->connection;
3147 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003148
Tony Barbour153cb062016-12-07 13:43:36 -07003149 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003150#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3151 err = demo_create_display_surface(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003152#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003153 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003154
Tony Barbourcc69f452015-10-08 13:59:42 -06003155 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003156 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003157 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003158 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003159 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003160 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003161 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003162
3163 // Search for a graphics and a present queue in the array of queue
3164 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003165 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3166 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003167 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003168 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3169 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3170 graphicsQueueFamilyIndex = i;
3171 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003172
Tony Barbourab2a0362016-09-06 11:40:12 -06003173 if (supportsPresent[i] == VK_TRUE) {
3174 graphicsQueueFamilyIndex = i;
3175 presentQueueFamilyIndex = i;
3176 break;
3177 }
3178 }
3179 }
3180
3181 if (presentQueueFamilyIndex == UINT32_MAX) {
3182 // If didn't find a queue that supports both graphics and present, then
3183 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003184 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003185 if (supportsPresent[i] == VK_TRUE) {
3186 presentQueueFamilyIndex = i;
3187 break;
3188 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003189 }
3190 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003191
3192 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003193 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3194 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003195 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003196 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003197 }
3198
Tony Barbourab2a0362016-09-06 11:40:12 -06003199 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3200 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003201 demo->separate_present_queue =
3202 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003203 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003204
Tony Barbourda2bad52016-01-22 14:36:40 -07003205 demo_create_device(demo);
3206
3207 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3208 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3209 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3210 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3211 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3212
Tony Barboura2a67812016-07-18 13:21:06 -06003213 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3214 &demo->graphics_queue);
3215
Tony Barbour22e06272016-09-07 16:07:56 -06003216 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003217 demo->present_queue = demo->graphics_queue;
3218 } else {
3219 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3220 &demo->present_queue);
3221 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003222
Ian Elliottaaae5352015-07-06 14:27:58 -06003223 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003224 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003225 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003226 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003227 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003228 VkSurfaceFormatKHR *surfFormats =
3229 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003230 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003231 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003232 assert(!err);
3233 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3234 // the surface has no preferred format. Otherwise, at least one
3235 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003236 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003237 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003238 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003239 assert(formatCount >= 1);
3240 demo->format = surfFormats[0].format;
3241 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003242 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003243
David Pinedo2bb7c932015-06-18 17:03:14 -06003244 demo->quit = false;
3245 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003246
Tony Barbource7524e2016-07-28 12:01:45 -06003247 // Create semaphores to synchronize acquiring presentable buffers before
3248 // rendering and waiting for drawing to be complete before presenting
3249 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3250 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3251 .pNext = NULL,
3252 .flags = 0,
3253 };
Tony Barbource7524e2016-07-28 12:01:45 -06003254
Tony Barbour66a56c32016-09-21 13:10:56 -06003255 // Create fences that we can use to throttle if we get too far
3256 // ahead of the image presents
3257 VkFenceCreateInfo fence_ci = {
3258 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3259 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003260 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003261 };
3262 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003263 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3264 assert(!err);
3265
Tony Barbour22e06272016-09-07 16:07:56 -06003266 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003267 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003268 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003269
3270 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3271 &demo->draw_complete_semaphores[i]);
3272 assert(!err);
3273
3274 if (demo->separate_present_queue) {
3275 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3276 &demo->image_ownership_semaphores[i]);
3277 assert(!err);
3278 }
Tony Barbour22e06272016-09-07 16:07:56 -06003279 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003280 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003281
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003282 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003283 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003284}
3285
Tony Barbour153cb062016-12-07 13:43:36 -07003286#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003287static void registry_handle_global(void *data, struct wl_registry *registry,
3288 uint32_t name, const char *interface,
3289 uint32_t version UNUSED) {
3290 struct demo *demo = data;
3291 if (strcmp(interface, "wl_compositor") == 0) {
3292 demo->compositor =
3293 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3294 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3295 * tp xdg_shell */
3296 } else if (strcmp(interface, "wl_shell") == 0) {
3297 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3298 }
3299}
3300
3301static void registry_handle_global_remove(void *data UNUSED,
3302 struct wl_registry *registry UNUSED,
3303 uint32_t name UNUSED) {}
3304
3305static const struct wl_registry_listener registry_listener = {
3306 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003307#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003308#endif
3309
Karl Schultze4dc75c2016-02-02 15:37:51 -07003310static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003311#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003312 const xcb_setup_t *setup;
3313 xcb_screen_iterator_t iter;
3314 int scr;
3315
3316 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003317 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003318 printf("Cannot find a compatible Vulkan installable client driver "
3319 "(ICD).\nExiting ...\n");
3320 fflush(stdout);
3321 exit(1);
3322 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003323
3324 setup = xcb_get_setup(demo->connection);
3325 iter = xcb_setup_roots_iterator(setup);
3326 while (scr-- > 0)
3327 xcb_screen_next(&iter);
3328
3329 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003330#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3331 demo->display = wl_display_connect(NULL);
3332
3333 if (demo->display == NULL) {
3334 printf("Cannot find a compatible Vulkan installable client driver "
3335 "(ICD).\nExiting ...\n");
3336 fflush(stdout);
3337 exit(1);
3338 }
3339
3340 demo->registry = wl_display_get_registry(demo->display);
3341 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3342 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003343#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003344#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003345}
3346
Karl Schultze4dc75c2016-02-02 15:37:51 -07003347static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003348 vec3 eye = {0.0f, 3.0f, 5.0f};
3349 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003350 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003351
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003352 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003353 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003354 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003355
Piers Daniell735ee532015-02-23 16:23:13 -07003356 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003357 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003358 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003359 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003360 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003361 if ((strcmp(argv[i], "--present_mode") == 0) &&
3362 (i < argc - 1)) {
3363 demo->presentMode = atoi(argv[i+1]);
3364 i++;
3365 continue;
3366 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003367 if (strcmp(argv[i], "--break") == 0) {
3368 demo->use_break = true;
3369 continue;
3370 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003371 if (strcmp(argv[i], "--validate") == 0) {
3372 demo->validate = true;
3373 continue;
3374 }
Tony Barbour2593b182016-04-19 10:57:58 -06003375 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003376 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003377 continue;
3378 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003379 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3380 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3381 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003382 i++;
3383 continue;
3384 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003385 if (strcmp(argv[i], "--suppress_popups") == 0) {
3386 demo->suppress_popups = true;
3387 continue;
3388 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003389
Cody Northropaf28a532016-09-27 10:50:57 -06003390#if defined(ANDROID)
3391 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3392#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003393 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour291d57b2016-10-21 14:47:07 -06003394 "[--c <framecount>] [--suppress_popups] [--present_mode <present mode enum>]\n"
3395 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3396 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3397 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3398 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3399 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3400 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003401 fflush(stderr);
3402 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003403#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003404 }
3405
Tony Barbour153cb062016-12-07 13:43:36 -07003406 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003407
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003408 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003409
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003410 demo->width = 500;
3411 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003412
Tony Barbour66a56c32016-09-21 13:10:56 -06003413 demo->spin_angle = 4.0f;
3414 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003415 demo->pause = false;
3416
Karl Schultze4dc75c2016-02-02 15:37:51 -07003417 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3418 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003419 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3420 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003421
3422 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003423}
3424
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003425#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003426// Include header required for parsing the command line options.
3427#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003428
Karl Schultze4dc75c2016-02-02 15:37:51 -07003429int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3430 int nCmdShow) {
3431 MSG msg; // message
3432 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003433 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003434 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003435
Karl Schultze4dc75c2016-02-02 15:37:51 -07003436 // Use the CommandLine functions to get the command line arguments.
3437 // Unfortunately, Microsoft outputs
3438 // this information as wide characters for Unicode, and we simply want the
3439 // Ascii version to be compatible
3440 // with the non-Windows side. So, we have to convert the information to
3441 // Ascii character strings.
3442 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3443 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003444 argc = 0;
3445 }
3446
Karl Schultze4dc75c2016-02-02 15:37:51 -07003447 if (argc > 0) {
3448 argv = (char **)malloc(sizeof(char *) * argc);
3449 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003450 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003451 } else {
3452 for (int iii = 0; iii < argc; iii++) {
3453 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003454 size_t numConverted = 0;
3455
Karl Schultze4dc75c2016-02-02 15:37:51 -07003456 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3457 if (argv[iii] != NULL) {
3458 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3459 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003460 }
3461 }
3462 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003463 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003464 argv = NULL;
3465 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003466
3467 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003468
3469 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003470 if (argc > 0 && argv != NULL) {
3471 for (int iii = 0; iii < argc; iii++) {
3472 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003473 free(argv[iii]);
3474 }
3475 }
3476 free(argv);
3477 }
3478
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003479 demo.connection = hInstance;
3480 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003481 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003482 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003483
3484 demo_prepare(&demo);
3485
Karl Schultze4dc75c2016-02-02 15:37:51 -07003486 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003487
3488 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003489 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003490 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003491 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003492 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003493 done = true; // if found, quit app
3494 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003495 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003496 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003497 DispatchMessage(&msg);
3498 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003499 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003500 }
3501
3502 demo_cleanup(&demo);
3503
Karl Schultze4dc75c2016-02-02 15:37:51 -07003504 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003505}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003506#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3507#include <android/log.h>
3508#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003509#include "android_util.h"
3510
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003511static bool initialized = false;
3512static bool active = false;
3513struct demo demo;
3514
3515static int32_t processInput(struct android_app* app, AInputEvent* event) {
3516 return 0;
3517}
3518
3519static void processCommand(struct android_app* app, int32_t cmd) {
3520 switch(cmd) {
3521 case APP_CMD_INIT_WINDOW: {
3522 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003523 // We're getting a new window. If the app is starting up, we
3524 // need to initialize. If the app has already been
3525 // initialized, that means that we lost our previous window,
3526 // which means that we have a lot of work to do. At a minimum,
3527 // we need to destroy the swapchain and surface associated with
3528 // the old window, and create a new surface and swapchain.
3529 // However, since there are a lot of other objects/state that
3530 // is tied to the swapchain, it's easiest to simply cleanup and
3531 // start over (i.e. use a brute-force approach of re-starting
3532 // the app)
3533 if (demo.prepared) {
3534 demo_cleanup(&demo);
3535 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003536
3537 // Parse Intents into argc, argv
3538 // Use the following key to send arguments, i.e.
3539 // --es args "--validate"
3540 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003541 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003542 int argc = 0;
3543 char** argv = get_args(app, key, appTag, &argc);
3544
3545 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3546 for (int i = 0; i < argc; i++)
3547 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3548
3549 demo_init(&demo, argc, argv);
3550
3551 // Free the argv malloc'd by get_args
3552 for (int i = 0; i < argc; i++)
3553 free(argv[i]);
3554
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003555 demo.window = (void*)app->window;
3556 demo_init_vk_swapchain(&demo);
3557 demo_prepare(&demo);
3558 initialized = true;
3559 }
3560 break;
3561 }
3562 case APP_CMD_GAINED_FOCUS: {
3563 active = true;
3564 break;
3565 }
3566 case APP_CMD_LOST_FOCUS: {
3567 active = false;
3568 break;
3569 }
3570 }
3571}
3572
3573void android_main(struct android_app *app)
3574{
3575 app_dummy();
3576
Cody Northrop8707a6e2016-04-26 19:59:19 -06003577#ifdef ANDROID
3578 int vulkanSupport = InitVulkan();
3579 if (vulkanSupport == 0)
3580 return;
3581#endif
3582
Ian Elliottf05d5d12016-05-17 12:03:35 -06003583 demo.prepared = false;
3584
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003585 app->onAppCmd = processCommand;
3586 app->onInputEvent = processInput;
3587
3588 while(1) {
3589 int events;
3590 struct android_poll_source* source;
3591 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3592 if (source) {
3593 source->process(app, source);
3594 }
3595
3596 if (app->destroyRequested != 0) {
3597 demo_cleanup(&demo);
3598 return;
3599 }
3600 }
3601 if (initialized && active) {
3602 demo_run(&demo);
3603 }
3604 }
3605
3606}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003607#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003608int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003609 struct demo demo;
3610
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003611 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003612#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003613 demo_create_xcb_window(&demo);
3614#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3615 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003616#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3617 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003618#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003619#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003620
Tony Barbourcc69f452015-10-08 13:59:42 -06003621 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003622
3623 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003624
Tony Barbour153cb062016-12-07 13:43:36 -07003625#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003626 demo_run_xcb(&demo);
3627#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3628 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003629#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3630 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003631#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003632#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3633 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003634#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003635
3636 demo_cleanup(&demo);
3637
Karl Schultz0218c002016-03-22 17:06:13 -06003638 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003639}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003640#endif