blob: 70fd9d5ae7fb4c4eb5fcf1942803f8ca6c41ed01 [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;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600394 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600395 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700396 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
397 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
398 VkDebugReportCallbackEXT msg_callback;
399 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600400
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600401 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600402 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403};
404
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700405VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
406 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600407 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600408 char *message = (char *)malloc(strlen(pMsg) + 100);
409
410 assert(message);
411
Cody Northropaf28a532016-09-27 10:50:57 -0600412 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
413 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600414 validation_error = 1;
415 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600416 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
417 validation_error = 1;
418 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600419 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600420 validation_error = 1;
421 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
422 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
423 validation_error = 1;
424 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
425 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600426 validation_error = 1;
427 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600428 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600429 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600430 }
431
432#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600433
Tony Barbour602ca4f2016-09-12 14:02:43 -0600434 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600435 struct demo *demo = (struct demo*) pUserData;
436 if (!demo->suppress_popups)
437 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600438 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600439
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600440#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600441
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600442 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600443 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600444 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600445 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600446 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600447 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600448 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600449 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600450 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600451 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600452 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600453 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600454 }
Cody Northropaf28a532016-09-27 10:50:57 -0600455
lenny-lunargfbe22392016-06-06 11:07:53 -0600456#else
Cody Northropaf28a532016-09-27 10:50:57 -0600457
lenny-lunargfbe22392016-06-06 11:07:53 -0600458 printf("%s\n", message);
459 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600460
lenny-lunargfbe22392016-06-06 11:07:53 -0600461#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600462
lenny-lunargfbe22392016-06-06 11:07:53 -0600463 free(message);
464
Cody Northropaf28a532016-09-27 10:50:57 -0600465 //clang-format on
466
lenny-lunargfbe22392016-06-06 11:07:53 -0600467 /*
468 * false indicates that layer should not bail-out of an
469 * API call that had validation failures. This may mean that the
470 * app dies inside the driver due to invalid parameter(s).
471 * That's what would happen without validation layers, so we'll
472 * keep that behavior here.
473 */
474 return false;
475}
476
Ian Elliottcf074d32015-10-16 18:02:43 -0600477// Forward declaration:
478static void demo_resize(struct demo *demo);
479
Karl Schultze4dc75c2016-02-02 15:37:51 -0700480static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
481 VkFlags requirements_mask,
482 uint32_t *typeIndex) {
483 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600484 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700485 if ((typeBits & 1) == 1) {
486 // Type is available, does it match user properties?
487 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
488 requirements_mask) == requirements_mask) {
489 *typeIndex = i;
490 return true;
491 }
492 }
493 typeBits >>= 1;
494 }
495 // No memory types matched, return failure
496 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600497}
498
Karl Schultze4dc75c2016-02-02 15:37:51 -0700499static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600500 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600501
Tony Barbource7524e2016-07-28 12:01:45 -0600502 // This function could get called twice if the texture uses a staging buffer
503 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505 return;
506
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600507 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600508 assert(!err);
509
Tony Barbource7524e2016-07-28 12:01:45 -0600510 VkFence fence;
511 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
512 .pNext = NULL,
513 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700514 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
515 assert(!err);
516
Karl Schultze4dc75c2016-02-02 15:37:51 -0700517 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700518 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
519 .pNext = NULL,
520 .waitSemaphoreCount = 0,
521 .pWaitSemaphores = NULL,
522 .pWaitDstStageMask = NULL,
523 .commandBufferCount = 1,
524 .pCommandBuffers = cmd_bufs,
525 .signalSemaphoreCount = 0,
526 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600527
Tony Barbource7524e2016-07-28 12:01:45 -0600528 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 assert(!err);
530
Tony Barbource7524e2016-07-28 12:01:45 -0600531 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532 assert(!err);
533
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600534 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600535 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600536 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600537}
538
Karl Schultze4dc75c2016-02-02 15:37:51 -0700539static void demo_set_image_layout(struct demo *demo, VkImage image,
540 VkImageAspectFlags aspectMask,
541 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700542 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600543 VkAccessFlagBits srcAccessMask,
544 VkPipelineStageFlags src_stages,
545 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600546 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600548 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600549 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700551 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800552 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553 .oldLayout = old_image_layout,
554 .newLayout = new_image_layout,
555 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700556 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557
Tony Barboureb5077b2016-10-19 15:23:36 -0600558 switch (new_image_layout) {
559 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600560 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600561 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
562 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600563
Tony Barboureb5077b2016-10-19 15:23:36 -0600564 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700565 image_memory_barrier.dstAccessMask =
566 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600567 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700568
Tony Barboureb5077b2016-10-19 15:23:36 -0600569 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700570 image_memory_barrier.dstAccessMask =
571 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600572 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700573
Tony Barboureb5077b2016-10-19 15:23:36 -0600574 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700575 image_memory_barrier.dstAccessMask =
576 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600577 break;
578
579 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
580 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
581 break;
582
583 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
584 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
585 break;
586
587 default:
588 image_memory_barrier.dstAccessMask = 0;
589 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600590 }
591
Tony Barbourf165b5d2016-10-03 16:01:41 -0600592
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600593 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600594
Karl Schultze4dc75c2016-02-02 15:37:51 -0700595 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
596 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600597}
598
Karl Schultze4dc75c2016-02-02 15:37:51 -0700599static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700600 const VkCommandBufferBeginInfo cmd_buf_info = {
601 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
602 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600603 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700604 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700605 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800606 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700607 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
608 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800609 };
610 const VkRenderPassBeginInfo rp_begin = {
611 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
612 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800613 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700614 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800615 .renderArea.offset.x = 0,
616 .renderArea.offset.y = 0,
617 .renderArea.extent.width = demo->width,
618 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600619 .clearValueCount = 2,
620 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700621 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800622 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600624 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800626 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700627 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
628 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700629 demo->pipeline_layout, 0, 1,
630 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
631 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600632 VkViewport viewport;
633 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700634 viewport.height = (float)demo->height;
635 viewport.width = (float)demo->width;
636 viewport.minDepth = (float)0.0f;
637 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700638 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600639
640 VkRect2D scissor;
641 memset(&scissor, 0, sizeof(scissor));
642 scissor.extent.width = demo->width;
643 scissor.extent.height = demo->height;
644 scissor.offset.x = 0;
645 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700646 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600647 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600648 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600649 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800650 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600651
Tony Barbour22e06272016-09-07 16:07:56 -0600652 if (demo->separate_present_queue) {
653 // We have to transfer ownership from the graphics queue family to the
654 // present queue family to be able to present. Note that we don't have
655 // to transfer from present queue family back to graphics queue family at
656 // the start of the next frame because we don't care about the image's
657 // contents at that point.
658 VkImageMemoryBarrier image_ownership_barrier = {
659 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
660 .pNext = NULL,
661 .srcAccessMask = 0,
662 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
663 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
664 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
665 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
666 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700667 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600668 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
669
670 vkCmdPipelineBarrier(cmd_buf,
671 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600672 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600673 0, NULL, 0, NULL, 1, &image_ownership_barrier);
674 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600675 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600676 assert(!err);
677}
678
Tony Barbour22e06272016-09-07 16:07:56 -0600679void demo_build_image_ownership_cmd(struct demo *demo, int i) {
680 VkResult U_ASSERT_ONLY err;
681
682 const VkCommandBufferBeginInfo cmd_buf_info = {
683 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
684 .pNext = NULL,
685 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
686 .pInheritanceInfo = NULL,
687 };
Tony Barboura72d9492017-01-11 13:35:09 -0700688 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600689 &cmd_buf_info);
690 assert(!err);
691
692 VkImageMemoryBarrier image_ownership_barrier = {
693 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
694 .pNext = NULL,
695 .srcAccessMask = 0,
696 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
697 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
698 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
699 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
700 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700701 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600702 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
703
Tony Barboura72d9492017-01-11 13:35:09 -0700704 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600705 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
706 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
707 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700708 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600709 assert(!err);
710}
711
Karl Schultze4dc75c2016-02-02 15:37:51 -0700712void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600713 mat4x4 MVP, Model, VP;
714 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600715 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600716 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600717
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600718 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600719
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700720 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600721 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700722 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
723 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600724 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600725
Tony Barboura72d9492017-01-11 13:35:09 -0700726 vkWaitForFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence,
727 VK_TRUE, UINT64_MAX);
728
729 err = vkMapMemory(demo->device,
730 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
731 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600732 assert(!err);
733
Karl Schultze4dc75c2016-02-02 15:37:51 -0700734 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600735
Tony Barboura72d9492017-01-11 13:35:09 -0700736 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600737}
738
Karl Schultze4dc75c2016-02-02 15:37:51 -0700739static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600740 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600741
szdarkhackd322ff02016-10-08 09:51:22 +0300742 // Ensure no more than FRAME_LAG presentations are outstanding
743 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
744 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600745
Ian Elliottaaae5352015-07-06 14:27:58 -0600746 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700747 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -0700748 demo->image_acquired_semaphores[demo->frame_index],
749 demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600750 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600751
Tony Barboura72d9492017-01-11 13:35:09 -0700752 demo_update_data_buffer(demo);
753
Ian Elliottcf074d32015-10-16 18:02:43 -0600754 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
755 // demo->swapchain is out of date (e.g. the window was resized) and
756 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600757 demo->frame_index += 1;
758 demo->frame_index %= FRAME_LAG;
759
Ian Elliottcf074d32015-10-16 18:02:43 -0600760 demo_resize(demo);
761 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600762 return;
763 } else if (err == VK_SUBOPTIMAL_KHR) {
764 // demo->swapchain is not as optimal as it could be, but the platform's
765 // presentation engine will still present the image correctly.
766 } else {
767 assert(!err);
768 }
Tony Barbource7524e2016-07-28 12:01:45 -0600769 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600770 // that the image won't be rendered to until the presentation
771 // engine has fully released ownership to the application, and it is
772 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -0600773 VkPipelineStageFlags pipe_stage_flags;
774 VkSubmitInfo submit_info;
775 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
776 submit_info.pNext = NULL;
777 submit_info.pWaitDstStageMask = &pipe_stage_flags;
778 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
779 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600780 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600781 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -0700782 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600783 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600784 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -0700785 vkResetFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence);
786 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
787 demo->swapchain_image_resources[demo->current_buffer].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600788 assert(!err);
789
Tony Barbour22e06272016-09-07 16:07:56 -0600790 if (demo->separate_present_queue) {
791 // If we are using separate queues, change image ownership to the
792 // present queue before presenting, waiting for the draw complete
793 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -0700794 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600795 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
796 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600797 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600798 submit_info.commandBufferCount = 1;
799 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -0700800 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600801 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600802 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600803 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
804 assert(!err);
805 }
806
807 // If we are using separate queues we have to wait for image ownership,
808 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600809 VkPresentInfoKHR present = {
810 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600811 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600812 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -0600813 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -0600814 ? &demo->image_ownership_semaphores[demo->frame_index]
815 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600816 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700817 .pSwapchains = &demo->swapchain,
818 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600819 };
820
Tony Barboura2a67812016-07-18 13:21:06 -0600821 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -0600822 demo->frame_index += 1;
823 demo->frame_index %= FRAME_LAG;
824
Ian Elliottcf074d32015-10-16 18:02:43 -0600825 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
826 // demo->swapchain is out of date (e.g. the window was resized) and
827 // must be recreated:
828 demo_resize(demo);
829 } else if (err == VK_SUBOPTIMAL_KHR) {
830 // demo->swapchain is not as optimal as it could be, but the platform's
831 // presentation engine will still present the image correctly.
832 } else {
833 assert(!err);
834 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835}
836
Karl Schultze4dc75c2016-02-02 15:37:51 -0700837static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600838 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600839 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600840
Ian Elliottb08e0d92015-11-20 11:55:46 -0700841 // Check the surface capabilities and formats
842 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700843 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
844 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600845 assert(!err);
846
Ian Elliott8528eff2015-08-07 15:56:59 -0600847 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700848 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
849 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600850 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600851 VkPresentModeKHR *presentModes =
852 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600853 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700854 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
855 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600856 assert(!err);
857
Ian Elliotta0781972015-08-21 15:09:33 -0600858 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600859 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
860 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
861 // If the surface size is undefined, the size is set to the size
862 // of the images requested, which must fit within the minimum and
863 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600864 swapchainExtent.width = demo->width;
865 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600866
867 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
868 swapchainExtent.width = surfCapabilities.minImageExtent.width;
869 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
870 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
871 }
872
873 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
874 swapchainExtent.height = surfCapabilities.minImageExtent.height;
875 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
876 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
877 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700878 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600879 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700880 swapchainExtent = surfCapabilities.currentExtent;
881 demo->width = surfCapabilities.currentExtent.width;
882 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600883 }
884
Tony Barbour66a56c32016-09-21 13:10:56 -0600885 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -0600886 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -0600887 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -0600888
Ian Elliottb18fdde2016-10-03 12:11:12 -0600889 // There are times when you may wish to use another present mode. The
890 // following code shows how to select them, and the comments provide some
891 // reasons you may wish to use them.
892 //
893 // It should be noted that Vulkan 1.0 doesn't provide a method for
894 // synchronizing rendering with the presentation engine's display. There
895 // is a method provided for throttling rendering with the display, but
896 // there are some presentation engines for which this method will not work.
897 // If an application doesn't throttle its rendering, and if it renders much
898 // faster than the refresh rate of the display, this can waste power on
899 // mobile devices. That is because power is being spent rendering images
900 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -0600901
Ian Elliottb18fdde2016-10-03 12:11:12 -0600902 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
903 // tearing, or have some way of synchronizing their rendering with the
904 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600905 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
906 // generally render a new presentable image every refresh cycle, but are
907 // occasionally early. In this case, the application wants the new image
908 // to be displayed instead of the previously-queued-for-presentation image
909 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -0600910 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
911 // render a new presentable image every refresh cycle, but are occasionally
912 // late. In this case (perhaps because of stuttering/latency concerns),
913 // the application wants the late image to be immediately displayed, even
914 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -0600915
916 if (demo->presentMode != swapchainPresentMode) {
917
918 for (size_t i = 0; i < presentModeCount; ++i) {
919 if (presentModes[i] == demo->presentMode) {
920 swapchainPresentMode = demo->presentMode;
921 break;
922 }
Ian Elliottb18fdde2016-10-03 12:11:12 -0600923 }
924 }
Tony Barbour291d57b2016-10-21 14:47:07 -0600925 if (swapchainPresentMode != demo->presentMode) {
926 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
927 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600928
Tony Barbour84376fe2017-01-06 15:54:22 -0700929 // Determine the number of VkImages to use in the swap chain.
930 // Application desires to acquire 3 images at a time for triple
931 // buffering
932 uint32_t desiredNumOfSwapchainImages = 3;
933 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
934 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
935 }
Ian Elliottcf7f7482016-08-19 03:46:58 -0600936 // If maxImageCount is 0, we can ask for as many images as we want;
937 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700938 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600939 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600940 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600941 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600942 }
943
Tony Barbour9fd6d352015-09-16 15:01:32 -0600944 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700945 if (surfCapabilities.supportedTransforms &
946 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700947 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600948 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700949 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600950 }
951
Tony Barboura2a67812016-07-18 13:21:06 -0600952 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600953 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800954 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700955 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600956 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800957 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600958 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700959 .imageExtent =
960 {
961 .width = swapchainExtent.width, .height = swapchainExtent.height,
962 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700963 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600964 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700965 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700966 .imageArrayLayers = 1,
967 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
968 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600969 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600970 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600971 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600972 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600973 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600974 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600975 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700976 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800977 assert(!err);
978
Ian Elliottcf074d32015-10-16 18:02:43 -0600979 // If we just re-created an existing swapchain, we should destroy the old
980 // swapchain at this point.
981 // Note: destroying the swapchain also cleans up all its associated
982 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800983 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700984 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600985 }
986
987 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600988 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600989 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800990
Karl Schultze4dc75c2016-02-02 15:37:51 -0700991 VkImage *swapchainImages =
992 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600993 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600994 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600995 &demo->swapchainImageCount,
996 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600997 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600998
Tony Barboura72d9492017-01-11 13:35:09 -0700999 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001000 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001001 assert(demo->swapchain_image_resources);
1002
1003 VkFenceCreateInfo fence_ci = {
1004 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1005 .pNext = NULL,
1006 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1007 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001008
Ian Elliotta0781972015-08-21 15:09:33 -06001009 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001010 VkImageViewCreateInfo color_image_view = {
1011 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001012 .pNext = NULL,
1013 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001014 .components =
1015 {
1016 .r = VK_COMPONENT_SWIZZLE_R,
1017 .g = VK_COMPONENT_SWIZZLE_G,
1018 .b = VK_COMPONENT_SWIZZLE_B,
1019 .a = VK_COMPONENT_SWIZZLE_A,
1020 },
1021 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1022 .baseMipLevel = 0,
1023 .levelCount = 1,
1024 .baseArrayLayer = 0,
1025 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001026 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1027 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028 };
1029
Tony Barboura72d9492017-01-11 13:35:09 -07001030 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001031
Tony Barboura72d9492017-01-11 13:35:09 -07001032 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001033
Karl Schultze4dc75c2016-02-02 15:37:51 -07001034 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001035 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001036 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001037
Tony Barboura72d9492017-01-11 13:35:09 -07001038 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1039 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001040 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001041
Mark Young04fd3d82016-01-25 14:43:50 -07001042 if (NULL != presentModes) {
1043 free(presentModes);
1044 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045}
1046
Karl Schultze4dc75c2016-02-02 15:37:51 -07001047static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001048 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001049 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001050 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001051 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001052 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001053 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001054 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001056 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001057 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001058 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001059 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001060 .flags = 0,
1061 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001062
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001063 VkImageViewCreateInfo view = {
1064 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001065 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001066 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001067 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001068 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1069 .baseMipLevel = 0,
1070 .levelCount = 1,
1071 .baseArrayLayer = 0,
1072 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001074 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001076
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001077 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001078 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001079 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001080
1081 demo->depth.format = depth_format;
1082
1083 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001084 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085 assert(!err);
1086
Karl Schultze4dc75c2016-02-02 15:37:51 -07001087 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001088 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001089
Chia-I Wuf48700b2015-11-10 16:21:09 +08001090 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001091 demo->depth.mem_alloc.pNext = NULL;
1092 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1093 demo->depth.mem_alloc.memoryTypeIndex = 0;
1094
Karl Schultze4dc75c2016-02-02 15:37:51 -07001095 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001096 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001097 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001098 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001099
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001100 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001101 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1102 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001103 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001104
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001105 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001106 err =
1107 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001108 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001109
1110 /* create image view */
1111 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001112 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001113 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114}
1115
Tony Barbour1a86d032015-09-21 15:17:33 -06001116/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001118 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001119
1120#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001121 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001122#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001123
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001124#ifdef __ANDROID__
1125#include <lunarg.ppm.h>
1126 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001127 cPtr = (char*)lunarg_ppm;
1128 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001129 return false;
1130 }
1131 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001132 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001133 if (rgba_data == NULL) {
1134 return true;
1135 }
1136 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001137 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001138 return false;
1139 }
1140 while(strncmp(cPtr++, "\n", 1));
1141
1142 for (int y = 0; y < *height; y++) {
1143 uint8_t *rowPtr = rgba_data;
1144 for (int x = 0; x < *width; x++) {
1145 memcpy(rowPtr, cPtr, 3);
1146 rowPtr[3] = 255; /* Alpha of 1 */
1147 rowPtr += 4;
1148 cPtr += 3;
1149 }
1150 rgba_data += layout->rowPitch;
1151 }
1152
1153 return true;
1154#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001155 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001156 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001157
Tony Barbour1a86d032015-09-21 15:17:33 -06001158 if (!fPtr)
1159 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001160
Tony Barbour1a86d032015-09-21 15:17:33 -06001161 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001162 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1163 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001164 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001165 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001166
Tony Barbour1a86d032015-09-21 15:17:33 -06001167 do {
1168 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001169 if (cPtr == NULL) {
1170 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001171 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001172 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001173 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001174
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001175 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001176 if (rgba_data == NULL) {
1177 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001178 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001179 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001180 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001181 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001182 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1183 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001184 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001185 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001186
Karl Schultze4dc75c2016-02-02 15:37:51 -07001187 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001188 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001189 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001190 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001191 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001192 rowPtr[3] = 255; /* Alpha of 1 */
1193 rowPtr += 4;
1194 }
1195 rgba_data += layout->rowPitch;
1196 }
1197 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001198 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001199#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001200}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001201
Karl Schultze4dc75c2016-02-02 15:37:51 -07001202static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001203 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001204 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001205 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001206 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001207 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001208 int32_t tex_width;
1209 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001210 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001211 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001212
Karl Schultze4dc75c2016-02-02 15:37:51 -07001213 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001214 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001215 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001217 tex_obj->tex_width = tex_width;
1218 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001220 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001221 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001222 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001223 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001224 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001225 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001227 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001228 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001229 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001230 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001232 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001233 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001235 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001236
Karl Schultze4dc75c2016-02-02 15:37:51 -07001237 err =
1238 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001239 assert(!err);
1240
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001241 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001242
Chia-I Wuf48700b2015-11-10 16:21:09 +08001243 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001244 tex_obj->mem_alloc.pNext = NULL;
1245 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1246 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001247
Karl Schultze4dc75c2016-02-02 15:37:51 -07001248 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1249 required_props,
1250 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001251 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001252
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001253 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001254 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001255 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001256 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001257
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001258 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001259 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001260 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001261
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001262 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001263 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001264 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001265 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001266 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001267 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001268 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001269 void *data;
1270
Karl Schultze4dc75c2016-02-02 15:37:51 -07001271 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1272 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273
Karl Schultze4dc75c2016-02-02 15:37:51 -07001274 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1275 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001276 assert(!err);
1277
1278 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1279 fprintf(stderr, "Error loading texture: %s\n", filename);
1280 }
1281
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001282 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001283 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001284
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001285 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001286}
1287
Karl Schultze4dc75c2016-02-02 15:37:51 -07001288static void demo_destroy_texture_image(struct demo *demo,
1289 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001290 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001291 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1292 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001293}
1294
Karl Schultze4dc75c2016-02-02 15:37:51 -07001295static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001296 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001297 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001298 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001299
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001300 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001301
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001303 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001304
Karl Schultze4dc75c2016-02-02 15:37:51 -07001305 if ((props.linearTilingFeatures &
1306 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1307 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001308 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001309 demo_prepare_texture_image(
1310 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1311 VK_IMAGE_USAGE_SAMPLED_BIT,
1312 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1313 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001314 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1315 // shader to run until layout transition completes
1316 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1317 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1318 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1319 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001320 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 } else if (props.optimalTilingFeatures &
1322 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001323 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001324
Tony Barbour16156cb2016-10-19 14:15:49 -06001325 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001326 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001327 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001328 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1329 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1330 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001331
Karl Schultze4dc75c2016-02-02 15:37:51 -07001332 demo_prepare_texture_image(
1333 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1334 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1335 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001336
Tony Barbour16156cb2016-10-19 14:15:49 -06001337 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001338 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001339 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001340 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001341 VK_ACCESS_HOST_WRITE_BIT,
1342 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1343 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001344
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001345 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001346 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001347 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001348 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001349 VK_ACCESS_HOST_WRITE_BIT,
1350 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1351 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001352
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001353 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001354 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1355 .srcOffset = {0, 0, 0},
1356 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1357 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001358 .extent = {demo->staging_texture.tex_width,
1359 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001360 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001361 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001362 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001363 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1364 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001365
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001366 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001367 VK_IMAGE_ASPECT_COLOR_BIT,
1368 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001369 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001370 VK_ACCESS_TRANSFER_WRITE_BIT,
1371 VK_PIPELINE_STAGE_TRANSFER_BIT,
1372 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001373
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001374 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001375 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1376 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001377 }
1378
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001379 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001382 .magFilter = VK_FILTER_NEAREST,
1383 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001384 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001385 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1386 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1387 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001389 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001390 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001391 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392 .minLod = 0.0f,
1393 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001394 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001395 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001397
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001398 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001399 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001401 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001402 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001403 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001404 .components =
1405 {
1406 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1407 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1408 },
1409 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001410 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001412
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001414 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001415 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416 assert(!err);
1417
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418 /* create image view */
1419 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001420 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001421 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422 assert(!err);
1423 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425
Tony Barboura72d9492017-01-11 13:35:09 -07001426void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001427 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001428 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001429 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001430 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001431 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001432 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001433 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001435
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001436 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001437 mat4x4_mul(MVP, VP, demo->model_matrix);
1438 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001439 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001440
Karl Schultza2615462017-01-20 13:19:20 -07001441 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001442 data.position[i][0] = g_vertex_buffer_data[i * 3];
1443 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1444 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001445 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001446 data.attr[i][0] = g_uv_buffer_data[2 * i];
1447 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001448 data.attr[i][2] = 0;
1449 data.attr[i][3] = 0;
1450 }
1451
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001452 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001453 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001454 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001455 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001456
Tony Barboura72d9492017-01-11 13:35:09 -07001457 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1458 err =
1459 vkCreateBuffer(demo->device, &buf_info, NULL,
1460 &demo->swapchain_image_resources[i].uniform_buffer);
1461 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001462
Tony Barboura72d9492017-01-11 13:35:09 -07001463 vkGetBufferMemoryRequirements(demo->device,
1464 demo->swapchain_image_resources[i].uniform_buffer,
1465 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001466
Tony Barboura72d9492017-01-11 13:35:09 -07001467 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1468 mem_alloc.pNext = NULL;
1469 mem_alloc.allocationSize = mem_reqs.size;
1470 mem_alloc.memoryTypeIndex = 0;
1471
1472 pass = memory_type_from_properties(
1473 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001474 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001475 &mem_alloc.memoryTypeIndex);
1476 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001477
Tony Barboura72d9492017-01-11 13:35:09 -07001478 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1479 &demo->swapchain_image_resources[i].uniform_memory);
1480 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001481
Tony Barboura72d9492017-01-11 13:35:09 -07001482 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1483 VK_WHOLE_SIZE, 0, (void **)&pData);
1484 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001485
Tony Barboura72d9492017-01-11 13:35:09 -07001486 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001487
Tony Barboura72d9492017-01-11 13:35:09 -07001488 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001489
Tony Barboura72d9492017-01-11 13:35:09 -07001490 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1491 demo->swapchain_image_resources[i].uniform_memory, 0);
1492 assert(!err);
1493 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001494}
1495
Karl Schultze4dc75c2016-02-02 15:37:51 -07001496static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001497 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001498 [0] =
1499 {
1500 .binding = 0,
1501 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1502 .descriptorCount = 1,
1503 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1504 .pImmutableSamplers = NULL,
1505 },
1506 [1] =
1507 {
1508 .binding = 1,
1509 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1510 .descriptorCount = DEMO_TEXTURE_COUNT,
1511 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1512 .pImmutableSamplers = NULL,
1513 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001514 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001515 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001517 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001518 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001519 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001521 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522
Karl Schultze4dc75c2016-02-02 15:37:51 -07001523 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1524 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001525 assert(!err);
1526
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001527 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001528 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1529 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001530 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001531 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001532 };
1533
Karl Schultze4dc75c2016-02-02 15:37:51 -07001534 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001535 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001536 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537}
1538
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001540 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1541 // because at the start of the renderpass, we don't care about their contents.
1542 // At the start of the subpass, the color attachment's layout will be transitioned
1543 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1544 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1545 // the renderpass, the color attachment's layout will be transitioned to
1546 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1547 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001548 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001549 [0] =
1550 {
1551 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001552 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001553 .samples = VK_SAMPLE_COUNT_1_BIT,
1554 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1555 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1556 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1557 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001558 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001559 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001560 },
1561 [1] =
1562 {
1563 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001564 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001565 .samples = VK_SAMPLE_COUNT_1_BIT,
1566 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1567 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1568 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1569 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1570 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001571 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001572 .finalLayout =
1573 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1574 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001575 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001576 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001577 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001578 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001579 const VkAttachmentReference depth_reference = {
1580 .attachment = 1,
1581 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1582 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001583 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001584 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1585 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001586 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001587 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001588 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001589 .pColorAttachments = &color_reference,
1590 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001591 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001592 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001593 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001594 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001595 const VkRenderPassCreateInfo rp_info = {
1596 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1597 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001598 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001599 .attachmentCount = 2,
1600 .pAttachments = attachments,
1601 .subpassCount = 1,
1602 .pSubpasses = &subpass,
1603 .dependencyCount = 0,
1604 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001605 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001606 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001607
Chia-I Wu63636062015-10-26 21:10:41 +08001608 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001609 assert(!err);
1610}
1611
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001612//TODO: Merge shader reading
1613#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001614static VkShaderModule
1615demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001616 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001617 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001618 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001619
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001620 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1621 moduleCreateInfo.pNext = NULL;
1622
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001623 moduleCreateInfo.codeSize = size;
1624 moduleCreateInfo.pCode = code;
1625 moduleCreateInfo.flags = 0;
1626 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1627 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001628
1629 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630}
1631
Karl Schultze4dc75c2016-02-02 15:37:51 -07001632char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001633 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001634 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001635 void *shader_code;
1636
Bill Hollingsf4352142017-02-14 22:58:56 -05001637#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001638 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001639#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001640
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001641 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001642 if (!fp)
1643 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001644
1645 fseek(fp, 0L, SEEK_END);
1646 size = ftell(fp);
1647
1648 fseek(fp, 0L, SEEK_SET);
1649
1650 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001651 retval = fread(shader_code, size, 1, fp);
1652 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001653
1654 *psize = size;
1655
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001656 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001657 return shader_code;
1658}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001659#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001660
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001662#ifdef __ANDROID__
1663 VkShaderModuleCreateInfo sh_info = {};
1664 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1665
1666#include "cube.vert.h"
1667 sh_info.codeSize = sizeof(cube_vert);
1668 sh_info.pCode = cube_vert;
1669 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1670 assert(!err);
1671#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001672 void *vertShaderCode;
1673 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001674
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001675 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001676 if (!vertShaderCode) {
1677 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1678 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001679
Karl Schultze4dc75c2016-02-02 15:37:51 -07001680 demo->vert_shader_module =
1681 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001682
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001683 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001684#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001685
1686 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687}
1688
Karl Schultze4dc75c2016-02-02 15:37:51 -07001689static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001690#ifdef __ANDROID__
1691 VkShaderModuleCreateInfo sh_info = {};
1692 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1693
1694#include "cube.frag.h"
1695 sh_info.codeSize = sizeof(cube_frag);
1696 sh_info.pCode = cube_frag;
1697 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1698 assert(!err);
1699#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001700 void *fragShaderCode;
1701 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001702
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001703 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001704 if (!fragShaderCode) {
1705 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
1706 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001707
Karl Schultze4dc75c2016-02-02 15:37:51 -07001708 demo->frag_shader_module =
1709 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001710
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001711 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001712#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001713
1714 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001715}
1716
Karl Schultze4dc75c2016-02-02 15:37:51 -07001717static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001718 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001719 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001720 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001721 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001722 VkPipelineRasterizationStateCreateInfo rs;
1723 VkPipelineColorBlendStateCreateInfo cb;
1724 VkPipelineDepthStencilStateCreateInfo ds;
1725 VkPipelineViewportStateCreateInfo vp;
1726 VkPipelineMultisampleStateCreateInfo ms;
1727 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1728 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001729 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730
Piers Daniellba839d12015-09-29 13:01:09 -06001731 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1732 memset(&dynamicState, 0, sizeof dynamicState);
1733 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1734 dynamicState.pDynamicStates = dynamicStateEnables;
1735
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001737 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001738 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001740 memset(&vi, 0, sizeof(vi));
1741 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1742
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001744 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001745 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746
1747 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001748 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1749 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001750 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001751 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001752 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001753 rs.rasterizerDiscardEnable = VK_FALSE;
1754 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001755 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001756
1757 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001758 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1759 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001760 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001761 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001762 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001763 cb.attachmentCount = 1;
1764 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765
Tony Barbour29645d02015-01-16 14:27:35 -07001766 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001767 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001768 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001769 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1770 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001771 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001772 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1773 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001774
1775 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001776 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001777 ds.depthTestEnable = VK_TRUE;
1778 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001779 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001780 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001781 ds.back.failOp = VK_STENCIL_OP_KEEP;
1782 ds.back.passOp = VK_STENCIL_OP_KEEP;
1783 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001784 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001785 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786
Tony Barbour29645d02015-01-16 14:27:35 -07001787 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001788 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001789 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001790 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001792 // Two stages: vs and fs
1793 pipeline.stageCount = 2;
1794 VkPipelineShaderStageCreateInfo shaderStages[2];
1795 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1796
Karl Schultze4dc75c2016-02-02 15:37:51 -07001797 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1798 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001799 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001800 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001801
Karl Schultze4dc75c2016-02-02 15:37:51 -07001802 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1803 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001804 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001805 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001806
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001807 memset(&pipelineCache, 0, sizeof(pipelineCache));
1808 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001809
Karl Schultze4dc75c2016-02-02 15:37:51 -07001810 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1811 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001812 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001813
Karl Schultze4dc75c2016-02-02 15:37:51 -07001814 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001815 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001816 pipeline.pRasterizationState = &rs;
1817 pipeline.pColorBlendState = &cb;
1818 pipeline.pMultisampleState = &ms;
1819 pipeline.pViewportState = &vp;
1820 pipeline.pDepthStencilState = &ds;
1821 pipeline.pStages = shaderStages;
1822 pipeline.renderPass = demo->render_pass;
1823 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001824
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001825 pipeline.renderPass = demo->render_pass;
1826
Karl Schultze4dc75c2016-02-02 15:37:51 -07001827 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1828 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001829 assert(!err);
1830
Chia-I Wu63636062015-10-26 21:10:41 +08001831 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1832 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001833}
1834
Karl Schultze4dc75c2016-02-02 15:37:51 -07001835static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001836 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001837 [0] =
1838 {
1839 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07001840 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001841 },
1842 [1] =
1843 {
1844 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07001845 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001846 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001847 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001848 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001849 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001850 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001851 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08001852 .poolSizeCount = 2,
1853 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001854 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001855 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001856
Karl Schultze4dc75c2016-02-02 15:37:51 -07001857 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1858 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001859 assert(!err);
1860}
1861
Karl Schultze4dc75c2016-02-02 15:37:51 -07001862static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001863 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001864 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001865 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001866
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001867 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001868 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001869 .pNext = NULL,
1870 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001871 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001872 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07001873
1874 VkDescriptorBufferInfo buffer_info;
1875 buffer_info.offset = 0;
1876 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001877
Chia-I Wuae721ba2015-05-25 16:27:55 +08001878 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07001879 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001880 tex_descs[i].sampler = demo->textures[i].sampler;
1881 tex_descs[i].imageView = demo->textures[i].view;
1882 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001883 }
1884
1885 memset(&writes, 0, sizeof(writes));
1886
1887 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001888 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001889 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07001890 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001891
1892 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001893 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001894 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001895 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001896 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001897
Tony Barboura72d9492017-01-11 13:35:09 -07001898 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1899 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
1900 assert(!err);
1901 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
1902 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1903 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
1904 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1905 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001906}
1907
Karl Schultze4dc75c2016-02-02 15:37:51 -07001908static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001909 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001910 attachments[1] = demo->depth.view;
1911
Chia-I Wuf973e322015-07-08 13:34:24 +08001912 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001913 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1914 .pNext = NULL,
1915 .renderPass = demo->render_pass,
1916 .attachmentCount = 2,
1917 .pAttachments = attachments,
1918 .width = demo->width,
1919 .height = demo->height,
1920 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001921 };
1922 VkResult U_ASSERT_ONLY err;
1923 uint32_t i;
1924
Tony Barbourd8e504f2015-10-08 14:26:24 -06001925 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07001926 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001927 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001928 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08001929 assert(!err);
1930 }
1931}
1932
Karl Schultze4dc75c2016-02-02 15:37:51 -07001933static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001934 VkResult U_ASSERT_ONLY err;
1935
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001936 const VkCommandPoolCreateInfo cmd_pool_info = {
1937 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001938 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001939 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001940 .flags = 0,
1941 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001942 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1943 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001944 assert(!err);
1945
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001946 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001947 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001949 .commandPool = demo->cmd_pool,
1950 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001951 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001952 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06001953 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
1954 assert(!err);
1955 VkCommandBufferBeginInfo cmd_buf_info = {
1956 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1957 .pNext = NULL,
1958 .flags = 0,
1959 .pInheritanceInfo = NULL,
1960 };
1961 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
1962 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001963
1964 demo_prepare_buffers(demo);
1965 demo_prepare_depth(demo);
1966 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07001967 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001969 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001970 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972
Ian Elliotta0781972015-08-21 15:09:33 -06001973 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001974 err =
Tony Barboura72d9492017-01-11 13:35:09 -07001975 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001976 assert(!err);
1977 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001978
Tony Barbour22e06272016-09-07 16:07:56 -06001979 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07001980 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06001981 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
1982 .pNext = NULL,
1983 .queueFamilyIndex = demo->present_queue_family_index,
1984 .flags = 0,
1985 };
Karl Schultza2615462017-01-20 13:19:20 -07001986 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06001987 &demo->present_cmd_pool);
1988 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07001989 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06001990 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
1991 .pNext = NULL,
1992 .commandPool = demo->present_cmd_pool,
1993 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1994 .commandBufferCount = 1,
1995 };
1996 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
1997 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07001998 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06001999 assert(!err);
2000 demo_build_image_ownership_cmd(demo, i);
2001 }
2002 }
2003
Chia-I Wu63ea9262015-03-26 13:14:16 +08002004 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002005 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002006
Chia-I Wuf973e322015-07-08 13:34:24 +08002007 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002008
Ian Elliotta0781972015-08-21 15:09:33 -06002009 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002010 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002011 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002012 }
2013
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002014 /*
2015 * Prepare functions above may generate pipeline commands
2016 * that need to be flushed before beginning the render loop.
2017 */
2018 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002019 if (demo->staging_texture.image) {
2020 demo_destroy_texture_image(demo, &demo->staging_texture);
2021 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002022
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002023 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002024 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002025}
2026
Karl Schultze4dc75c2016-02-02 15:37:51 -07002027static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002028 uint32_t i;
2029
2030 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002031 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002032
Tony Barbour66a56c32016-09-21 13:10:56 -06002033 // Wait for fences from present operations
2034 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002035 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002036 vkDestroyFence(demo->device, demo->fences[i], NULL);
2037 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2038 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2039 if (demo->separate_present_queue) {
2040 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2041 }
2042 }
2043
Tony Barbourd8e504f2015-10-08 14:26:24 -06002044 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002045 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002046 }
Chia-I Wu63636062015-10-26 21:10:41 +08002047 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002048
Chia-I Wu63636062015-10-26 21:10:41 +08002049 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2050 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2051 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2052 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2053 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002054
2055 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002056 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2057 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2058 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2059 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002060 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002061 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002062
Chia-I Wu63636062015-10-26 21:10:41 +08002063 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2064 vkDestroyImage(demo->device, demo->depth.image, NULL);
2065 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002066
Ian Elliotta0781972015-08-21 15:09:33 -06002067 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002068 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002069 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002070 &demo->swapchain_image_resources[i].cmd);
2071 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2072 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2073 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002074 }
Tony Barboura72d9492017-01-11 13:35:09 -07002075 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002076 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002077 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002078
Tony Barbour22e06272016-09-07 16:07:56 -06002079 if (demo->separate_present_queue) {
2080 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002081 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002082 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002083 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002084 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002085 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002086 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002087 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002088 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002089
Tony Barbour153cb062016-12-07 13:43:36 -07002090#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002091 XDestroyWindow(demo->display, demo->xlib_window);
2092 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002093#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002094 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002095 xcb_disconnect(demo->connection);
2096 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002097#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2098 wl_shell_surface_destroy(demo->shell_surface);
2099 wl_surface_destroy(demo->window);
2100 wl_shell_destroy(demo->shell);
2101 wl_compositor_destroy(demo->compositor);
2102 wl_registry_destroy(demo->registry);
2103 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002104#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002105#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002106}
2107
Karl Schultze4dc75c2016-02-02 15:37:51 -07002108static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002109 uint32_t i;
2110
Mike Stroyanbb60b382015-10-28 09:46:57 -06002111 // Don't react to resize until after first initialization.
2112 if (!demo->prepared) {
2113 return;
2114 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002115 // In order to properly resize the window, we must re-create the swapchain
2116 // AND redo the command buffers, etc.
2117 //
2118 // First, perform part of the demo_cleanup() function:
2119 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002120 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002121
2122 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002123 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002124 }
Chia-I Wu63636062015-10-26 21:10:41 +08002125 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002126
Chia-I Wu63636062015-10-26 21:10:41 +08002127 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2128 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2129 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2130 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2131 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002132
2133 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002134 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2135 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2136 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2137 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002138 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002139
Chia-I Wu63636062015-10-26 21:10:41 +08002140 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2141 vkDestroyImage(demo->device, demo->depth.image, NULL);
2142 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002143
Ian Elliottcf074d32015-10-16 18:02:43 -06002144 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002145 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002146 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002147 &demo->swapchain_image_resources[i].cmd);
2148 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2149 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2150 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002151 }
Chia-I Wu63636062015-10-26 21:10:41 +08002152 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002153 if (demo->separate_present_queue) {
2154 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2155 }
Tony Barboura72d9492017-01-11 13:35:09 -07002156 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002157
Ian Elliottcf074d32015-10-16 18:02:43 -06002158 // Second, re-perform the demo_prepare() function, which will re-create the
2159 // swapchain:
2160 demo_prepare(demo);
2161}
2162
David Pinedo2bb7c932015-06-18 17:03:14 -06002163// On MS-Windows, make this a global, so it's available to WndProc()
2164struct demo demo;
2165
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002166#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002167static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002168 if (!demo->prepared)
2169 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002170
Ian Elliott639ca472015-04-16 15:23:05 -06002171 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002172 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002173 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002174 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002175 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002176}
Ian Elliott639ca472015-04-16 15:23:05 -06002177
2178// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002179LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2180 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002181 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002182 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002183 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002184 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002185 // The validation callback calls MessageBox which can generate paint
2186 // events - don't make more Vulkan calls if we got here from the
2187 // callback
2188 if (!in_callback) {
2189 demo_run(&demo);
2190 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002191 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002192 case WM_GETMINMAXINFO: // set window's minimum size
2193 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2194 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002195 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002196 // Resize the application to the new window size, except when
2197 // it was minimized. Vulkan doesn't support images or swapchains
2198 // with width=0 and height=0.
2199 if (wParam != SIZE_MINIMIZED) {
2200 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002201 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002202 demo_resize(&demo);
2203 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002204 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002205 default:
2206 break;
2207 }
2208 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2209}
2210
Karl Schultze4dc75c2016-02-02 15:37:51 -07002211static void demo_create_window(struct demo *demo) {
2212 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002213
2214 // Initialize the window class structure:
2215 win_class.cbSize = sizeof(WNDCLASSEX);
2216 win_class.style = CS_HREDRAW | CS_VREDRAW;
2217 win_class.lpfnWndProc = WndProc;
2218 win_class.cbClsExtra = 0;
2219 win_class.cbWndExtra = 0;
2220 win_class.hInstance = demo->connection; // hInstance
2221 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2222 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2223 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2224 win_class.lpszMenuName = NULL;
2225 win_class.lpszClassName = demo->name;
2226 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2227 // Register window class:
2228 if (!RegisterClassEx(&win_class)) {
2229 // It didn't work, so try to give a useful error:
2230 printf("Unexpected error trying to start the application!\n");
2231 fflush(stdout);
2232 exit(1);
2233 }
2234 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002235 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002236 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002237 demo->window = CreateWindowEx(0,
2238 demo->name, // class name
2239 demo->name, // app name
2240 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002241 WS_VISIBLE | WS_SYSMENU,
2242 100, 100, // x/y coords
2243 wr.right - wr.left, // width
2244 wr.bottom - wr.top, // height
2245 NULL, // handle to parent
2246 NULL, // handle to menu
2247 demo->connection, // hInstance
2248 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002249 if (!demo->window) {
2250 // It didn't work, so try to give a useful error:
2251 printf("Cannot create a window in which to draw!\n");
2252 fflush(stdout);
2253 exit(1);
2254 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002255 // Window client area size must be at least 1 pixel high, to prevent crash.
2256 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2257 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002258}
Tony Barbour8295ac42016-08-08 11:04:17 -06002259#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002260static void demo_create_xlib_window(struct demo *demo) {
2261
Tony Barbouree9cd372017-01-31 16:09:58 -07002262 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002263 demo->display = XOpenDisplay(NULL);
2264 long visualMask = VisualScreenMask;
2265 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002266 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002267 vInfoTemplate.screen = DefaultScreen(demo->display);
2268 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2269 &vInfoTemplate, &numberOfVisuals);
2270
2271 Colormap colormap = XCreateColormap(
2272 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2273 visualInfo->visual, AllocNone);
2274
Rene Lindsay11612722016-06-13 17:20:39 -06002275 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002276 windowAttributes.colormap = colormap;
2277 windowAttributes.background_pixel = 0xFFFFFFFF;
2278 windowAttributes.border_pixel = 0;
2279 windowAttributes.event_mask =
2280 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2281
2282 demo->xlib_window = XCreateWindow(
2283 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2284 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2285 visualInfo->visual,
2286 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2287
2288 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2289 XMapWindow(demo->display, demo->xlib_window);
2290 XFlush(demo->display);
2291 demo->xlib_wm_delete_window =
2292 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2293}
2294static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2295 switch(event->type) {
2296 case ClientMessage:
2297 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2298 demo->quit = true;
2299 break;
2300 case KeyPress:
2301 switch (event->xkey.keycode) {
2302 case 0x9: // Escape
2303 demo->quit = true;
2304 break;
2305 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002306 demo->spin_angle -= demo->spin_increment;
2307 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002308 case 0x72: // right arrow key
2309 demo->spin_angle += demo->spin_increment;
2310 break;
2311 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002312 demo->pause = !demo->pause;
2313 break;
2314 }
2315 break;
2316 case ConfigureNotify:
2317 if ((demo->width != event->xconfigure.width) ||
2318 (demo->height != event->xconfigure.height)) {
2319 demo->width = event->xconfigure.width;
2320 demo->height = event->xconfigure.height;
2321 demo_resize(demo);
2322 }
2323 break;
2324 default:
2325 break;
2326 }
2327
2328}
2329
2330static void demo_run_xlib(struct demo *demo) {
2331
2332 while (!demo->quit) {
2333 XEvent event;
2334
2335 if (demo->pause) {
2336 XNextEvent(demo->display, &event);
2337 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002338 }
2339 while (XPending(demo->display) > 0) {
2340 XNextEvent(demo->display, &event);
2341 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002342 }
2343
Tony Barbour2593b182016-04-19 10:57:58 -06002344 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002345 demo->curFrame++;
2346 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2347 demo->quit = true;
2348 }
2349}
Tony Barbour153cb062016-12-07 13:43:36 -07002350#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002351static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002352 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002353 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002354 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002355 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002356 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002357 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002358 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002359 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2360 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002361 demo->quit = true;
2362 }
2363 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002364 case XCB_KEY_RELEASE: {
2365 const xcb_key_release_event_t *key =
2366 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002367
Karl Schultze4dc75c2016-02-02 15:37:51 -07002368 switch (key->detail) {
2369 case 0x9: // Escape
2370 demo->quit = true;
2371 break;
2372 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002373 demo->spin_angle -= demo->spin_increment;
2374 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002375 case 0x72: // right arrow key
2376 demo->spin_angle += demo->spin_increment;
2377 break;
2378 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002379 demo->pause = !demo->pause;
2380 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002381 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002382 } break;
2383 case XCB_CONFIGURE_NOTIFY: {
2384 const xcb_configure_notify_event_t *cfg =
2385 (const xcb_configure_notify_event_t *)event;
2386 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002387 demo->width = cfg->width;
2388 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002389 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002390 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002391 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002392 default:
2393 break;
2394 }
2395}
2396
Tony Barbour2593b182016-04-19 10:57:58 -06002397static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002398 xcb_flush(demo->connection);
2399
2400 while (!demo->quit) {
2401 xcb_generic_event_t *event;
2402
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002403 if (demo->pause) {
2404 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002405 }
2406 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002407 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002408 }
2409 while (event) {
2410 demo_handle_xcb_event(demo, event);
2411 free(event);
2412 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002413 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002414
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002415 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002416 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002417 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002418 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002419 }
2420}
2421
Tony Barbour2593b182016-04-19 10:57:58 -06002422static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002423 uint32_t value_mask, value_list[32];
2424
Tony Barbour2593b182016-04-19 10:57:58 -06002425 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002426
2427 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2428 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002429 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002430 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002431
Tony Barbour2593b182016-04-19 10:57:58 -06002432 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002433 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2434 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2435 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002436
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002437 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002438 xcb_intern_atom_cookie_t cookie =
2439 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2440 xcb_intern_atom_reply_t *reply =
2441 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002442
Karl Schultze4dc75c2016-02-02 15:37:51 -07002443 xcb_intern_atom_cookie_t cookie2 =
2444 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2445 demo->atom_wm_delete_window =
2446 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002447
Tony Barbour2593b182016-04-19 10:57:58 -06002448 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002449 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002450 &(*demo->atom_wm_delete_window).atom);
2451 free(reply);
2452
Tony Barbour2593b182016-04-19 10:57:58 -06002453 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002454
Karl Schultze4dc75c2016-02-02 15:37:51 -07002455 // Force the x/y coordinates to 100,100 results are identical in consecutive
2456 // runs
2457 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002458 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002459 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002460}
Tony Barbour6b131662016-08-25 13:14:45 -06002461// VK_USE_PLATFORM_XCB_KHR
2462#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002463static void demo_run(struct demo *demo) {
2464 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002465 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002466 demo->curFrame++;
2467 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2468 demo->quit = true;
2469 }
2470}
2471
2472static void handle_ping(void *data UNUSED,
2473 struct wl_shell_surface *shell_surface,
2474 uint32_t serial) {
2475 wl_shell_surface_pong(shell_surface, serial);
2476}
2477
2478static void handle_configure(void *data UNUSED,
2479 struct wl_shell_surface *shell_surface UNUSED,
2480 uint32_t edges UNUSED, int32_t width UNUSED,
2481 int32_t height UNUSED) {}
2482
2483static void handle_popup_done(void *data UNUSED,
2484 struct wl_shell_surface *shell_surface UNUSED) {}
2485
2486static const struct wl_shell_surface_listener shell_surface_listener = {
2487 handle_ping, handle_configure, handle_popup_done};
2488
2489static void demo_create_window(struct demo *demo) {
2490 demo->window = wl_compositor_create_surface(demo->compositor);
2491 if (!demo->window) {
2492 printf("Can not create wayland_surface from compositor!\n");
2493 fflush(stdout);
2494 exit(1);
2495 }
2496
2497 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2498 if (!demo->shell_surface) {
2499 printf("Can not get shell_surface from wayland_surface!\n");
2500 fflush(stdout);
2501 exit(1);
2502 }
2503 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2504 demo);
2505 wl_shell_surface_set_toplevel(demo->shell_surface);
2506 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2507}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002508#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2509static void demo_run(struct demo *demo) {
2510 if (!demo->prepared)
2511 return;
2512
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002513 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002514 demo->curFrame++;
2515}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002516#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002517#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2518static VkResult demo_create_display_surface(struct demo *demo) {
2519 VkResult U_ASSERT_ONLY err;
2520 uint32_t display_count;
2521 uint32_t mode_count;
2522 uint32_t plane_count;
2523 VkDisplayPropertiesKHR display_props;
2524 VkDisplayKHR display;
2525 VkDisplayModePropertiesKHR mode_props;
2526 VkDisplayPlanePropertiesKHR *plane_props;
2527 VkBool32 found_plane = VK_FALSE;
2528 uint32_t plane_index;
2529 VkExtent2D image_extent;
2530 VkDisplaySurfaceCreateInfoKHR create_info;
2531
2532 // Get the first display
2533 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2534 assert(!err);
2535
2536 if (display_count == 0) {
2537 printf("Cannot find any display!\n");
2538 fflush(stdout);
2539 exit(1);
2540 }
2541
2542 display_count = 1;
2543 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2544 assert(!err || (err == VK_INCOMPLETE));
2545
2546 display = display_props.display;
2547
2548 // Get the first mode of the display
2549 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2550 assert(!err);
2551
2552 if (mode_count == 0) {
2553 printf("Cannot find any mode for the display!\n");
2554 fflush(stdout);
2555 exit(1);
2556 }
2557
2558 mode_count = 1;
2559 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2560 assert(!err || (err == VK_INCOMPLETE));
2561
2562 // Get the list of planes
2563 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2564 assert(!err);
2565
2566 if (plane_count == 0) {
2567 printf("Cannot find any plane!\n");
2568 fflush(stdout);
2569 exit(1);
2570 }
2571
2572 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2573 assert(plane_props);
2574
2575 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2576 assert(!err);
2577
2578 // Find a plane compatible with the display
2579 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2580 uint32_t supported_count;
2581 VkDisplayKHR *supported_displays;
2582
2583 // Disqualify planes that are bound to a different display
2584 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2585 (plane_props[plane_index].currentDisplay != display)) {
2586 continue;
2587 }
2588
2589 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2590 assert(!err);
2591
2592 if (supported_count == 0) {
2593 continue;
2594 }
2595
2596 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2597 assert(supported_displays);
2598
2599 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2600 assert(!err);
2601
2602 for (uint32_t i = 0; i < supported_count; i++) {
2603 if (supported_displays[i] == display) {
2604 found_plane = VK_TRUE;
2605 break;
2606 }
2607 }
2608
2609 free(supported_displays);
2610
2611 if (found_plane) {
2612 break;
2613 }
2614 }
2615
2616 if (!found_plane) {
2617 printf("Cannot find a plane compatible with the display!\n");
2618 fflush(stdout);
2619 exit(1);
2620 }
2621
2622 free(plane_props);
2623
2624 image_extent.width = mode_props.parameters.visibleRegion.width;
2625 image_extent.height = mode_props.parameters.visibleRegion.height;
2626
2627 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2628 create_info.pNext = NULL;
2629 create_info.flags = 0;
2630 create_info.displayMode = mode_props.displayMode;
2631 create_info.planeIndex = plane_index;
2632 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2633 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
2634 create_info.alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2635 create_info.globalAlpha = 1.0f;
2636 create_info.imageExtent = image_extent;
2637
2638 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2639}
2640
2641static void demo_run_display(struct demo *demo)
2642{
2643 while (!demo->quit) {
2644 demo_draw(demo);
2645 demo->curFrame++;
2646
2647 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2648 demo->quit = true;
2649 }
2650 }
2651}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002652#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002653
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002654/*
2655 * Return 1 (true) if all layer names specified in check_names
2656 * can be found in given layer properties.
2657 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002658static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002659 uint32_t layer_count,
2660 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002661 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002662 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002663 for (uint32_t j = 0; j < layer_count; j++) {
2664 if (!strcmp(check_names[i], layers[j].layerName)) {
2665 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002666 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002667 }
2668 }
2669 if (!found) {
2670 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2671 return 0;
2672 }
2673 }
2674 return 1;
2675}
2676
Karl Schultze4dc75c2016-02-02 15:37:51 -07002677static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002678 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002679 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002680 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002681 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002682 char **instance_validation_layers = NULL;
2683 demo->enabled_extension_count = 0;
2684 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002685
Karl Schultz7c50ad62016-04-01 12:07:03 -06002686 char *instance_validation_layers_alt1[] = {
2687 "VK_LAYER_LUNARG_standard_validation"
2688 };
2689
2690 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07002691 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2692 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2693 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002694 };
2695
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002696 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002697 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002698 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002699
Karl Schultz7c50ad62016-04-01 12:07:03 -06002700 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002701 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002702
Karl Schultz7c50ad62016-04-01 12:07:03 -06002703 instance_validation_layers = instance_validation_layers_alt1;
2704 if (instance_layer_count > 0) {
2705 VkLayerProperties *instance_layers =
2706 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2707 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2708 instance_layers);
2709 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002710
Karl Schultz7c50ad62016-04-01 12:07:03 -06002711
2712 validation_found = demo_check_layers(
2713 ARRAY_SIZE(instance_validation_layers_alt1),
2714 instance_validation_layers, instance_layer_count,
2715 instance_layers);
2716 if (validation_found) {
2717 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002718 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2719 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002720 } else {
2721 // use alternative set of validation layers
2722 instance_validation_layers = instance_validation_layers_alt2;
2723 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2724 validation_found = demo_check_layers(
2725 ARRAY_SIZE(instance_validation_layers_alt2),
2726 instance_validation_layers, instance_layer_count,
2727 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002728 validation_layer_count =
2729 ARRAY_SIZE(instance_validation_layers_alt2);
2730 for (uint32_t i = 0; i < validation_layer_count; i++) {
2731 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002732 }
2733 }
2734 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002735 }
Dustin Graves7c287352016-01-27 13:48:06 -07002736
Karl Schultz7c50ad62016-04-01 12:07:03 -06002737 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002738 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002739 "required validation layer.\n\n"
2740 "Please look at the Getting Started guide for additional "
2741 "information.\n",
2742 "vkCreateInstance Failure");
2743 }
Dustin Graves7c287352016-01-27 13:48:06 -07002744 }
2745
2746 /* Look for instance extensions */
2747 VkBool32 surfaceExtFound = 0;
2748 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002749 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002750
Karl Schultze4dc75c2016-02-02 15:37:51 -07002751 err = vkEnumerateInstanceExtensionProperties(
2752 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002753 assert(!err);
2754
Dustin Graves7c287352016-01-27 13:48:06 -07002755 if (instance_extension_count > 0) {
2756 VkExtensionProperties *instance_extensions =
2757 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2758 err = vkEnumerateInstanceExtensionProperties(
2759 NULL, &instance_extension_count, instance_extensions);
2760 assert(!err);
2761 for (uint32_t i = 0; i < instance_extension_count; i++) {
2762 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2763 instance_extensions[i].extensionName)) {
2764 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002765 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002766 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002767 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002768#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002769 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2770 instance_extensions[i].extensionName)) {
2771 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002772 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002773 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2774 }
Tony Barbour153cb062016-12-07 13:43:36 -07002775#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002776 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2777 instance_extensions[i].extensionName)) {
2778 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06002779 demo->extension_names[demo->enabled_extension_count++] =
2780 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2781 }
Tony Barbour153cb062016-12-07 13:43:36 -07002782#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002783 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2784 instance_extensions[i].extensionName)) {
2785 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002786 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002787 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2788 }
Tony Barbour153cb062016-12-07 13:43:36 -07002789#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002790 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2791 instance_extensions[i].extensionName)) {
2792 platformSurfaceExtFound = 1;
2793 demo->extension_names[demo->enabled_extension_count++] =
2794 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2795 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002796#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002797#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2798 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
2799 instance_extensions[i].extensionName)) {
2800 platformSurfaceExtFound = 1;
2801 demo->extension_names[demo->enabled_extension_count++] =
2802 VK_KHR_DISPLAY_EXTENSION_NAME;
2803 }
Tony Barbour153cb062016-12-07 13:43:36 -07002804#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002805 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2806 instance_extensions[i].extensionName)) {
2807 platformSurfaceExtFound = 1;
2808 demo->extension_names[demo->enabled_extension_count++] =
2809 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2810 }
Bill Hollingsf4352142017-02-14 22:58:56 -05002811#elif defined(VK_USE_PLATFORM_IOS_MVK)
2812 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2813 platformSurfaceExtFound = 1;
2814 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
2815 }
2816#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2817 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2818 platformSurfaceExtFound = 1;
2819 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
2820 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002821#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002822 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2823 instance_extensions[i].extensionName)) {
2824 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002825 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002826 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2827 }
2828 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002829 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002830 }
Dustin Graves7c287352016-01-27 13:48:06 -07002831
2832 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002833 }
Dustin Graves7c287352016-01-27 13:48:06 -07002834
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002835 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002836 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2837 "the " VK_KHR_SURFACE_EXTENSION_NAME
2838 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002839 "Vulkan installable client driver (ICD) installed?\nPlease "
2840 "look at the Getting Started guide for additional "
2841 "information.\n",
2842 "vkCreateInstance Failure");
2843 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002844 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002845#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002846 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2847 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2848 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002849 "Vulkan installable client driver (ICD) installed?\nPlease "
2850 "look at the Getting Started guide for additional "
2851 "information.\n",
2852 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002853#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002854 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2855 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2856 "Vulkan installable client driver (ICD) installed?\nPlease "
2857 "look at the Getting Started guide for additional "
2858 "information.\n",
2859 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002860#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06002861 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2862 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2863 "Vulkan installable client driver (ICD) installed?\nPlease "
2864 "look at the Getting Started guide for additional "
2865 "information.\n",
2866 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002867#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002868 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2869 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2870 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002871 "Vulkan installable client driver (ICD) installed?\nPlease "
2872 "look at the Getting Started guide for additional "
2873 "information.\n",
2874 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002875#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2876 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2877 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2878 " extension.\n\nDo you have a compatible "
2879 "Vulkan installable client driver (ICD) installed?\nPlease "
2880 "look at the Getting Started guide for additional "
2881 "information.\n",
2882 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002883#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002884#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2885 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2886 "the " VK_KHR_DISPLAY_EXTENSION_NAME
2887 " extension.\n\nDo you have a compatible "
2888 "Vulkan installable client driver (ICD) installed?\nPlease "
2889 "look at the Getting Started guide for additional "
2890 "information.\n",
2891 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002892#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2893 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2894 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2895 " extension.\n\nDo you have a compatible "
2896 "Vulkan installable client driver (ICD) installed?\nPlease "
2897 "look at the Getting Started guide for additional "
2898 "information.\n",
2899 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002900#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002901 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2902 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2903 " extension.\n\nDo you have a compatible "
2904 "Vulkan installable client driver (ICD) installed?\nPlease "
2905 "look at the Getting Started guide for additional "
2906 "information.\n",
2907 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002908#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002909 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002910 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002911 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002912 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002913 .pApplicationName = APP_SHORT_NAME,
2914 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002915 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002916 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002917 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002918 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002919 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002920 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002921 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002922 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002923 .enabledLayerCount = demo->enabled_layer_count,
2924 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2925 .enabledExtensionCount = demo->enabled_extension_count,
2926 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002927 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002928
2929 /*
2930 * This is info for a temp callback to use during CreateInstance.
2931 * After the instance is created, we use the instance-based
2932 * function to register the final callback.
2933 */
Karl Schultza2615462017-01-20 13:19:20 -07002934 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002935 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07002936 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2937 dbgCreateInfoTemp.pNext = NULL;
2938 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
2939 dbgCreateInfoTemp.pUserData = demo;
2940 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06002941 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultza2615462017-01-20 13:19:20 -07002942 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002943 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002944
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002945 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002946
Chia-I Wu63636062015-10-26 21:10:41 +08002947 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002948 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2949 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002950 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002951 "additional information.\n",
2952 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002953 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002954 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002955 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002956 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002957 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002958 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2959 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002960 "the Getting Started guide for additional information.\n",
2961 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002962 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002963
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002964 /* Make initial call to query gpu_count, then second call for gpu info*/
2965 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2966 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002967
2968 if (gpu_count > 0) {
2969 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2970 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2971 assert(!err);
2972 /* For cube demo we just grab the first physical device */
2973 demo->gpu = physical_devices[0];
2974 free(physical_devices);
2975 } else {
2976 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2977 "Do you have a compatible Vulkan installable client driver (ICD) "
2978 "installed?\nPlease look at the Getting Started guide for "
2979 "additional information.\n",
2980 "vkEnumeratePhysicalDevices Failure");
2981 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002982
Dustin Graves7c287352016-01-27 13:48:06 -07002983 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002984 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002985 VkBool32 swapchainExtFound = 0;
2986 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002987 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002988
Karl Schultze4dc75c2016-02-02 15:37:51 -07002989 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2990 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002991 assert(!err);
2992
Dustin Graves7c287352016-01-27 13:48:06 -07002993 if (device_extension_count > 0) {
2994 VkExtensionProperties *device_extensions =
2995 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2996 err = vkEnumerateDeviceExtensionProperties(
2997 demo->gpu, NULL, &device_extension_count, device_extensions);
2998 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002999
Dustin Graves7c287352016-01-27 13:48:06 -07003000 for (uint32_t i = 0; i < device_extension_count; i++) {
3001 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3002 device_extensions[i].extensionName)) {
3003 swapchainExtFound = 1;
3004 demo->extension_names[demo->enabled_extension_count++] =
3005 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3006 }
3007 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003008 }
Dustin Graves7c287352016-01-27 13:48:06 -07003009
3010 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003011 }
Dustin Graves7c287352016-01-27 13:48:06 -07003012
Tony Barbourcc69f452015-10-08 13:59:42 -06003013 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003014 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3015 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3016 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003017 "Vulkan installable client driver (ICD) installed?\nPlease "
3018 "look at the Getting Started guide for additional "
3019 "information.\n",
3020 "vkCreateInstance Failure");
3021 }
3022
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003023 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003024 demo->CreateDebugReportCallback =
3025 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3026 demo->inst, "vkCreateDebugReportCallbackEXT");
3027 demo->DestroyDebugReportCallback =
3028 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3029 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003030 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003031 ERR_EXIT(
3032 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3033 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003034 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003035 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003036 ERR_EXIT(
3037 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3038 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003039 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003040 demo->DebugReportMessage =
3041 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3042 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003043 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003044 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003045 "vkGetProcAddr Failure");
3046 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003047
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003048 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003049 PFN_vkDebugReportCallbackEXT callback;
3050 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003051 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003052 dbgCreateInfo.pNext = NULL;
3053 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003054 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003055 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003056 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003057 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3058 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003059 switch (err) {
3060 case VK_SUCCESS:
3061 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003062 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003063 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3064 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003065 break;
3066 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003067 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3068 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003069 break;
3070 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003071 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003072 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003073
3074 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003075 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3076 &demo->queue_family_count, NULL);
3077 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003078
Karl Schultze4dc75c2016-02-02 15:37:51 -07003079 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003080 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3081 vkGetPhysicalDeviceQueueFamilyProperties(
3082 demo->gpu, &demo->queue_family_count, demo->queue_props);
3083
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003084 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003085 // If app has specific feature requirements it should check supported
3086 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003087 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003088 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003089
Tony Barbourda2bad52016-01-22 14:36:40 -07003090 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3091 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3092 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3093 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3094 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3095}
3096
Karl Schultze4dc75c2016-02-02 15:37:51 -07003097static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003098 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003099 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003100 VkDeviceQueueCreateInfo queues[2];
3101 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3102 queues[0].pNext = NULL;
3103 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3104 queues[0].queueCount = 1;
3105 queues[0].pQueuePriorities = queue_priorities;
3106 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003107
3108 VkDeviceCreateInfo device = {
3109 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3110 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003111 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003112 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003113 .enabledLayerCount = 0,
3114 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003115 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003116 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3117 .pEnabledFeatures =
3118 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003119 };
Tony Barbour22e06272016-09-07 16:07:56 -06003120 if (demo->separate_present_queue) {
3121 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3122 queues[1].pNext = NULL;
3123 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3124 queues[1].queueCount = 1;
3125 queues[1].pQueuePriorities = queue_priorities;
3126 queues[1].flags = 0;
3127 device.queueCreateInfoCount = 2;
3128 }
Chia-I Wu63636062015-10-26 21:10:41 +08003129 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003130 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003131}
3132
Karl Schultze4dc75c2016-02-02 15:37:51 -07003133static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003134 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003135
Karl Schultze4dc75c2016-02-02 15:37:51 -07003136// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003137#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003138 VkWin32SurfaceCreateInfoKHR createInfo;
3139 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3140 createInfo.pNext = NULL;
3141 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003142 createInfo.hinstance = demo->connection;
3143 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003144
Karl Schultze4dc75c2016-02-02 15:37:51 -07003145 err =
3146 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003147#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003148 VkWaylandSurfaceCreateInfoKHR createInfo;
3149 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3150 createInfo.pNext = NULL;
3151 createInfo.flags = 0;
3152 createInfo.display = demo->display;
3153 createInfo.surface = demo->window;
3154
3155 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3156 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003157#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003158#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3159 VkAndroidSurfaceCreateInfoKHR createInfo;
3160 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3161 createInfo.pNext = NULL;
3162 createInfo.flags = 0;
3163 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003164
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003165 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003166#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3167 VkXlibSurfaceCreateInfoKHR createInfo;
3168 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3169 createInfo.pNext = NULL;
3170 createInfo.flags = 0;
3171 createInfo.dpy = demo->display;
3172 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003173
Tony Barbour153cb062016-12-07 13:43:36 -07003174 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003175 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003176#elif defined(VK_USE_PLATFORM_XCB_KHR)
3177 VkXcbSurfaceCreateInfoKHR createInfo;
3178 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3179 createInfo.pNext = NULL;
3180 createInfo.flags = 0;
3181 createInfo.connection = demo->connection;
3182 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003183
Tony Barbour153cb062016-12-07 13:43:36 -07003184 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003185#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3186 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003187#elif defined(VK_USE_PLATFORM_IOS_MVK)
3188 VkIOSSurfaceCreateInfoMVK surface;
3189 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3190 surface.pNext = NULL;
3191 surface.flags = 0;
3192 surface.pView = demo->window;
3193
3194 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3195#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3196 VkMacOSSurfaceCreateInfoMVK surface;
3197 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3198 surface.pNext = NULL;
3199 surface.flags = 0;
3200 surface.pView = demo->window;
3201
3202 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003203#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003204 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003205
Tony Barbourcc69f452015-10-08 13:59:42 -06003206 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003207 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003208 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003209 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003210 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003211 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003212 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003213
3214 // Search for a graphics and a present queue in the array of queue
3215 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003216 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3217 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003218 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003219 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3220 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3221 graphicsQueueFamilyIndex = i;
3222 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003223
Tony Barbourab2a0362016-09-06 11:40:12 -06003224 if (supportsPresent[i] == VK_TRUE) {
3225 graphicsQueueFamilyIndex = i;
3226 presentQueueFamilyIndex = i;
3227 break;
3228 }
3229 }
3230 }
3231
3232 if (presentQueueFamilyIndex == UINT32_MAX) {
3233 // If didn't find a queue that supports both graphics and present, then
3234 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003235 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003236 if (supportsPresent[i] == VK_TRUE) {
3237 presentQueueFamilyIndex = i;
3238 break;
3239 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003240 }
3241 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003242
3243 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003244 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3245 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003246 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003247 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003248 }
3249
Tony Barbourab2a0362016-09-06 11:40:12 -06003250 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3251 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003252 demo->separate_present_queue =
3253 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003254 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003255
Tony Barbourda2bad52016-01-22 14:36:40 -07003256 demo_create_device(demo);
3257
3258 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3259 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3260 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3261 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3262 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3263
Tony Barboura2a67812016-07-18 13:21:06 -06003264 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3265 &demo->graphics_queue);
3266
Tony Barbour22e06272016-09-07 16:07:56 -06003267 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003268 demo->present_queue = demo->graphics_queue;
3269 } else {
3270 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3271 &demo->present_queue);
3272 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003273
Ian Elliottaaae5352015-07-06 14:27:58 -06003274 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003275 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003276 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003277 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003278 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003279 VkSurfaceFormatKHR *surfFormats =
3280 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003281 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003282 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003283 assert(!err);
3284 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3285 // the surface has no preferred format. Otherwise, at least one
3286 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003287 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003288 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003289 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003290 assert(formatCount >= 1);
3291 demo->format = surfFormats[0].format;
3292 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003293 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003294
David Pinedo2bb7c932015-06-18 17:03:14 -06003295 demo->quit = false;
3296 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003297
Tony Barbource7524e2016-07-28 12:01:45 -06003298 // Create semaphores to synchronize acquiring presentable buffers before
3299 // rendering and waiting for drawing to be complete before presenting
3300 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3301 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3302 .pNext = NULL,
3303 .flags = 0,
3304 };
Tony Barbource7524e2016-07-28 12:01:45 -06003305
Tony Barbour66a56c32016-09-21 13:10:56 -06003306 // Create fences that we can use to throttle if we get too far
3307 // ahead of the image presents
3308 VkFenceCreateInfo fence_ci = {
3309 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3310 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003311 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003312 };
3313 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003314 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3315 assert(!err);
3316
Tony Barbour22e06272016-09-07 16:07:56 -06003317 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003318 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003319 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003320
3321 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3322 &demo->draw_complete_semaphores[i]);
3323 assert(!err);
3324
3325 if (demo->separate_present_queue) {
3326 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3327 &demo->image_ownership_semaphores[i]);
3328 assert(!err);
3329 }
Tony Barbour22e06272016-09-07 16:07:56 -06003330 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003331 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003332
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003333 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003334 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003335}
3336
Tony Barbour153cb062016-12-07 13:43:36 -07003337#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003338static void registry_handle_global(void *data, struct wl_registry *registry,
3339 uint32_t name, const char *interface,
3340 uint32_t version UNUSED) {
3341 struct demo *demo = data;
3342 if (strcmp(interface, "wl_compositor") == 0) {
3343 demo->compositor =
3344 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3345 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3346 * tp xdg_shell */
3347 } else if (strcmp(interface, "wl_shell") == 0) {
3348 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3349 }
3350}
3351
3352static void registry_handle_global_remove(void *data UNUSED,
3353 struct wl_registry *registry UNUSED,
3354 uint32_t name UNUSED) {}
3355
3356static const struct wl_registry_listener registry_listener = {
3357 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003358#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003359#endif
3360
Karl Schultze4dc75c2016-02-02 15:37:51 -07003361static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003362#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003363 const xcb_setup_t *setup;
3364 xcb_screen_iterator_t iter;
3365 int scr;
3366
3367 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003368 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003369 printf("Cannot find a compatible Vulkan installable client driver "
3370 "(ICD).\nExiting ...\n");
3371 fflush(stdout);
3372 exit(1);
3373 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003374
3375 setup = xcb_get_setup(demo->connection);
3376 iter = xcb_setup_roots_iterator(setup);
3377 while (scr-- > 0)
3378 xcb_screen_next(&iter);
3379
3380 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003381#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3382 demo->display = wl_display_connect(NULL);
3383
3384 if (demo->display == NULL) {
3385 printf("Cannot find a compatible Vulkan installable client driver "
3386 "(ICD).\nExiting ...\n");
3387 fflush(stdout);
3388 exit(1);
3389 }
3390
3391 demo->registry = wl_display_get_registry(demo->display);
3392 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3393 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003394#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003395#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003396}
3397
Karl Schultze4dc75c2016-02-02 15:37:51 -07003398static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003399 vec3 eye = {0.0f, 3.0f, 5.0f};
3400 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003401 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003402
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003403 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003404 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003405 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003406
Piers Daniell735ee532015-02-23 16:23:13 -07003407 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003408 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003409 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003410 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003411 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003412 if ((strcmp(argv[i], "--present_mode") == 0) &&
3413 (i < argc - 1)) {
3414 demo->presentMode = atoi(argv[i+1]);
3415 i++;
3416 continue;
3417 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003418 if (strcmp(argv[i], "--break") == 0) {
3419 demo->use_break = true;
3420 continue;
3421 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003422 if (strcmp(argv[i], "--validate") == 0) {
3423 demo->validate = true;
3424 continue;
3425 }
Tony Barbour2593b182016-04-19 10:57:58 -06003426 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003427 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003428 continue;
3429 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003430 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3431 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3432 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003433 i++;
3434 continue;
3435 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003436 if (strcmp(argv[i], "--suppress_popups") == 0) {
3437 demo->suppress_popups = true;
3438 continue;
3439 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003440
Cody Northropaf28a532016-09-27 10:50:57 -06003441#if defined(ANDROID)
3442 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3443#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003444 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour291d57b2016-10-21 14:47:07 -06003445 "[--c <framecount>] [--suppress_popups] [--present_mode <present mode enum>]\n"
3446 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3447 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3448 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3449 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3450 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3451 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003452 fflush(stderr);
3453 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003454#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003455 }
3456
Tony Barbour153cb062016-12-07 13:43:36 -07003457 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003458
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003459 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003460
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003461 demo->width = 500;
3462 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003463
Tony Barbour66a56c32016-09-21 13:10:56 -06003464 demo->spin_angle = 4.0f;
3465 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003466 demo->pause = false;
3467
Karl Schultze4dc75c2016-02-02 15:37:51 -07003468 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3469 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003470 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3471 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003472
3473 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003474}
3475
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003476#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003477// Include header required for parsing the command line options.
3478#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003479
Karl Schultze4dc75c2016-02-02 15:37:51 -07003480int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3481 int nCmdShow) {
3482 MSG msg; // message
3483 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003484 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003485 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003486
Karl Schultze4dc75c2016-02-02 15:37:51 -07003487 // Use the CommandLine functions to get the command line arguments.
3488 // Unfortunately, Microsoft outputs
3489 // this information as wide characters for Unicode, and we simply want the
3490 // Ascii version to be compatible
3491 // with the non-Windows side. So, we have to convert the information to
3492 // Ascii character strings.
3493 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3494 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003495 argc = 0;
3496 }
3497
Karl Schultze4dc75c2016-02-02 15:37:51 -07003498 if (argc > 0) {
3499 argv = (char **)malloc(sizeof(char *) * argc);
3500 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003501 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003502 } else {
3503 for (int iii = 0; iii < argc; iii++) {
3504 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003505 size_t numConverted = 0;
3506
Karl Schultze4dc75c2016-02-02 15:37:51 -07003507 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3508 if (argv[iii] != NULL) {
3509 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3510 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003511 }
3512 }
3513 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003514 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003515 argv = NULL;
3516 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003517
3518 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003519
3520 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003521 if (argc > 0 && argv != NULL) {
3522 for (int iii = 0; iii < argc; iii++) {
3523 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003524 free(argv[iii]);
3525 }
3526 }
3527 free(argv);
3528 }
3529
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003530 demo.connection = hInstance;
3531 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003532 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003533 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003534
3535 demo_prepare(&demo);
3536
Karl Schultze4dc75c2016-02-02 15:37:51 -07003537 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003538
3539 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003540 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003541 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003542 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003543 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003544 done = true; // if found, quit app
3545 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003546 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003547 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003548 DispatchMessage(&msg);
3549 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003550 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003551 }
3552
3553 demo_cleanup(&demo);
3554
Karl Schultze4dc75c2016-02-02 15:37:51 -07003555 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003556}
Bill Hollingsf4352142017-02-14 22:58:56 -05003557
3558#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3559static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003560 const char* argv[] = { "CubeSample" };
3561 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003562
Tony Barbourc65cd752017-03-13 15:29:35 -06003563 demo_init(demo, argc, (char**)argv);
3564 demo->window = view;
3565 demo_init_vk_swapchain(demo);
3566 demo_prepare(demo);
3567 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003568}
3569
3570static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003571 // Wait for work to finish before updating MVP.
3572 vkDeviceWaitIdle(demo->device);
3573 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003574
Tony Barbourc65cd752017-03-13 15:29:35 -06003575 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003576}
3577
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003578#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3579#include <android/log.h>
3580#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003581#include "android_util.h"
3582
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003583static bool initialized = false;
3584static bool active = false;
3585struct demo demo;
3586
3587static int32_t processInput(struct android_app* app, AInputEvent* event) {
3588 return 0;
3589}
3590
3591static void processCommand(struct android_app* app, int32_t cmd) {
3592 switch(cmd) {
3593 case APP_CMD_INIT_WINDOW: {
3594 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003595 // We're getting a new window. If the app is starting up, we
3596 // need to initialize. If the app has already been
3597 // initialized, that means that we lost our previous window,
3598 // which means that we have a lot of work to do. At a minimum,
3599 // we need to destroy the swapchain and surface associated with
3600 // the old window, and create a new surface and swapchain.
3601 // However, since there are a lot of other objects/state that
3602 // is tied to the swapchain, it's easiest to simply cleanup and
3603 // start over (i.e. use a brute-force approach of re-starting
3604 // the app)
3605 if (demo.prepared) {
3606 demo_cleanup(&demo);
3607 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003608
3609 // Parse Intents into argc, argv
3610 // Use the following key to send arguments, i.e.
3611 // --es args "--validate"
3612 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003613 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003614 int argc = 0;
3615 char** argv = get_args(app, key, appTag, &argc);
3616
3617 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3618 for (int i = 0; i < argc; i++)
3619 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3620
3621 demo_init(&demo, argc, argv);
3622
3623 // Free the argv malloc'd by get_args
3624 for (int i = 0; i < argc; i++)
3625 free(argv[i]);
3626
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003627 demo.window = (void*)app->window;
3628 demo_init_vk_swapchain(&demo);
3629 demo_prepare(&demo);
3630 initialized = true;
3631 }
3632 break;
3633 }
3634 case APP_CMD_GAINED_FOCUS: {
3635 active = true;
3636 break;
3637 }
3638 case APP_CMD_LOST_FOCUS: {
3639 active = false;
3640 break;
3641 }
3642 }
3643}
3644
3645void android_main(struct android_app *app)
3646{
3647 app_dummy();
3648
Cody Northrop8707a6e2016-04-26 19:59:19 -06003649#ifdef ANDROID
3650 int vulkanSupport = InitVulkan();
3651 if (vulkanSupport == 0)
3652 return;
3653#endif
3654
Ian Elliottf05d5d12016-05-17 12:03:35 -06003655 demo.prepared = false;
3656
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003657 app->onAppCmd = processCommand;
3658 app->onInputEvent = processInput;
3659
3660 while(1) {
3661 int events;
3662 struct android_poll_source* source;
3663 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3664 if (source) {
3665 source->process(app, source);
3666 }
3667
3668 if (app->destroyRequested != 0) {
3669 demo_cleanup(&demo);
3670 return;
3671 }
3672 }
3673 if (initialized && active) {
3674 demo_run(&demo);
3675 }
3676 }
3677
3678}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003679#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003680int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003681 struct demo demo;
3682
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003683 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003684#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003685 demo_create_xcb_window(&demo);
3686#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3687 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003688#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3689 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003690#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003691#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003692
Tony Barbourcc69f452015-10-08 13:59:42 -06003693 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003694
3695 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003696
Tony Barbour153cb062016-12-07 13:43:36 -07003697#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003698 demo_run_xcb(&demo);
3699#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3700 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003701#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3702 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003703#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003704#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3705 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003706#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003707
3708 demo_cleanup(&demo);
3709
Karl Schultz0218c002016-03-22 17:06:13 -06003710 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003711}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003712#endif