blob: 5c4a6dc7e46c92c9a45f784c8c88796374168f41 [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>
23*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070024
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060025#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdbool.h>
30#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070031#include <signal.h>
Cody Northrop346d7a42016-08-05 12:30:44 -060032#if defined(__linux__) && !defined(ANDROID)
Tony Barbour2593b182016-04-19 10:57:58 -060033#include <X11/Xutil.h>
34#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060035
Ian Elliott639ca472015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060038#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060039#endif // _WIN32
40
Cody Northrop8707a6e2016-04-26 19:59:19 -060041#ifdef ANDROID
42#include "vulkan_wrapper.h"
43#else
David Pinedoc5193c12015-11-06 12:54:48 -070044#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060045#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070046
David Pinedo541526f2015-11-24 09:00:24 -070047#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048#include "linmath.h"
49
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090062#if defined(__GNUC__)
63#define UNUSED __attribute__((unused))
64#else
65#define UNUSED
66#endif
67
Ian Elliott07264132015-04-28 11:35:02 -060068#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070069#define ERR_EXIT(err_msg, err_class) \
70 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060071 if (!demo->suppress_popups) \
72 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070073 exit(1); \
74 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060075
Michael Lentinec6cde4b2016-04-18 13:20:45 -050076#elif defined __ANDROID__
77#include <android/log.h>
78#define ERR_EXIT(err_msg, err_class) \
79 do { \
80 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
81 exit(1); \
82 } while (0)
83#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070084#define ERR_EXIT(err_msg, err_class) \
85 do { \
86 printf(err_msg); \
87 fflush(stdout); \
88 exit(1); \
89 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050090#endif
Ian Elliott07264132015-04-28 11:35:02 -060091
Karl Schultze4dc75c2016-02-02 15:37:51 -070092#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
93 { \
94 demo->fp##entrypoint = \
95 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
96 if (demo->fp##entrypoint == NULL) { \
97 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
98 "vkGetInstanceProcAddr Failure"); \
99 } \
100 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600101
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600102static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
103
Karl Schultze4dc75c2016-02-02 15:37:51 -0700104#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
105 { \
106 if (!g_gdpa) \
107 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
108 demo->inst, "vkGetDeviceProcAddr"); \
109 demo->fp##entrypoint = \
110 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
111 if (demo->fp##entrypoint == NULL) { \
112 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
113 "vkGetDeviceProcAddr Failure"); \
114 } \
115 }
Ian Elliott673898b2015-06-22 15:07:49 -0600116
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600117/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700118 * structure to track all objects related to a texture.
119 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600120struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600121 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700122
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600123 VkImage image;
124 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600125
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800126 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500127 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600128 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700129 int32_t tex_width, tex_height;
130};
131
Karl Schultze4dc75c2016-02-02 15:37:51 -0700132static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600133
Karl Schultz0218c002016-03-22 17:06:13 -0600134static int validation_error = 0;
135
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600136struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600137 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700138 float mvp[4][4];
139 float position[12 * 3][4];
140 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600141};
142
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600143struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600144 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700145 float mvp[4][4];
146 float position[12 * 3][4];
147 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600148};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600149
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150//--------------------------------------------------------------------------------------
151// Mesh and VertexFormat Data
152//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700153// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600154static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 -1.0f,-1.0f, 1.0f,
157 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 -1.0f, 1.0f,-1.0f,
160 -1.0f,-1.0f,-1.0f,
161
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 1.0f, 1.0f,-1.0f,
164 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f,
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,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600171 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 -1.0f, 1.0f, 1.0f,
178 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600181 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 1.0f, 1.0f, 1.0f,
185 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f,-1.0f,-1.0f,
188 1.0f, 1.0f,-1.0f,
189
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600192 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600194 1.0f,-1.0f, 1.0f,
195 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600196};
197
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600198static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700199 0.0f, 1.0f, // -X side
200 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800201 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 0.0f, 0.0f,
204 0.0f, 1.0f,
205
Rene Lindsay76867e82016-07-29 10:49:11 -0700206 1.0f, 1.0f, // -Z side
207 0.0f, 0.0f,
208 0.0f, 1.0f,
209 1.0f, 1.0f,
210 1.0f, 0.0f,
211 0.0f, 0.0f,
212
213 1.0f, 0.0f, // -Y side
214 1.0f, 1.0f,
215 0.0f, 1.0f,
216 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600217 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219
Rene Lindsay76867e82016-07-29 10:49:11 -0700220 1.0f, 0.0f, // +Y side
221 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700224 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226
Rene Lindsay76867e82016-07-29 10:49:11 -0700227 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f,
230 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700233
234 0.0f, 0.0f, // +Z side
235 0.0f, 1.0f,
236 1.0f, 0.0f,
237 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600238 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700239 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600240};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700241// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600242
Karl Schultze4dc75c2016-02-02 15:37:51 -0700243void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600244 int i;
245
246 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700247 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600248 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
249 }
250 printf("\n");
251 fflush(stdout);
252}
253
Karl Schultze4dc75c2016-02-02 15:37:51 -0700254void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600255 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700256 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600257 printf("\n");
258 fflush(stdout);
259}
260
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600262BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
263 uint64_t srcObject, size_t location, int32_t msgCode,
264 const char *pLayerPrefix, const char *pMsg,
265 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700266#ifndef WIN32
267 raise(SIGTRAP);
268#else
269 DebugBreak();
270#endif
271
272 return false;
273}
274
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600275typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600276 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800277 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600278 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600279} SwapchainBuffers;
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
284 HINSTANCE connection; // hInstance - Windows Instance
285 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700286 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600287 POINT minsize; // minimum window size
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600288#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600289 Display* display;
290 Window xlib_window;
291 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600292
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600293 xcb_connection_t *connection;
294 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600295 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800296 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900297#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
298 struct wl_display *display;
299 struct wl_registry *registry;
300 struct wl_compositor *compositor;
301 struct wl_surface *window;
302 struct wl_shell *shell;
303 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500304#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
305 ANativeWindow* window;
306#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700307 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600308 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700309 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600310 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600312 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600313 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600314 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600315 VkQueue graphics_queue;
316 VkQueue present_queue;
317 uint32_t graphics_queue_family_index;
318 uint32_t present_queue_family_index;
Tony Barbource7524e2016-07-28 12:01:45 -0600319 VkSemaphore image_acquired_semaphore;
320 VkSemaphore draw_complete_semaphore;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600321 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600322 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600323 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600324
Tony Barbourda2bad52016-01-22 14:36:40 -0700325 uint32_t enabled_extension_count;
326 uint32_t enabled_layer_count;
327 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600328 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700329
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600331 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600332 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333
Karl Schultze4dc75c2016-02-02 15:37:51 -0700334 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
335 fpGetPhysicalDeviceSurfaceSupportKHR;
336 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
337 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
338 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
339 fpGetPhysicalDeviceSurfaceFormatsKHR;
340 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
341 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600342 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
343 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
344 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
345 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
346 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600347 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600348 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600349 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800351 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352
353 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600354 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800357 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500358 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600359 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360 } depth;
361
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600362 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
364 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600365 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800366 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500367 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600368 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600369 } uniform_data;
370
Karl Schultze4dc75c2016-02-02 15:37:51 -0700371 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500372 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600373 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600374 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800375 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600378 mat4x4 projection_matrix;
379 mat4x4 view_matrix;
380 mat4x4 model_matrix;
381
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600382 float spin_angle;
383 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600384 bool pause;
385
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600386 VkShaderModule vert_shader_module;
387 VkShaderModule frag_shader_module;
388
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600389 VkDescriptorPool desc_pool;
390 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800391
Tony Barbourd8e504f2015-10-08 14:26:24 -0600392 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800393
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600394 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600395 int32_t curFrame;
396 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600397 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600398 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600399 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700400 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
401 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
402 VkDebugReportCallbackEXT msg_callback;
403 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600404
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600405 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600406 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407};
408
lenny-lunargfbe22392016-06-06 11:07:53 -0600409VKAPI_ATTR VkBool32 VKAPI_CALL
410dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
411 uint64_t srcObject, size_t location, int32_t msgCode,
412 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
413 char *message = (char *)malloc(strlen(pMsg) + 100);
414
415 assert(message);
416
417 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
418 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
419 pMsg);
420 validation_error = 1;
421 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
422 // We know that we're submitting queues without fences, ignore this
423 // warning
424 if (strstr(pMsg,
425 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
426 return false;
427 }
428 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
429 pMsg);
430 validation_error = 1;
431 } else {
432 validation_error = 1;
433 return false;
434 }
435
436#ifdef _WIN32
437 struct demo *demo = (struct demo*) pUserData;
438 if (!demo->suppress_popups)
439 MessageBox(NULL, message, "Alert", MB_OK);
440#else
441 printf("%s\n", message);
442 fflush(stdout);
443#endif
444 free(message);
445
446 /*
447 * false indicates that layer should not bail-out of an
448 * API call that had validation failures. This may mean that the
449 * app dies inside the driver due to invalid parameter(s).
450 * That's what would happen without validation layers, so we'll
451 * keep that behavior here.
452 */
453 return false;
454}
455
Ian Elliottcf074d32015-10-16 18:02:43 -0600456// Forward declaration:
457static void demo_resize(struct demo *demo);
458
Karl Schultze4dc75c2016-02-02 15:37:51 -0700459static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
460 VkFlags requirements_mask,
461 uint32_t *typeIndex) {
462 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600463 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700464 if ((typeBits & 1) == 1) {
465 // Type is available, does it match user properties?
466 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
467 requirements_mask) == requirements_mask) {
468 *typeIndex = i;
469 return true;
470 }
471 }
472 typeBits >>= 1;
473 }
474 // No memory types matched, return failure
475 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600476}
477
Karl Schultze4dc75c2016-02-02 15:37:51 -0700478static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600479 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600480
Tony Barbource7524e2016-07-28 12:01:45 -0600481 // This function could get called twice if the texture uses a staging buffer
482 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600484 return;
485
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600486 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487 assert(!err);
488
Tony Barbource7524e2016-07-28 12:01:45 -0600489 VkFence fence;
490 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
491 .pNext = NULL,
492 .flags = 0};
493 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700494 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700495 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
496 .pNext = NULL,
497 .waitSemaphoreCount = 0,
498 .pWaitSemaphores = NULL,
499 .pWaitDstStageMask = NULL,
500 .commandBufferCount = 1,
501 .pCommandBuffers = cmd_bufs,
502 .signalSemaphoreCount = 0,
503 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600504
Tony Barbource7524e2016-07-28 12:01:45 -0600505 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600506 assert(!err);
507
Tony Barbource7524e2016-07-28 12:01:45 -0600508 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509 assert(!err);
510
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600511 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600512 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600513 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600514}
515
Karl Schultze4dc75c2016-02-02 15:37:51 -0700516static void demo_set_image_layout(struct demo *demo, VkImage image,
517 VkImageAspectFlags aspectMask,
518 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700519 VkImageLayout new_image_layout,
520 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600521 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600523 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800524 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800525 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800527 .commandPool = demo->cmd_pool,
528 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700529 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530 };
531
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800532 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 assert(!err);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700534 VkCommandBufferBeginInfo cmd_buf_info = {
535 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
536 .pNext = NULL,
537 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700538 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700539 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600540 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600541 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600542 }
543
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600544 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600545 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600546 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700547 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800548 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549 .oldLayout = old_image_layout,
550 .newLayout = new_image_layout,
551 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700552 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800554 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600555 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800556 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557 }
558
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700559 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700560 image_memory_barrier.dstAccessMask =
561 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700562 }
563
564 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700565 image_memory_barrier.dstAccessMask =
566 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700567 }
568
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600569 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600570 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700571 image_memory_barrier.dstAccessMask =
572 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600573 }
574
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600575 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600576
Tony Barbour50bbca42015-06-29 16:20:35 -0600577 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
578 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600579
Karl Schultze4dc75c2016-02-02 15:37:51 -0700580 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
581 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582}
583
Karl Schultze4dc75c2016-02-02 15:37:51 -0700584static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700585 const VkCommandBufferBeginInfo cmd_buf_info = {
586 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
587 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600588 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700589 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700590 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800591 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700592 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
593 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800594 };
595 const VkRenderPassBeginInfo rp_begin = {
596 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
597 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800598 .renderPass = demo->render_pass,
599 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800600 .renderArea.offset.x = 0,
601 .renderArea.offset.y = 0,
602 .renderArea.extent.width = demo->width,
603 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600604 .clearValueCount = 2,
605 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700606 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800607 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600608
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600609 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610 assert(!err);
611
Tony Barbour5c5b1632016-04-26 11:13:48 -0600612 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
613 // happens to the previous contents of the image
614 VkImageMemoryBarrier image_memory_barrier = {
615 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
616 .pNext = NULL,
617 .srcAccessMask = 0,
618 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
619 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
620 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
621 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
622 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
623 .image = demo->buffers[demo->current_buffer].image,
624 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
625
Tony Barbource7524e2016-07-28 12:01:45 -0600626 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
627 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
628 NULL, 0, NULL, 1, &image_memory_barrier);
Tony Barbour5c5b1632016-04-26 11:13:48 -0600629
Chia-I Wuf051d262015-10-27 19:25:11 +0800630 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800631
Karl Schultze4dc75c2016-02-02 15:37:51 -0700632 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
633 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
634 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
635 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600636 VkViewport viewport;
637 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700638 viewport.height = (float)demo->height;
639 viewport.width = (float)demo->width;
640 viewport.minDepth = (float)0.0f;
641 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700642 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600643
644 VkRect2D scissor;
645 memset(&scissor, 0, sizeof(scissor));
646 scissor.extent.width = demo->width;
647 scissor.extent.height = demo->height;
648 scissor.offset.x = 0;
649 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700650 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600651 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600652 // Note that ending the renderpass changes the image's layout from
653 // COLOR_ATTACHEMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800654 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600656 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657 assert(!err);
658}
659
Karl Schultze4dc75c2016-02-02 15:37:51 -0700660void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600661 mat4x4 MVP, Model, VP;
662 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600663 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600664 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600665
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600666 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600668 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600669 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700670 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
671 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600672 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600673
Karl Schultze4dc75c2016-02-02 15:37:51 -0700674 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
675 demo->uniform_data.mem_alloc.allocationSize, 0,
676 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677 assert(!err);
678
Karl Schultze4dc75c2016-02-02 15:37:51 -0700679 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600680
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600681 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600682}
683
Karl Schultze4dc75c2016-02-02 15:37:51 -0700684static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600685 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600686
Ian Elliottaaae5352015-07-06 14:27:58 -0600687 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700688 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbource7524e2016-07-28 12:01:45 -0600689 demo->image_acquired_semaphore, (VkFence)0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600690 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600691 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
692 // demo->swapchain is out of date (e.g. the window was resized) and
693 // must be recreated:
694 demo_resize(demo);
695 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600696 return;
697 } else if (err == VK_SUBOPTIMAL_KHR) {
698 // demo->swapchain is not as optimal as it could be, but the platform's
699 // presentation engine will still present the image correctly.
700 } else {
701 assert(!err);
702 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600703
Tony Barbource7524e2016-07-28 12:01:45 -0600704 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600705 // that the image won't be rendered to until the presentation
706 // engine has fully released ownership to the application, and it is
707 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600708 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700709 VkPipelineStageFlags pipe_stage_flags =
Tony Barbource7524e2016-07-28 12:01:45 -0600710 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
711 VkSubmitInfo submit_info = {
712 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
713 .pNext = NULL,
714 .waitSemaphoreCount = 1,
715 .pWaitSemaphores = &demo->image_acquired_semaphore,
716 .pWaitDstStageMask = &pipe_stage_flags,
717 .commandBufferCount = 1,
718 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
719 .signalSemaphoreCount = 1,
720 .pSignalSemaphores = &demo->draw_complete_semaphore};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600721
Tony Barboura2a67812016-07-18 13:21:06 -0600722 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600723 assert(!err);
724
Ian Elliotta0781972015-08-21 15:09:33 -0600725 VkPresentInfoKHR present = {
726 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600727 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600728 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700729 .pSwapchains = &demo->swapchain,
730 .pImageIndices = &demo->current_buffer,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600731 .waitSemaphoreCount = 1,
Tony Barbource7524e2016-07-28 12:01:45 -0600732 .pWaitSemaphores = &demo->draw_complete_semaphore,
Ian Elliottaaae5352015-07-06 14:27:58 -0600733 };
734
Karl Schultze4dc75c2016-02-02 15:37:51 -0700735 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Tony Barboura2a67812016-07-18 13:21:06 -0600736 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600737 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
738 // demo->swapchain is out of date (e.g. the window was resized) and
739 // must be recreated:
740 demo_resize(demo);
741 } else if (err == VK_SUBOPTIMAL_KHR) {
742 // demo->swapchain is not as optimal as it could be, but the platform's
743 // presentation engine will still present the image correctly.
744 } else {
745 assert(!err);
746 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600747}
748
Karl Schultze4dc75c2016-02-02 15:37:51 -0700749static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600750 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600751 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600752
Ian Elliottb08e0d92015-11-20 11:55:46 -0700753 // Check the surface capabilities and formats
754 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700755 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
756 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600757 assert(!err);
758
Ian Elliott8528eff2015-08-07 15:56:59 -0600759 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700760 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
761 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600762 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600763 VkPresentModeKHR *presentModes =
764 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600765 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700766 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
767 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600768 assert(!err);
769
Ian Elliotta0781972015-08-21 15:09:33 -0600770 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600771 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700772 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600773 // If the surface size is undefined, the size is set to
774 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600775 swapchainExtent.width = demo->width;
776 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700777 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600778 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700779 swapchainExtent = surfCapabilities.currentExtent;
780 demo->width = surfCapabilities.currentExtent.width;
781 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600782 }
783
784 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600785 // tearing mode. If not, try IMMEDIATE which will usually be available,
786 // and is fastest (though it tears). If not, fall back to FIFO which is
787 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600788 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600790 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
791 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600792 break;
793 }
Ian Elliotta0781972015-08-21 15:09:33 -0600794 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
795 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
796 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600797 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600798 }
799
800 // Determine the number of VkImage's to use in the swap chain (we desire to
801 // own only 1 image at a time, besides the images being displayed and
802 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700803 uint32_t desiredNumberOfSwapchainImages =
804 surfCapabilities.minImageCount + 1;
Tony Barboura2a67812016-07-18 13:21:06 -0600805 // If maxImageCount is 0, we can ask for as many images as we want, otherwise
806 // we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700807 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700808 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600809 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700810 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600811 }
812
Tony Barbour9fd6d352015-09-16 15:01:32 -0600813 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700814 if (surfCapabilities.supportedTransforms &
815 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700816 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600817 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700818 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600819 }
820
Tony Barboura2a67812016-07-18 13:21:06 -0600821 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600822 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800823 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700824 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600825 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800826 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600827 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700828 .imageExtent =
829 {
830 .width = swapchainExtent.width, .height = swapchainExtent.height,
831 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700832 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600833 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700834 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700835 .imageArrayLayers = 1,
836 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
837 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600838 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600839 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600840 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600841 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600842 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600843 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600844 uint32_t queueFamilyIndices[2] = {(uint32_t) demo->graphics_queue_family_index, (uint32_t) demo->present_queue_family_index};
845 if (demo->graphics_queue_family_index != demo->present_queue_family_index)
846 {
847 // If the graphics and present queues are from different queue families, we either have to
848 // explicitly transfer ownership of images between the queues, or we have to create the swapchain
849 // with imageSharingMode as VK_SHARING_MODE_CONCURRENT
850 swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
851 swapchain_ci.queueFamilyIndexCount = 2;
852 swapchain_ci.pQueueFamilyIndices = queueFamilyIndices;
853 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600854
Tony Barboura2a67812016-07-18 13:21:06 -0600855 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700856 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800857 assert(!err);
858
Ian Elliottcf074d32015-10-16 18:02:43 -0600859 // If we just re-created an existing swapchain, we should destroy the old
860 // swapchain at this point.
861 // Note: destroying the swapchain also cleans up all its associated
862 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800863 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700864 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600865 }
866
867 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600868 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600869 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800870
Karl Schultze4dc75c2016-02-02 15:37:51 -0700871 VkImage *swapchainImages =
872 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600873 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600874 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600875 &demo->swapchainImageCount,
876 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600877 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600878
Karl Schultze4dc75c2016-02-02 15:37:51 -0700879 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
880 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600881 assert(demo->buffers);
882
Ian Elliotta0781972015-08-21 15:09:33 -0600883 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600884 VkImageViewCreateInfo color_image_view = {
885 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600886 .pNext = NULL,
887 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700888 .components =
889 {
890 .r = VK_COMPONENT_SWIZZLE_R,
891 .g = VK_COMPONENT_SWIZZLE_G,
892 .b = VK_COMPONENT_SWIZZLE_B,
893 .a = VK_COMPONENT_SWIZZLE_A,
894 },
895 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
896 .baseMipLevel = 0,
897 .levelCount = 1,
898 .baseArrayLayer = 0,
899 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600900 .viewType = VK_IMAGE_VIEW_TYPE_2D,
901 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600902 };
903
Ian Elliotta0781972015-08-21 15:09:33 -0600904 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600905
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600906 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600907
Karl Schultze4dc75c2016-02-02 15:37:51 -0700908 err = vkCreateImageView(demo->device, &color_image_view, NULL,
909 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600910 assert(!err);
911 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700912
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500913
Mark Young04fd3d82016-01-25 14:43:50 -0700914 if (NULL != presentModes) {
915 free(presentModes);
916 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600917}
918
Karl Schultze4dc75c2016-02-02 15:37:51 -0700919static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600920 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600921 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600922 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600923 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600924 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600925 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700926 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600927 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600928 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800929 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600930 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600931 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600932 .flags = 0,
933 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200934
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600935 VkImageViewCreateInfo view = {
936 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600937 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800938 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600939 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700940 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
941 .baseMipLevel = 0,
942 .levelCount = 1,
943 .baseArrayLayer = 0,
944 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600945 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600946 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600947 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600948
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500949 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600950 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600951 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600952
953 demo->depth.format = depth_format;
954
955 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700956 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600957 assert(!err);
958
Karl Schultze4dc75c2016-02-02 15:37:51 -0700959 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600960 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600961
Chia-I Wuf48700b2015-11-10 16:21:09 +0800962 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200963 demo->depth.mem_alloc.pNext = NULL;
964 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
965 demo->depth.mem_alloc.memoryTypeIndex = 0;
966
Karl Schultze4dc75c2016-02-02 15:37:51 -0700967 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
968 0, /* No requirements */
969 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600970 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500972 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
974 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500975 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600976
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500977 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700978 err =
979 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500980 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600981
Karl Schultze4dc75c2016-02-02 15:37:51 -0700982 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
983 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700984 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
985 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600986
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600987 /* create image view */
988 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800989 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991}
992
Tony Barbour1a86d032015-09-21 15:17:33 -0600993/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700995 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500996#ifdef __ANDROID__
997#include <lunarg.ppm.h>
998 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600999 cPtr = (char*)lunarg_ppm;
1000 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001001 return false;
1002 }
1003 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001004 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001005 if (rgba_data == NULL) {
1006 return true;
1007 }
1008 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001009 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001010 return false;
1011 }
1012 while(strncmp(cPtr++, "\n", 1));
1013
1014 for (int y = 0; y < *height; y++) {
1015 uint8_t *rowPtr = rgba_data;
1016 for (int x = 0; x < *width; x++) {
1017 memcpy(rowPtr, cPtr, 3);
1018 rowPtr[3] = 255; /* Alpha of 1 */
1019 rowPtr += 4;
1020 cPtr += 3;
1021 }
1022 rgba_data += layout->rowPitch;
1023 }
1024
1025 return true;
1026#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001027 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001028 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001029
Tony Barbour1a86d032015-09-21 15:17:33 -06001030 if (!fPtr)
1031 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001032
Tony Barbour1a86d032015-09-21 15:17:33 -06001033 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001034 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1035 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001036 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001037 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001038
Tony Barbour1a86d032015-09-21 15:17:33 -06001039 do {
1040 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001041 if (cPtr == NULL) {
1042 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001043 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001044 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001045 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001046
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001047 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001048 if (rgba_data == NULL) {
1049 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001050 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001051 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001052 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001053 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001054 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1055 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001056 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001057 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001058
Karl Schultze4dc75c2016-02-02 15:37:51 -07001059 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001060 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001061 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001062 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001063 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001064 rowPtr[3] = 255; /* Alpha of 1 */
1065 rowPtr += 4;
1066 }
1067 rgba_data += layout->rowPitch;
1068 }
1069 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001070 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001071#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001072}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001073
Karl Schultze4dc75c2016-02-02 15:37:51 -07001074static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001075 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001076 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001077 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001078 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001079 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001080 int32_t tex_width;
1081 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001082 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001083 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001084
Karl Schultze4dc75c2016-02-02 15:37:51 -07001085 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001086 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001087 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001088
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001089 tex_obj->tex_width = tex_width;
1090 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001091
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001092 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001093 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001094 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001095 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001096 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001097 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001098 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001099 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001100 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001101 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001102 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001104 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001105 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001106
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001107 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001108
Karl Schultze4dc75c2016-02-02 15:37:51 -07001109 err =
1110 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111 assert(!err);
1112
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001113 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001114
Chia-I Wuf48700b2015-11-10 16:21:09 +08001115 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001116 tex_obj->mem_alloc.pNext = NULL;
1117 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1118 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119
Karl Schultze4dc75c2016-02-02 15:37:51 -07001120 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1121 required_props,
1122 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001123 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001124
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001125 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001126 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001127 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001128 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001130 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001131 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001132 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001134 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001135 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001136 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001138 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001140 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001141 void *data;
1142
Karl Schultze4dc75c2016-02-02 15:37:51 -07001143 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1144 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001145
Karl Schultze4dc75c2016-02-02 15:37:51 -07001146 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1147 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148 assert(!err);
1149
1150 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1151 fprintf(stderr, "Error loading texture: %s\n", filename);
1152 }
1153
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001154 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001156
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001157 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001158 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001159 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1160 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001161 /* setting the image layout does not reference the actual memory so no need
1162 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163}
1164
Karl Schultze4dc75c2016-02-02 15:37:51 -07001165static void demo_destroy_texture_image(struct demo *demo,
1166 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001168 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1169 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170}
1171
Karl Schultze4dc75c2016-02-02 15:37:51 -07001172static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001173 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001174 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001175 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001176
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001177 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001179 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001180 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001181
Karl Schultze4dc75c2016-02-02 15:37:51 -07001182 if ((props.linearTilingFeatures &
1183 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1184 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001185 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001186 demo_prepare_texture_image(
1187 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1188 VK_IMAGE_USAGE_SAMPLED_BIT,
1189 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1190 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001191 } else if (props.optimalTilingFeatures &
1192 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001193 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001194 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195
1196 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001197 demo_prepare_texture_image(
1198 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1199 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1200 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1201 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001202
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203 demo_prepare_texture_image(
1204 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1205 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1206 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001207
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001208 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001209 VK_IMAGE_ASPECT_COLOR_BIT,
1210 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001211 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1212 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001213
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001214 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001215 VK_IMAGE_ASPECT_COLOR_BIT,
1216 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001217 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1218 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001220 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001221 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1222 .srcOffset = {0, 0, 0},
1223 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1224 .dstOffset = {0, 0, 0},
1225 .extent = {staging_texture.tex_width,
1226 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001227 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001228 vkCmdCopyImage(
1229 demo->cmd, staging_texture.image,
1230 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1231 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001232
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001233 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001234 VK_IMAGE_ASPECT_COLOR_BIT,
1235 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001236 demo->textures[i].imageLayout,
1237 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001239 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001241 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001242 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001243 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1244 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001245 }
1246
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001247 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001248 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001249 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001250 .magFilter = VK_FILTER_NEAREST,
1251 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001252 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001253 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1254 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1255 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001256 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001257 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001258 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001259 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001260 .minLod = 0.0f,
1261 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001262 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001263 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001264 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001265
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001266 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001267 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001269 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001270 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001271 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001272 .components =
1273 {
1274 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1275 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1276 },
1277 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001278 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001279 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001280
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001281 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001282 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001283 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001284 assert(!err);
1285
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001286 /* create image view */
1287 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001288 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001289 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290 assert(!err);
1291 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001292}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293
Karl Schultze4dc75c2016-02-02 15:37:51 -07001294void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001295 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001296 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001297 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001298 int i;
1299 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001300 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001301 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001302 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001303
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001304 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001305 mat4x4_mul(MVP, VP, demo->model_matrix);
1306 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001307 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001308
Karl Schultze4dc75c2016-02-02 15:37:51 -07001309 for (i = 0; i < 12 * 3; i++) {
1310 data.position[i][0] = g_vertex_buffer_data[i * 3];
1311 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1312 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001313 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001314 data.attr[i][0] = g_uv_buffer_data[2 * i];
1315 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001316 data.attr[i][2] = 0;
1317 data.attr[i][3] = 0;
1318 }
1319
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001320 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001321 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001322 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001323 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001324 err =
1325 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001326 assert(!err);
1327
Karl Schultze4dc75c2016-02-02 15:37:51 -07001328 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1329 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001330
Chia-I Wuf48700b2015-11-10 16:21:09 +08001331 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001332 demo->uniform_data.mem_alloc.pNext = NULL;
1333 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1334 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1335
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001337 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1338 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001340 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001341
Karl Schultze4dc75c2016-02-02 15:37:51 -07001342 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1343 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001344 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001345
Karl Schultze4dc75c2016-02-02 15:37:51 -07001346 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1347 demo->uniform_data.mem_alloc.allocationSize, 0,
1348 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001349 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001350
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001351 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001352
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001353 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001354
Karl Schultze4dc75c2016-02-02 15:37:51 -07001355 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1356 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001357 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001358
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001359 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1360 demo->uniform_data.buffer_info.offset = 0;
1361 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001362}
1363
Karl Schultze4dc75c2016-02-02 15:37:51 -07001364static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001365 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 [0] =
1367 {
1368 .binding = 0,
1369 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1370 .descriptorCount = 1,
1371 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1372 .pImmutableSamplers = NULL,
1373 },
1374 [1] =
1375 {
1376 .binding = 1,
1377 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1378 .descriptorCount = DEMO_TEXTURE_COUNT,
1379 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1380 .pImmutableSamplers = NULL,
1381 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001382 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001383 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001384 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001385 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001386 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001387 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001388 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001389 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390
Karl Schultze4dc75c2016-02-02 15:37:51 -07001391 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1392 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001393 assert(!err);
1394
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001395 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001396 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1397 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001398 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001399 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001400 };
1401
Karl Schultze4dc75c2016-02-02 15:37:51 -07001402 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001403 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001405}
1406
Karl Schultze4dc75c2016-02-02 15:37:51 -07001407static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001408 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001409 [0] =
1410 {
1411 .format = demo->format,
1412 .samples = VK_SAMPLE_COUNT_1_BIT,
1413 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1414 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1415 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1416 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1417 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Tony Barbour98390392016-07-28 10:50:13 -06001418 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001419 },
1420 [1] =
1421 {
1422 .format = demo->depth.format,
1423 .samples = VK_SAMPLE_COUNT_1_BIT,
1424 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1425 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1426 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1427 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1428 .initialLayout =
1429 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1430 .finalLayout =
1431 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1432 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001433 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001434 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001435 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001436 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001437 const VkAttachmentReference depth_reference = {
1438 .attachment = 1,
1439 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1440 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001441 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001442 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1443 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001444 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001445 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001446 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001447 .pColorAttachments = &color_reference,
1448 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001449 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001450 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001451 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001452 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001453 const VkRenderPassCreateInfo rp_info = {
1454 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1455 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001456 .attachmentCount = 2,
1457 .pAttachments = attachments,
1458 .subpassCount = 1,
1459 .pSubpasses = &subpass,
1460 .dependencyCount = 0,
1461 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001462 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001463 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001464
Chia-I Wu63636062015-10-26 21:10:41 +08001465 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001466 assert(!err);
1467}
1468
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001469//TODO: Merge shader reading
1470#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001471static VkShaderModule
1472demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001473 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001474 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001475 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001476
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001477 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1478 moduleCreateInfo.pNext = NULL;
1479
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001480 moduleCreateInfo.codeSize = size;
1481 moduleCreateInfo.pCode = code;
1482 moduleCreateInfo.flags = 0;
1483 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1484 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001485
1486 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001487}
1488
Karl Schultze4dc75c2016-02-02 15:37:51 -07001489char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001490 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001491 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001492 void *shader_code;
1493
1494 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001495 if (!fp)
1496 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001497
1498 fseek(fp, 0L, SEEK_END);
1499 size = ftell(fp);
1500
1501 fseek(fp, 0L, SEEK_SET);
1502
1503 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001504 retval = fread(shader_code, size, 1, fp);
1505 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001506
1507 *psize = size;
1508
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001509 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001510 return shader_code;
1511}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001512#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001513
Karl Schultze4dc75c2016-02-02 15:37:51 -07001514static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001515#ifdef __ANDROID__
1516 VkShaderModuleCreateInfo sh_info = {};
1517 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1518
1519#include "cube.vert.h"
1520 sh_info.codeSize = sizeof(cube_vert);
1521 sh_info.pCode = cube_vert;
1522 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1523 assert(!err);
1524#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001525 void *vertShaderCode;
1526 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001527
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001528 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1529
Karl Schultze4dc75c2016-02-02 15:37:51 -07001530 demo->vert_shader_module =
1531 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001532
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001533 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001534#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001535
1536 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537}
1538
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001540#ifdef __ANDROID__
1541 VkShaderModuleCreateInfo sh_info = {};
1542 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1543
1544#include "cube.frag.h"
1545 sh_info.codeSize = sizeof(cube_frag);
1546 sh_info.pCode = cube_frag;
1547 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1548 assert(!err);
1549#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001550 void *fragShaderCode;
1551 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001552
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001553 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001554
Karl Schultze4dc75c2016-02-02 15:37:51 -07001555 demo->frag_shader_module =
1556 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001557
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001558 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001559#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001560
1561 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562}
1563
Karl Schultze4dc75c2016-02-02 15:37:51 -07001564static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001565 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001566 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001567 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001568 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 VkPipelineRasterizationStateCreateInfo rs;
1570 VkPipelineColorBlendStateCreateInfo cb;
1571 VkPipelineDepthStencilStateCreateInfo ds;
1572 VkPipelineViewportStateCreateInfo vp;
1573 VkPipelineMultisampleStateCreateInfo ms;
1574 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1575 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001576 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001577
Piers Daniellba839d12015-09-29 13:01:09 -06001578 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1579 memset(&dynamicState, 0, sizeof dynamicState);
1580 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1581 dynamicState.pDynamicStates = dynamicStateEnables;
1582
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001584 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001585 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001586
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001587 memset(&vi, 0, sizeof(vi));
1588 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1589
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001590 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001591 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001592 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593
1594 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001595 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1596 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001597 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001598 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001599 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001600 rs.rasterizerDiscardEnable = VK_FALSE;
1601 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001602 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001603
1604 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001605 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1606 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001607 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001608 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001609 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001610 cb.attachmentCount = 1;
1611 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001612
Tony Barbour29645d02015-01-16 14:27:35 -07001613 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001614 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001615 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001616 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1617 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001618 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001619 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1620 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001621
1622 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001623 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001624 ds.depthTestEnable = VK_TRUE;
1625 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001626 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001627 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001628 ds.back.failOp = VK_STENCIL_OP_KEEP;
1629 ds.back.passOp = VK_STENCIL_OP_KEEP;
1630 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001631 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001632 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001633
Tony Barbour29645d02015-01-16 14:27:35 -07001634 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001635 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001636 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001637 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001639 // Two stages: vs and fs
1640 pipeline.stageCount = 2;
1641 VkPipelineShaderStageCreateInfo shaderStages[2];
1642 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1643
Karl Schultze4dc75c2016-02-02 15:37:51 -07001644 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1645 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001646 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001647 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001648
Karl Schultze4dc75c2016-02-02 15:37:51 -07001649 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1650 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001651 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001652 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001653
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001654 memset(&pipelineCache, 0, sizeof(pipelineCache));
1655 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001656
Karl Schultze4dc75c2016-02-02 15:37:51 -07001657 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1658 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001659 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001660
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001662 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001663 pipeline.pRasterizationState = &rs;
1664 pipeline.pColorBlendState = &cb;
1665 pipeline.pMultisampleState = &ms;
1666 pipeline.pViewportState = &vp;
1667 pipeline.pDepthStencilState = &ds;
1668 pipeline.pStages = shaderStages;
1669 pipeline.renderPass = demo->render_pass;
1670 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001671
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001672 pipeline.renderPass = demo->render_pass;
1673
Karl Schultze4dc75c2016-02-02 15:37:51 -07001674 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1675 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676 assert(!err);
1677
Chia-I Wu63636062015-10-26 21:10:41 +08001678 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1679 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001680}
1681
Karl Schultze4dc75c2016-02-02 15:37:51 -07001682static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001683 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001684 [0] =
1685 {
1686 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1687 .descriptorCount = 1,
1688 },
1689 [1] =
1690 {
1691 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1692 .descriptorCount = DEMO_TEXTURE_COUNT,
1693 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001694 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001695 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001696 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001697 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001698 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001699 .poolSizeCount = 2,
1700 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001701 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001702 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001703
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1705 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001706 assert(!err);
1707}
1708
Karl Schultze4dc75c2016-02-02 15:37:51 -07001709static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001710 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001711 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001712 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001713 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001714
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001715 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001716 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001717 .pNext = NULL,
1718 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001719 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001720 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001721 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001722 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001723
Chia-I Wuae721ba2015-05-25 16:27:55 +08001724 memset(&tex_descs, 0, sizeof(tex_descs));
1725 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001726 tex_descs[i].sampler = demo->textures[i].sampler;
1727 tex_descs[i].imageView = demo->textures[i].view;
1728 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001729 }
1730
1731 memset(&writes, 0, sizeof(writes));
1732
1733 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001734 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001735 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001736 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001737 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001738
1739 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001740 writes[1].dstSet = demo->desc_set;
1741 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001742 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001743 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001744 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001745
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001746 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001747}
1748
Karl Schultze4dc75c2016-02-02 15:37:51 -07001749static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001750 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001751 attachments[1] = demo->depth.view;
1752
Chia-I Wuf973e322015-07-08 13:34:24 +08001753 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001754 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1755 .pNext = NULL,
1756 .renderPass = demo->render_pass,
1757 .attachmentCount = 2,
1758 .pAttachments = attachments,
1759 .width = demo->width,
1760 .height = demo->height,
1761 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001762 };
1763 VkResult U_ASSERT_ONLY err;
1764 uint32_t i;
1765
Karl Schultze4dc75c2016-02-02 15:37:51 -07001766 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1767 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001768 assert(demo->framebuffers);
1769
1770 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001771 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001772 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1773 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001774 assert(!err);
1775 }
1776}
1777
Karl Schultze4dc75c2016-02-02 15:37:51 -07001778static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001779 VkResult U_ASSERT_ONLY err;
1780
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001781 const VkCommandPoolCreateInfo cmd_pool_info = {
1782 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001783 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001784 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001785 .flags = 0,
1786 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001787 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1788 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001789 assert(!err);
1790
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001791 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001792 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001793 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001794 .commandPool = demo->cmd_pool,
1795 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001796 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001797 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001798
1799 demo_prepare_buffers(demo);
1800 demo_prepare_depth(demo);
1801 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001802 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001803
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001804 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001805 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001806 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001807
Ian Elliotta0781972015-08-21 15:09:33 -06001808 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001809 err =
1810 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001811 assert(!err);
1812 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001813
Chia-I Wu63ea9262015-03-26 13:14:16 +08001814 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001815 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001816
Chia-I Wuf973e322015-07-08 13:34:24 +08001817 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001818
Ian Elliotta0781972015-08-21 15:09:33 -06001819 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001820 demo->current_buffer = i;
1821 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1822 }
1823
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001824 /*
1825 * Prepare functions above may generate pipeline commands
1826 * that need to be flushed before beginning the render loop.
1827 */
1828 demo_flush_init_cmd(demo);
1829
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001830 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001831 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001832}
1833
Karl Schultze4dc75c2016-02-02 15:37:51 -07001834static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001835 uint32_t i;
1836
1837 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06001838 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001839
Tony Barbourd8e504f2015-10-08 14:26:24 -06001840 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001841 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001842 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001843 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001844 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001845
Chia-I Wu63636062015-10-26 21:10:41 +08001846 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1847 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1848 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1849 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1850 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001851
1852 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001853 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1854 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1855 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1856 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001857 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001858 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001859
Chia-I Wu63636062015-10-26 21:10:41 +08001860 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1861 vkDestroyImage(demo->device, demo->depth.image, NULL);
1862 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001863
Chia-I Wu63636062015-10-26 21:10:41 +08001864 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1865 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001866
Ian Elliotta0781972015-08-21 15:09:33 -06001867 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001868 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001869 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1870 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001871 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001872 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001873
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001874 free(demo->queue_props);
1875
Chia-I Wu63636062015-10-26 21:10:41 +08001876 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbource7524e2016-07-28 12:01:45 -06001877 vkDestroySemaphore(demo->device, demo->image_acquired_semaphore, NULL);
1878 vkDestroySemaphore(demo->device, demo->draw_complete_semaphore, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001879 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001880 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001881 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001882 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001883 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001884 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001885
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001886#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001887 if (demo->use_xlib) {
1888 XDestroyWindow(demo->display, demo->xlib_window);
1889 XCloseDisplay(demo->display);
1890 } else {
1891 xcb_destroy_window(demo->connection, demo->xcb_window);
1892 xcb_disconnect(demo->connection);
1893 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001894 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001895#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001896 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001897 xcb_disconnect(demo->connection);
1898 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001899#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
1900 wl_shell_surface_destroy(demo->shell_surface);
1901 wl_surface_destroy(demo->window);
1902 wl_shell_destroy(demo->shell);
1903 wl_compositor_destroy(demo->compositor);
1904 wl_registry_destroy(demo->registry);
1905 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001906#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001907}
1908
Karl Schultze4dc75c2016-02-02 15:37:51 -07001909static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001910 uint32_t i;
1911
Mike Stroyanbb60b382015-10-28 09:46:57 -06001912 // Don't react to resize until after first initialization.
1913 if (!demo->prepared) {
1914 return;
1915 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001916 // In order to properly resize the window, we must re-create the swapchain
1917 // AND redo the command buffers, etc.
1918 //
1919 // First, perform part of the demo_cleanup() function:
1920 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06001921 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06001922
1923 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001924 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001925 }
1926 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001927 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001928
Chia-I Wu63636062015-10-26 21:10:41 +08001929 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1930 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1931 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1932 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1933 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001934
1935 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001936 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1937 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1938 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1939 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001940 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001941
Chia-I Wu63636062015-10-26 21:10:41 +08001942 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1943 vkDestroyImage(demo->device, demo->depth.image, NULL);
1944 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001945
Chia-I Wu63636062015-10-26 21:10:41 +08001946 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1947 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001948
1949 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001950 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001951 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1952 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001953 }
Chia-I Wu63636062015-10-26 21:10:41 +08001954 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001955 free(demo->buffers);
1956
Ian Elliottcf074d32015-10-16 18:02:43 -06001957 // Second, re-perform the demo_prepare() function, which will re-create the
1958 // swapchain:
1959 demo_prepare(demo);
1960}
1961
David Pinedo2bb7c932015-06-18 17:03:14 -06001962// On MS-Windows, make this a global, so it's available to WndProc()
1963struct demo demo;
1964
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001965#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07001966static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001967 if (!demo->prepared)
1968 return;
Tony Barbource7524e2016-07-28 12:01:45 -06001969
Ian Elliott639ca472015-04-16 15:23:05 -06001970 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06001971 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06001972 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001973 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06001974 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06001975 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001976}
Ian Elliott639ca472015-04-16 15:23:05 -06001977
1978// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001979LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1980 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001981 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06001982 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001983 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001984 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001985 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001986 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06001987 case WM_GETMINMAXINFO: // set window's minimum size
1988 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
1989 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001990 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07001991 // Resize the application to the new window size, except when
1992 // it was minimized. Vulkan doesn't support images or swapchains
1993 // with width=0 and height=0.
1994 if (wParam != SIZE_MINIMIZED) {
1995 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06001996 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07001997 demo_resize(&demo);
1998 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06001999 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002000 default:
2001 break;
2002 }
2003 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2004}
2005
Karl Schultze4dc75c2016-02-02 15:37:51 -07002006static void demo_create_window(struct demo *demo) {
2007 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002008
2009 // Initialize the window class structure:
2010 win_class.cbSize = sizeof(WNDCLASSEX);
2011 win_class.style = CS_HREDRAW | CS_VREDRAW;
2012 win_class.lpfnWndProc = WndProc;
2013 win_class.cbClsExtra = 0;
2014 win_class.cbWndExtra = 0;
2015 win_class.hInstance = demo->connection; // hInstance
2016 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2017 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2018 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2019 win_class.lpszMenuName = NULL;
2020 win_class.lpszClassName = demo->name;
2021 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2022 // Register window class:
2023 if (!RegisterClassEx(&win_class)) {
2024 // It didn't work, so try to give a useful error:
2025 printf("Unexpected error trying to start the application!\n");
2026 fflush(stdout);
2027 exit(1);
2028 }
2029 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002030 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002031 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002032 demo->window = CreateWindowEx(0,
2033 demo->name, // class name
2034 demo->name, // app name
2035 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002036 WS_VISIBLE | WS_SYSMENU,
2037 100, 100, // x/y coords
2038 wr.right - wr.left, // width
2039 wr.bottom - wr.top, // height
2040 NULL, // handle to parent
2041 NULL, // handle to menu
2042 demo->connection, // hInstance
2043 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002044 if (!demo->window) {
2045 // It didn't work, so try to give a useful error:
2046 printf("Cannot create a window in which to draw!\n");
2047 fflush(stdout);
2048 exit(1);
2049 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002050 // Window client area size must be at least 1 pixel high, to prevent crash.
2051 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2052 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002053}
Tony Barbour8295ac42016-08-08 11:04:17 -06002054#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002055static void demo_create_xlib_window(struct demo *demo) {
2056
2057 demo->display = XOpenDisplay(NULL);
2058 long visualMask = VisualScreenMask;
2059 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002060 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002061 vInfoTemplate.screen = DefaultScreen(demo->display);
2062 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2063 &vInfoTemplate, &numberOfVisuals);
2064
2065 Colormap colormap = XCreateColormap(
2066 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2067 visualInfo->visual, AllocNone);
2068
Rene Lindsay11612722016-06-13 17:20:39 -06002069 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002070 windowAttributes.colormap = colormap;
2071 windowAttributes.background_pixel = 0xFFFFFFFF;
2072 windowAttributes.border_pixel = 0;
2073 windowAttributes.event_mask =
2074 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2075
2076 demo->xlib_window = XCreateWindow(
2077 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2078 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2079 visualInfo->visual,
2080 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2081
2082 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2083 XMapWindow(demo->display, demo->xlib_window);
2084 XFlush(demo->display);
2085 demo->xlib_wm_delete_window =
2086 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2087}
2088static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2089 switch(event->type) {
2090 case ClientMessage:
2091 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2092 demo->quit = true;
2093 break;
2094 case KeyPress:
2095 switch (event->xkey.keycode) {
2096 case 0x9: // Escape
2097 demo->quit = true;
2098 break;
2099 case 0x71: // left arrow key
2100 demo->spin_angle += demo->spin_increment;
2101 break;
2102 case 0x72: // right arrow key
2103 demo->spin_angle -= demo->spin_increment;
2104 break;
2105 case 0x41:
2106 demo->pause = !demo->pause;
2107 break;
2108 }
2109 break;
2110 case ConfigureNotify:
2111 if ((demo->width != event->xconfigure.width) ||
2112 (demo->height != event->xconfigure.height)) {
2113 demo->width = event->xconfigure.width;
2114 demo->height = event->xconfigure.height;
2115 demo_resize(demo);
2116 }
2117 break;
2118 default:
2119 break;
2120 }
2121
2122}
2123
2124static void demo_run_xlib(struct demo *demo) {
2125
2126 while (!demo->quit) {
2127 XEvent event;
2128
2129 if (demo->pause) {
2130 XNextEvent(demo->display, &event);
2131 demo_handle_xlib_event(demo, &event);
2132 } else {
2133 while (XPending(demo->display) > 0) {
2134 XNextEvent(demo->display, &event);
2135 demo_handle_xlib_event(demo, &event);
2136 }
2137 }
2138
Tony Barbour2593b182016-04-19 10:57:58 -06002139 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002140 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002141 demo->curFrame++;
2142 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2143 demo->quit = true;
2144 }
2145}
Tony Barbour8295ac42016-08-08 11:04:17 -06002146#endif // VK_USE_PLATFORM_XLIB_KHR
2147#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002148static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002149 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002150 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002151 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002152 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002153 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002154 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002155 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002156 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2157 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002158 demo->quit = true;
2159 }
2160 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002161 case XCB_KEY_RELEASE: {
2162 const xcb_key_release_event_t *key =
2163 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002164
Karl Schultze4dc75c2016-02-02 15:37:51 -07002165 switch (key->detail) {
2166 case 0x9: // Escape
2167 demo->quit = true;
2168 break;
2169 case 0x71: // left arrow key
2170 demo->spin_angle += demo->spin_increment;
2171 break;
2172 case 0x72: // right arrow key
2173 demo->spin_angle -= demo->spin_increment;
2174 break;
2175 case 0x41:
2176 demo->pause = !demo->pause;
2177 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002178 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002179 } break;
2180 case XCB_CONFIGURE_NOTIFY: {
2181 const xcb_configure_notify_event_t *cfg =
2182 (const xcb_configure_notify_event_t *)event;
2183 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002184 demo->width = cfg->width;
2185 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002187 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002188 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002189 default:
2190 break;
2191 }
2192}
2193
Tony Barbour2593b182016-04-19 10:57:58 -06002194static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002195 xcb_flush(demo->connection);
2196
2197 while (!demo->quit) {
2198 xcb_generic_event_t *event;
2199
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002200 if (demo->pause) {
2201 event = xcb_wait_for_event(demo->connection);
2202 } else {
2203 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002204 while(event) {
2205 demo_handle_xcb_event(demo, event);
2206 free(event);
2207 event = xcb_poll_for_event(demo->connection);
2208 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002209 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002210
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002211 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002212 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002213 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002214 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002215 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002216 }
2217}
2218
Tony Barbour2593b182016-04-19 10:57:58 -06002219static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002220 uint32_t value_mask, value_list[32];
2221
Tony Barbour2593b182016-04-19 10:57:58 -06002222 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002223
2224 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2225 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002226 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002227 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002228
Tony Barbour2593b182016-04-19 10:57:58 -06002229 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002230 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2231 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2232 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002233
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002234 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002235 xcb_intern_atom_cookie_t cookie =
2236 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2237 xcb_intern_atom_reply_t *reply =
2238 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002239
Karl Schultze4dc75c2016-02-02 15:37:51 -07002240 xcb_intern_atom_cookie_t cookie2 =
2241 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2242 demo->atom_wm_delete_window =
2243 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002244
Tony Barbour2593b182016-04-19 10:57:58 -06002245 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002246 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002247 &(*demo->atom_wm_delete_window).atom);
2248 free(reply);
2249
Tony Barbour2593b182016-04-19 10:57:58 -06002250 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002251
Karl Schultze4dc75c2016-02-02 15:37:51 -07002252 // Force the x/y coordinates to 100,100 results are identical in consecutive
2253 // runs
2254 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002255 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002256 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002257}
Tony Barbour8295ac42016-08-08 11:04:17 -06002258#endif // VK_USE_PLATFORM_XCB_KHR
2259#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002260static void demo_run(struct demo *demo) {
2261 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002262 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002263 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002264 demo->curFrame++;
2265 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2266 demo->quit = true;
2267 }
2268}
2269
2270static void handle_ping(void *data UNUSED,
2271 struct wl_shell_surface *shell_surface,
2272 uint32_t serial) {
2273 wl_shell_surface_pong(shell_surface, serial);
2274}
2275
2276static void handle_configure(void *data UNUSED,
2277 struct wl_shell_surface *shell_surface UNUSED,
2278 uint32_t edges UNUSED, int32_t width UNUSED,
2279 int32_t height UNUSED) {}
2280
2281static void handle_popup_done(void *data UNUSED,
2282 struct wl_shell_surface *shell_surface UNUSED) {}
2283
2284static const struct wl_shell_surface_listener shell_surface_listener = {
2285 handle_ping, handle_configure, handle_popup_done};
2286
2287static void demo_create_window(struct demo *demo) {
2288 demo->window = wl_compositor_create_surface(demo->compositor);
2289 if (!demo->window) {
2290 printf("Can not create wayland_surface from compositor!\n");
2291 fflush(stdout);
2292 exit(1);
2293 }
2294
2295 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2296 if (!demo->shell_surface) {
2297 printf("Can not get shell_surface from wayland_surface!\n");
2298 fflush(stdout);
2299 exit(1);
2300 }
2301 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2302 demo);
2303 wl_shell_surface_set_toplevel(demo->shell_surface);
2304 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2305}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002306#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2307static void demo_run(struct demo *demo) {
2308 if (!demo->prepared)
2309 return;
2310
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002311 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002312 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002313 demo->curFrame++;
2314}
2315#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002316
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002317/*
2318 * Return 1 (true) if all layer names specified in check_names
2319 * can be found in given layer properties.
2320 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002321static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002322 uint32_t layer_count,
2323 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002324 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002325 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002326 for (uint32_t j = 0; j < layer_count; j++) {
2327 if (!strcmp(check_names[i], layers[j].layerName)) {
2328 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002329 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002330 }
2331 }
2332 if (!found) {
2333 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2334 return 0;
2335 }
2336 }
2337 return 1;
2338}
2339
Karl Schultze4dc75c2016-02-02 15:37:51 -07002340static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002341 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002342 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002343 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002344 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002345 char **instance_validation_layers = NULL;
2346 demo->enabled_extension_count = 0;
2347 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002348
Karl Schultz7c50ad62016-04-01 12:07:03 -06002349 char *instance_validation_layers_alt1[] = {
2350 "VK_LAYER_LUNARG_standard_validation"
2351 };
2352
2353 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002354 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2355 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2356 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2357 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002358 };
2359
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002360 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002361 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002362 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002363
Karl Schultz7c50ad62016-04-01 12:07:03 -06002364 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002365 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002366
Karl Schultz7c50ad62016-04-01 12:07:03 -06002367 instance_validation_layers = instance_validation_layers_alt1;
2368 if (instance_layer_count > 0) {
2369 VkLayerProperties *instance_layers =
2370 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2371 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2372 instance_layers);
2373 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002374
Karl Schultz7c50ad62016-04-01 12:07:03 -06002375
2376 validation_found = demo_check_layers(
2377 ARRAY_SIZE(instance_validation_layers_alt1),
2378 instance_validation_layers, instance_layer_count,
2379 instance_layers);
2380 if (validation_found) {
2381 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002382 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2383 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002384 } else {
2385 // use alternative set of validation layers
2386 instance_validation_layers = instance_validation_layers_alt2;
2387 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2388 validation_found = demo_check_layers(
2389 ARRAY_SIZE(instance_validation_layers_alt2),
2390 instance_validation_layers, instance_layer_count,
2391 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002392 validation_layer_count =
2393 ARRAY_SIZE(instance_validation_layers_alt2);
2394 for (uint32_t i = 0; i < validation_layer_count; i++) {
2395 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002396 }
2397 }
2398 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002399 }
Dustin Graves7c287352016-01-27 13:48:06 -07002400
Karl Schultz7c50ad62016-04-01 12:07:03 -06002401 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002402 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002403 "required validation layer.\n\n"
2404 "Please look at the Getting Started guide for additional "
2405 "information.\n",
2406 "vkCreateInstance Failure");
2407 }
Dustin Graves7c287352016-01-27 13:48:06 -07002408 }
2409
2410 /* Look for instance extensions */
2411 VkBool32 surfaceExtFound = 0;
2412 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002413#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002414 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002415#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002416 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002417
Karl Schultze4dc75c2016-02-02 15:37:51 -07002418 err = vkEnumerateInstanceExtensionProperties(
2419 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002420 assert(!err);
2421
Dustin Graves7c287352016-01-27 13:48:06 -07002422 if (instance_extension_count > 0) {
2423 VkExtensionProperties *instance_extensions =
2424 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2425 err = vkEnumerateInstanceExtensionProperties(
2426 NULL, &instance_extension_count, instance_extensions);
2427 assert(!err);
2428 for (uint32_t i = 0; i < instance_extension_count; i++) {
2429 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2430 instance_extensions[i].extensionName)) {
2431 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002432 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002433 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002434 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002435#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002436 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2437 instance_extensions[i].extensionName)) {
2438 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002439 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002440 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2441 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002442#endif
2443#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002444 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2445 instance_extensions[i].extensionName)) {
2446 platformSurfaceExtFound = 1;
2447 xlibSurfaceExtFound = 1;
2448 demo->extension_names[demo->enabled_extension_count++] =
2449 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2450 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002451#endif
2452#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002453 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2454 instance_extensions[i].extensionName)) {
2455 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002456 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002457 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2458 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002459#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002460#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2461 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2462 instance_extensions[i].extensionName)) {
2463 platformSurfaceExtFound = 1;
2464 demo->extension_names[demo->enabled_extension_count++] =
2465 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2466 }
2467#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002468#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002469 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2470 instance_extensions[i].extensionName)) {
2471 platformSurfaceExtFound = 1;
2472 demo->extension_names[demo->enabled_extension_count++] =
2473 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2474 }
2475#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002476 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2477 instance_extensions[i].extensionName)) {
2478 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002479 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002480 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2481 }
2482 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002483 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002484 }
Dustin Graves7c287352016-01-27 13:48:06 -07002485
2486 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002487 }
Dustin Graves7c287352016-01-27 13:48:06 -07002488
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002489 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002490 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2491 "the " VK_KHR_SURFACE_EXTENSION_NAME
2492 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002493 "Vulkan installable client driver (ICD) installed?\nPlease "
2494 "look at the Getting Started guide for additional "
2495 "information.\n",
2496 "vkCreateInstance Failure");
2497 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002498 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002499#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002500 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2501 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2502 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002503 "Vulkan installable client driver (ICD) installed?\nPlease "
2504 "look at the Getting Started guide for additional "
2505 "information.\n",
2506 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002507#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002508 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2509 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2510 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002511 "Vulkan installable client driver (ICD) installed?\nPlease "
2512 "look at the Getting Started guide for additional "
2513 "information.\n",
2514 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002515#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2516 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2517 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2518 " extension.\n\nDo you have a compatible "
2519 "Vulkan installable client driver (ICD) installed?\nPlease "
2520 "look at the Getting Started guide for additional "
2521 "information.\n",
2522 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002523#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2524 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2525 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2526 " extension.\n\nDo you have a compatible "
2527 "Vulkan installable client driver (ICD) installed?\nPlease "
2528 "look at the Getting Started guide for additional "
2529 "information.\n",
2530 "vkCreateInstance Failure");
2531#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002532 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002533#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002534 if (demo->use_xlib && !xlibSurfaceExtFound) {
2535 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2536 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2537 " extension.\n\nDo you have a compatible "
2538 "Vulkan installable client driver (ICD) installed?\nPlease "
2539 "look at the Getting Started guide for additional "
2540 "information.\n",
2541 "vkCreateInstance Failure");
2542 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002543#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002544 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002545 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002546 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002547 .pApplicationName = APP_SHORT_NAME,
2548 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002549 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002550 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002551 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002552 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002553 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002554 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002555 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002556 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002557 .enabledLayerCount = demo->enabled_layer_count,
2558 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2559 .enabledExtensionCount = demo->enabled_extension_count,
2560 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002561 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002562
2563 /*
2564 * This is info for a temp callback to use during CreateInstance.
2565 * After the instance is created, we use the instance-based
2566 * function to register the final callback.
2567 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002568 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002569 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002570 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2571 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002572 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002573 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002574 dbgCreateInfo.flags =
2575 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2576 inst_info.pNext = &dbgCreateInfo;
2577 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002578
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002579 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002580
Chia-I Wu63636062015-10-26 21:10:41 +08002581 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002582 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2583 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002584 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002585 "additional information.\n",
2586 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002587 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002588 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002589 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002590 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002591 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002592 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2593 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002594 "the Getting Started guide for additional information.\n",
2595 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002596 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002597
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002598 /* Make initial call to query gpu_count, then second call for gpu info*/
2599 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2600 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002601
2602 if (gpu_count > 0) {
2603 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2604 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2605 assert(!err);
2606 /* For cube demo we just grab the first physical device */
2607 demo->gpu = physical_devices[0];
2608 free(physical_devices);
2609 } else {
2610 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2611 "Do you have a compatible Vulkan installable client driver (ICD) "
2612 "installed?\nPlease look at the Getting Started guide for "
2613 "additional information.\n",
2614 "vkEnumeratePhysicalDevices Failure");
2615 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002616
Dustin Graves7c287352016-01-27 13:48:06 -07002617 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002618 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002619 VkBool32 swapchainExtFound = 0;
2620 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002621 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002622
Karl Schultze4dc75c2016-02-02 15:37:51 -07002623 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2624 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002625 assert(!err);
2626
Dustin Graves7c287352016-01-27 13:48:06 -07002627 if (device_extension_count > 0) {
2628 VkExtensionProperties *device_extensions =
2629 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2630 err = vkEnumerateDeviceExtensionProperties(
2631 demo->gpu, NULL, &device_extension_count, device_extensions);
2632 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002633
Dustin Graves7c287352016-01-27 13:48:06 -07002634 for (uint32_t i = 0; i < device_extension_count; i++) {
2635 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2636 device_extensions[i].extensionName)) {
2637 swapchainExtFound = 1;
2638 demo->extension_names[demo->enabled_extension_count++] =
2639 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2640 }
2641 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002642 }
Dustin Graves7c287352016-01-27 13:48:06 -07002643
2644 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002645 }
Dustin Graves7c287352016-01-27 13:48:06 -07002646
Tony Barbourcc69f452015-10-08 13:59:42 -06002647 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002648 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2649 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2650 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002651 "Vulkan installable client driver (ICD) installed?\nPlease "
2652 "look at the Getting Started guide for additional "
2653 "information.\n",
2654 "vkCreateInstance Failure");
2655 }
2656
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002657 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002658 demo->CreateDebugReportCallback =
2659 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2660 demo->inst, "vkCreateDebugReportCallbackEXT");
2661 demo->DestroyDebugReportCallback =
2662 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2663 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002664 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002665 ERR_EXIT(
2666 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2667 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002668 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002669 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002670 ERR_EXIT(
2671 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2672 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002673 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002674 demo->DebugReportMessage =
2675 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2676 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002677 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002678 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002679 "vkGetProcAddr Failure");
2680 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002681
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002682 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002683 PFN_vkDebugReportCallbackEXT callback;
2684 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002685 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002686 dbgCreateInfo.pNext = NULL;
2687 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002688 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002689 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002690 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002691 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2692 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002693 switch (err) {
2694 case VK_SUCCESS:
2695 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002696 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002697 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2698 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002699 break;
2700 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002701 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2702 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002703 break;
2704 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002705 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002706 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002707
2708 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002709 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2710 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002711 assert(demo->queue_count >= 1);
2712
Karl Schultze4dc75c2016-02-02 15:37:51 -07002713 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2714 demo->queue_count * sizeof(VkQueueFamilyProperties));
2715 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2716 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002717 // Find a queue that supports gfx
2718 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002719 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2720 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002721 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2722 break;
2723 }
2724 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002725 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002726 // If app has specific feature requirements it should check supported
2727 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002728 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002729 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002730
Tony Barbourda2bad52016-01-22 14:36:40 -07002731 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2732 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2733 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2734 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2735 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2736}
2737
Karl Schultze4dc75c2016-02-02 15:37:51 -07002738static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002739 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002740 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002741 const VkDeviceQueueCreateInfo queue = {
2742 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2743 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002744 .queueFamilyIndex = demo->graphics_queue_family_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002745 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002746 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002747
2748 VkDeviceCreateInfo device = {
2749 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2750 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002751 .queueCreateInfoCount = 1,
2752 .pQueueCreateInfos = &queue,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002753 .enabledLayerCount = 0,
2754 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002755 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002756 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2757 .pEnabledFeatures =
2758 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002759 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002760
Chia-I Wu63636062015-10-26 21:10:41 +08002761 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002762 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002763}
2764
Karl Schultze4dc75c2016-02-02 15:37:51 -07002765static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002766 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002767 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002768
Karl Schultze4dc75c2016-02-02 15:37:51 -07002769// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002770#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002771 VkWin32SurfaceCreateInfoKHR createInfo;
2772 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2773 createInfo.pNext = NULL;
2774 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002775 createInfo.hinstance = demo->connection;
2776 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002777
Karl Schultze4dc75c2016-02-02 15:37:51 -07002778 err =
2779 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002780#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2781 VkWaylandSurfaceCreateInfoKHR createInfo;
2782 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2783 createInfo.pNext = NULL;
2784 createInfo.flags = 0;
2785 createInfo.display = demo->display;
2786 createInfo.surface = demo->window;
2787
2788 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2789 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002790#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2791 VkAndroidSurfaceCreateInfoKHR createInfo;
2792 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2793 createInfo.pNext = NULL;
2794 createInfo.flags = 0;
2795 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002796
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002797 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2798#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002799 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002800#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002801 VkXlibSurfaceCreateInfoKHR createInfo;
2802 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2803 createInfo.pNext = NULL;
2804 createInfo.flags = 0;
2805 createInfo.dpy = demo->display;
2806 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002807
Tony Barbour2593b182016-04-19 10:57:58 -06002808 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2809 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002810#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002811 }
2812 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002813#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002814 VkXcbSurfaceCreateInfoKHR createInfo;
2815 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2816 createInfo.pNext = NULL;
2817 createInfo.flags = 0;
2818 createInfo.connection = demo->connection;
2819 createInfo.window = demo->xcb_window;
2820
2821 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002822#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002823 }
Tony Barbour2593b182016-04-19 10:57:58 -06002824 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002825
Tony Barbourcc69f452015-10-08 13:59:42 -06002826 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002827 VkBool32 *supportsPresent =
2828 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002829 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002830 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002831 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002832 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002833
2834 // Search for a graphics and a present queue in the array of queue
2835 // families, try to find one that supports both
2836 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002837 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002838 for (i = 0; i < demo->queue_count; i++) {
Tony Barboura2a67812016-07-18 13:21:06 -06002839 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0
2840 && graphicsQueueNodeIndex == UINT32_MAX) {
2841 graphicsQueueNodeIndex = i;
2842 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002843
Tony Barboura2a67812016-07-18 13:21:06 -06002844 if (supportsPresent[i] == VK_TRUE && presentQueueNodeIndex == UINT32_MAX) {
2845 presentQueueNodeIndex = i;
Ian Elliottaaae5352015-07-06 14:27:58 -06002846 }
2847 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002848
2849 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002850 if (graphicsQueueNodeIndex == UINT32_MAX ||
2851 presentQueueNodeIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06002852 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002853 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002854 }
2855
Tony Barboura2a67812016-07-18 13:21:06 -06002856 demo->graphics_queue_family_index = graphicsQueueNodeIndex;
2857 demo->present_queue_family_index = presentQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002858
Tony Barbourda2bad52016-01-22 14:36:40 -07002859 demo_create_device(demo);
2860
2861 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2862 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2863 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2864 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2865 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2866
Tony Barboura2a67812016-07-18 13:21:06 -06002867 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
2868 &demo->graphics_queue);
2869
2870 if (demo->graphics_queue_family_index == demo->present_queue_family_index) {
2871 demo->present_queue = demo->graphics_queue;
2872 } else {
2873 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
2874 &demo->present_queue);
2875 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002876
Ian Elliottaaae5352015-07-06 14:27:58 -06002877 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002878 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002879 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002880 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002881 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002882 VkSurfaceFormatKHR *surfFormats =
2883 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002884 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002885 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002886 assert(!err);
2887 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2888 // the surface has no preferred format. Otherwise, at least one
2889 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002890 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002891 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002892 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002893 assert(formatCount >= 1);
2894 demo->format = surfFormats[0].format;
2895 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002896 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002897
David Pinedo2bb7c932015-06-18 17:03:14 -06002898 demo->quit = false;
2899 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002900
Tony Barbource7524e2016-07-28 12:01:45 -06002901 // Create semaphores to synchronize acquiring presentable buffers before
2902 // rendering and waiting for drawing to be complete before presenting
2903 VkSemaphoreCreateInfo semaphoreCreateInfo = {
2904 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
2905 .pNext = NULL,
2906 .flags = 0,
2907 };
2908 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
2909 &demo->image_acquired_semaphore);
2910 assert(!err);
2911
2912 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
2913 &demo->draw_complete_semaphore);
2914 assert(!err);
2915
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002916 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002917 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002918}
2919
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002920#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2921static void registry_handle_global(void *data, struct wl_registry *registry,
2922 uint32_t name, const char *interface,
2923 uint32_t version UNUSED) {
2924 struct demo *demo = data;
2925 if (strcmp(interface, "wl_compositor") == 0) {
2926 demo->compositor =
2927 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
2928 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
2929 * tp xdg_shell */
2930 } else if (strcmp(interface, "wl_shell") == 0) {
2931 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
2932 }
2933}
2934
2935static void registry_handle_global_remove(void *data UNUSED,
2936 struct wl_registry *registry UNUSED,
2937 uint32_t name UNUSED) {}
2938
2939static const struct wl_registry_listener registry_listener = {
2940 registry_handle_global, registry_handle_global_remove};
2941#endif
2942
Karl Schultze4dc75c2016-02-02 15:37:51 -07002943static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002944#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002945 const xcb_setup_t *setup;
2946 xcb_screen_iterator_t iter;
2947 int scr;
2948
2949 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06002950 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06002951 printf("Cannot find a compatible Vulkan installable client driver "
2952 "(ICD).\nExiting ...\n");
2953 fflush(stdout);
2954 exit(1);
2955 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002956
2957 setup = xcb_get_setup(demo->connection);
2958 iter = xcb_setup_roots_iterator(setup);
2959 while (scr-- > 0)
2960 xcb_screen_next(&iter);
2961
2962 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002963#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2964 demo->display = wl_display_connect(NULL);
2965
2966 if (demo->display == NULL) {
2967 printf("Cannot find a compatible Vulkan installable client driver "
2968 "(ICD).\nExiting ...\n");
2969 fflush(stdout);
2970 exit(1);
2971 }
2972
2973 demo->registry = wl_display_get_registry(demo->display);
2974 wl_registry_add_listener(demo->registry, &registry_listener, demo);
2975 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002976#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002977}
2978
Karl Schultze4dc75c2016-02-02 15:37:51 -07002979static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002980 vec3 eye = {0.0f, 3.0f, 5.0f};
2981 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002982 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002983
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002984 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002985 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002986
Piers Daniell735ee532015-02-23 16:23:13 -07002987 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002988 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002989 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002990 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002991 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002992 if (strcmp(argv[i], "--break") == 0) {
2993 demo->use_break = true;
2994 continue;
2995 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002996 if (strcmp(argv[i], "--validate") == 0) {
2997 demo->validate = true;
2998 continue;
2999 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003000#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003001 if (strcmp(argv[i], "--xlib") == 0) {
3002 demo->use_xlib = true;
3003 continue;
3004 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003005#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003006 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3007 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3008 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003009 i++;
3010 continue;
3011 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003012 if (strcmp(argv[i], "--suppress_popups") == 0) {
3013 demo->suppress_popups = true;
3014 continue;
3015 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003016
Karl Schultze4dc75c2016-02-02 15:37:51 -07003017 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003018#if defined(VK_USE_PLATFORM_XLIB_KHR)
3019 "[--xlib] "
3020#endif
lenny-lunargfbe22392016-06-06 11:07:53 -06003021 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003022 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003023 fflush(stderr);
3024 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003025 }
3026
Tony Barbour2593b182016-04-19 10:57:58 -06003027 if (!demo->use_xlib)
3028 demo_init_connection(demo);
3029
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003030 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003031
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003032 demo->width = 500;
3033 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003034
3035 demo->spin_angle = 0.01f;
3036 demo->spin_increment = 0.01f;
3037 demo->pause = false;
3038
Karl Schultze4dc75c2016-02-02 15:37:51 -07003039 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3040 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003041 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3042 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003043
3044 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003045}
3046
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003047#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003048// Include header required for parsing the command line options.
3049#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003050
Karl Schultze4dc75c2016-02-02 15:37:51 -07003051int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3052 int nCmdShow) {
3053 MSG msg; // message
3054 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003055 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003056 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003057
Karl Schultze4dc75c2016-02-02 15:37:51 -07003058 // Use the CommandLine functions to get the command line arguments.
3059 // Unfortunately, Microsoft outputs
3060 // this information as wide characters for Unicode, and we simply want the
3061 // Ascii version to be compatible
3062 // with the non-Windows side. So, we have to convert the information to
3063 // Ascii character strings.
3064 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3065 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003066 argc = 0;
3067 }
3068
Karl Schultze4dc75c2016-02-02 15:37:51 -07003069 if (argc > 0) {
3070 argv = (char **)malloc(sizeof(char *) * argc);
3071 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003072 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003073 } else {
3074 for (int iii = 0; iii < argc; iii++) {
3075 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003076 size_t numConverted = 0;
3077
Karl Schultze4dc75c2016-02-02 15:37:51 -07003078 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3079 if (argv[iii] != NULL) {
3080 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3081 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003082 }
3083 }
3084 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003085 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003086 argv = NULL;
3087 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003088
3089 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003090
3091 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003092 if (argc > 0 && argv != NULL) {
3093 for (int iii = 0; iii < argc; iii++) {
3094 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003095 free(argv[iii]);
3096 }
3097 }
3098 free(argv);
3099 }
3100
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003101 demo.connection = hInstance;
3102 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003103 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003104 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003105
3106 demo_prepare(&demo);
3107
Karl Schultze4dc75c2016-02-02 15:37:51 -07003108 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003109
3110 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003111 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003112 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003113 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003114 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003115 done = true; // if found, quit app
3116 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003117 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003118 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003119 DispatchMessage(&msg);
3120 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003121 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003122 }
3123
3124 demo_cleanup(&demo);
3125
Karl Schultze4dc75c2016-02-02 15:37:51 -07003126 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003127}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003128#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3129#include <android/log.h>
3130#include <android_native_app_glue.h>
3131static bool initialized = false;
3132static bool active = false;
3133struct demo demo;
3134
3135static int32_t processInput(struct android_app* app, AInputEvent* event) {
3136 return 0;
3137}
3138
3139static void processCommand(struct android_app* app, int32_t cmd) {
3140 switch(cmd) {
3141 case APP_CMD_INIT_WINDOW: {
3142 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003143 // We're getting a new window. If the app is starting up, we
3144 // need to initialize. If the app has already been
3145 // initialized, that means that we lost our previous window,
3146 // which means that we have a lot of work to do. At a minimum,
3147 // we need to destroy the swapchain and surface associated with
3148 // the old window, and create a new surface and swapchain.
3149 // However, since there are a lot of other objects/state that
3150 // is tied to the swapchain, it's easiest to simply cleanup and
3151 // start over (i.e. use a brute-force approach of re-starting
3152 // the app)
3153 if (demo.prepared) {
3154 demo_cleanup(&demo);
3155 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003156 demo_init(&demo, 0, NULL);
3157 demo.window = (void*)app->window;
3158 demo_init_vk_swapchain(&demo);
3159 demo_prepare(&demo);
3160 initialized = true;
3161 }
3162 break;
3163 }
3164 case APP_CMD_GAINED_FOCUS: {
3165 active = true;
3166 break;
3167 }
3168 case APP_CMD_LOST_FOCUS: {
3169 active = false;
3170 break;
3171 }
3172 }
3173}
3174
3175void android_main(struct android_app *app)
3176{
3177 app_dummy();
3178
Cody Northrop8707a6e2016-04-26 19:59:19 -06003179#ifdef ANDROID
3180 int vulkanSupport = InitVulkan();
3181 if (vulkanSupport == 0)
3182 return;
3183#endif
3184
Ian Elliottf05d5d12016-05-17 12:03:35 -06003185 demo.prepared = false;
3186
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003187 app->onAppCmd = processCommand;
3188 app->onInputEvent = processInput;
3189
3190 while(1) {
3191 int events;
3192 struct android_poll_source* source;
3193 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3194 if (source) {
3195 source->process(app, source);
3196 }
3197
3198 if (app->destroyRequested != 0) {
3199 demo_cleanup(&demo);
3200 return;
3201 }
3202 }
3203 if (initialized && active) {
3204 demo_run(&demo);
3205 }
3206 }
3207
3208}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003209#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003210int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003211 struct demo demo;
3212
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003213 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003214#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003215 if (demo.use_xlib)
3216 demo_create_xlib_window(&demo);
3217 else
3218 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003219#elif defined(VK_USE_PLATFORM_XCB_KHR)
3220 demo_create_xcb_window(&demo);
3221#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3222 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003223#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3224 demo_create_window(&demo);
3225#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003226
Tony Barbourcc69f452015-10-08 13:59:42 -06003227 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003228
3229 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003230
Tony Barbour8295ac42016-08-08 11:04:17 -06003231#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003232 if (demo.use_xlib)
3233 demo_run_xlib(&demo);
3234 else
3235 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003236#elif defined(VK_USE_PLATFORM_XCB_KHR)
3237 demo_run_xcb(&demo);
3238#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3239 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003240#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3241 demo_run(&demo);
3242#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003243
3244 demo_cleanup(&demo);
3245
Karl Schultz0218c002016-03-22 17:06:13 -06003246 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003247}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003248#endif