blob: 2d87073d25a39c4eedc856b06cb39b83bbfa4e3c [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>
Bill Hollingsf4352142017-02-14 22:58:56 -050024* Author: Bill Hollings <bill.hollings@brenwill.com>
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090025*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070026
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060027#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdbool.h>
32#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070033#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090034#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060035#include <X11/Xutil.h>
36#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060037
Ian Elliott639ca472015-04-16 15:23:05 -060038#ifdef _WIN32
39#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060040#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070041#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060042
Tony Barbourefd0c5a2016-12-07 14:45:12 -070043#if defined(VK_USE_PLATFORM_MIR_KHR)
44#warning "Cube does not have code for Mir at this time"
45#endif
46
Cody Northrop8707a6e2016-04-26 19:59:19 -060047#ifdef ANDROID
48#include "vulkan_wrapper.h"
49#else
David Pinedoc5193c12015-11-06 12:54:48 -070050#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060051#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070052
David Pinedo541526f2015-11-24 09:00:24 -070053#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060054#include "linmath.h"
55
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060056#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060057#define APP_SHORT_NAME "cube"
58#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060059
Tony Barbour66a56c32016-09-21 13:10:56 -060060// Allow a maximum of two outstanding presentation operations.
61#define FRAME_LAG 2
62
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060063#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
64
Tony Barbourfdc2d352015-04-22 09:02:32 -060065#if defined(NDEBUG) && defined(__GNUC__)
66#define U_ASSERT_ONLY __attribute__((unused))
67#else
68#define U_ASSERT_ONLY
69#endif
70
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090071#if defined(__GNUC__)
72#define UNUSED __attribute__((unused))
73#else
74#define UNUSED
75#endif
76
Ian Elliott07264132015-04-28 11:35:02 -060077#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060078bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070079#define ERR_EXIT(err_msg, err_class) \
80 do { \
81 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
82 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070083 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060084
Michael Lentinec6cde4b2016-04-18 13:20:45 -050085#elif defined __ANDROID__
86#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070087#define ERR_EXIT(err_msg, err_class) \
88 do { \
89 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
90 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -050091 } while (0)
92#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070093#define ERR_EXIT(err_msg, err_class) \
94 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -080095 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070096 fflush(stdout); \
97 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070098 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050099#endif
Ian Elliott07264132015-04-28 11:35:02 -0600100
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700101#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
102 { \
103 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
104 if (demo->fp##entrypoint == NULL) { \
105 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
106 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700107 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600108
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600109static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
110
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700111#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
112 { \
113 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
114 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
115 if (demo->fp##entrypoint == NULL) { \
116 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
117 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700118 }
Ian Elliott673898b2015-06-22 15:07:49 -0600119
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600120/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700121 * structure to track all objects related to a texture.
122 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600123struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600124 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700125
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600126 VkImage image;
127 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600128
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800129 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500130 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600131 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700132 int32_t tex_width, tex_height;
133};
134
Karl Schultze4dc75c2016-02-02 15:37:51 -0700135static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600136
Karl Schultz0218c002016-03-22 17:06:13 -0600137static int validation_error = 0;
138
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600139struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600140 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700141 float mvp[4][4];
142 float position[12 * 3][4];
143 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600144};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600145
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146//--------------------------------------------------------------------------------------
147// Mesh and VertexFormat Data
148//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700149// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600150static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800151 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 -1.0f,-1.0f, 1.0f,
153 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800154 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155 -1.0f, 1.0f,-1.0f,
156 -1.0f,-1.0f,-1.0f,
157
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 1.0f, 1.0f,-1.0f,
160 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800161 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600163 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800168 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600170 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 -1.0f, 1.0f, 1.0f,
174 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800175 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600177 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f, 1.0f, 1.0f,
181 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800182 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183 1.0f,-1.0f,-1.0f,
184 1.0f, 1.0f,-1.0f,
185
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600188 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800189 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190 1.0f,-1.0f, 1.0f,
191 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600192};
193
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600194static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700195 0.0f, 1.0f, // -X side
196 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 0.0f, 0.0f,
200 0.0f, 1.0f,
201
Rene Lindsay76867e82016-07-29 10:49:11 -0700202 1.0f, 1.0f, // -Z side
203 0.0f, 0.0f,
204 0.0f, 1.0f,
205 1.0f, 1.0f,
206 1.0f, 0.0f,
207 0.0f, 0.0f,
208
209 1.0f, 0.0f, // -Y side
210 1.0f, 1.0f,
211 0.0f, 1.0f,
212 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600213 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800214 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600215
Rene Lindsay76867e82016-07-29 10:49:11 -0700216 1.0f, 0.0f, // +Y side
217 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700220 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600221 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222
Rene Lindsay76867e82016-07-29 10:49:11 -0700223 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800224 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700225 0.0f, 1.0f,
226 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700229
230 0.0f, 0.0f, // +Z side
231 0.0f, 1.0f,
232 1.0f, 0.0f,
233 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700235 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600236};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700237// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600238
Karl Schultze4dc75c2016-02-02 15:37:51 -0700239void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600240 int i;
241
242 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700243 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600244 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
245 }
246 printf("\n");
247 fflush(stdout);
248}
249
Karl Schultze4dc75c2016-02-02 15:37:51 -0700250void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600251 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700252 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600253 printf("\n");
254 fflush(stdout);
255}
256
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700257VKAPI_ATTR VkBool32 VKAPI_CALL BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
258 size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg,
259 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700260#ifndef WIN32
261 raise(SIGTRAP);
262#else
263 DebugBreak();
264#endif
265
266 return false;
267}
268
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600269typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600270 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800271 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600272 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600273 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700274 VkBuffer uniform_buffer;
275 VkDeviceMemory uniform_memory;
276 VkFramebuffer framebuffer;
277 VkDescriptorSet descriptor_set;
278 VkFence fence;
279} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600280
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600281struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500282#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600283#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700284 HINSTANCE connection; // hInstance - Windows Instance
285 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
286 HWND window; // hWnd - window handle
287 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700288#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700289 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600290 Window xlib_window;
291 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700292#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700293 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600294 xcb_connection_t *connection;
295 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600296 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800297 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900298#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
299 struct wl_display *display;
300 struct wl_registry *registry;
301 struct wl_compositor *compositor;
302 struct wl_surface *window;
303 struct wl_shell *shell;
304 struct wl_shell_surface *shell_surface;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700305#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500306#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700307 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500308#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
309 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500310#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700311 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600312 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700313 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600314 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600315
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600316 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600317 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600319 VkQueue graphics_queue;
320 VkQueue present_queue;
321 uint32_t graphics_queue_family_index;
322 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600323 VkSemaphore image_acquired_semaphores[FRAME_LAG];
324 VkSemaphore draw_complete_semaphores[FRAME_LAG];
325 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600326 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600327 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600328 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Tony Barbourda2bad52016-01-22 14:36:40 -0700330 uint32_t enabled_extension_count;
331 uint32_t enabled_layer_count;
332 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600333 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700334
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600336 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600337 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700339 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
340 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
341 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
342 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600343 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
344 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
345 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
346 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
347 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600348 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600349 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700350 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600351 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600352 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600353 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800355 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600356 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357
358 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600361 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800362 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500363 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600364 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365 } depth;
366
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600367 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600368 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700370 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500371 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600372 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600373 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800374 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600377 mat4x4 projection_matrix;
378 mat4x4 view_matrix;
379 mat4x4 model_matrix;
380
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600381 float spin_angle;
382 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600383 bool pause;
384
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600385 VkShaderModule vert_shader_module;
386 VkShaderModule frag_shader_module;
387
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800389
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600391 int32_t curFrame;
392 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600393 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600394 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600395 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600396 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700397 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
398 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
399 VkDebugReportCallbackEXT msg_callback;
400 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600401
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600402 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600403 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404};
405
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700406VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
407 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600408 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600409 char *message = (char *)malloc(strlen(pMsg) + 100);
410
411 assert(message);
412
Cody Northropaf28a532016-09-27 10:50:57 -0600413 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
414 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600415 validation_error = 1;
416 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600417 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
418 validation_error = 1;
419 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600420 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600421 validation_error = 1;
422 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
423 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
424 validation_error = 1;
425 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
426 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600427 validation_error = 1;
428 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600429 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600430 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600431 }
432
433#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600434
Tony Barbour602ca4f2016-09-12 14:02:43 -0600435 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600436 struct demo *demo = (struct demo*) pUserData;
437 if (!demo->suppress_popups)
438 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600439 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600440
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600441#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600442
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600443 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600444 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600445 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600446 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600447 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600448 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600449 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600450 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600451 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600452 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600453 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600454 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600455 }
Cody Northropaf28a532016-09-27 10:50:57 -0600456
lenny-lunargfbe22392016-06-06 11:07:53 -0600457#else
Cody Northropaf28a532016-09-27 10:50:57 -0600458
lenny-lunargfbe22392016-06-06 11:07:53 -0600459 printf("%s\n", message);
460 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600461
lenny-lunargfbe22392016-06-06 11:07:53 -0600462#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600463
lenny-lunargfbe22392016-06-06 11:07:53 -0600464 free(message);
465
Cody Northropaf28a532016-09-27 10:50:57 -0600466 //clang-format on
467
lenny-lunargfbe22392016-06-06 11:07:53 -0600468 /*
469 * false indicates that layer should not bail-out of an
470 * API call that had validation failures. This may mean that the
471 * app dies inside the driver due to invalid parameter(s).
472 * That's what would happen without validation layers, so we'll
473 * keep that behavior here.
474 */
475 return false;
476}
477
Ian Elliottcf074d32015-10-16 18:02:43 -0600478// Forward declaration:
479static void demo_resize(struct demo *demo);
480
Karl Schultze4dc75c2016-02-02 15:37:51 -0700481static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
482 VkFlags requirements_mask,
483 uint32_t *typeIndex) {
484 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600485 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700486 if ((typeBits & 1) == 1) {
487 // Type is available, does it match user properties?
488 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
489 requirements_mask) == requirements_mask) {
490 *typeIndex = i;
491 return true;
492 }
493 }
494 typeBits >>= 1;
495 }
496 // No memory types matched, return failure
497 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600498}
499
Karl Schultze4dc75c2016-02-02 15:37:51 -0700500static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600501 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502
Tony Barbource7524e2016-07-28 12:01:45 -0600503 // This function could get called twice if the texture uses a staging buffer
504 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600505 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600506 return;
507
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600508 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509 assert(!err);
510
Tony Barbource7524e2016-07-28 12:01:45 -0600511 VkFence fence;
512 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
513 .pNext = NULL,
514 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700515 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
516 assert(!err);
517
Karl Schultze4dc75c2016-02-02 15:37:51 -0700518 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700519 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
520 .pNext = NULL,
521 .waitSemaphoreCount = 0,
522 .pWaitSemaphores = NULL,
523 .pWaitDstStageMask = NULL,
524 .commandBufferCount = 1,
525 .pCommandBuffers = cmd_bufs,
526 .signalSemaphoreCount = 0,
527 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528
Tony Barbource7524e2016-07-28 12:01:45 -0600529 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530 assert(!err);
531
Tony Barbource7524e2016-07-28 12:01:45 -0600532 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 assert(!err);
534
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600535 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600536 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600538}
539
Karl Schultze4dc75c2016-02-02 15:37:51 -0700540static void demo_set_image_layout(struct demo *demo, VkImage image,
541 VkImageAspectFlags aspectMask,
542 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700543 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600544 VkAccessFlagBits srcAccessMask,
545 VkPipelineStageFlags src_stages,
546 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600547 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600548
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600549 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600550 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700552 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800553 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600554 .oldLayout = old_image_layout,
555 .newLayout = new_image_layout,
556 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700557 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600558
Tony Barboureb5077b2016-10-19 15:23:36 -0600559 switch (new_image_layout) {
560 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600561 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600562 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
563 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600564
Tony Barboureb5077b2016-10-19 15:23:36 -0600565 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700566 image_memory_barrier.dstAccessMask =
567 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600568 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700569
Tony Barboureb5077b2016-10-19 15:23:36 -0600570 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700571 image_memory_barrier.dstAccessMask =
572 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600573 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700574
Tony Barboureb5077b2016-10-19 15:23:36 -0600575 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700576 image_memory_barrier.dstAccessMask =
577 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600578 break;
579
580 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
581 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
582 break;
583
584 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
585 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
586 break;
587
588 default:
589 image_memory_barrier.dstAccessMask = 0;
590 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600591 }
592
Tony Barbourf165b5d2016-10-03 16:01:41 -0600593
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600594 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600595
Karl Schultze4dc75c2016-02-02 15:37:51 -0700596 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
597 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600598}
599
Karl Schultze4dc75c2016-02-02 15:37:51 -0700600static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700601 const VkCommandBufferBeginInfo cmd_buf_info = {
602 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
603 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600604 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700605 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700606 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800607 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
609 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800610 };
611 const VkRenderPassBeginInfo rp_begin = {
612 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
613 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800614 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700615 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800616 .renderArea.offset.x = 0,
617 .renderArea.offset.y = 0,
618 .renderArea.extent.width = demo->width,
619 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600620 .clearValueCount = 2,
621 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700622 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800623 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600625 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800627 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700628 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
629 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700630 demo->pipeline_layout, 0, 1,
631 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
632 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600633 VkViewport viewport;
634 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700635 viewport.height = (float)demo->height;
636 viewport.width = (float)demo->width;
637 viewport.minDepth = (float)0.0f;
638 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700639 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600640
641 VkRect2D scissor;
642 memset(&scissor, 0, sizeof(scissor));
643 scissor.extent.width = demo->width;
644 scissor.extent.height = demo->height;
645 scissor.offset.x = 0;
646 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700647 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600648 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600649 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600650 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800651 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Tony Barbour22e06272016-09-07 16:07:56 -0600653 if (demo->separate_present_queue) {
654 // We have to transfer ownership from the graphics queue family to the
655 // present queue family to be able to present. Note that we don't have
656 // to transfer from present queue family back to graphics queue family at
657 // the start of the next frame because we don't care about the image's
658 // contents at that point.
659 VkImageMemoryBarrier image_ownership_barrier = {
660 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
661 .pNext = NULL,
662 .srcAccessMask = 0,
663 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
664 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
665 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
666 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
667 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700668 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600669 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
670
671 vkCmdPipelineBarrier(cmd_buf,
672 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600673 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600674 0, NULL, 0, NULL, 1, &image_ownership_barrier);
675 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600676 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677 assert(!err);
678}
679
Tony Barbour22e06272016-09-07 16:07:56 -0600680void demo_build_image_ownership_cmd(struct demo *demo, int i) {
681 VkResult U_ASSERT_ONLY err;
682
683 const VkCommandBufferBeginInfo cmd_buf_info = {
684 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
685 .pNext = NULL,
686 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
687 .pInheritanceInfo = NULL,
688 };
Tony Barboura72d9492017-01-11 13:35:09 -0700689 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600690 &cmd_buf_info);
691 assert(!err);
692
693 VkImageMemoryBarrier image_ownership_barrier = {
694 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
695 .pNext = NULL,
696 .srcAccessMask = 0,
697 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
698 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
699 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
700 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
701 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700702 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600703 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
704
Tony Barboura72d9492017-01-11 13:35:09 -0700705 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600706 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
707 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
708 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700709 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600710 assert(!err);
711}
712
Karl Schultze4dc75c2016-02-02 15:37:51 -0700713void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600714 mat4x4 MVP, Model, VP;
715 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600716 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600717 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600718
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600719 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700721 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600722 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700723 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
724 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600725 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600726
Tony Barboura72d9492017-01-11 13:35:09 -0700727 vkWaitForFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence,
728 VK_TRUE, UINT64_MAX);
729
730 err = vkMapMemory(demo->device,
731 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
732 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733 assert(!err);
734
Karl Schultze4dc75c2016-02-02 15:37:51 -0700735 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600736
Tony Barboura72d9492017-01-11 13:35:09 -0700737 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738}
739
Karl Schultze4dc75c2016-02-02 15:37:51 -0700740static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600741 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600742
szdarkhackd322ff02016-10-08 09:51:22 +0300743 // Ensure no more than FRAME_LAG presentations are outstanding
744 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
745 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600746
Ian Elliottaaae5352015-07-06 14:27:58 -0600747 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700748 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -0700749 demo->image_acquired_semaphores[demo->frame_index],
750 demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600751 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600752
Tony Barboura72d9492017-01-11 13:35:09 -0700753 demo_update_data_buffer(demo);
754
Ian Elliottcf074d32015-10-16 18:02:43 -0600755 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
756 // demo->swapchain is out of date (e.g. the window was resized) and
757 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600758 demo->frame_index += 1;
759 demo->frame_index %= FRAME_LAG;
760
Ian Elliottcf074d32015-10-16 18:02:43 -0600761 demo_resize(demo);
762 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600763 return;
764 } else if (err == VK_SUBOPTIMAL_KHR) {
765 // demo->swapchain is not as optimal as it could be, but the platform's
766 // presentation engine will still present the image correctly.
767 } else {
768 assert(!err);
769 }
Tony Barbource7524e2016-07-28 12:01:45 -0600770 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600771 // that the image won't be rendered to until the presentation
772 // engine has fully released ownership to the application, and it is
773 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -0600774 VkPipelineStageFlags pipe_stage_flags;
775 VkSubmitInfo submit_info;
776 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
777 submit_info.pNext = NULL;
778 submit_info.pWaitDstStageMask = &pipe_stage_flags;
779 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
780 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600781 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600782 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -0700783 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600784 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600785 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -0700786 vkResetFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence);
787 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
788 demo->swapchain_image_resources[demo->current_buffer].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600789 assert(!err);
790
Tony Barbour22e06272016-09-07 16:07:56 -0600791 if (demo->separate_present_queue) {
792 // If we are using separate queues, change image ownership to the
793 // present queue before presenting, waiting for the draw complete
794 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -0700795 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600796 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
797 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600798 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600799 submit_info.commandBufferCount = 1;
800 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -0700801 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600802 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600803 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600804 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
805 assert(!err);
806 }
807
808 // If we are using separate queues we have to wait for image ownership,
809 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600810 VkPresentInfoKHR present = {
811 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600812 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600813 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600814 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600815 ? &demo->image_ownership_semaphores[demo->frame_index]
816 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600817 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700818 .pSwapchains = &demo->swapchain,
819 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600820 };
821
Tony Barboura2a67812016-07-18 13:21:06 -0600822 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600823 demo->frame_index += 1;
824 demo->frame_index %= FRAME_LAG;
825
Ian Elliottcf074d32015-10-16 18:02:43 -0600826 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
827 // demo->swapchain is out of date (e.g. the window was resized) and
828 // must be recreated:
829 demo_resize(demo);
830 } else if (err == VK_SUBOPTIMAL_KHR) {
831 // demo->swapchain is not as optimal as it could be, but the platform's
832 // presentation engine will still present the image correctly.
833 } else {
834 assert(!err);
835 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836}
837
Karl Schultze4dc75c2016-02-02 15:37:51 -0700838static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600839 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600840 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600841
Ian Elliottb08e0d92015-11-20 11:55:46 -0700842 // Check the surface capabilities and formats
843 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700844 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
845 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600846 assert(!err);
847
Ian Elliott8528eff2015-08-07 15:56:59 -0600848 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700849 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
850 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600851 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600852 VkPresentModeKHR *presentModes =
853 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600854 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700855 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
856 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600857 assert(!err);
858
Ian Elliotta0781972015-08-21 15:09:33 -0600859 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600860 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
861 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
862 // If the surface size is undefined, the size is set to the size
863 // of the images requested, which must fit within the minimum and
864 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600865 swapchainExtent.width = demo->width;
866 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600867
868 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
869 swapchainExtent.width = surfCapabilities.minImageExtent.width;
870 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
871 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
872 }
873
874 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
875 swapchainExtent.height = surfCapabilities.minImageExtent.height;
876 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
877 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
878 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700879 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700881 swapchainExtent = surfCapabilities.currentExtent;
882 demo->width = surfCapabilities.currentExtent.width;
883 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600884 }
885
Tony Barbour66a56c32016-09-21 13:10:56 -0600886 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600887 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600888 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -0600889
Ian Elliottb18fdde2016-10-03 12:11:12 -0600890 // There are times when you may wish to use another present mode. The
891 // following code shows how to select them, and the comments provide some
892 // reasons you may wish to use them.
893 //
894 // It should be noted that Vulkan 1.0 doesn't provide a method for
895 // synchronizing rendering with the presentation engine's display. There
896 // is a method provided for throttling rendering with the display, but
897 // there are some presentation engines for which this method will not work.
898 // If an application doesn't throttle its rendering, and if it renders much
899 // faster than the refresh rate of the display, this can waste power on
900 // mobile devices. That is because power is being spent rendering images
901 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -0600902
Ian Elliottb18fdde2016-10-03 12:11:12 -0600903 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
904 // tearing, or have some way of synchronizing their rendering with the
905 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600906 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
907 // generally render a new presentable image every refresh cycle, but are
908 // occasionally early. In this case, the application wants the new image
909 // to be displayed instead of the previously-queued-for-presentation image
910 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600911 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
912 // render a new presentable image every refresh cycle, but are occasionally
913 // late. In this case (perhaps because of stuttering/latency concerns),
914 // the application wants the late image to be immediately displayed, even
915 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -0600916
917 if (demo->presentMode != swapchainPresentMode) {
918
919 for (size_t i = 0; i < presentModeCount; ++i) {
920 if (presentModes[i] == demo->presentMode) {
921 swapchainPresentMode = demo->presentMode;
922 break;
923 }
Ian Elliottb18fdde2016-10-03 12:11:12 -0600924 }
925 }
Tony Barbour291d57b2016-10-21 14:47:07 -0600926 if (swapchainPresentMode != demo->presentMode) {
927 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
928 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600929
Tony Barbour84376fe2017-01-06 15:54:22 -0700930 // Determine the number of VkImages to use in the swap chain.
931 // Application desires to acquire 3 images at a time for triple
932 // buffering
933 uint32_t desiredNumOfSwapchainImages = 3;
934 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
935 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
936 }
Ian Elliottcf7f7482016-08-19 03:46:58 -0600937 // If maxImageCount is 0, we can ask for as many images as we want;
938 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700939 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600940 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600941 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600942 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600943 }
944
Tony Barbour9fd6d352015-09-16 15:01:32 -0600945 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700946 if (surfCapabilities.supportedTransforms &
947 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700948 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600949 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700950 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600951 }
952
Tony Barboura2a67812016-07-18 13:21:06 -0600953 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600954 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800955 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700956 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600957 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800958 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600959 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700960 .imageExtent =
961 {
962 .width = swapchainExtent.width, .height = swapchainExtent.height,
963 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700964 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600965 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700966 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700967 .imageArrayLayers = 1,
968 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
969 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600970 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600971 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600972 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600973 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600974 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600975 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600976 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700977 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800978 assert(!err);
979
Ian Elliottcf074d32015-10-16 18:02:43 -0600980 // If we just re-created an existing swapchain, we should destroy the old
981 // swapchain at this point.
982 // Note: destroying the swapchain also cleans up all its associated
983 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800984 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700985 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600986 }
987
988 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600989 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600990 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800991
Karl Schultze4dc75c2016-02-02 15:37:51 -0700992 VkImage *swapchainImages =
993 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600994 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600995 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600996 &demo->swapchainImageCount,
997 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600998 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600999
Tony Barboura72d9492017-01-11 13:35:09 -07001000 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001001 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001002 assert(demo->swapchain_image_resources);
1003
1004 VkFenceCreateInfo fence_ci = {
1005 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1006 .pNext = NULL,
1007 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1008 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001009
Ian Elliotta0781972015-08-21 15:09:33 -06001010 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001011 VkImageViewCreateInfo color_image_view = {
1012 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013 .pNext = NULL,
1014 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001015 .components =
1016 {
1017 .r = VK_COMPONENT_SWIZZLE_R,
1018 .g = VK_COMPONENT_SWIZZLE_G,
1019 .b = VK_COMPONENT_SWIZZLE_B,
1020 .a = VK_COMPONENT_SWIZZLE_A,
1021 },
1022 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1023 .baseMipLevel = 0,
1024 .levelCount = 1,
1025 .baseArrayLayer = 0,
1026 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001027 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1028 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001029 };
1030
Tony Barboura72d9492017-01-11 13:35:09 -07001031 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001032
Tony Barboura72d9492017-01-11 13:35:09 -07001033 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034
Karl Schultze4dc75c2016-02-02 15:37:51 -07001035 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001036 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001037 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001038
Tony Barboura72d9492017-01-11 13:35:09 -07001039 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1040 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001041 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001042
Mark Young04fd3d82016-01-25 14:43:50 -07001043 if (NULL != presentModes) {
1044 free(presentModes);
1045 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001046}
1047
Karl Schultze4dc75c2016-02-02 15:37:51 -07001048static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001049 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001050 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001053 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001055 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001057 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001058 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001059 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001060 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 .flags = 0,
1062 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001063
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001064 VkImageViewCreateInfo view = {
1065 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001066 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001067 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001068 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001069 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1070 .baseMipLevel = 0,
1071 .levelCount = 1,
1072 .baseArrayLayer = 0,
1073 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001074 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001075 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001076 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001077
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001078 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001079 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001080 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001081
1082 demo->depth.format = depth_format;
1083
1084 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001085 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001086 assert(!err);
1087
Karl Schultze4dc75c2016-02-02 15:37:51 -07001088 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001089 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001090
Chia-I Wuf48700b2015-11-10 16:21:09 +08001091 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001092 demo->depth.mem_alloc.pNext = NULL;
1093 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1094 demo->depth.mem_alloc.memoryTypeIndex = 0;
1095
Karl Schultze4dc75c2016-02-02 15:37:51 -07001096 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001097 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001098 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001099 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001100
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001101 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001102 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1103 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001104 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001105
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001106 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001107 err =
1108 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001109 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001110
1111 /* create image view */
1112 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001113 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001115}
1116
Tony Barbour1a86d032015-09-21 15:17:33 -06001117/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001119 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001120
1121#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001122 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001123#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001124
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001125#ifdef __ANDROID__
1126#include <lunarg.ppm.h>
1127 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001128 cPtr = (char*)lunarg_ppm;
1129 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001130 return false;
1131 }
1132 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001133 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001134 if (rgba_data == NULL) {
1135 return true;
1136 }
1137 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001138 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001139 return false;
1140 }
1141 while(strncmp(cPtr++, "\n", 1));
1142
1143 for (int y = 0; y < *height; y++) {
1144 uint8_t *rowPtr = rgba_data;
1145 for (int x = 0; x < *width; x++) {
1146 memcpy(rowPtr, cPtr, 3);
1147 rowPtr[3] = 255; /* Alpha of 1 */
1148 rowPtr += 4;
1149 cPtr += 3;
1150 }
1151 rgba_data += layout->rowPitch;
1152 }
1153
1154 return true;
1155#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001156 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001157 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001158
Tony Barbour1a86d032015-09-21 15:17:33 -06001159 if (!fPtr)
1160 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001161
Tony Barbour1a86d032015-09-21 15:17:33 -06001162 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001163 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1164 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001165 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001166 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001167
Tony Barbour1a86d032015-09-21 15:17:33 -06001168 do {
1169 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001170 if (cPtr == NULL) {
1171 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001172 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001173 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001175
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001176 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001177 if (rgba_data == NULL) {
1178 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001179 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001180 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001181 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001182 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001183 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1184 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001185 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001186 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001187
Karl Schultze4dc75c2016-02-02 15:37:51 -07001188 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001189 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001190 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001191 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001192 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001193 rowPtr[3] = 255; /* Alpha of 1 */
1194 rowPtr += 4;
1195 }
1196 rgba_data += layout->rowPitch;
1197 }
1198 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001199 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001200#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001201}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001202
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001204 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001205 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001206 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001207 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001208 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001209 int32_t tex_width;
1210 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001211 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001212 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001213
Karl Schultze4dc75c2016-02-02 15:37:51 -07001214 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001215 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001216 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001218 tex_obj->tex_width = tex_width;
1219 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001220
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001221 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001222 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001223 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001224 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001225 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001226 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001227 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001228 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001229 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001230 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001231 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001232 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001233 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001235
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001236 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001237
Karl Schultze4dc75c2016-02-02 15:37:51 -07001238 err =
1239 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240 assert(!err);
1241
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001242 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001243
Chia-I Wuf48700b2015-11-10 16:21:09 +08001244 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001245 tex_obj->mem_alloc.pNext = NULL;
1246 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1247 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001248
Karl Schultze4dc75c2016-02-02 15:37:51 -07001249 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1250 required_props,
1251 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001252 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001253
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001254 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001255 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001256 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001257 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001258
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001259 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001260 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001261 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001262
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001263 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001264 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001265 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001267 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001268 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001269 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001270 void *data;
1271
Karl Schultze4dc75c2016-02-02 15:37:51 -07001272 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1273 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001274
Karl Schultze4dc75c2016-02-02 15:37:51 -07001275 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1276 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001277 assert(!err);
1278
1279 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1280 fprintf(stderr, "Error loading texture: %s\n", filename);
1281 }
1282
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001283 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001284 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001285
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001286 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001287}
1288
Karl Schultze4dc75c2016-02-02 15:37:51 -07001289static void demo_destroy_texture_image(struct demo *demo,
1290 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001291 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001292 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1293 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001294}
1295
Karl Schultze4dc75c2016-02-02 15:37:51 -07001296static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001297 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001298 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001299 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001300
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001301 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001302
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001303 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001304 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001305
Karl Schultze4dc75c2016-02-02 15:37:51 -07001306 if ((props.linearTilingFeatures &
1307 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1308 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001309 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001310 demo_prepare_texture_image(
1311 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1312 VK_IMAGE_USAGE_SAMPLED_BIT,
1313 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1314 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001315 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1316 // shader to run until layout transition completes
1317 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1318 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1319 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1320 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001321 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001322 } else if (props.optimalTilingFeatures &
1323 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001324 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001325
Tony Barbour16156cb2016-10-19 14:15:49 -06001326 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001327 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001328 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001329 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1330 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1331 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001332
Karl Schultze4dc75c2016-02-02 15:37:51 -07001333 demo_prepare_texture_image(
1334 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1335 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1336 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001337
Tony Barbour16156cb2016-10-19 14:15:49 -06001338 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001340 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001341 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001342 VK_ACCESS_HOST_WRITE_BIT,
1343 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1344 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001345
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001346 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001347 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001348 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001349 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001350 VK_ACCESS_HOST_WRITE_BIT,
1351 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1352 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001353
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001354 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001355 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1356 .srcOffset = {0, 0, 0},
1357 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1358 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001359 .extent = {demo->staging_texture.tex_width,
1360 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001361 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001362 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001363 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001364 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1365 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001366
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001367 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001368 VK_IMAGE_ASPECT_COLOR_BIT,
1369 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001370 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001371 VK_ACCESS_TRANSFER_WRITE_BIT,
1372 VK_PIPELINE_STAGE_TRANSFER_BIT,
1373 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001374
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001375 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001376 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1377 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001378 }
1379
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001380 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001381 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001382 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001383 .magFilter = VK_FILTER_NEAREST,
1384 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001385 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001386 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1387 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1388 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001390 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001391 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001392 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393 .minLod = 0.0f,
1394 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001395 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001396 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001398
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001399 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001400 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001401 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001402 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001403 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001404 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001405 .components =
1406 {
1407 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1408 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1409 },
1410 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001411 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001413
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001415 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417 assert(!err);
1418
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 /* create image view */
1420 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001421 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001422 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423 assert(!err);
1424 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001426
Tony Barboura72d9492017-01-11 13:35:09 -07001427void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001428 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001429 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001430 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001431 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001432 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001433 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001434 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001435 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001436
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001437 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001438 mat4x4_mul(MVP, VP, demo->model_matrix);
1439 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001440 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001441
Karl Schultza2615462017-01-20 13:19:20 -07001442 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001443 data.position[i][0] = g_vertex_buffer_data[i * 3];
1444 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1445 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001446 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001447 data.attr[i][0] = g_uv_buffer_data[2 * i];
1448 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001449 data.attr[i][2] = 0;
1450 data.attr[i][3] = 0;
1451 }
1452
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001453 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001454 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001455 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001456 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001457
Tony Barboura72d9492017-01-11 13:35:09 -07001458 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1459 err =
1460 vkCreateBuffer(demo->device, &buf_info, NULL,
1461 &demo->swapchain_image_resources[i].uniform_buffer);
1462 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001463
Tony Barboura72d9492017-01-11 13:35:09 -07001464 vkGetBufferMemoryRequirements(demo->device,
1465 demo->swapchain_image_resources[i].uniform_buffer,
1466 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001467
Tony Barboura72d9492017-01-11 13:35:09 -07001468 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1469 mem_alloc.pNext = NULL;
1470 mem_alloc.allocationSize = mem_reqs.size;
1471 mem_alloc.memoryTypeIndex = 0;
1472
1473 pass = memory_type_from_properties(
1474 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001475 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001476 &mem_alloc.memoryTypeIndex);
1477 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001478
Tony Barboura72d9492017-01-11 13:35:09 -07001479 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1480 &demo->swapchain_image_resources[i].uniform_memory);
1481 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001482
Tony Barboura72d9492017-01-11 13:35:09 -07001483 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1484 VK_WHOLE_SIZE, 0, (void **)&pData);
1485 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001486
Tony Barboura72d9492017-01-11 13:35:09 -07001487 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001488
Tony Barboura72d9492017-01-11 13:35:09 -07001489 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001490
Tony Barboura72d9492017-01-11 13:35:09 -07001491 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1492 demo->swapchain_image_resources[i].uniform_memory, 0);
1493 assert(!err);
1494 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001495}
1496
Karl Schultze4dc75c2016-02-02 15:37:51 -07001497static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001498 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001499 [0] =
1500 {
1501 .binding = 0,
1502 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1503 .descriptorCount = 1,
1504 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1505 .pImmutableSamplers = NULL,
1506 },
1507 [1] =
1508 {
1509 .binding = 1,
1510 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1511 .descriptorCount = DEMO_TEXTURE_COUNT,
1512 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1513 .pImmutableSamplers = NULL,
1514 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001515 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001516 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001517 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001518 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001519 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001520 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001521 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001522 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001523
Karl Schultze4dc75c2016-02-02 15:37:51 -07001524 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1525 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001526 assert(!err);
1527
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001528 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001529 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1530 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001531 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001532 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001533 };
1534
Karl Schultze4dc75c2016-02-02 15:37:51 -07001535 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001536 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001538}
1539
Karl Schultze4dc75c2016-02-02 15:37:51 -07001540static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001541 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1542 // because at the start of the renderpass, we don't care about their contents.
1543 // At the start of the subpass, the color attachment's layout will be transitioned
1544 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1545 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1546 // the renderpass, the color attachment's layout will be transitioned to
1547 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1548 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001549 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001550 [0] =
1551 {
1552 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001553 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001554 .samples = VK_SAMPLE_COUNT_1_BIT,
1555 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1556 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1557 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1558 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001559 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001560 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001561 },
1562 [1] =
1563 {
1564 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001565 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001566 .samples = VK_SAMPLE_COUNT_1_BIT,
1567 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1568 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1569 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1570 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1571 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001572 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001573 .finalLayout =
1574 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1575 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001576 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001577 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001578 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001579 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001580 const VkAttachmentReference depth_reference = {
1581 .attachment = 1,
1582 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1583 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001584 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001585 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1586 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001587 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001588 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001589 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001590 .pColorAttachments = &color_reference,
1591 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001592 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001593 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001594 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001595 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001596 const VkRenderPassCreateInfo rp_info = {
1597 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1598 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001599 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001600 .attachmentCount = 2,
1601 .pAttachments = attachments,
1602 .subpassCount = 1,
1603 .pSubpasses = &subpass,
1604 .dependencyCount = 0,
1605 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001606 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001607 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001608
Chia-I Wu63636062015-10-26 21:10:41 +08001609 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001610 assert(!err);
1611}
1612
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001613//TODO: Merge shader reading
1614#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001615static VkShaderModule
1616demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001617 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001618 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001619 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001620
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001621 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1622 moduleCreateInfo.pNext = NULL;
1623
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001624 moduleCreateInfo.codeSize = size;
1625 moduleCreateInfo.pCode = code;
1626 moduleCreateInfo.flags = 0;
1627 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1628 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001629
1630 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631}
1632
Karl Schultze4dc75c2016-02-02 15:37:51 -07001633char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001634 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001635 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001636 void *shader_code;
1637
Bill Hollingsf4352142017-02-14 22:58:56 -05001638#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001639 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001640#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001641
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001642 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001643 if (!fp)
1644 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001645
1646 fseek(fp, 0L, SEEK_END);
1647 size = ftell(fp);
1648
1649 fseek(fp, 0L, SEEK_SET);
1650
1651 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001652 retval = fread(shader_code, size, 1, fp);
1653 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001654
1655 *psize = size;
1656
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001657 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001658 return shader_code;
1659}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001660#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001661
Karl Schultze4dc75c2016-02-02 15:37:51 -07001662static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001663#ifdef __ANDROID__
1664 VkShaderModuleCreateInfo sh_info = {};
1665 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1666
1667#include "cube.vert.h"
1668 sh_info.codeSize = sizeof(cube_vert);
1669 sh_info.pCode = cube_vert;
1670 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1671 assert(!err);
1672#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001673 void *vertShaderCode;
1674 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001675
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001676 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001677 if (!vertShaderCode) {
1678 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1679 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001680
Karl Schultze4dc75c2016-02-02 15:37:51 -07001681 demo->vert_shader_module =
1682 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001683
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001684 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001685#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001686
1687 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688}
1689
Karl Schultze4dc75c2016-02-02 15:37:51 -07001690static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001691#ifdef __ANDROID__
1692 VkShaderModuleCreateInfo sh_info = {};
1693 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1694
1695#include "cube.frag.h"
1696 sh_info.codeSize = sizeof(cube_frag);
1697 sh_info.pCode = cube_frag;
1698 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1699 assert(!err);
1700#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001701 void *fragShaderCode;
1702 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001703
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001704 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001705 if (!fragShaderCode) {
1706 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
1707 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001708
Karl Schultze4dc75c2016-02-02 15:37:51 -07001709 demo->frag_shader_module =
1710 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001711
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001712 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001713#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001714
1715 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001716}
1717
Karl Schultze4dc75c2016-02-02 15:37:51 -07001718static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001719 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001720 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001721 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001722 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001723 VkPipelineRasterizationStateCreateInfo rs;
1724 VkPipelineColorBlendStateCreateInfo cb;
1725 VkPipelineDepthStencilStateCreateInfo ds;
1726 VkPipelineViewportStateCreateInfo vp;
1727 VkPipelineMultisampleStateCreateInfo ms;
1728 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1729 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001730 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001731
Piers Daniellba839d12015-09-29 13:01:09 -06001732 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1733 memset(&dynamicState, 0, sizeof dynamicState);
1734 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1735 dynamicState.pDynamicStates = dynamicStateEnables;
1736
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001737 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001738 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001739 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001741 memset(&vi, 0, sizeof(vi));
1742 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1743
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001745 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001746 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747
1748 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001749 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1750 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001751 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001752 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001753 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001754 rs.rasterizerDiscardEnable = VK_FALSE;
1755 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001756 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757
1758 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001759 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1760 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001761 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001762 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001763 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001764 cb.attachmentCount = 1;
1765 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001766
Tony Barbour29645d02015-01-16 14:27:35 -07001767 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001768 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001769 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001770 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1771 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001772 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001773 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1774 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001775
1776 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001777 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001778 ds.depthTestEnable = VK_TRUE;
1779 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001780 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001781 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001782 ds.back.failOp = VK_STENCIL_OP_KEEP;
1783 ds.back.passOp = VK_STENCIL_OP_KEEP;
1784 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001785 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001786 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001787
Tony Barbour29645d02015-01-16 14:27:35 -07001788 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001789 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001790 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001791 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001792
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001793 // Two stages: vs and fs
1794 pipeline.stageCount = 2;
1795 VkPipelineShaderStageCreateInfo shaderStages[2];
1796 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1797
Karl Schultze4dc75c2016-02-02 15:37:51 -07001798 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1799 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001800 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001801 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001802
Karl Schultze4dc75c2016-02-02 15:37:51 -07001803 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1804 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001805 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001806 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001807
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001808 memset(&pipelineCache, 0, sizeof(pipelineCache));
1809 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810
Karl Schultze4dc75c2016-02-02 15:37:51 -07001811 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1812 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001813 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001814
Karl Schultze4dc75c2016-02-02 15:37:51 -07001815 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001816 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001817 pipeline.pRasterizationState = &rs;
1818 pipeline.pColorBlendState = &cb;
1819 pipeline.pMultisampleState = &ms;
1820 pipeline.pViewportState = &vp;
1821 pipeline.pDepthStencilState = &ds;
1822 pipeline.pStages = shaderStages;
1823 pipeline.renderPass = demo->render_pass;
1824 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001825
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001826 pipeline.renderPass = demo->render_pass;
1827
Karl Schultze4dc75c2016-02-02 15:37:51 -07001828 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1829 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830 assert(!err);
1831
Chia-I Wu63636062015-10-26 21:10:41 +08001832 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1833 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001834}
1835
Karl Schultze4dc75c2016-02-02 15:37:51 -07001836static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001837 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001838 [0] =
1839 {
1840 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07001841 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001842 },
1843 [1] =
1844 {
1845 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07001846 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001847 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001848 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001849 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001850 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001851 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001852 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08001853 .poolSizeCount = 2,
1854 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001855 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001856 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001857
Karl Schultze4dc75c2016-02-02 15:37:51 -07001858 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1859 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001860 assert(!err);
1861}
1862
Karl Schultze4dc75c2016-02-02 15:37:51 -07001863static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001864 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001865 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001866 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001867
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001868 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001869 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001870 .pNext = NULL,
1871 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001872 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001873 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07001874
1875 VkDescriptorBufferInfo buffer_info;
1876 buffer_info.offset = 0;
1877 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001878
Chia-I Wuae721ba2015-05-25 16:27:55 +08001879 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07001880 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001881 tex_descs[i].sampler = demo->textures[i].sampler;
1882 tex_descs[i].imageView = demo->textures[i].view;
1883 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001884 }
1885
1886 memset(&writes, 0, sizeof(writes));
1887
1888 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001889 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001890 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07001891 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001892
1893 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001894 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001895 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001896 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001897 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001898
Tony Barboura72d9492017-01-11 13:35:09 -07001899 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1900 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
1901 assert(!err);
1902 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
1903 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1904 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1905 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1906 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001907}
1908
Karl Schultze4dc75c2016-02-02 15:37:51 -07001909static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001910 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001911 attachments[1] = demo->depth.view;
1912
Chia-I Wuf973e322015-07-08 13:34:24 +08001913 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001914 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1915 .pNext = NULL,
1916 .renderPass = demo->render_pass,
1917 .attachmentCount = 2,
1918 .pAttachments = attachments,
1919 .width = demo->width,
1920 .height = demo->height,
1921 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001922 };
1923 VkResult U_ASSERT_ONLY err;
1924 uint32_t i;
1925
Tony Barbourd8e504f2015-10-08 14:26:24 -06001926 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07001927 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001928 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001929 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08001930 assert(!err);
1931 }
1932}
1933
Karl Schultze4dc75c2016-02-02 15:37:51 -07001934static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001935 VkResult U_ASSERT_ONLY err;
1936
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001937 const VkCommandPoolCreateInfo cmd_pool_info = {
1938 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001939 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001940 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001941 .flags = 0,
1942 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001943 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1944 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001945 assert(!err);
1946
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001947 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001948 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001949 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001950 .commandPool = demo->cmd_pool,
1951 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001952 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001954 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1955 assert(!err);
1956 VkCommandBufferBeginInfo cmd_buf_info = {
1957 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1958 .pNext = NULL,
1959 .flags = 0,
1960 .pInheritanceInfo = NULL,
1961 };
1962 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1963 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964
1965 demo_prepare_buffers(demo);
1966 demo_prepare_depth(demo);
1967 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07001968 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001969
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001970 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001971 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001973
Ian Elliotta0781972015-08-21 15:09:33 -06001974 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001975 err =
Tony Barboura72d9492017-01-11 13:35:09 -07001976 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001977 assert(!err);
1978 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001979
Tony Barbour22e06272016-09-07 16:07:56 -06001980 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07001981 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06001982 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1983 .pNext = NULL,
1984 .queueFamilyIndex = demo->present_queue_family_index,
1985 .flags = 0,
1986 };
Karl Schultza2615462017-01-20 13:19:20 -07001987 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06001988 &demo->present_cmd_pool);
1989 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07001990 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06001991 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1992 .pNext = NULL,
1993 .commandPool = demo->present_cmd_pool,
1994 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1995 .commandBufferCount = 1,
1996 };
1997 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1998 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07001999 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002000 assert(!err);
2001 demo_build_image_ownership_cmd(demo, i);
2002 }
2003 }
2004
Chia-I Wu63ea9262015-03-26 13:14:16 +08002005 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002006 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002007
Chia-I Wuf973e322015-07-08 13:34:24 +08002008 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002009
Ian Elliotta0781972015-08-21 15:09:33 -06002010 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002011 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002012 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002013 }
2014
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002015 /*
2016 * Prepare functions above may generate pipeline commands
2017 * that need to be flushed before beginning the render loop.
2018 */
2019 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002020 if (demo->staging_texture.image) {
2021 demo_destroy_texture_image(demo, &demo->staging_texture);
2022 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002023
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002024 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002025 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002026}
2027
Karl Schultze4dc75c2016-02-02 15:37:51 -07002028static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002029 uint32_t i;
2030
2031 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002032 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002033
Tony Barbour66a56c32016-09-21 13:10:56 -06002034 // Wait for fences from present operations
2035 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002036 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002037 vkDestroyFence(demo->device, demo->fences[i], NULL);
2038 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2039 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2040 if (demo->separate_present_queue) {
2041 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2042 }
2043 }
2044
Tony Barbourd8e504f2015-10-08 14:26:24 -06002045 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002046 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002047 }
Chia-I Wu63636062015-10-26 21:10:41 +08002048 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002049
Chia-I Wu63636062015-10-26 21:10:41 +08002050 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2051 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2052 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2053 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2054 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002055
2056 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002057 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2058 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2059 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2060 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002061 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002062 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002063
Chia-I Wu63636062015-10-26 21:10:41 +08002064 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2065 vkDestroyImage(demo->device, demo->depth.image, NULL);
2066 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002067
Ian Elliotta0781972015-08-21 15:09:33 -06002068 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002069 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002070 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002071 &demo->swapchain_image_resources[i].cmd);
2072 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2073 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2074 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002075 }
Tony Barboura72d9492017-01-11 13:35:09 -07002076 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002077 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002078 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002079
Tony Barbour22e06272016-09-07 16:07:56 -06002080 if (demo->separate_present_queue) {
2081 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002082 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002083 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002084 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002085 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002086 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002087 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002088 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002089 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002090
Tony Barbour153cb062016-12-07 13:43:36 -07002091#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002092 XDestroyWindow(demo->display, demo->xlib_window);
2093 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002094#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002095 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002096 xcb_disconnect(demo->connection);
2097 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002098#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2099 wl_shell_surface_destroy(demo->shell_surface);
2100 wl_surface_destroy(demo->window);
2101 wl_shell_destroy(demo->shell);
2102 wl_compositor_destroy(demo->compositor);
2103 wl_registry_destroy(demo->registry);
2104 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002105#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002106#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002107}
2108
Karl Schultze4dc75c2016-02-02 15:37:51 -07002109static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002110 uint32_t i;
2111
Mike Stroyanbb60b382015-10-28 09:46:57 -06002112 // Don't react to resize until after first initialization.
2113 if (!demo->prepared) {
2114 return;
2115 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002116 // In order to properly resize the window, we must re-create the swapchain
2117 // AND redo the command buffers, etc.
2118 //
2119 // First, perform part of the demo_cleanup() function:
2120 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002121 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002122
2123 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002124 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002125 }
Chia-I Wu63636062015-10-26 21:10:41 +08002126 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002127
Chia-I Wu63636062015-10-26 21:10:41 +08002128 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2129 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2130 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2131 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2132 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002133
2134 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002135 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2136 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2137 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2138 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002139 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002140
Chia-I Wu63636062015-10-26 21:10:41 +08002141 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2142 vkDestroyImage(demo->device, demo->depth.image, NULL);
2143 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002144
Ian Elliottcf074d32015-10-16 18:02:43 -06002145 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002146 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002147 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002148 &demo->swapchain_image_resources[i].cmd);
2149 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2150 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2151 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002152 }
Chia-I Wu63636062015-10-26 21:10:41 +08002153 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002154 if (demo->separate_present_queue) {
2155 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2156 }
Tony Barboura72d9492017-01-11 13:35:09 -07002157 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002158
Ian Elliottcf074d32015-10-16 18:02:43 -06002159 // Second, re-perform the demo_prepare() function, which will re-create the
2160 // swapchain:
2161 demo_prepare(demo);
2162}
2163
David Pinedo2bb7c932015-06-18 17:03:14 -06002164// On MS-Windows, make this a global, so it's available to WndProc()
2165struct demo demo;
2166
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002167#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002168static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002169 if (!demo->prepared)
2170 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002171
Ian Elliott639ca472015-04-16 15:23:05 -06002172 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002173 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002174 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002175 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002176 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002177}
Ian Elliott639ca472015-04-16 15:23:05 -06002178
2179// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002180LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2181 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002182 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002183 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002184 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002185 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002186 // The validation callback calls MessageBox which can generate paint
2187 // events - don't make more Vulkan calls if we got here from the
2188 // callback
2189 if (!in_callback) {
2190 demo_run(&demo);
2191 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002192 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002193 case WM_GETMINMAXINFO: // set window's minimum size
2194 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2195 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002196 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002197 // Resize the application to the new window size, except when
2198 // it was minimized. Vulkan doesn't support images or swapchains
2199 // with width=0 and height=0.
2200 if (wParam != SIZE_MINIMIZED) {
2201 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002202 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002203 demo_resize(&demo);
2204 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002205 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002206 default:
2207 break;
2208 }
2209 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2210}
2211
Karl Schultze4dc75c2016-02-02 15:37:51 -07002212static void demo_create_window(struct demo *demo) {
2213 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002214
2215 // Initialize the window class structure:
2216 win_class.cbSize = sizeof(WNDCLASSEX);
2217 win_class.style = CS_HREDRAW | CS_VREDRAW;
2218 win_class.lpfnWndProc = WndProc;
2219 win_class.cbClsExtra = 0;
2220 win_class.cbWndExtra = 0;
2221 win_class.hInstance = demo->connection; // hInstance
2222 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2223 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2224 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2225 win_class.lpszMenuName = NULL;
2226 win_class.lpszClassName = demo->name;
2227 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2228 // Register window class:
2229 if (!RegisterClassEx(&win_class)) {
2230 // It didn't work, so try to give a useful error:
2231 printf("Unexpected error trying to start the application!\n");
2232 fflush(stdout);
2233 exit(1);
2234 }
2235 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002236 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002237 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002238 demo->window = CreateWindowEx(0,
2239 demo->name, // class name
2240 demo->name, // app name
2241 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002242 WS_VISIBLE | WS_SYSMENU,
2243 100, 100, // x/y coords
2244 wr.right - wr.left, // width
2245 wr.bottom - wr.top, // height
2246 NULL, // handle to parent
2247 NULL, // handle to menu
2248 demo->connection, // hInstance
2249 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002250 if (!demo->window) {
2251 // It didn't work, so try to give a useful error:
2252 printf("Cannot create a window in which to draw!\n");
2253 fflush(stdout);
2254 exit(1);
2255 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002256 // Window client area size must be at least 1 pixel high, to prevent crash.
2257 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2258 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002259}
Tony Barbour8295ac42016-08-08 11:04:17 -06002260#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002261static void demo_create_xlib_window(struct demo *demo) {
2262
Tony Barbouree9cd372017-01-31 16:09:58 -07002263 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002264 demo->display = XOpenDisplay(NULL);
2265 long visualMask = VisualScreenMask;
2266 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002267 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002268 vInfoTemplate.screen = DefaultScreen(demo->display);
2269 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2270 &vInfoTemplate, &numberOfVisuals);
2271
2272 Colormap colormap = XCreateColormap(
2273 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2274 visualInfo->visual, AllocNone);
2275
Rene Lindsay11612722016-06-13 17:20:39 -06002276 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002277 windowAttributes.colormap = colormap;
2278 windowAttributes.background_pixel = 0xFFFFFFFF;
2279 windowAttributes.border_pixel = 0;
2280 windowAttributes.event_mask =
2281 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2282
2283 demo->xlib_window = XCreateWindow(
2284 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2285 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2286 visualInfo->visual,
2287 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2288
2289 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2290 XMapWindow(demo->display, demo->xlib_window);
2291 XFlush(demo->display);
2292 demo->xlib_wm_delete_window =
2293 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2294}
2295static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2296 switch(event->type) {
2297 case ClientMessage:
2298 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2299 demo->quit = true;
2300 break;
2301 case KeyPress:
2302 switch (event->xkey.keycode) {
2303 case 0x9: // Escape
2304 demo->quit = true;
2305 break;
2306 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002307 demo->spin_angle -= demo->spin_increment;
2308 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002309 case 0x72: // right arrow key
2310 demo->spin_angle += demo->spin_increment;
2311 break;
2312 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002313 demo->pause = !demo->pause;
2314 break;
2315 }
2316 break;
2317 case ConfigureNotify:
2318 if ((demo->width != event->xconfigure.width) ||
2319 (demo->height != event->xconfigure.height)) {
2320 demo->width = event->xconfigure.width;
2321 demo->height = event->xconfigure.height;
2322 demo_resize(demo);
2323 }
2324 break;
2325 default:
2326 break;
2327 }
2328
2329}
2330
2331static void demo_run_xlib(struct demo *demo) {
2332
2333 while (!demo->quit) {
2334 XEvent event;
2335
2336 if (demo->pause) {
2337 XNextEvent(demo->display, &event);
2338 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002339 }
2340 while (XPending(demo->display) > 0) {
2341 XNextEvent(demo->display, &event);
2342 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002343 }
2344
Tony Barbour2593b182016-04-19 10:57:58 -06002345 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002346 demo->curFrame++;
2347 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2348 demo->quit = true;
2349 }
2350}
Tony Barbour153cb062016-12-07 13:43:36 -07002351#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002352static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002353 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002354 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002355 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002356 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002357 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002358 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002359 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002360 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2361 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002362 demo->quit = true;
2363 }
2364 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002365 case XCB_KEY_RELEASE: {
2366 const xcb_key_release_event_t *key =
2367 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002368
Karl Schultze4dc75c2016-02-02 15:37:51 -07002369 switch (key->detail) {
2370 case 0x9: // Escape
2371 demo->quit = true;
2372 break;
2373 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002374 demo->spin_angle -= demo->spin_increment;
2375 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002376 case 0x72: // right arrow key
2377 demo->spin_angle += demo->spin_increment;
2378 break;
2379 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002380 demo->pause = !demo->pause;
2381 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002382 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002383 } break;
2384 case XCB_CONFIGURE_NOTIFY: {
2385 const xcb_configure_notify_event_t *cfg =
2386 (const xcb_configure_notify_event_t *)event;
2387 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002388 demo->width = cfg->width;
2389 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002390 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002391 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002392 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002393 default:
2394 break;
2395 }
2396}
2397
Tony Barbour2593b182016-04-19 10:57:58 -06002398static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002399 xcb_flush(demo->connection);
2400
2401 while (!demo->quit) {
2402 xcb_generic_event_t *event;
2403
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002404 if (demo->pause) {
2405 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002406 }
2407 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002408 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002409 }
2410 while (event) {
2411 demo_handle_xcb_event(demo, event);
2412 free(event);
2413 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002414 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002415
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002416 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002417 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002418 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002419 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002420 }
2421}
2422
Tony Barbour2593b182016-04-19 10:57:58 -06002423static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002424 uint32_t value_mask, value_list[32];
2425
Tony Barbour2593b182016-04-19 10:57:58 -06002426 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002427
2428 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2429 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002430 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002431 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002432
Tony Barbour2593b182016-04-19 10:57:58 -06002433 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002434 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2435 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2436 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002437
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002438 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002439 xcb_intern_atom_cookie_t cookie =
2440 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2441 xcb_intern_atom_reply_t *reply =
2442 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002443
Karl Schultze4dc75c2016-02-02 15:37:51 -07002444 xcb_intern_atom_cookie_t cookie2 =
2445 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2446 demo->atom_wm_delete_window =
2447 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002448
Tony Barbour2593b182016-04-19 10:57:58 -06002449 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002450 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002451 &(*demo->atom_wm_delete_window).atom);
2452 free(reply);
2453
Tony Barbour2593b182016-04-19 10:57:58 -06002454 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002455
Karl Schultze4dc75c2016-02-02 15:37:51 -07002456 // Force the x/y coordinates to 100,100 results are identical in consecutive
2457 // runs
2458 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002459 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002460 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002461}
Tony Barbour6b131662016-08-25 13:14:45 -06002462// VK_USE_PLATFORM_XCB_KHR
2463#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002464static void demo_run(struct demo *demo) {
2465 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002466 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002467 demo->curFrame++;
2468 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2469 demo->quit = true;
2470 }
2471}
2472
2473static void handle_ping(void *data UNUSED,
2474 struct wl_shell_surface *shell_surface,
2475 uint32_t serial) {
2476 wl_shell_surface_pong(shell_surface, serial);
2477}
2478
2479static void handle_configure(void *data UNUSED,
2480 struct wl_shell_surface *shell_surface UNUSED,
2481 uint32_t edges UNUSED, int32_t width UNUSED,
2482 int32_t height UNUSED) {}
2483
2484static void handle_popup_done(void *data UNUSED,
2485 struct wl_shell_surface *shell_surface UNUSED) {}
2486
2487static const struct wl_shell_surface_listener shell_surface_listener = {
2488 handle_ping, handle_configure, handle_popup_done};
2489
2490static void demo_create_window(struct demo *demo) {
2491 demo->window = wl_compositor_create_surface(demo->compositor);
2492 if (!demo->window) {
2493 printf("Can not create wayland_surface from compositor!\n");
2494 fflush(stdout);
2495 exit(1);
2496 }
2497
2498 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2499 if (!demo->shell_surface) {
2500 printf("Can not get shell_surface from wayland_surface!\n");
2501 fflush(stdout);
2502 exit(1);
2503 }
2504 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2505 demo);
2506 wl_shell_surface_set_toplevel(demo->shell_surface);
2507 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2508}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002509#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2510static void demo_run(struct demo *demo) {
2511 if (!demo->prepared)
2512 return;
2513
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002514 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002515 demo->curFrame++;
2516}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002517#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002518#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2519static VkResult demo_create_display_surface(struct demo *demo) {
2520 VkResult U_ASSERT_ONLY err;
2521 uint32_t display_count;
2522 uint32_t mode_count;
2523 uint32_t plane_count;
2524 VkDisplayPropertiesKHR display_props;
2525 VkDisplayKHR display;
2526 VkDisplayModePropertiesKHR mode_props;
2527 VkDisplayPlanePropertiesKHR *plane_props;
2528 VkBool32 found_plane = VK_FALSE;
2529 uint32_t plane_index;
2530 VkExtent2D image_extent;
2531 VkDisplaySurfaceCreateInfoKHR create_info;
2532
2533 // Get the first display
2534 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2535 assert(!err);
2536
2537 if (display_count == 0) {
2538 printf("Cannot find any display!\n");
2539 fflush(stdout);
2540 exit(1);
2541 }
2542
2543 display_count = 1;
2544 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2545 assert(!err || (err == VK_INCOMPLETE));
2546
2547 display = display_props.display;
2548
2549 // Get the first mode of the display
2550 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2551 assert(!err);
2552
2553 if (mode_count == 0) {
2554 printf("Cannot find any mode for the display!\n");
2555 fflush(stdout);
2556 exit(1);
2557 }
2558
2559 mode_count = 1;
2560 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2561 assert(!err || (err == VK_INCOMPLETE));
2562
2563 // Get the list of planes
2564 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2565 assert(!err);
2566
2567 if (plane_count == 0) {
2568 printf("Cannot find any plane!\n");
2569 fflush(stdout);
2570 exit(1);
2571 }
2572
2573 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2574 assert(plane_props);
2575
2576 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2577 assert(!err);
2578
2579 // Find a plane compatible with the display
2580 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2581 uint32_t supported_count;
2582 VkDisplayKHR *supported_displays;
2583
2584 // Disqualify planes that are bound to a different display
2585 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2586 (plane_props[plane_index].currentDisplay != display)) {
2587 continue;
2588 }
2589
2590 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2591 assert(!err);
2592
2593 if (supported_count == 0) {
2594 continue;
2595 }
2596
2597 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2598 assert(supported_displays);
2599
2600 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2601 assert(!err);
2602
2603 for (uint32_t i = 0; i < supported_count; i++) {
2604 if (supported_displays[i] == display) {
2605 found_plane = VK_TRUE;
2606 break;
2607 }
2608 }
2609
2610 free(supported_displays);
2611
2612 if (found_plane) {
2613 break;
2614 }
2615 }
2616
2617 if (!found_plane) {
2618 printf("Cannot find a plane compatible with the display!\n");
2619 fflush(stdout);
2620 exit(1);
2621 }
2622
2623 free(plane_props);
2624
2625 image_extent.width = mode_props.parameters.visibleRegion.width;
2626 image_extent.height = mode_props.parameters.visibleRegion.height;
2627
2628 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2629 create_info.pNext = NULL;
2630 create_info.flags = 0;
2631 create_info.displayMode = mode_props.displayMode;
2632 create_info.planeIndex = plane_index;
2633 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2634 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
2635 create_info.alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2636 create_info.globalAlpha = 1.0f;
2637 create_info.imageExtent = image_extent;
2638
2639 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2640}
2641
2642static void demo_run_display(struct demo *demo)
2643{
2644 while (!demo->quit) {
2645 demo_draw(demo);
2646 demo->curFrame++;
2647
2648 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2649 demo->quit = true;
2650 }
2651 }
2652}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002653#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002654
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002655/*
2656 * Return 1 (true) if all layer names specified in check_names
2657 * can be found in given layer properties.
2658 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002659static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002660 uint32_t layer_count,
2661 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002662 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002663 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002664 for (uint32_t j = 0; j < layer_count; j++) {
2665 if (!strcmp(check_names[i], layers[j].layerName)) {
2666 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002667 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002668 }
2669 }
2670 if (!found) {
2671 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2672 return 0;
2673 }
2674 }
2675 return 1;
2676}
2677
Karl Schultze4dc75c2016-02-02 15:37:51 -07002678static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002679 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002680 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002681 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002682 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002683 char **instance_validation_layers = NULL;
2684 demo->enabled_extension_count = 0;
2685 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002686
Karl Schultz7c50ad62016-04-01 12:07:03 -06002687 char *instance_validation_layers_alt1[] = {
2688 "VK_LAYER_LUNARG_standard_validation"
2689 };
2690
2691 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07002692 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2693 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2694 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002695 };
2696
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002697 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002698 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002699 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002700
Karl Schultz7c50ad62016-04-01 12:07:03 -06002701 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002702 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002703
Karl Schultz7c50ad62016-04-01 12:07:03 -06002704 instance_validation_layers = instance_validation_layers_alt1;
2705 if (instance_layer_count > 0) {
2706 VkLayerProperties *instance_layers =
2707 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2708 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2709 instance_layers);
2710 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002711
Karl Schultz7c50ad62016-04-01 12:07:03 -06002712
2713 validation_found = demo_check_layers(
2714 ARRAY_SIZE(instance_validation_layers_alt1),
2715 instance_validation_layers, instance_layer_count,
2716 instance_layers);
2717 if (validation_found) {
2718 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002719 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2720 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002721 } else {
2722 // use alternative set of validation layers
2723 instance_validation_layers = instance_validation_layers_alt2;
2724 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2725 validation_found = demo_check_layers(
2726 ARRAY_SIZE(instance_validation_layers_alt2),
2727 instance_validation_layers, instance_layer_count,
2728 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002729 validation_layer_count =
2730 ARRAY_SIZE(instance_validation_layers_alt2);
2731 for (uint32_t i = 0; i < validation_layer_count; i++) {
2732 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002733 }
2734 }
2735 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002736 }
Dustin Graves7c287352016-01-27 13:48:06 -07002737
Karl Schultz7c50ad62016-04-01 12:07:03 -06002738 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002739 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002740 "required validation layer.\n\n"
2741 "Please look at the Getting Started guide for additional "
2742 "information.\n",
2743 "vkCreateInstance Failure");
2744 }
Dustin Graves7c287352016-01-27 13:48:06 -07002745 }
2746
2747 /* Look for instance extensions */
2748 VkBool32 surfaceExtFound = 0;
2749 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002750 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002751
Karl Schultze4dc75c2016-02-02 15:37:51 -07002752 err = vkEnumerateInstanceExtensionProperties(
2753 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002754 assert(!err);
2755
Dustin Graves7c287352016-01-27 13:48:06 -07002756 if (instance_extension_count > 0) {
2757 VkExtensionProperties *instance_extensions =
2758 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2759 err = vkEnumerateInstanceExtensionProperties(
2760 NULL, &instance_extension_count, instance_extensions);
2761 assert(!err);
2762 for (uint32_t i = 0; i < instance_extension_count; i++) {
2763 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2764 instance_extensions[i].extensionName)) {
2765 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002766 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002767 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002768 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002769#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002770 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2771 instance_extensions[i].extensionName)) {
2772 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002773 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002774 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2775 }
Tony Barbour153cb062016-12-07 13:43:36 -07002776#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002777 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2778 instance_extensions[i].extensionName)) {
2779 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06002780 demo->extension_names[demo->enabled_extension_count++] =
2781 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2782 }
Tony Barbour153cb062016-12-07 13:43:36 -07002783#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002784 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2785 instance_extensions[i].extensionName)) {
2786 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002787 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002788 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2789 }
Tony Barbour153cb062016-12-07 13:43:36 -07002790#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002791 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2792 instance_extensions[i].extensionName)) {
2793 platformSurfaceExtFound = 1;
2794 demo->extension_names[demo->enabled_extension_count++] =
2795 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2796 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002797#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002798#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2799 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
2800 instance_extensions[i].extensionName)) {
2801 platformSurfaceExtFound = 1;
2802 demo->extension_names[demo->enabled_extension_count++] =
2803 VK_KHR_DISPLAY_EXTENSION_NAME;
2804 }
Tony Barbour153cb062016-12-07 13:43:36 -07002805#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002806 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2807 instance_extensions[i].extensionName)) {
2808 platformSurfaceExtFound = 1;
2809 demo->extension_names[demo->enabled_extension_count++] =
2810 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2811 }
Bill Hollingsf4352142017-02-14 22:58:56 -05002812#elif defined(VK_USE_PLATFORM_IOS_MVK)
2813 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2814 platformSurfaceExtFound = 1;
2815 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
2816 }
2817#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2818 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2819 platformSurfaceExtFound = 1;
2820 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
2821 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002822#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002823 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2824 instance_extensions[i].extensionName)) {
2825 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002826 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002827 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2828 }
2829 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002830 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002831 }
Dustin Graves7c287352016-01-27 13:48:06 -07002832
2833 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002834 }
Dustin Graves7c287352016-01-27 13:48:06 -07002835
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002836 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002837 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2838 "the " VK_KHR_SURFACE_EXTENSION_NAME
2839 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002840 "Vulkan installable client driver (ICD) installed?\nPlease "
2841 "look at the Getting Started guide for additional "
2842 "information.\n",
2843 "vkCreateInstance Failure");
2844 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002845 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002846#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002847 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2848 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2849 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002850 "Vulkan installable client driver (ICD) installed?\nPlease "
2851 "look at the Getting Started guide for additional "
2852 "information.\n",
2853 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002854#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002855 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2856 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2857 "Vulkan installable client driver (ICD) installed?\nPlease "
2858 "look at the Getting Started guide for additional "
2859 "information.\n",
2860 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002861#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002862 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2863 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2864 "Vulkan installable client driver (ICD) installed?\nPlease "
2865 "look at the Getting Started guide for additional "
2866 "information.\n",
2867 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002868#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002869 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2870 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2871 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002872 "Vulkan installable client driver (ICD) installed?\nPlease "
2873 "look at the Getting Started guide for additional "
2874 "information.\n",
2875 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002876#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2877 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2878 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2879 " extension.\n\nDo you have a compatible "
2880 "Vulkan installable client driver (ICD) installed?\nPlease "
2881 "look at the Getting Started guide for additional "
2882 "information.\n",
2883 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002884#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002885#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2886 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2887 "the " VK_KHR_DISPLAY_EXTENSION_NAME
2888 " extension.\n\nDo you have a compatible "
2889 "Vulkan installable client driver (ICD) installed?\nPlease "
2890 "look at the Getting Started guide for additional "
2891 "information.\n",
2892 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002893#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2894 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2895 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2896 " extension.\n\nDo you have a compatible "
2897 "Vulkan installable client driver (ICD) installed?\nPlease "
2898 "look at the Getting Started guide for additional "
2899 "information.\n",
2900 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002901#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002902 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2903 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2904 " extension.\n\nDo you have a compatible "
2905 "Vulkan installable client driver (ICD) installed?\nPlease "
2906 "look at the Getting Started guide for additional "
2907 "information.\n",
2908 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002909#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002910 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002911 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002912 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002913 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002914 .pApplicationName = APP_SHORT_NAME,
2915 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002916 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002917 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002918 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002919 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002920 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002921 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002922 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002923 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002924 .enabledLayerCount = demo->enabled_layer_count,
2925 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2926 .enabledExtensionCount = demo->enabled_extension_count,
2927 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002928 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002929
2930 /*
2931 * This is info for a temp callback to use during CreateInstance.
2932 * After the instance is created, we use the instance-based
2933 * function to register the final callback.
2934 */
Karl Schultza2615462017-01-20 13:19:20 -07002935 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002936 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002937 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07002938 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2939 dbgCreateInfoTemp.pNext = NULL;
2940 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
2941 dbgCreateInfoTemp.pUserData = demo;
2942 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06002943 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002944 if (demo->validate_checks_disabled) {
2945 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
2946 val_flags.pNext = NULL;
2947 val_flags.disabledValidationCheckCount = 1;
2948 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
2949 val_flags.pDisabledValidationChecks = &disabled_check;
2950 dbgCreateInfoTemp.pNext = (void*)&val_flags;
2951 }
Karl Schultza2615462017-01-20 13:19:20 -07002952 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002953 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002954
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002955 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002956
Chia-I Wu63636062015-10-26 21:10:41 +08002957 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002958 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2959 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002960 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002961 "additional information.\n",
2962 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002963 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002964 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002965 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002966 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002967 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002968 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2969 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002970 "the Getting Started guide for additional information.\n",
2971 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002972 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002973
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002974 /* Make initial call to query gpu_count, then second call for gpu info*/
2975 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2976 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002977
2978 if (gpu_count > 0) {
2979 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2980 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2981 assert(!err);
2982 /* For cube demo we just grab the first physical device */
2983 demo->gpu = physical_devices[0];
2984 free(physical_devices);
2985 } else {
2986 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2987 "Do you have a compatible Vulkan installable client driver (ICD) "
2988 "installed?\nPlease look at the Getting Started guide for "
2989 "additional information.\n",
2990 "vkEnumeratePhysicalDevices Failure");
2991 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002992
Dustin Graves7c287352016-01-27 13:48:06 -07002993 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002994 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002995 VkBool32 swapchainExtFound = 0;
2996 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002997 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002998
Karl Schultze4dc75c2016-02-02 15:37:51 -07002999 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3000 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003001 assert(!err);
3002
Dustin Graves7c287352016-01-27 13:48:06 -07003003 if (device_extension_count > 0) {
3004 VkExtensionProperties *device_extensions =
3005 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3006 err = vkEnumerateDeviceExtensionProperties(
3007 demo->gpu, NULL, &device_extension_count, device_extensions);
3008 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003009
Dustin Graves7c287352016-01-27 13:48:06 -07003010 for (uint32_t i = 0; i < device_extension_count; i++) {
3011 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3012 device_extensions[i].extensionName)) {
3013 swapchainExtFound = 1;
3014 demo->extension_names[demo->enabled_extension_count++] =
3015 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3016 }
3017 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003018 }
Dustin Graves7c287352016-01-27 13:48:06 -07003019
3020 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003021 }
Dustin Graves7c287352016-01-27 13:48:06 -07003022
Tony Barbourcc69f452015-10-08 13:59:42 -06003023 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003024 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3025 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3026 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003027 "Vulkan installable client driver (ICD) installed?\nPlease "
3028 "look at the Getting Started guide for additional "
3029 "information.\n",
3030 "vkCreateInstance Failure");
3031 }
3032
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003033 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003034 demo->CreateDebugReportCallback =
3035 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3036 demo->inst, "vkCreateDebugReportCallbackEXT");
3037 demo->DestroyDebugReportCallback =
3038 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3039 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003040 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003041 ERR_EXIT(
3042 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3043 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003044 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003045 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003046 ERR_EXIT(
3047 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3048 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003049 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003050 demo->DebugReportMessage =
3051 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3052 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003053 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003054 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003055 "vkGetProcAddr Failure");
3056 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003057
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003058 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003059 PFN_vkDebugReportCallbackEXT callback;
3060 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003061 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003062 dbgCreateInfo.pNext = NULL;
3063 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003064 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003065 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003066 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003067 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3068 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003069 switch (err) {
3070 case VK_SUCCESS:
3071 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003072 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003073 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3074 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003075 break;
3076 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003077 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3078 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003079 break;
3080 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003081 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003082 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003083
3084 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003085 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3086 &demo->queue_family_count, NULL);
3087 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003088
Karl Schultze4dc75c2016-02-02 15:37:51 -07003089 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003090 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3091 vkGetPhysicalDeviceQueueFamilyProperties(
3092 demo->gpu, &demo->queue_family_count, demo->queue_props);
3093
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003094 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003095 // If app has specific feature requirements it should check supported
3096 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003097 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003098 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003099
Tony Barbourda2bad52016-01-22 14:36:40 -07003100 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3101 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3102 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3103 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3104 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3105}
3106
Karl Schultze4dc75c2016-02-02 15:37:51 -07003107static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003108 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003109 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003110 VkDeviceQueueCreateInfo queues[2];
3111 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3112 queues[0].pNext = NULL;
3113 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3114 queues[0].queueCount = 1;
3115 queues[0].pQueuePriorities = queue_priorities;
3116 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003117
3118 VkDeviceCreateInfo device = {
3119 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3120 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003121 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003122 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003123 .enabledLayerCount = 0,
3124 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003125 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003126 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3127 .pEnabledFeatures =
3128 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003129 };
Tony Barbour22e06272016-09-07 16:07:56 -06003130 if (demo->separate_present_queue) {
3131 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3132 queues[1].pNext = NULL;
3133 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3134 queues[1].queueCount = 1;
3135 queues[1].pQueuePriorities = queue_priorities;
3136 queues[1].flags = 0;
3137 device.queueCreateInfoCount = 2;
3138 }
Chia-I Wu63636062015-10-26 21:10:41 +08003139 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003140 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003141}
3142
Karl Schultze4dc75c2016-02-02 15:37:51 -07003143static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003144 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003145
Karl Schultze4dc75c2016-02-02 15:37:51 -07003146// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003147#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003148 VkWin32SurfaceCreateInfoKHR createInfo;
3149 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3150 createInfo.pNext = NULL;
3151 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003152 createInfo.hinstance = demo->connection;
3153 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003154
Karl Schultze4dc75c2016-02-02 15:37:51 -07003155 err =
3156 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003157#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003158 VkWaylandSurfaceCreateInfoKHR createInfo;
3159 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3160 createInfo.pNext = NULL;
3161 createInfo.flags = 0;
3162 createInfo.display = demo->display;
3163 createInfo.surface = demo->window;
3164
3165 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3166 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003167#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003168#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3169 VkAndroidSurfaceCreateInfoKHR createInfo;
3170 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3171 createInfo.pNext = NULL;
3172 createInfo.flags = 0;
3173 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003174
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003175 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003176#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3177 VkXlibSurfaceCreateInfoKHR createInfo;
3178 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3179 createInfo.pNext = NULL;
3180 createInfo.flags = 0;
3181 createInfo.dpy = demo->display;
3182 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003183
Tony Barbour153cb062016-12-07 13:43:36 -07003184 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003185 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003186#elif defined(VK_USE_PLATFORM_XCB_KHR)
3187 VkXcbSurfaceCreateInfoKHR createInfo;
3188 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3189 createInfo.pNext = NULL;
3190 createInfo.flags = 0;
3191 createInfo.connection = demo->connection;
3192 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003193
Tony Barbour153cb062016-12-07 13:43:36 -07003194 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003195#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3196 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003197#elif defined(VK_USE_PLATFORM_IOS_MVK)
3198 VkIOSSurfaceCreateInfoMVK surface;
3199 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3200 surface.pNext = NULL;
3201 surface.flags = 0;
3202 surface.pView = demo->window;
3203
3204 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3205#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3206 VkMacOSSurfaceCreateInfoMVK surface;
3207 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3208 surface.pNext = NULL;
3209 surface.flags = 0;
3210 surface.pView = demo->window;
3211
3212 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003213#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003214 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003215
Tony Barbourcc69f452015-10-08 13:59:42 -06003216 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003217 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003218 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003219 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003220 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003221 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003222 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003223
3224 // Search for a graphics and a present queue in the array of queue
3225 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003226 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3227 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003228 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003229 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3230 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3231 graphicsQueueFamilyIndex = i;
3232 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003233
Tony Barbourab2a0362016-09-06 11:40:12 -06003234 if (supportsPresent[i] == VK_TRUE) {
3235 graphicsQueueFamilyIndex = i;
3236 presentQueueFamilyIndex = i;
3237 break;
3238 }
3239 }
3240 }
3241
3242 if (presentQueueFamilyIndex == UINT32_MAX) {
3243 // If didn't find a queue that supports both graphics and present, then
3244 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003245 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003246 if (supportsPresent[i] == VK_TRUE) {
3247 presentQueueFamilyIndex = i;
3248 break;
3249 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003250 }
3251 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003252
3253 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003254 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3255 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003256 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003257 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003258 }
3259
Tony Barbourab2a0362016-09-06 11:40:12 -06003260 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3261 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003262 demo->separate_present_queue =
3263 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003264 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003265
Tony Barbourda2bad52016-01-22 14:36:40 -07003266 demo_create_device(demo);
3267
3268 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3269 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3270 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3271 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3272 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3273
Tony Barboura2a67812016-07-18 13:21:06 -06003274 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3275 &demo->graphics_queue);
3276
Tony Barbour22e06272016-09-07 16:07:56 -06003277 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003278 demo->present_queue = demo->graphics_queue;
3279 } else {
3280 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3281 &demo->present_queue);
3282 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003283
Ian Elliottaaae5352015-07-06 14:27:58 -06003284 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003285 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003286 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003287 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003288 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003289 VkSurfaceFormatKHR *surfFormats =
3290 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003291 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003292 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003293 assert(!err);
3294 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3295 // the surface has no preferred format. Otherwise, at least one
3296 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003297 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003298 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003299 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003300 assert(formatCount >= 1);
3301 demo->format = surfFormats[0].format;
3302 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003303 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003304
David Pinedo2bb7c932015-06-18 17:03:14 -06003305 demo->quit = false;
3306 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003307
Tony Barbource7524e2016-07-28 12:01:45 -06003308 // Create semaphores to synchronize acquiring presentable buffers before
3309 // rendering and waiting for drawing to be complete before presenting
3310 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3311 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3312 .pNext = NULL,
3313 .flags = 0,
3314 };
Tony Barbource7524e2016-07-28 12:01:45 -06003315
Tony Barbour66a56c32016-09-21 13:10:56 -06003316 // Create fences that we can use to throttle if we get too far
3317 // ahead of the image presents
3318 VkFenceCreateInfo fence_ci = {
3319 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3320 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003321 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003322 };
3323 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003324 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3325 assert(!err);
3326
Tony Barbour22e06272016-09-07 16:07:56 -06003327 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003328 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003329 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003330
3331 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3332 &demo->draw_complete_semaphores[i]);
3333 assert(!err);
3334
3335 if (demo->separate_present_queue) {
3336 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3337 &demo->image_ownership_semaphores[i]);
3338 assert(!err);
3339 }
Tony Barbour22e06272016-09-07 16:07:56 -06003340 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003341 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003342
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003343 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003344 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003345}
3346
Tony Barbour153cb062016-12-07 13:43:36 -07003347#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003348static void registry_handle_global(void *data, struct wl_registry *registry,
3349 uint32_t name, const char *interface,
3350 uint32_t version UNUSED) {
3351 struct demo *demo = data;
3352 if (strcmp(interface, "wl_compositor") == 0) {
3353 demo->compositor =
3354 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3355 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3356 * tp xdg_shell */
3357 } else if (strcmp(interface, "wl_shell") == 0) {
3358 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3359 }
3360}
3361
3362static void registry_handle_global_remove(void *data UNUSED,
3363 struct wl_registry *registry UNUSED,
3364 uint32_t name UNUSED) {}
3365
3366static const struct wl_registry_listener registry_listener = {
3367 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003368#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003369#endif
3370
Karl Schultze4dc75c2016-02-02 15:37:51 -07003371static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003372#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003373 const xcb_setup_t *setup;
3374 xcb_screen_iterator_t iter;
3375 int scr;
3376
3377 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003378 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003379 printf("Cannot find a compatible Vulkan installable client driver "
3380 "(ICD).\nExiting ...\n");
3381 fflush(stdout);
3382 exit(1);
3383 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003384
3385 setup = xcb_get_setup(demo->connection);
3386 iter = xcb_setup_roots_iterator(setup);
3387 while (scr-- > 0)
3388 xcb_screen_next(&iter);
3389
3390 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003391#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3392 demo->display = wl_display_connect(NULL);
3393
3394 if (demo->display == NULL) {
3395 printf("Cannot find a compatible Vulkan installable client driver "
3396 "(ICD).\nExiting ...\n");
3397 fflush(stdout);
3398 exit(1);
3399 }
3400
3401 demo->registry = wl_display_get_registry(demo->display);
3402 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3403 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003404#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003405#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003406}
3407
Karl Schultze4dc75c2016-02-02 15:37:51 -07003408static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003409 vec3 eye = {0.0f, 3.0f, 5.0f};
3410 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003411 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003412
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003413 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003414 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003415 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003416
Piers Daniell735ee532015-02-23 16:23:13 -07003417 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003418 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003419 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003420 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003421 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003422 if ((strcmp(argv[i], "--present_mode") == 0) &&
3423 (i < argc - 1)) {
3424 demo->presentMode = atoi(argv[i+1]);
3425 i++;
3426 continue;
3427 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003428 if (strcmp(argv[i], "--break") == 0) {
3429 demo->use_break = true;
3430 continue;
3431 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003432 if (strcmp(argv[i], "--validate") == 0) {
3433 demo->validate = true;
3434 continue;
3435 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003436 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3437 demo->validate = true;
3438 demo->validate_checks_disabled = true;
3439 continue;
3440 }
Tony Barbour2593b182016-04-19 10:57:58 -06003441 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003442 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003443 continue;
3444 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003445 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3446 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3447 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003448 i++;
3449 continue;
3450 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003451 if (strcmp(argv[i], "--suppress_popups") == 0) {
3452 demo->suppress_popups = true;
3453 continue;
3454 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003455
Cody Northropaf28a532016-09-27 10:50:57 -06003456#if defined(ANDROID)
3457 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3458#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003459 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Tony Barbour291d57b2016-10-21 14:47:07 -06003460 "[--c <framecount>] [--suppress_popups] [--present_mode <present mode enum>]\n"
3461 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3462 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3463 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3464 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3465 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3466 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003467 fflush(stderr);
3468 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003469#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003470 }
3471
Tony Barbour153cb062016-12-07 13:43:36 -07003472 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003474 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003475
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003476 demo->width = 500;
3477 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003478
Tony Barbour66a56c32016-09-21 13:10:56 -06003479 demo->spin_angle = 4.0f;
3480 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003481 demo->pause = false;
3482
Karl Schultze4dc75c2016-02-02 15:37:51 -07003483 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3484 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003485 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3486 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003487
3488 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003489}
3490
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003491#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003492// Include header required for parsing the command line options.
3493#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003494
Karl Schultze4dc75c2016-02-02 15:37:51 -07003495int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3496 int nCmdShow) {
3497 MSG msg; // message
3498 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003499 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003500 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003501
Karl Schultze4dc75c2016-02-02 15:37:51 -07003502 // Use the CommandLine functions to get the command line arguments.
3503 // Unfortunately, Microsoft outputs
3504 // this information as wide characters for Unicode, and we simply want the
3505 // Ascii version to be compatible
3506 // with the non-Windows side. So, we have to convert the information to
3507 // Ascii character strings.
3508 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3509 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003510 argc = 0;
3511 }
3512
Karl Schultze4dc75c2016-02-02 15:37:51 -07003513 if (argc > 0) {
3514 argv = (char **)malloc(sizeof(char *) * argc);
3515 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003516 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003517 } else {
3518 for (int iii = 0; iii < argc; iii++) {
3519 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003520 size_t numConverted = 0;
3521
Karl Schultze4dc75c2016-02-02 15:37:51 -07003522 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3523 if (argv[iii] != NULL) {
3524 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3525 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003526 }
3527 }
3528 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003529 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003530 argv = NULL;
3531 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003532
3533 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003534
3535 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003536 if (argc > 0 && argv != NULL) {
3537 for (int iii = 0; iii < argc; iii++) {
3538 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003539 free(argv[iii]);
3540 }
3541 }
3542 free(argv);
3543 }
3544
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003545 demo.connection = hInstance;
3546 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003547 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003548 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003549
3550 demo_prepare(&demo);
3551
Karl Schultze4dc75c2016-02-02 15:37:51 -07003552 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003553
3554 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003555 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003556 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003557 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003558 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003559 done = true; // if found, quit app
3560 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003561 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003562 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003563 DispatchMessage(&msg);
3564 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003565 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003566 }
3567
3568 demo_cleanup(&demo);
3569
Karl Schultze4dc75c2016-02-02 15:37:51 -07003570 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003571}
Bill Hollingsf4352142017-02-14 22:58:56 -05003572
3573#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3574static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003575 const char* argv[] = { "CubeSample" };
3576 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003577
Tony Barbourc65cd752017-03-13 15:29:35 -06003578 demo_init(demo, argc, (char**)argv);
3579 demo->window = view;
3580 demo_init_vk_swapchain(demo);
3581 demo_prepare(demo);
3582 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003583}
3584
3585static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003586 // Wait for work to finish before updating MVP.
3587 vkDeviceWaitIdle(demo->device);
3588 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003589
Tony Barbourc65cd752017-03-13 15:29:35 -06003590 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003591}
3592
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003593#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3594#include <android/log.h>
3595#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003596#include "android_util.h"
3597
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003598static bool initialized = false;
3599static bool active = false;
3600struct demo demo;
3601
3602static int32_t processInput(struct android_app* app, AInputEvent* event) {
3603 return 0;
3604}
3605
3606static void processCommand(struct android_app* app, int32_t cmd) {
3607 switch(cmd) {
3608 case APP_CMD_INIT_WINDOW: {
3609 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003610 // We're getting a new window. If the app is starting up, we
3611 // need to initialize. If the app has already been
3612 // initialized, that means that we lost our previous window,
3613 // which means that we have a lot of work to do. At a minimum,
3614 // we need to destroy the swapchain and surface associated with
3615 // the old window, and create a new surface and swapchain.
3616 // However, since there are a lot of other objects/state that
3617 // is tied to the swapchain, it's easiest to simply cleanup and
3618 // start over (i.e. use a brute-force approach of re-starting
3619 // the app)
3620 if (demo.prepared) {
3621 demo_cleanup(&demo);
3622 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003623
3624 // Parse Intents into argc, argv
3625 // Use the following key to send arguments, i.e.
3626 // --es args "--validate"
3627 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003628 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003629 int argc = 0;
3630 char** argv = get_args(app, key, appTag, &argc);
3631
3632 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3633 for (int i = 0; i < argc; i++)
3634 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3635
3636 demo_init(&demo, argc, argv);
3637
3638 // Free the argv malloc'd by get_args
3639 for (int i = 0; i < argc; i++)
3640 free(argv[i]);
3641
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003642 demo.window = (void*)app->window;
3643 demo_init_vk_swapchain(&demo);
3644 demo_prepare(&demo);
3645 initialized = true;
3646 }
3647 break;
3648 }
3649 case APP_CMD_GAINED_FOCUS: {
3650 active = true;
3651 break;
3652 }
3653 case APP_CMD_LOST_FOCUS: {
3654 active = false;
3655 break;
3656 }
3657 }
3658}
3659
3660void android_main(struct android_app *app)
3661{
3662 app_dummy();
3663
Cody Northrop8707a6e2016-04-26 19:59:19 -06003664#ifdef ANDROID
3665 int vulkanSupport = InitVulkan();
3666 if (vulkanSupport == 0)
3667 return;
3668#endif
3669
Ian Elliottf05d5d12016-05-17 12:03:35 -06003670 demo.prepared = false;
3671
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003672 app->onAppCmd = processCommand;
3673 app->onInputEvent = processInput;
3674
3675 while(1) {
3676 int events;
3677 struct android_poll_source* source;
3678 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3679 if (source) {
3680 source->process(app, source);
3681 }
3682
3683 if (app->destroyRequested != 0) {
3684 demo_cleanup(&demo);
3685 return;
3686 }
3687 }
3688 if (initialized && active) {
3689 demo_run(&demo);
3690 }
3691 }
3692
3693}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003694#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003695int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003696 struct demo demo;
3697
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003698 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003699#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003700 demo_create_xcb_window(&demo);
3701#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3702 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003703#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3704 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003705#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003706#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003707
Tony Barbourcc69f452015-10-08 13:59:42 -06003708 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003709
3710 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003711
Tony Barbour153cb062016-12-07 13:43:36 -07003712#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003713 demo_run_xcb(&demo);
3714#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3715 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003716#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3717 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003718#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003719#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3720 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003721#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003722
3723 demo_cleanup(&demo);
3724
Karl Schultz0218c002016-03-22 17:06:13 -06003725 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003726}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003727#endif