blob: 95284caff27c03b10be27fe4ca6f2f1d3819f186 [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>
Tony Barbour2593b182016-04-19 10:57:58 -060032#ifdef __linux__
33#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
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600154struct Vertex
155{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600156 float posX, posY, posZ, posW; // Position data
157 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600158};
159
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600160struct VertexPosTex
161{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600162 float posX, posY, posZ, posW; // Position data
163 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600164};
165
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600166#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600167#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600168
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600169static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f,-1.0f, 1.0f,
172 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 -1.0f, 1.0f,-1.0f,
175 -1.0f,-1.0f,-1.0f,
176
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 1.0f,-1.0f,
179 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600182 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600189 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 -1.0f, 1.0f, 1.0f,
193 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800194 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600195 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600196 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600197
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 1.0f, 1.0f, 1.0f,
200 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800201 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600202 1.0f,-1.0f,-1.0f,
203 1.0f, 1.0f,-1.0f,
204
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800205 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600206 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600207 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600209 1.0f,-1.0f, 1.0f,
210 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600211};
212
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600213static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800214 0.0f, 0.0f, // -X side
215 1.0f, 0.0f,
216 1.0f, 1.0f,
217 1.0f, 1.0f,
218 0.0f, 1.0f,
219 0.0f, 0.0f,
220
221 1.0f, 0.0f, // -Z side
222 0.0f, 1.0f,
223 0.0f, 0.0f,
224 1.0f, 0.0f,
225 1.0f, 1.0f,
226 0.0f, 1.0f,
227
228 1.0f, 1.0f, // -Y side
229 1.0f, 0.0f,
230 0.0f, 0.0f,
231 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 0.0f, 0.0f,
233 0.0f, 1.0f,
234
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600236 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600238 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600239 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600240 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600241
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800242 1.0f, 1.0f, // +X side
243 0.0f, 1.0f,
244 0.0f, 0.0f,
245 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600246 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600247 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600248
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800249 0.0f, 1.0f, // +Z side
250 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600251 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600252 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800253 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600254 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600255};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700256// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600257
Karl Schultze4dc75c2016-02-02 15:37:51 -0700258void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600259 int i;
260
261 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700262 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600263 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
264 }
265 printf("\n");
266 fflush(stdout);
267}
268
Karl Schultze4dc75c2016-02-02 15:37:51 -0700269void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600270 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700271 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600272 printf("\n");
273 fflush(stdout);
274}
275
Karl Schultze4dc75c2016-02-02 15:37:51 -0700276VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600277BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
278 uint64_t srcObject, size_t location, int32_t msgCode,
279 const char *pLayerPrefix, const char *pMsg,
280 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700281#ifndef WIN32
282 raise(SIGTRAP);
283#else
284 DebugBreak();
285#endif
286
287 return false;
288}
289
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600290typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600291 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800292 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600293 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600294} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600295
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600296struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500297#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600298#define APP_NAME_STR_LEN 80
299 HINSTANCE connection; // hInstance - Windows Instance
300 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700301 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600302 POINT minsize; // minimum window size
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600303#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600304 Display* display;
305 Window xlib_window;
306 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600307
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600308 xcb_connection_t *connection;
309 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600310 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800311 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900312#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
313 struct wl_display *display;
314 struct wl_registry *registry;
315 struct wl_compositor *compositor;
316 struct wl_surface *window;
317 struct wl_shell *shell;
318 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500319#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
320 ANativeWindow* window;
321#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700322 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600323 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700324 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600325 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600328 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600329 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600330 VkQueue graphics_queue;
331 VkQueue present_queue;
332 uint32_t graphics_queue_family_index;
333 uint32_t present_queue_family_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600334 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600335 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600336 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337
Tony Barbourda2bad52016-01-22 14:36:40 -0700338 uint32_t enabled_extension_count;
339 uint32_t enabled_layer_count;
340 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600341 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700342
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600343 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600344 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600345 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346
Karl Schultze4dc75c2016-02-02 15:37:51 -0700347 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
348 fpGetPhysicalDeviceSurfaceSupportKHR;
349 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
350 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
351 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
352 fpGetPhysicalDeviceSurfaceFormatsKHR;
353 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
354 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600355 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
356 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
357 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
358 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
359 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600360 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600361 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600362 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800364 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365
366 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800370 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500371 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600372 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373 } depth;
374
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600375 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376
377 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800379 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500380 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600381 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600382 } uniform_data;
383
Karl Schultze4dc75c2016-02-02 15:37:51 -0700384 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500385 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600387 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800388 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600389 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600391 mat4x4 projection_matrix;
392 mat4x4 view_matrix;
393 mat4x4 model_matrix;
394
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600395 float spin_angle;
396 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600397 bool pause;
398
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600399 VkShaderModule vert_shader_module;
400 VkShaderModule frag_shader_module;
401
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600402 VkDescriptorPool desc_pool;
403 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800404
Tony Barbourd8e504f2015-10-08 14:26:24 -0600405 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800406
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600408 int32_t curFrame;
409 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600410 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600411 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600412 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700413 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
414 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
415 VkDebugReportCallbackEXT msg_callback;
416 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600417
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600418 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600419 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600420};
421
lenny-lunargfbe22392016-06-06 11:07:53 -0600422VKAPI_ATTR VkBool32 VKAPI_CALL
423dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
424 uint64_t srcObject, size_t location, int32_t msgCode,
425 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
426 char *message = (char *)malloc(strlen(pMsg) + 100);
427
428 assert(message);
429
430 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
431 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
432 pMsg);
433 validation_error = 1;
434 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
435 // We know that we're submitting queues without fences, ignore this
436 // warning
437 if (strstr(pMsg,
438 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
439 return false;
440 }
441 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
442 pMsg);
443 validation_error = 1;
444 } else {
445 validation_error = 1;
446 return false;
447 }
448
449#ifdef _WIN32
450 struct demo *demo = (struct demo*) pUserData;
451 if (!demo->suppress_popups)
452 MessageBox(NULL, message, "Alert", MB_OK);
453#else
454 printf("%s\n", message);
455 fflush(stdout);
456#endif
457 free(message);
458
459 /*
460 * false indicates that layer should not bail-out of an
461 * API call that had validation failures. This may mean that the
462 * app dies inside the driver due to invalid parameter(s).
463 * That's what would happen without validation layers, so we'll
464 * keep that behavior here.
465 */
466 return false;
467}
468
Ian Elliottcf074d32015-10-16 18:02:43 -0600469// Forward declaration:
470static void demo_resize(struct demo *demo);
471
Karl Schultze4dc75c2016-02-02 15:37:51 -0700472static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
473 VkFlags requirements_mask,
474 uint32_t *typeIndex) {
475 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600476 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700477 if ((typeBits & 1) == 1) {
478 // Type is available, does it match user properties?
479 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
480 requirements_mask) == requirements_mask) {
481 *typeIndex = i;
482 return true;
483 }
484 }
485 typeBits >>= 1;
486 }
487 // No memory types matched, return failure
488 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600489}
490
Karl Schultze4dc75c2016-02-02 15:37:51 -0700491static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600492 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 return;
496
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 assert(!err);
499
Karl Schultze4dc75c2016-02-02 15:37:51 -0700500 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800501 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700502 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
503 .pNext = NULL,
504 .waitSemaphoreCount = 0,
505 .pWaitSemaphores = NULL,
506 .pWaitDstStageMask = NULL,
507 .commandBufferCount = 1,
508 .pCommandBuffers = cmd_bufs,
509 .signalSemaphoreCount = 0,
510 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511
Tony Barboura2a67812016-07-18 13:21:06 -0600512 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600513 assert(!err);
514
Tony Barboura2a67812016-07-18 13:21:06 -0600515 err = vkQueueWaitIdle(demo->graphics_queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600516 assert(!err);
517
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600518 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600519 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600520}
521
Karl Schultze4dc75c2016-02-02 15:37:51 -0700522static void demo_set_image_layout(struct demo *demo, VkImage image,
523 VkImageAspectFlags aspectMask,
524 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700525 VkImageLayout new_image_layout,
526 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600527 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800530 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800531 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800533 .commandPool = demo->cmd_pool,
534 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700535 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600536 };
537
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800538 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600539 assert(!err);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700540 VkCommandBufferBeginInfo cmd_buf_info = {
541 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
542 .pNext = NULL,
543 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700544 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700545 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600546 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600547 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600548 }
549
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600550 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600551 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700553 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800554 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600555 .oldLayout = old_image_layout,
556 .newLayout = new_image_layout,
557 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700558 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800560 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600561 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800562 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600563 }
564
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700565 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700566 image_memory_barrier.dstAccessMask =
567 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700568 }
569
570 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700571 image_memory_barrier.dstAccessMask =
572 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700573 }
574
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600575 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600576 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700577 image_memory_barrier.dstAccessMask =
578 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600579 }
580
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600581 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582
Tony Barbour50bbca42015-06-29 16:20:35 -0600583 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
584 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585
Karl Schultze4dc75c2016-02-02 15:37:51 -0700586 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
587 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600588}
589
Karl Schultze4dc75c2016-02-02 15:37:51 -0700590static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700591 const VkCommandBufferBeginInfo cmd_buf_info = {
592 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
593 .pNext = NULL,
594 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700595 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700596 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800597 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700598 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
599 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800600 };
601 const VkRenderPassBeginInfo rp_begin = {
602 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
603 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800604 .renderPass = demo->render_pass,
605 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800606 .renderArea.offset.x = 0,
607 .renderArea.offset.y = 0,
608 .renderArea.extent.width = demo->width,
609 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600610 .clearValueCount = 2,
611 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700612 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800613 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600615 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 assert(!err);
617
Tony Barbour5c5b1632016-04-26 11:13:48 -0600618 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
619 // happens to the previous contents of the image
620 VkImageMemoryBarrier image_memory_barrier = {
621 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
622 .pNext = NULL,
623 .srcAccessMask = 0,
624 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
625 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
626 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
627 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
628 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
629 .image = demo->buffers[demo->current_buffer].image,
630 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
631
632 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
633 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
634 NULL, 1, &image_memory_barrier);
635
Chia-I Wuf051d262015-10-27 19:25:11 +0800636 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800637
Karl Schultze4dc75c2016-02-02 15:37:51 -0700638 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
639 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
640 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
641 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600642 VkViewport viewport;
643 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700644 viewport.height = (float)demo->height;
645 viewport.width = (float)demo->width;
646 viewport.minDepth = (float)0.0f;
647 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700648 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600649
650 VkRect2D scissor;
651 memset(&scissor, 0, sizeof(scissor));
652 scissor.extent.width = demo->width;
653 scissor.extent.height = demo->height;
654 scissor.offset.x = 0;
655 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700656 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600657 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800658 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659
Tony Barbour15a4ef62015-10-21 10:14:48 -0600660 VkImageMemoryBarrier prePresentBarrier = {
661 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
662 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800663 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700664 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600665 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700666 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600667 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800668 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700669 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600670
671 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
672 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700673 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
674 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
675 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600676
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600677 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600678 assert(!err);
679}
680
Karl Schultze4dc75c2016-02-02 15:37:51 -0700681void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600682 mat4x4 MVP, Model, VP;
683 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600684 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600685 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600687 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600689 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600690 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
692 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600693 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600694
Karl Schultze4dc75c2016-02-02 15:37:51 -0700695 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
696 demo->uniform_data.mem_alloc.allocationSize, 0,
697 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600698 assert(!err);
699
Karl Schultze4dc75c2016-02-02 15:37:51 -0700700 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600702 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600703}
704
Karl Schultze4dc75c2016-02-02 15:37:51 -0700705static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600706 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600707 VkSemaphore imageAcquiredSemaphore, drawCompleteSemaphore;
708 VkSemaphoreCreateInfo semaphoreCreateInfo = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600709 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
710 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600711 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800713 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600714
Tony Barboure35e8aa2016-06-20 10:44:08 -0600715 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
Tony Barbour416c6502016-06-20 10:30:23 -0600716 NULL, &imageAcquiredSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 assert(!err);
718
Tony Barboure35e8aa2016-06-20 10:44:08 -0600719 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
720 NULL, &drawCompleteSemaphore);
721 assert(!err);
722
Ian Elliottaaae5352015-07-06 14:27:58 -0600723 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700724 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour416c6502016-06-20 10:30:23 -0600725 imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700726 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600727 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600728 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
729 // demo->swapchain is out of date (e.g. the window was resized) and
730 // must be recreated:
731 demo_resize(demo);
732 demo_draw(demo);
Tony Barbour416c6502016-06-20 10:30:23 -0600733 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600734 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600735 return;
736 } else if (err == VK_SUBOPTIMAL_KHR) {
737 // demo->swapchain is not as optimal as it could be, but the platform's
738 // presentation engine will still present the image correctly.
739 } else {
740 assert(!err);
741 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600742
Tony Barbour15a4ef62015-10-21 10:14:48 -0600743 demo_flush_init_cmd(demo);
744
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600745 // Wait for the present complete semaphore to be signaled to ensure
746 // that the image won't be rendered to until the presentation
747 // engine has fully released ownership to the application, and it is
748 // okay to render to the image.
749
Karl Schultze4dc75c2016-02-02 15:37:51 -0700750 VkPipelineStageFlags pipe_stage_flags =
751 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
752 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
753 .pNext = NULL,
754 .waitSemaphoreCount = 1,
Tony Barbour416c6502016-06-20 10:30:23 -0600755 .pWaitSemaphores = &imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700756 .pWaitDstStageMask = &pipe_stage_flags,
757 .commandBufferCount = 1,
758 .pCommandBuffers =
759 &demo->buffers[demo->current_buffer].cmd,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600760 .signalSemaphoreCount = 1,
761 .pSignalSemaphores = &drawCompleteSemaphore};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600762
Tony Barboura2a67812016-07-18 13:21:06 -0600763 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600764 assert(!err);
765
Ian Elliotta0781972015-08-21 15:09:33 -0600766 VkPresentInfoKHR present = {
767 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600768 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600769 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700770 .pSwapchains = &demo->swapchain,
771 .pImageIndices = &demo->current_buffer,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600772 .waitSemaphoreCount = 1,
773 .pWaitSemaphores = &drawCompleteSemaphore,
Ian Elliottaaae5352015-07-06 14:27:58 -0600774 };
775
Karl Schultze4dc75c2016-02-02 15:37:51 -0700776 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Tony Barboura2a67812016-07-18 13:21:06 -0600777 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600778 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
779 // demo->swapchain is out of date (e.g. the window was resized) and
780 // must be recreated:
781 demo_resize(demo);
782 } else if (err == VK_SUBOPTIMAL_KHR) {
783 // demo->swapchain is not as optimal as it could be, but the platform's
784 // presentation engine will still present the image correctly.
785 } else {
786 assert(!err);
787 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600788
Tony Barboura2a67812016-07-18 13:21:06 -0600789 err = vkQueueWaitIdle(demo->present_queue);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800790 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600791
Tony Barbour416c6502016-06-20 10:30:23 -0600792 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600793 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600794}
795
Karl Schultze4dc75c2016-02-02 15:37:51 -0700796static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600797 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600798 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600799
Ian Elliottb08e0d92015-11-20 11:55:46 -0700800 // Check the surface capabilities and formats
801 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700802 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
803 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600804 assert(!err);
805
Ian Elliott8528eff2015-08-07 15:56:59 -0600806 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700807 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
808 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600809 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600810 VkPresentModeKHR *presentModes =
811 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600812 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700813 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
814 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600815 assert(!err);
816
Ian Elliotta0781972015-08-21 15:09:33 -0600817 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600818 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700819 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600820 // If the surface size is undefined, the size is set to
821 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600822 swapchainExtent.width = demo->width;
823 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700824 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600825 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700826 swapchainExtent = surfCapabilities.currentExtent;
827 demo->width = surfCapabilities.currentExtent.width;
828 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600829 }
830
831 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600832 // tearing mode. If not, try IMMEDIATE which will usually be available,
833 // and is fastest (though it tears). If not, fall back to FIFO which is
834 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600835 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600836 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600837 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
838 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600839 break;
840 }
Ian Elliotta0781972015-08-21 15:09:33 -0600841 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
842 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
843 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600844 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600845 }
846
847 // Determine the number of VkImage's to use in the swap chain (we desire to
848 // own only 1 image at a time, besides the images being displayed and
849 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700850 uint32_t desiredNumberOfSwapchainImages =
851 surfCapabilities.minImageCount + 1;
Tony Barboura2a67812016-07-18 13:21:06 -0600852 // If maxImageCount is 0, we can ask for as many images as we want, otherwise
853 // we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700854 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700855 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600856 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700857 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600858 }
859
Tony Barbour9fd6d352015-09-16 15:01:32 -0600860 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700861 if (surfCapabilities.supportedTransforms &
862 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700863 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600864 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700865 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600866 }
867
Tony Barboura2a67812016-07-18 13:21:06 -0600868 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600869 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800870 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700871 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600872 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800873 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600874 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700875 .imageExtent =
876 {
877 .width = swapchainExtent.width, .height = swapchainExtent.height,
878 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700879 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700881 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700882 .imageArrayLayers = 1,
883 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
884 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600885 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600886 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600887 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600888 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600889 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600890 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600891 uint32_t queueFamilyIndices[2] = {(uint32_t) demo->graphics_queue_family_index, (uint32_t) demo->present_queue_family_index};
892 if (demo->graphics_queue_family_index != demo->present_queue_family_index)
893 {
894 // If the graphics and present queues are from different queue families, we either have to
895 // explicitly transfer ownership of images between the queues, or we have to create the swapchain
896 // with imageSharingMode as VK_SHARING_MODE_CONCURRENT
897 swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
898 swapchain_ci.queueFamilyIndexCount = 2;
899 swapchain_ci.pQueueFamilyIndices = queueFamilyIndices;
900 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600901
Tony Barboura2a67812016-07-18 13:21:06 -0600902 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700903 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800904 assert(!err);
905
Ian Elliottcf074d32015-10-16 18:02:43 -0600906 // If we just re-created an existing swapchain, we should destroy the old
907 // swapchain at this point.
908 // Note: destroying the swapchain also cleans up all its associated
909 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800910 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700911 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600912 }
913
914 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600915 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600916 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800917
Karl Schultze4dc75c2016-02-02 15:37:51 -0700918 VkImage *swapchainImages =
919 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600920 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600921 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600922 &demo->swapchainImageCount,
923 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600924 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600925
Karl Schultze4dc75c2016-02-02 15:37:51 -0700926 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
927 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600928 assert(demo->buffers);
929
Ian Elliotta0781972015-08-21 15:09:33 -0600930 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600931 VkImageViewCreateInfo color_image_view = {
932 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600933 .pNext = NULL,
934 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700935 .components =
936 {
937 .r = VK_COMPONENT_SWIZZLE_R,
938 .g = VK_COMPONENT_SWIZZLE_G,
939 .b = VK_COMPONENT_SWIZZLE_B,
940 .a = VK_COMPONENT_SWIZZLE_A,
941 },
942 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
943 .baseMipLevel = 0,
944 .levelCount = 1,
945 .baseArrayLayer = 0,
946 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600947 .viewType = VK_IMAGE_VIEW_TYPE_2D,
948 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600949 };
950
Ian Elliotta0781972015-08-21 15:09:33 -0600951 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600952
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600953 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954
Karl Schultze4dc75c2016-02-02 15:37:51 -0700955 err = vkCreateImageView(demo->device, &color_image_view, NULL,
956 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600957 assert(!err);
958 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700959
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500960
Mark Young04fd3d82016-01-25 14:43:50 -0700961 if (NULL != presentModes) {
962 free(presentModes);
963 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600964}
965
Karl Schultze4dc75c2016-02-02 15:37:51 -0700966static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600967 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600968 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600969 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600970 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600971 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600974 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600975 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800976 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600977 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600978 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600979 .flags = 0,
980 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200981
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600982 VkImageViewCreateInfo view = {
983 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800985 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600986 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700987 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
988 .baseMipLevel = 0,
989 .levelCount = 1,
990 .baseArrayLayer = 0,
991 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600992 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600993 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600994 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600995
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500996 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600997 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600998 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600999
1000 demo->depth.format = depth_format;
1001
1002 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001003 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004 assert(!err);
1005
Karl Schultze4dc75c2016-02-02 15:37:51 -07001006 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001007 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001008
Chia-I Wuf48700b2015-11-10 16:21:09 +08001009 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001010 demo->depth.mem_alloc.pNext = NULL;
1011 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1012 demo->depth.mem_alloc.memoryTypeIndex = 0;
1013
Karl Schultze4dc75c2016-02-02 15:37:51 -07001014 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1015 0, /* No requirements */
1016 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001017 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001019 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001020 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1021 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001022 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001023
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001024 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001025 err =
1026 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001027 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028
Karl Schultze4dc75c2016-02-02 15:37:51 -07001029 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1030 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001031 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1032 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001033
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034 /* create image view */
1035 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001036 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001037 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038}
1039
Tony Barbour1a86d032015-09-21 15:17:33 -06001040/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001042 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001043#ifdef __ANDROID__
1044#include <lunarg.ppm.h>
1045 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001046 cPtr = (char*)lunarg_ppm;
1047 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001048 return false;
1049 }
1050 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001051 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001052 if (rgba_data == NULL) {
1053 return true;
1054 }
1055 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001056 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001057 return false;
1058 }
1059 while(strncmp(cPtr++, "\n", 1));
1060
1061 for (int y = 0; y < *height; y++) {
1062 uint8_t *rowPtr = rgba_data;
1063 for (int x = 0; x < *width; x++) {
1064 memcpy(rowPtr, cPtr, 3);
1065 rowPtr[3] = 255; /* Alpha of 1 */
1066 rowPtr += 4;
1067 cPtr += 3;
1068 }
1069 rgba_data += layout->rowPitch;
1070 }
1071
1072 return true;
1073#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001074 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001075 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001076
Tony Barbour1a86d032015-09-21 15:17:33 -06001077 if (!fPtr)
1078 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001079
Tony Barbour1a86d032015-09-21 15:17:33 -06001080 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001081 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1082 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001083 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001084 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001085
Tony Barbour1a86d032015-09-21 15:17:33 -06001086 do {
1087 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001088 if (cPtr == NULL) {
1089 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001090 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001091 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001092 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001093
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001094 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001095 if (rgba_data == NULL) {
1096 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001097 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001098 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001099 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001100 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001101 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1102 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001103 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001104 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001105
Karl Schultze4dc75c2016-02-02 15:37:51 -07001106 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001107 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001108 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001109 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001110 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001111 rowPtr[3] = 255; /* Alpha of 1 */
1112 rowPtr += 4;
1113 }
1114 rgba_data += layout->rowPitch;
1115 }
1116 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001117 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001118#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001119}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001120
Karl Schultze4dc75c2016-02-02 15:37:51 -07001121static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001122 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001123 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001124 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001125 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001126 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001127 int32_t tex_width;
1128 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001129 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001130 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001131
Karl Schultze4dc75c2016-02-02 15:37:51 -07001132 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001133 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001134 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001135
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001136 tex_obj->tex_width = tex_width;
1137 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001139 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001140 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001141 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001142 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001144 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001145 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001146 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001147 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001149 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001150 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001151 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001152 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001153
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001154 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155
Karl Schultze4dc75c2016-02-02 15:37:51 -07001156 err =
1157 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001158 assert(!err);
1159
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001160 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001161
Chia-I Wuf48700b2015-11-10 16:21:09 +08001162 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001163 tex_obj->mem_alloc.pNext = NULL;
1164 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1165 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166
Karl Schultze4dc75c2016-02-02 15:37:51 -07001167 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1168 required_props,
1169 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001170 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001171
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001172 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001173 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001175 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001176
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001177 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001178 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001179 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001180
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001181 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001182 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001183 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001185 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001186 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001187 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001188 void *data;
1189
Karl Schultze4dc75c2016-02-02 15:37:51 -07001190 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1191 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001192
Karl Schultze4dc75c2016-02-02 15:37:51 -07001193 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1194 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195 assert(!err);
1196
1197 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1198 fprintf(stderr, "Error loading texture: %s\n", filename);
1199 }
1200
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001201 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001202 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001203
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001204 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001206 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1207 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001208 /* setting the image layout does not reference the actual memory so no need
1209 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001210}
1211
Karl Schultze4dc75c2016-02-02 15:37:51 -07001212static void demo_destroy_texture_image(struct demo *demo,
1213 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001214 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001215 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1216 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217}
1218
Karl Schultze4dc75c2016-02-02 15:37:51 -07001219static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001220 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001221 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001222 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001223
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001224 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001225
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001227 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001228
Karl Schultze4dc75c2016-02-02 15:37:51 -07001229 if ((props.linearTilingFeatures &
1230 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1231 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001232 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001233 demo_prepare_texture_image(
1234 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1235 VK_IMAGE_USAGE_SAMPLED_BIT,
1236 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1237 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001238 } else if (props.optimalTilingFeatures &
1239 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001241 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001242
1243 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001244 demo_prepare_texture_image(
1245 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1246 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1247 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1248 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001249
Karl Schultze4dc75c2016-02-02 15:37:51 -07001250 demo_prepare_texture_image(
1251 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1252 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1253 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001254
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001255 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001256 VK_IMAGE_ASPECT_COLOR_BIT,
1257 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001258 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1259 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001260
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001261 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001262 VK_IMAGE_ASPECT_COLOR_BIT,
1263 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001264 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1265 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001267 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001268 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1269 .srcOffset = {0, 0, 0},
1270 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1271 .dstOffset = {0, 0, 0},
1272 .extent = {staging_texture.tex_width,
1273 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001274 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001275 vkCmdCopyImage(
1276 demo->cmd, staging_texture.image,
1277 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1278 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001279
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001280 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001281 VK_IMAGE_ASPECT_COLOR_BIT,
1282 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001283 demo->textures[i].imageLayout,
1284 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001285
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001286 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001287
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001288 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001289 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001290 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1291 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001292 }
1293
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001294 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001295 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001296 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001297 .magFilter = VK_FILTER_NEAREST,
1298 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001299 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001300 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1301 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1302 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001303 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001304 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001305 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001306 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307 .minLod = 0.0f,
1308 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001309 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001310 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001313 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001314 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001316 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001317 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001318 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001319 .components =
1320 {
1321 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1322 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1323 },
1324 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001325 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001326 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001327
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001329 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001330 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001331 assert(!err);
1332
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001333 /* create image view */
1334 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001335 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001337 assert(!err);
1338 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001339}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001340
Karl Schultze4dc75c2016-02-02 15:37:51 -07001341void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001342 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001343 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001344 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001345 int i;
1346 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001347 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001348 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001349 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001350
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001351 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001352 mat4x4_mul(MVP, VP, demo->model_matrix);
1353 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001354 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001355
Karl Schultze4dc75c2016-02-02 15:37:51 -07001356 for (i = 0; i < 12 * 3; i++) {
1357 data.position[i][0] = g_vertex_buffer_data[i * 3];
1358 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1359 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001360 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001361 data.attr[i][0] = g_uv_buffer_data[2 * i];
1362 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001363 data.attr[i][2] = 0;
1364 data.attr[i][3] = 0;
1365 }
1366
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001367 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001368 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001369 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001370 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001371 err =
1372 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001373 assert(!err);
1374
Karl Schultze4dc75c2016-02-02 15:37:51 -07001375 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1376 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001377
Chia-I Wuf48700b2015-11-10 16:21:09 +08001378 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001379 demo->uniform_data.mem_alloc.pNext = NULL;
1380 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1381 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1382
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001384 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1385 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001386 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001387 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001388
Karl Schultze4dc75c2016-02-02 15:37:51 -07001389 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1390 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001391 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001392
Karl Schultze4dc75c2016-02-02 15:37:51 -07001393 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1394 demo->uniform_data.mem_alloc.allocationSize, 0,
1395 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001396 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001397
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001398 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001399
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001400 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001401
Karl Schultze4dc75c2016-02-02 15:37:51 -07001402 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1403 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001404 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001405
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001406 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1407 demo->uniform_data.buffer_info.offset = 0;
1408 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001409}
1410
Karl Schultze4dc75c2016-02-02 15:37:51 -07001411static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001412 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001413 [0] =
1414 {
1415 .binding = 0,
1416 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1417 .descriptorCount = 1,
1418 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1419 .pImmutableSamplers = NULL,
1420 },
1421 [1] =
1422 {
1423 .binding = 1,
1424 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1425 .descriptorCount = DEMO_TEXTURE_COUNT,
1426 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1427 .pImmutableSamplers = NULL,
1428 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001429 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001430 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001431 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001432 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001433 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001434 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001435 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001436 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437
Karl Schultze4dc75c2016-02-02 15:37:51 -07001438 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1439 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001440 assert(!err);
1441
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001442 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001443 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1444 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001445 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001446 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001447 };
1448
Karl Schultze4dc75c2016-02-02 15:37:51 -07001449 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001450 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001451 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452}
1453
Karl Schultze4dc75c2016-02-02 15:37:51 -07001454static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001455 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001456 [0] =
1457 {
1458 .format = demo->format,
1459 .samples = VK_SAMPLE_COUNT_1_BIT,
1460 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1461 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1462 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1463 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1464 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1465 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1466 },
1467 [1] =
1468 {
1469 .format = demo->depth.format,
1470 .samples = VK_SAMPLE_COUNT_1_BIT,
1471 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1472 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1473 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1474 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1475 .initialLayout =
1476 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1477 .finalLayout =
1478 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1479 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001480 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001481 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001482 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001483 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001484 const VkAttachmentReference depth_reference = {
1485 .attachment = 1,
1486 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1487 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001488 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001489 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1490 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001491 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001492 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001493 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001494 .pColorAttachments = &color_reference,
1495 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001496 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001497 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001498 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001499 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001500 const VkRenderPassCreateInfo rp_info = {
1501 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1502 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001503 .attachmentCount = 2,
1504 .pAttachments = attachments,
1505 .subpassCount = 1,
1506 .pSubpasses = &subpass,
1507 .dependencyCount = 0,
1508 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001509 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001510 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001511
Chia-I Wu63636062015-10-26 21:10:41 +08001512 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001513 assert(!err);
1514}
1515
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001516//TODO: Merge shader reading
1517#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001518static VkShaderModule
1519demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001520 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001521 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001522 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001523
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001524 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1525 moduleCreateInfo.pNext = NULL;
1526
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001527 moduleCreateInfo.codeSize = size;
1528 moduleCreateInfo.pCode = code;
1529 moduleCreateInfo.flags = 0;
1530 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1531 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001532
1533 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001534}
1535
Karl Schultze4dc75c2016-02-02 15:37:51 -07001536char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001537 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001538 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001539 void *shader_code;
1540
1541 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001542 if (!fp)
1543 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001544
1545 fseek(fp, 0L, SEEK_END);
1546 size = ftell(fp);
1547
1548 fseek(fp, 0L, SEEK_SET);
1549
1550 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001551 retval = fread(shader_code, size, 1, fp);
1552 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001553
1554 *psize = size;
1555
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001556 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001557 return shader_code;
1558}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001559#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001560
Karl Schultze4dc75c2016-02-02 15:37:51 -07001561static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001562#ifdef __ANDROID__
1563 VkShaderModuleCreateInfo sh_info = {};
1564 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1565
1566#include "cube.vert.h"
1567 sh_info.codeSize = sizeof(cube_vert);
1568 sh_info.pCode = cube_vert;
1569 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1570 assert(!err);
1571#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001572 void *vertShaderCode;
1573 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001574
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001575 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1576
Karl Schultze4dc75c2016-02-02 15:37:51 -07001577 demo->vert_shader_module =
1578 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001579
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001580 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001581#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001582
1583 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001584}
1585
Karl Schultze4dc75c2016-02-02 15:37:51 -07001586static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001587#ifdef __ANDROID__
1588 VkShaderModuleCreateInfo sh_info = {};
1589 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1590
1591#include "cube.frag.h"
1592 sh_info.codeSize = sizeof(cube_frag);
1593 sh_info.pCode = cube_frag;
1594 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1595 assert(!err);
1596#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001597 void *fragShaderCode;
1598 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001599
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001600 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001601
Karl Schultze4dc75c2016-02-02 15:37:51 -07001602 demo->frag_shader_module =
1603 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001604
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001605 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001606#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001607
1608 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001609}
1610
Karl Schultze4dc75c2016-02-02 15:37:51 -07001611static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001612 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001613 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001614 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001615 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001616 VkPipelineRasterizationStateCreateInfo rs;
1617 VkPipelineColorBlendStateCreateInfo cb;
1618 VkPipelineDepthStencilStateCreateInfo ds;
1619 VkPipelineViewportStateCreateInfo vp;
1620 VkPipelineMultisampleStateCreateInfo ms;
1621 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1622 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001623 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001624
Piers Daniellba839d12015-09-29 13:01:09 -06001625 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1626 memset(&dynamicState, 0, sizeof dynamicState);
1627 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1628 dynamicState.pDynamicStates = dynamicStateEnables;
1629
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001631 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001632 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001633
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001634 memset(&vi, 0, sizeof(vi));
1635 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1636
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001637 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001638 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001639 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001640
1641 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001642 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1643 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001644 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001645 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001646 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001647 rs.rasterizerDiscardEnable = VK_FALSE;
1648 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001649 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001650
1651 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001652 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1653 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001654 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001655 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001656 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001657 cb.attachmentCount = 1;
1658 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001659
Tony Barbour29645d02015-01-16 14:27:35 -07001660 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001661 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001662 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001663 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1664 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001665 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001666 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1667 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001668
1669 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001670 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001671 ds.depthTestEnable = VK_TRUE;
1672 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001673 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001674 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001675 ds.back.failOp = VK_STENCIL_OP_KEEP;
1676 ds.back.passOp = VK_STENCIL_OP_KEEP;
1677 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001678 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001679 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001680
Tony Barbour29645d02015-01-16 14:27:35 -07001681 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001682 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001683 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001684 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001685
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001686 // Two stages: vs and fs
1687 pipeline.stageCount = 2;
1688 VkPipelineShaderStageCreateInfo shaderStages[2];
1689 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1690
Karl Schultze4dc75c2016-02-02 15:37:51 -07001691 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1692 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001693 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001694 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001695
Karl Schultze4dc75c2016-02-02 15:37:51 -07001696 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1697 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001698 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001699 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001700
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001701 memset(&pipelineCache, 0, sizeof(pipelineCache));
1702 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1705 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001706 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001707
Karl Schultze4dc75c2016-02-02 15:37:51 -07001708 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001709 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001710 pipeline.pRasterizationState = &rs;
1711 pipeline.pColorBlendState = &cb;
1712 pipeline.pMultisampleState = &ms;
1713 pipeline.pViewportState = &vp;
1714 pipeline.pDepthStencilState = &ds;
1715 pipeline.pStages = shaderStages;
1716 pipeline.renderPass = demo->render_pass;
1717 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001718
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001719 pipeline.renderPass = demo->render_pass;
1720
Karl Schultze4dc75c2016-02-02 15:37:51 -07001721 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1722 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723 assert(!err);
1724
Chia-I Wu63636062015-10-26 21:10:41 +08001725 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1726 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727}
1728
Karl Schultze4dc75c2016-02-02 15:37:51 -07001729static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001730 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001731 [0] =
1732 {
1733 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1734 .descriptorCount = 1,
1735 },
1736 [1] =
1737 {
1738 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1739 .descriptorCount = DEMO_TEXTURE_COUNT,
1740 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001741 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001742 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001743 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001744 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001745 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001746 .poolSizeCount = 2,
1747 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001748 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001749 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001750
Karl Schultze4dc75c2016-02-02 15:37:51 -07001751 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1752 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001753 assert(!err);
1754}
1755
Karl Schultze4dc75c2016-02-02 15:37:51 -07001756static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001757 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001758 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001759 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001760 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001761
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001762 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001763 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001764 .pNext = NULL,
1765 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001766 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001767 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001768 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001769 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001770
Chia-I Wuae721ba2015-05-25 16:27:55 +08001771 memset(&tex_descs, 0, sizeof(tex_descs));
1772 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001773 tex_descs[i].sampler = demo->textures[i].sampler;
1774 tex_descs[i].imageView = demo->textures[i].view;
1775 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001776 }
1777
1778 memset(&writes, 0, sizeof(writes));
1779
1780 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001781 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001782 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001783 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001784 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001785
1786 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001787 writes[1].dstSet = demo->desc_set;
1788 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001789 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001790 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001791 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001792
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001793 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001794}
1795
Karl Schultze4dc75c2016-02-02 15:37:51 -07001796static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001797 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001798 attachments[1] = demo->depth.view;
1799
Chia-I Wuf973e322015-07-08 13:34:24 +08001800 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001801 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1802 .pNext = NULL,
1803 .renderPass = demo->render_pass,
1804 .attachmentCount = 2,
1805 .pAttachments = attachments,
1806 .width = demo->width,
1807 .height = demo->height,
1808 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001809 };
1810 VkResult U_ASSERT_ONLY err;
1811 uint32_t i;
1812
Karl Schultze4dc75c2016-02-02 15:37:51 -07001813 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1814 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001815 assert(demo->framebuffers);
1816
1817 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001818 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001819 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1820 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001821 assert(!err);
1822 }
1823}
1824
Karl Schultze4dc75c2016-02-02 15:37:51 -07001825static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001826 VkResult U_ASSERT_ONLY err;
1827
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001828 const VkCommandPoolCreateInfo cmd_pool_info = {
1829 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001830 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001831 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001832 .flags = 0,
1833 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001834 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1835 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001836 assert(!err);
1837
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001838 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001839 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001840 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001841 .commandPool = demo->cmd_pool,
1842 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001843 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001844 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001845
1846 demo_prepare_buffers(demo);
1847 demo_prepare_depth(demo);
1848 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001849 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001850
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001851 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001852 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001854
Ian Elliotta0781972015-08-21 15:09:33 -06001855 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001856 err =
1857 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001858 assert(!err);
1859 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001860
Chia-I Wu63ea9262015-03-26 13:14:16 +08001861 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001862 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001863
Chia-I Wuf973e322015-07-08 13:34:24 +08001864 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001865
Ian Elliotta0781972015-08-21 15:09:33 -06001866 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001867 demo->current_buffer = i;
1868 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1869 }
1870
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001871 /*
1872 * Prepare functions above may generate pipeline commands
1873 * that need to be flushed before beginning the render loop.
1874 */
1875 demo_flush_init_cmd(demo);
1876
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001877 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001878 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001879}
1880
Karl Schultze4dc75c2016-02-02 15:37:51 -07001881static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001882 uint32_t i;
1883
1884 demo->prepared = false;
1885
Tony Barbourd8e504f2015-10-08 14:26:24 -06001886 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001887 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001888 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001889 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001890 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001891
Chia-I Wu63636062015-10-26 21:10:41 +08001892 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1893 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1894 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1895 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1896 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001897
1898 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001899 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1900 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1901 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1902 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001903 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001904 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001905
Chia-I Wu63636062015-10-26 21:10:41 +08001906 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1907 vkDestroyImage(demo->device, demo->depth.image, NULL);
1908 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001909
Chia-I Wu63636062015-10-26 21:10:41 +08001910 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1911 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001912
Ian Elliotta0781972015-08-21 15:09:33 -06001913 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001914 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001915 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1916 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001917 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001918 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001919
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001920 free(demo->queue_props);
1921
Chia-I Wu63636062015-10-26 21:10:41 +08001922 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1923 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001924 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001925 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001926 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001927 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001928 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001929
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001930#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001931 if (demo->use_xlib) {
1932 XDestroyWindow(demo->display, demo->xlib_window);
1933 XCloseDisplay(demo->display);
1934 } else {
1935 xcb_destroy_window(demo->connection, demo->xcb_window);
1936 xcb_disconnect(demo->connection);
1937 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001938 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001939#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001940 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001941 xcb_disconnect(demo->connection);
1942 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001943#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
1944 wl_shell_surface_destroy(demo->shell_surface);
1945 wl_surface_destroy(demo->window);
1946 wl_shell_destroy(demo->shell);
1947 wl_compositor_destroy(demo->compositor);
1948 wl_registry_destroy(demo->registry);
1949 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001950#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001951}
1952
Karl Schultze4dc75c2016-02-02 15:37:51 -07001953static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001954 uint32_t i;
1955
Mike Stroyanbb60b382015-10-28 09:46:57 -06001956 // Don't react to resize until after first initialization.
1957 if (!demo->prepared) {
1958 return;
1959 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001960 // In order to properly resize the window, we must re-create the swapchain
1961 // AND redo the command buffers, etc.
1962 //
1963 // First, perform part of the demo_cleanup() function:
1964 demo->prepared = false;
1965
1966 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001967 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001968 }
1969 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001970 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001971
Chia-I Wu63636062015-10-26 21:10:41 +08001972 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1973 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1974 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1975 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1976 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001977
1978 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001979 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1980 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1981 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1982 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001983 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001984
Chia-I Wu63636062015-10-26 21:10:41 +08001985 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1986 vkDestroyImage(demo->device, demo->depth.image, NULL);
1987 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001988
Chia-I Wu63636062015-10-26 21:10:41 +08001989 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1990 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001991
1992 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001993 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001994 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1995 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001996 }
Chia-I Wu63636062015-10-26 21:10:41 +08001997 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001998 free(demo->buffers);
1999
Ian Elliottcf074d32015-10-16 18:02:43 -06002000 // Second, re-perform the demo_prepare() function, which will re-create the
2001 // swapchain:
2002 demo_prepare(demo);
2003}
2004
David Pinedo2bb7c932015-06-18 17:03:14 -06002005// On MS-Windows, make this a global, so it's available to WndProc()
2006struct demo demo;
2007
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002008#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002009static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002010 if (!demo->prepared)
2011 return;
Ian Elliott639ca472015-04-16 15:23:05 -06002012 // Wait for work to finish before updating MVP.
2013 vkDeviceWaitIdle(demo->device);
2014 demo_update_data_buffer(demo);
2015
2016 demo_draw(demo);
2017
2018 // Wait for work to finish before updating MVP.
2019 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06002020
David Pinedo2bb7c932015-06-18 17:03:14 -06002021 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002022 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002023 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002024 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002025}
Ian Elliott639ca472015-04-16 15:23:05 -06002026
2027// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002028LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2029 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002030 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002031 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002032 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002033 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002034 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002035 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002036 case WM_GETMINMAXINFO: // set window's minimum size
2037 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2038 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002039 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002040 // Resize the application to the new window size, except when
2041 // it was minimized. Vulkan doesn't support images or swapchains
2042 // with width=0 and height=0.
2043 if (wParam != SIZE_MINIMIZED) {
2044 demo.width = lParam & 0xffff;
2045 demo.height = lParam & 0xffff0000 >> 16;
2046 demo_resize(&demo);
2047 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002048 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002049 default:
2050 break;
2051 }
2052 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2053}
2054
Karl Schultze4dc75c2016-02-02 15:37:51 -07002055static void demo_create_window(struct demo *demo) {
2056 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002057
2058 // Initialize the window class structure:
2059 win_class.cbSize = sizeof(WNDCLASSEX);
2060 win_class.style = CS_HREDRAW | CS_VREDRAW;
2061 win_class.lpfnWndProc = WndProc;
2062 win_class.cbClsExtra = 0;
2063 win_class.cbWndExtra = 0;
2064 win_class.hInstance = demo->connection; // hInstance
2065 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2066 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2067 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2068 win_class.lpszMenuName = NULL;
2069 win_class.lpszClassName = demo->name;
2070 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2071 // Register window class:
2072 if (!RegisterClassEx(&win_class)) {
2073 // It didn't work, so try to give a useful error:
2074 printf("Unexpected error trying to start the application!\n");
2075 fflush(stdout);
2076 exit(1);
2077 }
2078 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002079 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002080 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002081 demo->window = CreateWindowEx(0,
2082 demo->name, // class name
2083 demo->name, // app name
2084 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002085 WS_VISIBLE | WS_SYSMENU,
2086 100, 100, // x/y coords
2087 wr.right - wr.left, // width
2088 wr.bottom - wr.top, // height
2089 NULL, // handle to parent
2090 NULL, // handle to menu
2091 demo->connection, // hInstance
2092 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002093 if (!demo->window) {
2094 // It didn't work, so try to give a useful error:
2095 printf("Cannot create a window in which to draw!\n");
2096 fflush(stdout);
2097 exit(1);
2098 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002099 // Window client area size must be at least 1 pixel high, to prevent crash.
2100 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2101 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002102}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002103#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002104static void demo_create_xlib_window(struct demo *demo) {
2105
2106 demo->display = XOpenDisplay(NULL);
2107 long visualMask = VisualScreenMask;
2108 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002109 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002110 vInfoTemplate.screen = DefaultScreen(demo->display);
2111 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2112 &vInfoTemplate, &numberOfVisuals);
2113
2114 Colormap colormap = XCreateColormap(
2115 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2116 visualInfo->visual, AllocNone);
2117
Rene Lindsay11612722016-06-13 17:20:39 -06002118 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002119 windowAttributes.colormap = colormap;
2120 windowAttributes.background_pixel = 0xFFFFFFFF;
2121 windowAttributes.border_pixel = 0;
2122 windowAttributes.event_mask =
2123 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2124
2125 demo->xlib_window = XCreateWindow(
2126 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2127 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2128 visualInfo->visual,
2129 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2130
2131 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2132 XMapWindow(demo->display, demo->xlib_window);
2133 XFlush(demo->display);
2134 demo->xlib_wm_delete_window =
2135 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2136}
2137static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2138 switch(event->type) {
2139 case ClientMessage:
2140 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2141 demo->quit = true;
2142 break;
2143 case KeyPress:
2144 switch (event->xkey.keycode) {
2145 case 0x9: // Escape
2146 demo->quit = true;
2147 break;
2148 case 0x71: // left arrow key
2149 demo->spin_angle += demo->spin_increment;
2150 break;
2151 case 0x72: // right arrow key
2152 demo->spin_angle -= demo->spin_increment;
2153 break;
2154 case 0x41:
2155 demo->pause = !demo->pause;
2156 break;
2157 }
2158 break;
2159 case ConfigureNotify:
2160 if ((demo->width != event->xconfigure.width) ||
2161 (demo->height != event->xconfigure.height)) {
2162 demo->width = event->xconfigure.width;
2163 demo->height = event->xconfigure.height;
2164 demo_resize(demo);
2165 }
2166 break;
2167 default:
2168 break;
2169 }
2170
2171}
2172
2173static void demo_run_xlib(struct demo *demo) {
2174
2175 while (!demo->quit) {
2176 XEvent event;
2177
2178 if (demo->pause) {
2179 XNextEvent(demo->display, &event);
2180 demo_handle_xlib_event(demo, &event);
2181 } else {
2182 while (XPending(demo->display) > 0) {
2183 XNextEvent(demo->display, &event);
2184 demo_handle_xlib_event(demo, &event);
2185 }
2186 }
2187
2188 // Wait for work to finish before updating MVP.
2189 vkDeviceWaitIdle(demo->device);
2190 demo_update_data_buffer(demo);
2191
2192 demo_draw(demo);
2193
2194 // Wait for work to finish before updating MVP.
2195 vkDeviceWaitIdle(demo->device);
2196 demo->curFrame++;
2197 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2198 demo->quit = true;
2199 }
2200}
2201
2202static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002203 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002204 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002205 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002206 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002207 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002208 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002209 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002210 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2211 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002212 demo->quit = true;
2213 }
2214 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002215 case XCB_KEY_RELEASE: {
2216 const xcb_key_release_event_t *key =
2217 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002218
Karl Schultze4dc75c2016-02-02 15:37:51 -07002219 switch (key->detail) {
2220 case 0x9: // Escape
2221 demo->quit = true;
2222 break;
2223 case 0x71: // left arrow key
2224 demo->spin_angle += demo->spin_increment;
2225 break;
2226 case 0x72: // right arrow key
2227 demo->spin_angle -= demo->spin_increment;
2228 break;
2229 case 0x41:
2230 demo->pause = !demo->pause;
2231 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002232 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002233 } break;
2234 case XCB_CONFIGURE_NOTIFY: {
2235 const xcb_configure_notify_event_t *cfg =
2236 (const xcb_configure_notify_event_t *)event;
2237 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002238 demo->width = cfg->width;
2239 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002240 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002241 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002242 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002243 default:
2244 break;
2245 }
2246}
2247
Tony Barbour2593b182016-04-19 10:57:58 -06002248static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002249 xcb_flush(demo->connection);
2250
2251 while (!demo->quit) {
2252 xcb_generic_event_t *event;
2253
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002254 if (demo->pause) {
2255 event = xcb_wait_for_event(demo->connection);
2256 } else {
2257 event = xcb_poll_for_event(demo->connection);
2258 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002259 if (event) {
Tony Barbour2593b182016-04-19 10:57:58 -06002260 demo_handle_xcb_event(demo, event);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002261 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002262 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002263
2264 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002265 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002266 demo_update_data_buffer(demo);
2267
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002268 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002269
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002270 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002271 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002272 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002273 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002274 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002275 }
2276}
2277
Tony Barbour2593b182016-04-19 10:57:58 -06002278static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002279 uint32_t value_mask, value_list[32];
2280
Tony Barbour2593b182016-04-19 10:57:58 -06002281 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002282
2283 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2284 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002285 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002286 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002287
Tony Barbour2593b182016-04-19 10:57:58 -06002288 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002289 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2290 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2291 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002292
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002293 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002294 xcb_intern_atom_cookie_t cookie =
2295 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2296 xcb_intern_atom_reply_t *reply =
2297 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002298
Karl Schultze4dc75c2016-02-02 15:37:51 -07002299 xcb_intern_atom_cookie_t cookie2 =
2300 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2301 demo->atom_wm_delete_window =
2302 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002303
Tony Barbour2593b182016-04-19 10:57:58 -06002304 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002305 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002306 &(*demo->atom_wm_delete_window).atom);
2307 free(reply);
2308
Tony Barbour2593b182016-04-19 10:57:58 -06002309 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002310
Karl Schultze4dc75c2016-02-02 15:37:51 -07002311 // Force the x/y coordinates to 100,100 results are identical in consecutive
2312 // runs
2313 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002314 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002315 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002316}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002317#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2318static void demo_run(struct demo *demo) {
2319 while (!demo->quit) {
2320 // Wait for work to finish before updating MVP.
2321 vkDeviceWaitIdle(demo->device);
2322 demo_update_data_buffer(demo);
2323
2324 demo_draw(demo);
2325
2326 // Wait for work to finish before updating MVP.
2327 vkDeviceWaitIdle(demo->device);
2328 demo->curFrame++;
2329 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2330 demo->quit = true;
2331 }
2332}
2333
2334static void handle_ping(void *data UNUSED,
2335 struct wl_shell_surface *shell_surface,
2336 uint32_t serial) {
2337 wl_shell_surface_pong(shell_surface, serial);
2338}
2339
2340static void handle_configure(void *data UNUSED,
2341 struct wl_shell_surface *shell_surface UNUSED,
2342 uint32_t edges UNUSED, int32_t width UNUSED,
2343 int32_t height UNUSED) {}
2344
2345static void handle_popup_done(void *data UNUSED,
2346 struct wl_shell_surface *shell_surface UNUSED) {}
2347
2348static const struct wl_shell_surface_listener shell_surface_listener = {
2349 handle_ping, handle_configure, handle_popup_done};
2350
2351static void demo_create_window(struct demo *demo) {
2352 demo->window = wl_compositor_create_surface(demo->compositor);
2353 if (!demo->window) {
2354 printf("Can not create wayland_surface from compositor!\n");
2355 fflush(stdout);
2356 exit(1);
2357 }
2358
2359 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2360 if (!demo->shell_surface) {
2361 printf("Can not get shell_surface from wayland_surface!\n");
2362 fflush(stdout);
2363 exit(1);
2364 }
2365 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2366 demo);
2367 wl_shell_surface_set_toplevel(demo->shell_surface);
2368 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2369}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002370#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2371static void demo_run(struct demo *demo) {
2372 if (!demo->prepared)
2373 return;
2374
2375 // Wait for work to finish before updating MVP.
2376 vkDeviceWaitIdle(demo->device);
2377 demo_update_data_buffer(demo);
2378
2379 demo_draw(demo);
2380
2381 // Wait for work to finish before updating MVP.
2382 vkDeviceWaitIdle(demo->device);
2383
2384 demo->curFrame++;
2385}
2386#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002387
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002388/*
2389 * Return 1 (true) if all layer names specified in check_names
2390 * can be found in given layer properties.
2391 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002392static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002393 uint32_t layer_count,
2394 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002395 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002396 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002397 for (uint32_t j = 0; j < layer_count; j++) {
2398 if (!strcmp(check_names[i], layers[j].layerName)) {
2399 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002400 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002401 }
2402 }
2403 if (!found) {
2404 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2405 return 0;
2406 }
2407 }
2408 return 1;
2409}
2410
Karl Schultze4dc75c2016-02-02 15:37:51 -07002411static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002412 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002413 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002414 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002415 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002416 char **instance_validation_layers = NULL;
2417 demo->enabled_extension_count = 0;
2418 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002419
Karl Schultz7c50ad62016-04-01 12:07:03 -06002420 char *instance_validation_layers_alt1[] = {
2421 "VK_LAYER_LUNARG_standard_validation"
2422 };
2423
2424 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002425 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2426 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2427 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2428 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002429 };
2430
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002431 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002432 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002433 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002434
Karl Schultz7c50ad62016-04-01 12:07:03 -06002435 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002436 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002437
Karl Schultz7c50ad62016-04-01 12:07:03 -06002438 instance_validation_layers = instance_validation_layers_alt1;
2439 if (instance_layer_count > 0) {
2440 VkLayerProperties *instance_layers =
2441 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2442 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2443 instance_layers);
2444 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002445
Karl Schultz7c50ad62016-04-01 12:07:03 -06002446
2447 validation_found = demo_check_layers(
2448 ARRAY_SIZE(instance_validation_layers_alt1),
2449 instance_validation_layers, instance_layer_count,
2450 instance_layers);
2451 if (validation_found) {
2452 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002453 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2454 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002455 } else {
2456 // use alternative set of validation layers
2457 instance_validation_layers = instance_validation_layers_alt2;
2458 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2459 validation_found = demo_check_layers(
2460 ARRAY_SIZE(instance_validation_layers_alt2),
2461 instance_validation_layers, instance_layer_count,
2462 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002463 validation_layer_count =
2464 ARRAY_SIZE(instance_validation_layers_alt2);
2465 for (uint32_t i = 0; i < validation_layer_count; i++) {
2466 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002467 }
2468 }
2469 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002470 }
Dustin Graves7c287352016-01-27 13:48:06 -07002471
Karl Schultz7c50ad62016-04-01 12:07:03 -06002472 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002473 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002474 "required validation layer.\n\n"
2475 "Please look at the Getting Started guide for additional "
2476 "information.\n",
2477 "vkCreateInstance Failure");
2478 }
Dustin Graves7c287352016-01-27 13:48:06 -07002479 }
2480
2481 /* Look for instance extensions */
2482 VkBool32 surfaceExtFound = 0;
2483 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002484#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002485 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002486#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002487 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002488
Karl Schultze4dc75c2016-02-02 15:37:51 -07002489 err = vkEnumerateInstanceExtensionProperties(
2490 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002491 assert(!err);
2492
Dustin Graves7c287352016-01-27 13:48:06 -07002493 if (instance_extension_count > 0) {
2494 VkExtensionProperties *instance_extensions =
2495 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2496 err = vkEnumerateInstanceExtensionProperties(
2497 NULL, &instance_extension_count, instance_extensions);
2498 assert(!err);
2499 for (uint32_t i = 0; i < instance_extension_count; i++) {
2500 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2501 instance_extensions[i].extensionName)) {
2502 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002503 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002504 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002505 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002506#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002507 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2508 instance_extensions[i].extensionName)) {
2509 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002510 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002511 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2512 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002513#endif
2514#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002515 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2516 instance_extensions[i].extensionName)) {
2517 platformSurfaceExtFound = 1;
2518 xlibSurfaceExtFound = 1;
2519 demo->extension_names[demo->enabled_extension_count++] =
2520 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2521 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002522#endif
2523#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002524 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2525 instance_extensions[i].extensionName)) {
2526 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002527 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002528 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2529 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002530#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002531#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2532 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2533 instance_extensions[i].extensionName)) {
2534 platformSurfaceExtFound = 1;
2535 demo->extension_names[demo->enabled_extension_count++] =
2536 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2537 }
2538#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002539#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002540 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2541 instance_extensions[i].extensionName)) {
2542 platformSurfaceExtFound = 1;
2543 demo->extension_names[demo->enabled_extension_count++] =
2544 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2545 }
2546#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002547 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2548 instance_extensions[i].extensionName)) {
2549 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002550 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002551 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2552 }
2553 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002554 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002555 }
Dustin Graves7c287352016-01-27 13:48:06 -07002556
2557 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002558 }
Dustin Graves7c287352016-01-27 13:48:06 -07002559
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002560 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002561 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2562 "the " VK_KHR_SURFACE_EXTENSION_NAME
2563 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002564 "Vulkan installable client driver (ICD) installed?\nPlease "
2565 "look at the Getting Started guide for additional "
2566 "information.\n",
2567 "vkCreateInstance Failure");
2568 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002569 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002570#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002571 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2572 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2573 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002574 "Vulkan installable client driver (ICD) installed?\nPlease "
2575 "look at the Getting Started guide for additional "
2576 "information.\n",
2577 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002578#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002579 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2580 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2581 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002582 "Vulkan installable client driver (ICD) installed?\nPlease "
2583 "look at the Getting Started guide for additional "
2584 "information.\n",
2585 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002586#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2587 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2588 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2589 " extension.\n\nDo you have a compatible "
2590 "Vulkan installable client driver (ICD) installed?\nPlease "
2591 "look at the Getting Started guide for additional "
2592 "information.\n",
2593 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002594#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2595 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2596 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2597 " extension.\n\nDo you have a compatible "
2598 "Vulkan installable client driver (ICD) installed?\nPlease "
2599 "look at the Getting Started guide for additional "
2600 "information.\n",
2601 "vkCreateInstance Failure");
2602#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002603 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002604#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002605 if (demo->use_xlib && !xlibSurfaceExtFound) {
2606 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2607 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2608 " extension.\n\nDo you have a compatible "
2609 "Vulkan installable client driver (ICD) installed?\nPlease "
2610 "look at the Getting Started guide for additional "
2611 "information.\n",
2612 "vkCreateInstance Failure");
2613 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002614#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002615 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002616 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002617 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002618 .pApplicationName = APP_SHORT_NAME,
2619 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002620 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002621 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002622 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002623 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002624 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002625 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002626 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002627 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002628 .enabledLayerCount = demo->enabled_layer_count,
2629 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2630 .enabledExtensionCount = demo->enabled_extension_count,
2631 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002632 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002633
2634 /*
2635 * This is info for a temp callback to use during CreateInstance.
2636 * After the instance is created, we use the instance-based
2637 * function to register the final callback.
2638 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002639 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002640 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002641 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2642 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002643 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002644 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002645 dbgCreateInfo.flags =
2646 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2647 inst_info.pNext = &dbgCreateInfo;
2648 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002649
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002650 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002651
Chia-I Wu63636062015-10-26 21:10:41 +08002652 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002653 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2654 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002655 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002656 "additional information.\n",
2657 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002658 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002659 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002660 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002661 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002662 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002663 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2664 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002665 "the Getting Started guide for additional information.\n",
2666 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002667 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002668
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002669 /* Make initial call to query gpu_count, then second call for gpu info*/
2670 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2671 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002672
2673 if (gpu_count > 0) {
2674 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2675 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2676 assert(!err);
2677 /* For cube demo we just grab the first physical device */
2678 demo->gpu = physical_devices[0];
2679 free(physical_devices);
2680 } else {
2681 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2682 "Do you have a compatible Vulkan installable client driver (ICD) "
2683 "installed?\nPlease look at the Getting Started guide for "
2684 "additional information.\n",
2685 "vkEnumeratePhysicalDevices Failure");
2686 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002687
Dustin Graves7c287352016-01-27 13:48:06 -07002688 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002689 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002690 VkBool32 swapchainExtFound = 0;
2691 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002692 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002693
Karl Schultze4dc75c2016-02-02 15:37:51 -07002694 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2695 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002696 assert(!err);
2697
Dustin Graves7c287352016-01-27 13:48:06 -07002698 if (device_extension_count > 0) {
2699 VkExtensionProperties *device_extensions =
2700 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2701 err = vkEnumerateDeviceExtensionProperties(
2702 demo->gpu, NULL, &device_extension_count, device_extensions);
2703 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002704
Dustin Graves7c287352016-01-27 13:48:06 -07002705 for (uint32_t i = 0; i < device_extension_count; i++) {
2706 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2707 device_extensions[i].extensionName)) {
2708 swapchainExtFound = 1;
2709 demo->extension_names[demo->enabled_extension_count++] =
2710 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2711 }
2712 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002713 }
Dustin Graves7c287352016-01-27 13:48:06 -07002714
2715 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002716 }
Dustin Graves7c287352016-01-27 13:48:06 -07002717
Tony Barbourcc69f452015-10-08 13:59:42 -06002718 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002719 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2720 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2721 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002722 "Vulkan installable client driver (ICD) installed?\nPlease "
2723 "look at the Getting Started guide for additional "
2724 "information.\n",
2725 "vkCreateInstance Failure");
2726 }
2727
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002728 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002729 demo->CreateDebugReportCallback =
2730 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2731 demo->inst, "vkCreateDebugReportCallbackEXT");
2732 demo->DestroyDebugReportCallback =
2733 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2734 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002735 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002736 ERR_EXIT(
2737 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2738 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002739 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002740 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002741 ERR_EXIT(
2742 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2743 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002744 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002745 demo->DebugReportMessage =
2746 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2747 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002748 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002749 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002750 "vkGetProcAddr Failure");
2751 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002752
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002753 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002754 PFN_vkDebugReportCallbackEXT callback;
2755 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002756 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002757 dbgCreateInfo.pNext = NULL;
2758 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002759 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002760 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002761 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002762 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2763 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002764 switch (err) {
2765 case VK_SUCCESS:
2766 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002767 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002768 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2769 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002770 break;
2771 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002772 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2773 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002774 break;
2775 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002776 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002777 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002778
2779 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002780 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2781 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002782 assert(demo->queue_count >= 1);
2783
Karl Schultze4dc75c2016-02-02 15:37:51 -07002784 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2785 demo->queue_count * sizeof(VkQueueFamilyProperties));
2786 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2787 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002788 // Find a queue that supports gfx
2789 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002790 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2791 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002792 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2793 break;
2794 }
2795 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002796 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002797 // If app has specific feature requirements it should check supported
2798 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002799 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002800 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002801
Tony Barbourda2bad52016-01-22 14:36:40 -07002802 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2803 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2804 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2805 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2806 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2807}
2808
Karl Schultze4dc75c2016-02-02 15:37:51 -07002809static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002810 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002811 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002812 const VkDeviceQueueCreateInfo queue = {
2813 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2814 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002815 .queueFamilyIndex = demo->graphics_queue_family_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002816 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002817 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002818
2819 VkDeviceCreateInfo device = {
2820 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2821 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002822 .queueCreateInfoCount = 1,
2823 .pQueueCreateInfos = &queue,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002824 .enabledLayerCount = 0,
2825 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002826 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002827 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2828 .pEnabledFeatures =
2829 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002830 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002831
Chia-I Wu63636062015-10-26 21:10:41 +08002832 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002833 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002834}
2835
Karl Schultze4dc75c2016-02-02 15:37:51 -07002836static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002837 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002838 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002839
Karl Schultze4dc75c2016-02-02 15:37:51 -07002840// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002841#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002842 VkWin32SurfaceCreateInfoKHR createInfo;
2843 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2844 createInfo.pNext = NULL;
2845 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002846 createInfo.hinstance = demo->connection;
2847 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002848
Karl Schultze4dc75c2016-02-02 15:37:51 -07002849 err =
2850 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002851#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2852 VkWaylandSurfaceCreateInfoKHR createInfo;
2853 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2854 createInfo.pNext = NULL;
2855 createInfo.flags = 0;
2856 createInfo.display = demo->display;
2857 createInfo.surface = demo->window;
2858
2859 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2860 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002861#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2862 VkAndroidSurfaceCreateInfoKHR createInfo;
2863 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2864 createInfo.pNext = NULL;
2865 createInfo.flags = 0;
2866 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002867
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002868 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2869#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002870 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002871#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002872 VkXlibSurfaceCreateInfoKHR createInfo;
2873 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2874 createInfo.pNext = NULL;
2875 createInfo.flags = 0;
2876 createInfo.dpy = demo->display;
2877 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002878
Tony Barbour2593b182016-04-19 10:57:58 -06002879 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2880 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002881#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002882 }
2883 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002884#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002885 VkXcbSurfaceCreateInfoKHR createInfo;
2886 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2887 createInfo.pNext = NULL;
2888 createInfo.flags = 0;
2889 createInfo.connection = demo->connection;
2890 createInfo.window = demo->xcb_window;
2891
2892 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002893#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002894 }
Tony Barbour2593b182016-04-19 10:57:58 -06002895 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002896
Tony Barbourcc69f452015-10-08 13:59:42 -06002897 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002898 VkBool32 *supportsPresent =
2899 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002900 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002901 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002902 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002903 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002904
2905 // Search for a graphics and a present queue in the array of queue
2906 // families, try to find one that supports both
2907 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002908 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002909 for (i = 0; i < demo->queue_count; i++) {
Tony Barboura2a67812016-07-18 13:21:06 -06002910 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0
2911 && graphicsQueueNodeIndex == UINT32_MAX) {
2912 graphicsQueueNodeIndex = i;
2913 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002914
Tony Barboura2a67812016-07-18 13:21:06 -06002915 if (supportsPresent[i] == VK_TRUE && presentQueueNodeIndex == UINT32_MAX) {
2916 presentQueueNodeIndex = i;
Ian Elliottaaae5352015-07-06 14:27:58 -06002917 }
2918 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002919
2920 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002921 if (graphicsQueueNodeIndex == UINT32_MAX ||
2922 presentQueueNodeIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06002923 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002924 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002925 }
2926
Tony Barboura2a67812016-07-18 13:21:06 -06002927 demo->graphics_queue_family_index = graphicsQueueNodeIndex;
2928 demo->present_queue_family_index = presentQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002929
Tony Barbourda2bad52016-01-22 14:36:40 -07002930 demo_create_device(demo);
2931
2932 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2933 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2934 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2935 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2936 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2937
Tony Barboura2a67812016-07-18 13:21:06 -06002938 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
2939 &demo->graphics_queue);
2940
2941 if (demo->graphics_queue_family_index == demo->present_queue_family_index) {
2942 demo->present_queue = demo->graphics_queue;
2943 } else {
2944 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
2945 &demo->present_queue);
2946 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002947
Ian Elliottaaae5352015-07-06 14:27:58 -06002948 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002949 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002950 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002951 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002952 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002953 VkSurfaceFormatKHR *surfFormats =
2954 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002955 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002956 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002957 assert(!err);
2958 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2959 // the surface has no preferred format. Otherwise, at least one
2960 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002961 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002962 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002963 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002964 assert(formatCount >= 1);
2965 demo->format = surfFormats[0].format;
2966 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002967 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002968
David Pinedo2bb7c932015-06-18 17:03:14 -06002969 demo->quit = false;
2970 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002971
2972 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002973 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002974}
2975
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002976#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2977static void registry_handle_global(void *data, struct wl_registry *registry,
2978 uint32_t name, const char *interface,
2979 uint32_t version UNUSED) {
2980 struct demo *demo = data;
2981 if (strcmp(interface, "wl_compositor") == 0) {
2982 demo->compositor =
2983 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
2984 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
2985 * tp xdg_shell */
2986 } else if (strcmp(interface, "wl_shell") == 0) {
2987 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
2988 }
2989}
2990
2991static void registry_handle_global_remove(void *data UNUSED,
2992 struct wl_registry *registry UNUSED,
2993 uint32_t name UNUSED) {}
2994
2995static const struct wl_registry_listener registry_listener = {
2996 registry_handle_global, registry_handle_global_remove};
2997#endif
2998
Karl Schultze4dc75c2016-02-02 15:37:51 -07002999static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003000#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003001 const xcb_setup_t *setup;
3002 xcb_screen_iterator_t iter;
3003 int scr;
3004
3005 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06003006 if (demo->connection == NULL) {
3007 printf("Cannot find a compatible Vulkan installable client driver "
3008 "(ICD).\nExiting ...\n");
3009 fflush(stdout);
3010 exit(1);
3011 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003012
3013 setup = xcb_get_setup(demo->connection);
3014 iter = xcb_setup_roots_iterator(setup);
3015 while (scr-- > 0)
3016 xcb_screen_next(&iter);
3017
3018 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003019#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3020 demo->display = wl_display_connect(NULL);
3021
3022 if (demo->display == NULL) {
3023 printf("Cannot find a compatible Vulkan installable client driver "
3024 "(ICD).\nExiting ...\n");
3025 fflush(stdout);
3026 exit(1);
3027 }
3028
3029 demo->registry = wl_display_get_registry(demo->display);
3030 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3031 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003032#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003033}
3034
Karl Schultze4dc75c2016-02-02 15:37:51 -07003035static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003036 vec3 eye = {0.0f, 3.0f, 5.0f};
3037 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003038 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003039
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003040 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003041 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003042
Piers Daniell735ee532015-02-23 16:23:13 -07003043 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003044 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003045 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003046 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003047 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003048 if (strcmp(argv[i], "--break") == 0) {
3049 demo->use_break = true;
3050 continue;
3051 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003052 if (strcmp(argv[i], "--validate") == 0) {
3053 demo->validate = true;
3054 continue;
3055 }
Tony Barbour2593b182016-04-19 10:57:58 -06003056 if (strcmp(argv[i], "--xlib") == 0) {
3057 demo->use_xlib = true;
3058 continue;
3059 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003060 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3061 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3062 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003063 i++;
3064 continue;
3065 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003066 if (strcmp(argv[i], "--suppress_popups") == 0) {
3067 demo->suppress_popups = true;
3068 continue;
3069 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003070
Karl Schultze4dc75c2016-02-02 15:37:51 -07003071 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
lenny-lunargfbe22392016-06-06 11:07:53 -06003072 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003073 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003074 fflush(stderr);
3075 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003076 }
3077
Tony Barbour2593b182016-04-19 10:57:58 -06003078 if (!demo->use_xlib)
3079 demo_init_connection(demo);
3080
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003081 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003082
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003083 demo->width = 500;
3084 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003085
3086 demo->spin_angle = 0.01f;
3087 demo->spin_increment = 0.01f;
3088 demo->pause = false;
3089
Karl Schultze4dc75c2016-02-02 15:37:51 -07003090 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3091 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003092 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3093 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003094}
3095
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003096#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003097// Include header required for parsing the command line options.
3098#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003099
Karl Schultze4dc75c2016-02-02 15:37:51 -07003100int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3101 int nCmdShow) {
3102 MSG msg; // message
3103 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003104 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003105 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003106
Karl Schultze4dc75c2016-02-02 15:37:51 -07003107 // Use the CommandLine functions to get the command line arguments.
3108 // Unfortunately, Microsoft outputs
3109 // this information as wide characters for Unicode, and we simply want the
3110 // Ascii version to be compatible
3111 // with the non-Windows side. So, we have to convert the information to
3112 // Ascii character strings.
3113 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3114 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003115 argc = 0;
3116 }
3117
Karl Schultze4dc75c2016-02-02 15:37:51 -07003118 if (argc > 0) {
3119 argv = (char **)malloc(sizeof(char *) * argc);
3120 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003121 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003122 } else {
3123 for (int iii = 0; iii < argc; iii++) {
3124 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003125 size_t numConverted = 0;
3126
Karl Schultze4dc75c2016-02-02 15:37:51 -07003127 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3128 if (argv[iii] != NULL) {
3129 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3130 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003131 }
3132 }
3133 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003134 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003135 argv = NULL;
3136 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003137
3138 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003139
3140 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003141 if (argc > 0 && argv != NULL) {
3142 for (int iii = 0; iii < argc; iii++) {
3143 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003144 free(argv[iii]);
3145 }
3146 }
3147 free(argv);
3148 }
3149
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003150 demo.connection = hInstance;
3151 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003152 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003153 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003154
3155 demo_prepare(&demo);
3156
Karl Schultze4dc75c2016-02-02 15:37:51 -07003157 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003158
3159 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003160 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003161 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003162 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003163 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003164 done = true; // if found, quit app
3165 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003166 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003167 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003168 DispatchMessage(&msg);
3169 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003170 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003171 }
3172
3173 demo_cleanup(&demo);
3174
Karl Schultze4dc75c2016-02-02 15:37:51 -07003175 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003176}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003177#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3178#include <android/log.h>
3179#include <android_native_app_glue.h>
3180static bool initialized = false;
3181static bool active = false;
3182struct demo demo;
3183
3184static int32_t processInput(struct android_app* app, AInputEvent* event) {
3185 return 0;
3186}
3187
3188static void processCommand(struct android_app* app, int32_t cmd) {
3189 switch(cmd) {
3190 case APP_CMD_INIT_WINDOW: {
3191 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003192 // We're getting a new window. If the app is starting up, we
3193 // need to initialize. If the app has already been
3194 // initialized, that means that we lost our previous window,
3195 // which means that we have a lot of work to do. At a minimum,
3196 // we need to destroy the swapchain and surface associated with
3197 // the old window, and create a new surface and swapchain.
3198 // However, since there are a lot of other objects/state that
3199 // is tied to the swapchain, it's easiest to simply cleanup and
3200 // start over (i.e. use a brute-force approach of re-starting
3201 // the app)
3202 if (demo.prepared) {
3203 demo_cleanup(&demo);
3204 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003205 demo_init(&demo, 0, NULL);
3206 demo.window = (void*)app->window;
3207 demo_init_vk_swapchain(&demo);
3208 demo_prepare(&demo);
3209 initialized = true;
3210 }
3211 break;
3212 }
3213 case APP_CMD_GAINED_FOCUS: {
3214 active = true;
3215 break;
3216 }
3217 case APP_CMD_LOST_FOCUS: {
3218 active = false;
3219 break;
3220 }
3221 }
3222}
3223
3224void android_main(struct android_app *app)
3225{
3226 app_dummy();
3227
Cody Northrop8707a6e2016-04-26 19:59:19 -06003228#ifdef ANDROID
3229 int vulkanSupport = InitVulkan();
3230 if (vulkanSupport == 0)
3231 return;
3232#endif
3233
Ian Elliottf05d5d12016-05-17 12:03:35 -06003234 demo.prepared = false;
3235
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003236 app->onAppCmd = processCommand;
3237 app->onInputEvent = processInput;
3238
3239 while(1) {
3240 int events;
3241 struct android_poll_source* source;
3242 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3243 if (source) {
3244 source->process(app, source);
3245 }
3246
3247 if (app->destroyRequested != 0) {
3248 demo_cleanup(&demo);
3249 return;
3250 }
3251 }
3252 if (initialized && active) {
3253 demo_run(&demo);
3254 }
3255 }
3256
3257}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003258#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003259int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003260 struct demo demo;
3261
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003262 demo_init(&demo, argc, argv);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003263#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003264 if (demo.use_xlib)
3265 demo_create_xlib_window(&demo);
3266 else
3267 demo_create_xcb_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003268#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3269 demo_create_window(&demo);
3270#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003271
Tony Barbourcc69f452015-10-08 13:59:42 -06003272 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003273
3274 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003275
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003276#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003277 if (demo.use_xlib)
3278 demo_run_xlib(&demo);
3279 else
3280 demo_run_xcb(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003281#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3282 demo_run(&demo);
3283#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003284
3285 demo_cleanup(&demo);
3286
Karl Schultz0218c002016-03-22 17:06:13 -06003287 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003288}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003289#endif