blob: f675ca5a372c479a8a837b60268f26ad5c2e4773 [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
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600302#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600303 Display* display;
304 Window xlib_window;
305 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600306
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600307 xcb_connection_t *connection;
308 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600309 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800310 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900311#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
312 struct wl_display *display;
313 struct wl_registry *registry;
314 struct wl_compositor *compositor;
315 struct wl_surface *window;
316 struct wl_shell *shell;
317 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500318#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
319 ANativeWindow* window;
320#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700321 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600322 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700323 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600324 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600326 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600327 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkDevice device;
329 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700330 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600331 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600332 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600333 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
Tony Barbourda2bad52016-01-22 14:36:40 -0700335 uint32_t enabled_extension_count;
336 uint32_t enabled_layer_count;
337 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600338 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700339
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600340 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600341 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600342 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600343
Karl Schultze4dc75c2016-02-02 15:37:51 -0700344 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
345 fpGetPhysicalDeviceSurfaceSupportKHR;
346 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
347 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
348 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
349 fpGetPhysicalDeviceSurfaceFormatsKHR;
350 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
351 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600352 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
353 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
354 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
355 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
356 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600357 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600358 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600359 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800361 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600362
363 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800367 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500368 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600369 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370 } depth;
371
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600372 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
374 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800376 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500377 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600378 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600379 } uniform_data;
380
Karl Schultze4dc75c2016-02-02 15:37:51 -0700381 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500382 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600383 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600384 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800385 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600388 mat4x4 projection_matrix;
389 mat4x4 view_matrix;
390 mat4x4 model_matrix;
391
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600392 float spin_angle;
393 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600394 bool pause;
395
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600396 VkShaderModule vert_shader_module;
397 VkShaderModule frag_shader_module;
398
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600399 VkDescriptorPool desc_pool;
400 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800401
Tony Barbourd8e504f2015-10-08 14:26:24 -0600402 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800403
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600405 int32_t curFrame;
406 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600407 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600408 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600409 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700410 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
411 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
412 VkDebugReportCallbackEXT msg_callback;
413 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600414
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600415 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600416 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600417};
418
lenny-lunargfbe22392016-06-06 11:07:53 -0600419VKAPI_ATTR VkBool32 VKAPI_CALL
420dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
421 uint64_t srcObject, size_t location, int32_t msgCode,
422 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
423 char *message = (char *)malloc(strlen(pMsg) + 100);
424
425 assert(message);
426
427 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
428 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
429 pMsg);
430 validation_error = 1;
431 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
432 // We know that we're submitting queues without fences, ignore this
433 // warning
434 if (strstr(pMsg,
435 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
436 return false;
437 }
438 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
439 pMsg);
440 validation_error = 1;
441 } else {
442 validation_error = 1;
443 return false;
444 }
445
446#ifdef _WIN32
447 struct demo *demo = (struct demo*) pUserData;
448 if (!demo->suppress_popups)
449 MessageBox(NULL, message, "Alert", MB_OK);
450#else
451 printf("%s\n", message);
452 fflush(stdout);
453#endif
454 free(message);
455
456 /*
457 * false indicates that layer should not bail-out of an
458 * API call that had validation failures. This may mean that the
459 * app dies inside the driver due to invalid parameter(s).
460 * That's what would happen without validation layers, so we'll
461 * keep that behavior here.
462 */
463 return false;
464}
465
Ian Elliottcf074d32015-10-16 18:02:43 -0600466// Forward declaration:
467static void demo_resize(struct demo *demo);
468
Karl Schultze4dc75c2016-02-02 15:37:51 -0700469static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
470 VkFlags requirements_mask,
471 uint32_t *typeIndex) {
472 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600473 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700474 if ((typeBits & 1) == 1) {
475 // Type is available, does it match user properties?
476 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
477 requirements_mask) == requirements_mask) {
478 *typeIndex = i;
479 return true;
480 }
481 }
482 typeBits >>= 1;
483 }
484 // No memory types matched, return failure
485 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600486}
487
Karl Schultze4dc75c2016-02-02 15:37:51 -0700488static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600489 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600490
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600491 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 return;
493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 assert(!err);
496
Karl Schultze4dc75c2016-02-02 15:37:51 -0700497 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800498 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700499 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
500 .pNext = NULL,
501 .waitSemaphoreCount = 0,
502 .pWaitSemaphores = NULL,
503 .pWaitDstStageMask = NULL,
504 .commandBufferCount = 1,
505 .pCommandBuffers = cmd_bufs,
506 .signalSemaphoreCount = 0,
507 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600508
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600509 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510 assert(!err);
511
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600512 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600513 assert(!err);
514
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600515 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600517}
518
Karl Schultze4dc75c2016-02-02 15:37:51 -0700519static void demo_set_image_layout(struct demo *demo, VkImage image,
520 VkImageAspectFlags aspectMask,
521 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700522 VkImageLayout new_image_layout,
523 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600524 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800527 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800528 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800530 .commandPool = demo->cmd_pool,
531 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700532 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 };
534
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800535 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600536 assert(!err);
537
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700538 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
539 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600540 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800541 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600542 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800543 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800544 .occlusionQueryEnable = VK_FALSE,
545 .queryFlags = 0,
546 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700548 VkCommandBufferBeginInfo cmd_buf_info = {
549 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
550 .pNext = NULL,
551 .flags = 0,
552 .pInheritanceInfo = &cmd_buf_hinfo,
553 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600554 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600555 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600556 }
557
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600558 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600559 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600560 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700561 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800562 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600563 .oldLayout = old_image_layout,
564 .newLayout = new_image_layout,
565 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700566 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600567
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800568 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600569 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800570 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600571 }
572
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700573 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700574 image_memory_barrier.dstAccessMask =
575 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700576 }
577
578 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700579 image_memory_barrier.dstAccessMask =
580 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700581 }
582
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600583 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600584 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700585 image_memory_barrier.dstAccessMask =
586 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600587 }
588
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600589 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600590
Tony Barbour50bbca42015-06-29 16:20:35 -0600591 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
592 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600593
Karl Schultze4dc75c2016-02-02 15:37:51 -0700594 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
595 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600596}
597
Karl Schultze4dc75c2016-02-02 15:37:51 -0700598static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700599 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
600 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600601 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800602 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600603 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800604 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800605 .occlusionQueryEnable = VK_FALSE,
606 .queryFlags = 0,
607 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700608 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700609 const VkCommandBufferBeginInfo cmd_buf_info = {
610 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
611 .pNext = NULL,
612 .flags = 0,
613 .pInheritanceInfo = &cmd_buf_hinfo,
614 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800615 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700616 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
617 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800618 };
619 const VkRenderPassBeginInfo rp_begin = {
620 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
621 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800622 .renderPass = demo->render_pass,
623 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800624 .renderArea.offset.x = 0,
625 .renderArea.offset.y = 0,
626 .renderArea.extent.width = demo->width,
627 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600628 .clearValueCount = 2,
629 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700630 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800631 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600633 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600634 assert(!err);
635
Tony Barbour5c5b1632016-04-26 11:13:48 -0600636 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
637 // happens to the previous contents of the image
638 VkImageMemoryBarrier image_memory_barrier = {
639 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
640 .pNext = NULL,
641 .srcAccessMask = 0,
642 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
643 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
644 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
645 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
646 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
647 .image = demo->buffers[demo->current_buffer].image,
648 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
649
650 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
651 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
652 NULL, 1, &image_memory_barrier);
653
Chia-I Wuf051d262015-10-27 19:25:11 +0800654 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800655
Karl Schultze4dc75c2016-02-02 15:37:51 -0700656 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
657 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
658 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
659 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600660 VkViewport viewport;
661 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700662 viewport.height = (float)demo->height;
663 viewport.width = (float)demo->width;
664 viewport.minDepth = (float)0.0f;
665 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700666 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600667
668 VkRect2D scissor;
669 memset(&scissor, 0, sizeof(scissor));
670 scissor.extent.width = demo->width;
671 scissor.extent.height = demo->height;
672 scissor.offset.x = 0;
673 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700674 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600675 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800676 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677
Tony Barbour15a4ef62015-10-21 10:14:48 -0600678 VkImageMemoryBarrier prePresentBarrier = {
679 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
680 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800681 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700682 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600683 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700684 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600685 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800686 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700687 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600688
689 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
690 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
692 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
693 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600694
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600695 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600696 assert(!err);
697}
698
Karl Schultze4dc75c2016-02-02 15:37:51 -0700699void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600700 mat4x4 MVP, Model, VP;
701 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600702 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600703 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600704
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600705 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600706
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600707 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600708 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700709 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
710 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600711 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600712
Karl Schultze4dc75c2016-02-02 15:37:51 -0700713 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
714 demo->uniform_data.mem_alloc.allocationSize, 0,
715 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716 assert(!err);
717
Karl Schultze4dc75c2016-02-02 15:37:51 -0700718 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600719
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600720 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600721}
722
Karl Schultze4dc75c2016-02-02 15:37:51 -0700723static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600724 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600725 VkSemaphore imageAcquiredSemaphore, drawCompleteSemaphore;
726 VkSemaphoreCreateInfo semaphoreCreateInfo = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600727 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
728 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600729 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600730 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800731 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600732
Tony Barboure35e8aa2016-06-20 10:44:08 -0600733 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
Tony Barbour416c6502016-06-20 10:30:23 -0600734 NULL, &imageAcquiredSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600735 assert(!err);
736
Tony Barboure35e8aa2016-06-20 10:44:08 -0600737 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
738 NULL, &drawCompleteSemaphore);
739 assert(!err);
740
Ian Elliottaaae5352015-07-06 14:27:58 -0600741 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700742 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour416c6502016-06-20 10:30:23 -0600743 imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700744 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600745 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600746 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
747 // demo->swapchain is out of date (e.g. the window was resized) and
748 // must be recreated:
749 demo_resize(demo);
750 demo_draw(demo);
Tony Barbour416c6502016-06-20 10:30:23 -0600751 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600752 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600753 return;
754 } else if (err == VK_SUBOPTIMAL_KHR) {
755 // demo->swapchain is not as optimal as it could be, but the platform's
756 // presentation engine will still present the image correctly.
757 } else {
758 assert(!err);
759 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600760
Tony Barbour15a4ef62015-10-21 10:14:48 -0600761 demo_flush_init_cmd(demo);
762
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600763 // Wait for the present complete semaphore to be signaled to ensure
764 // that the image won't be rendered to until the presentation
765 // engine has fully released ownership to the application, and it is
766 // okay to render to the image.
767
Karl Schultze4dc75c2016-02-02 15:37:51 -0700768 VkPipelineStageFlags pipe_stage_flags =
769 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
770 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
771 .pNext = NULL,
772 .waitSemaphoreCount = 1,
Tony Barbour416c6502016-06-20 10:30:23 -0600773 .pWaitSemaphores = &imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700774 .pWaitDstStageMask = &pipe_stage_flags,
775 .commandBufferCount = 1,
776 .pCommandBuffers =
777 &demo->buffers[demo->current_buffer].cmd,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600778 .signalSemaphoreCount = 1,
779 .pSignalSemaphores = &drawCompleteSemaphore};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600780
781 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600782 assert(!err);
783
Ian Elliotta0781972015-08-21 15:09:33 -0600784 VkPresentInfoKHR present = {
785 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600786 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600787 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700788 .pSwapchains = &demo->swapchain,
789 .pImageIndices = &demo->current_buffer,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600790 .waitSemaphoreCount = 1,
791 .pWaitSemaphores = &drawCompleteSemaphore,
Ian Elliottaaae5352015-07-06 14:27:58 -0600792 };
793
Karl Schultze4dc75c2016-02-02 15:37:51 -0700794 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600795 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600796 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
797 // demo->swapchain is out of date (e.g. the window was resized) and
798 // must be recreated:
799 demo_resize(demo);
800 } else if (err == VK_SUBOPTIMAL_KHR) {
801 // demo->swapchain is not as optimal as it could be, but the platform's
802 // presentation engine will still present the image correctly.
803 } else {
804 assert(!err);
805 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600806
Chia-I Wucbb564e2015-04-16 22:02:10 +0800807 err = vkQueueWaitIdle(demo->queue);
808 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600809
Tony Barbour416c6502016-06-20 10:30:23 -0600810 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600811 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600812}
813
Karl Schultze4dc75c2016-02-02 15:37:51 -0700814static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600815 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600816 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600817
Ian Elliottb08e0d92015-11-20 11:55:46 -0700818 // Check the surface capabilities and formats
819 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700820 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
821 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600822 assert(!err);
823
Ian Elliott8528eff2015-08-07 15:56:59 -0600824 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700825 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
826 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600827 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600828 VkPresentModeKHR *presentModes =
829 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600830 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700831 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
832 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600833 assert(!err);
834
Ian Elliotta0781972015-08-21 15:09:33 -0600835 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600836 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700837 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600838 // If the surface size is undefined, the size is set to
839 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600840 swapchainExtent.width = demo->width;
841 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700842 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600843 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700844 swapchainExtent = surfCapabilities.currentExtent;
845 demo->width = surfCapabilities.currentExtent.width;
846 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600847 }
848
849 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600850 // tearing mode. If not, try IMMEDIATE which will usually be available,
851 // and is fastest (though it tears). If not, fall back to FIFO which is
852 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600853 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600854 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600855 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
856 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600857 break;
858 }
Ian Elliotta0781972015-08-21 15:09:33 -0600859 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
860 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
861 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600862 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600863 }
864
865 // Determine the number of VkImage's to use in the swap chain (we desire to
866 // own only 1 image at a time, besides the images being displayed and
867 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700868 uint32_t desiredNumberOfSwapchainImages =
869 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700870 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700871 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600872 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700873 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600874 }
875
Tony Barbour9fd6d352015-09-16 15:01:32 -0600876 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700877 if (surfCapabilities.supportedTransforms &
878 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700879 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700881 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600882 }
883
Ian Elliottcf074d32015-10-16 18:02:43 -0600884 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600885 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800886 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700887 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600888 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800889 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600890 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700891 .imageExtent =
892 {
893 .width = swapchainExtent.width, .height = swapchainExtent.height,
894 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700895 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600896 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700897 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700898 .imageArrayLayers = 1,
899 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
900 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600901 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600902 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600903 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600904 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600905 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600906 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600907
Karl Schultze4dc75c2016-02-02 15:37:51 -0700908 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
909 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800910 assert(!err);
911
Ian Elliottcf074d32015-10-16 18:02:43 -0600912 // If we just re-created an existing swapchain, we should destroy the old
913 // swapchain at this point.
914 // Note: destroying the swapchain also cleans up all its associated
915 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800916 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700917 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600918 }
919
920 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600921 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600922 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800923
Karl Schultze4dc75c2016-02-02 15:37:51 -0700924 VkImage *swapchainImages =
925 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600926 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600927 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600928 &demo->swapchainImageCount,
929 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600930 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600931
Karl Schultze4dc75c2016-02-02 15:37:51 -0700932 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
933 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600934 assert(demo->buffers);
935
Ian Elliotta0781972015-08-21 15:09:33 -0600936 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600937 VkImageViewCreateInfo color_image_view = {
938 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600939 .pNext = NULL,
940 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700941 .components =
942 {
943 .r = VK_COMPONENT_SWIZZLE_R,
944 .g = VK_COMPONENT_SWIZZLE_G,
945 .b = VK_COMPONENT_SWIZZLE_B,
946 .a = VK_COMPONENT_SWIZZLE_A,
947 },
948 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
949 .baseMipLevel = 0,
950 .levelCount = 1,
951 .baseArrayLayer = 0,
952 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600953 .viewType = VK_IMAGE_VIEW_TYPE_2D,
954 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955 };
956
Ian Elliotta0781972015-08-21 15:09:33 -0600957 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600958
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600959 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600960
Karl Schultze4dc75c2016-02-02 15:37:51 -0700961 err = vkCreateImageView(demo->device, &color_image_view, NULL,
962 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600963 assert(!err);
964 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700965
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500966
Mark Young04fd3d82016-01-25 14:43:50 -0700967 if (NULL != presentModes) {
968 free(presentModes);
969 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600970}
971
Karl Schultze4dc75c2016-02-02 15:37:51 -0700972static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600973 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600974 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600975 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600976 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600977 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700979 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600980 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600981 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800982 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600983 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600984 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985 .flags = 0,
986 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200987
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600988 VkImageViewCreateInfo view = {
989 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800991 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600992 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700993 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
994 .baseMipLevel = 0,
995 .levelCount = 1,
996 .baseArrayLayer = 0,
997 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600999 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001000 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001001
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001002 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001003 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001004 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001005
1006 demo->depth.format = depth_format;
1007
1008 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001010 assert(!err);
1011
Karl Schultze4dc75c2016-02-02 15:37:51 -07001012 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001013 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001014
Chia-I Wuf48700b2015-11-10 16:21:09 +08001015 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001016 demo->depth.mem_alloc.pNext = NULL;
1017 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1018 demo->depth.mem_alloc.memoryTypeIndex = 0;
1019
Karl Schultze4dc75c2016-02-02 15:37:51 -07001020 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1021 0, /* No requirements */
1022 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001023 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001024
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001025 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001026 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1027 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001028 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001029
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001030 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001031 err =
1032 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001033 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034
Karl Schultze4dc75c2016-02-02 15:37:51 -07001035 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1036 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001037 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1038 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001039
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001040 /* create image view */
1041 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001042 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001043 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044}
1045
Tony Barbour1a86d032015-09-21 15:17:33 -06001046/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001048 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001049#ifdef __ANDROID__
1050#include <lunarg.ppm.h>
1051 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001052 cPtr = (char*)lunarg_ppm;
1053 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001054 return false;
1055 }
1056 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001057 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001058 if (rgba_data == NULL) {
1059 return true;
1060 }
1061 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001062 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001063 return false;
1064 }
1065 while(strncmp(cPtr++, "\n", 1));
1066
1067 for (int y = 0; y < *height; y++) {
1068 uint8_t *rowPtr = rgba_data;
1069 for (int x = 0; x < *width; x++) {
1070 memcpy(rowPtr, cPtr, 3);
1071 rowPtr[3] = 255; /* Alpha of 1 */
1072 rowPtr += 4;
1073 cPtr += 3;
1074 }
1075 rgba_data += layout->rowPitch;
1076 }
1077
1078 return true;
1079#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001080 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001081 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001082
Tony Barbour1a86d032015-09-21 15:17:33 -06001083 if (!fPtr)
1084 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001085
Tony Barbour1a86d032015-09-21 15:17:33 -06001086 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001087 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1088 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001089 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001090 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001091
Tony Barbour1a86d032015-09-21 15:17:33 -06001092 do {
1093 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001094 if (cPtr == NULL) {
1095 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001096 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001097 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001098 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001099
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001100 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001101 if (rgba_data == NULL) {
1102 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001103 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001104 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001105 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001106 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001107 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1108 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001109 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001110 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001111
Karl Schultze4dc75c2016-02-02 15:37:51 -07001112 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001113 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001114 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001115 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001116 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001117 rowPtr[3] = 255; /* Alpha of 1 */
1118 rowPtr += 4;
1119 }
1120 rgba_data += layout->rowPitch;
1121 }
1122 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001123 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001124#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001125}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001126
Karl Schultze4dc75c2016-02-02 15:37:51 -07001127static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001128 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001129 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001130 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001131 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001132 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001133 int32_t tex_width;
1134 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001135 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001136 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137
Karl Schultze4dc75c2016-02-02 15:37:51 -07001138 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001139 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001140 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001141
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001142 tex_obj->tex_width = tex_width;
1143 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001145 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001146 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001147 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001148 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001149 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001150 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001152 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001153 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001154 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001155 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001156 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001157 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001158 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001160 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001161
Karl Schultze4dc75c2016-02-02 15:37:51 -07001162 err =
1163 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164 assert(!err);
1165
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001166 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001167
Chia-I Wuf48700b2015-11-10 16:21:09 +08001168 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001169 tex_obj->mem_alloc.pNext = NULL;
1170 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1171 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172
Karl Schultze4dc75c2016-02-02 15:37:51 -07001173 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1174 required_props,
1175 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001176 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001177
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001178 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001179 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001180 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001181 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001183 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001184 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001185 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001186
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001187 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001188 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001189 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001191 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001192 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001193 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001194 void *data;
1195
Karl Schultze4dc75c2016-02-02 15:37:51 -07001196 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1197 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001198
Karl Schultze4dc75c2016-02-02 15:37:51 -07001199 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1200 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201 assert(!err);
1202
1203 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1204 fprintf(stderr, "Error loading texture: %s\n", filename);
1205 }
1206
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001207 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001209
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001210 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001211 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001212 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1213 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001214 /* setting the image layout does not reference the actual memory so no need
1215 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216}
1217
Karl Schultze4dc75c2016-02-02 15:37:51 -07001218static void demo_destroy_texture_image(struct demo *demo,
1219 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001220 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001221 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1222 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001223}
1224
Karl Schultze4dc75c2016-02-02 15:37:51 -07001225static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001226 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001227 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001228 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001230 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001233 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234
Karl Schultze4dc75c2016-02-02 15:37:51 -07001235 if ((props.linearTilingFeatures &
1236 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1237 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001239 demo_prepare_texture_image(
1240 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1241 VK_IMAGE_USAGE_SAMPLED_BIT,
1242 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1243 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001244 } else if (props.optimalTilingFeatures &
1245 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001246 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001247 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001248
1249 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001250 demo_prepare_texture_image(
1251 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1252 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1253 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1254 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001255
Karl Schultze4dc75c2016-02-02 15:37:51 -07001256 demo_prepare_texture_image(
1257 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1258 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1259 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001260
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001261 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001262 VK_IMAGE_ASPECT_COLOR_BIT,
1263 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001264 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1265 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001267 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001268 VK_IMAGE_ASPECT_COLOR_BIT,
1269 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001270 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1271 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001272
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001273 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001274 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1275 .srcOffset = {0, 0, 0},
1276 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1277 .dstOffset = {0, 0, 0},
1278 .extent = {staging_texture.tex_width,
1279 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001280 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001281 vkCmdCopyImage(
1282 demo->cmd, staging_texture.image,
1283 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1284 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001285
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001286 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001287 VK_IMAGE_ASPECT_COLOR_BIT,
1288 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001289 demo->textures[i].imageLayout,
1290 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001291
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001292 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001293
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001294 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001295 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001296 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1297 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001298 }
1299
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001300 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001301 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001303 .magFilter = VK_FILTER_NEAREST,
1304 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001305 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001306 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1307 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1308 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001309 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001310 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001311 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001312 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313 .minLod = 0.0f,
1314 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001315 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001316 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001318
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001319 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001320 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001321 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001322 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001323 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001324 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001325 .components =
1326 {
1327 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1328 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1329 },
1330 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001331 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001332 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001333
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001335 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001337 assert(!err);
1338
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001339 /* create image view */
1340 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001341 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001342 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001343 assert(!err);
1344 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346
Karl Schultze4dc75c2016-02-02 15:37:51 -07001347void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001348 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001349 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001350 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001351 int i;
1352 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001353 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001354 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001355 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001356
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001357 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001358 mat4x4_mul(MVP, VP, demo->model_matrix);
1359 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001360 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001361
Karl Schultze4dc75c2016-02-02 15:37:51 -07001362 for (i = 0; i < 12 * 3; i++) {
1363 data.position[i][0] = g_vertex_buffer_data[i * 3];
1364 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1365 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001366 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001367 data.attr[i][0] = g_uv_buffer_data[2 * i];
1368 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001369 data.attr[i][2] = 0;
1370 data.attr[i][3] = 0;
1371 }
1372
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001373 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001374 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001375 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001376 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001377 err =
1378 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001379 assert(!err);
1380
Karl Schultze4dc75c2016-02-02 15:37:51 -07001381 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1382 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001383
Chia-I Wuf48700b2015-11-10 16:21:09 +08001384 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001385 demo->uniform_data.mem_alloc.pNext = NULL;
1386 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1387 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1388
Karl Schultze4dc75c2016-02-02 15:37:51 -07001389 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001390 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1391 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001392 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001393 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001394
Karl Schultze4dc75c2016-02-02 15:37:51 -07001395 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1396 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001397 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001398
Karl Schultze4dc75c2016-02-02 15:37:51 -07001399 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1400 demo->uniform_data.mem_alloc.allocationSize, 0,
1401 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001402 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001403
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001404 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001405
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001406 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001407
Karl Schultze4dc75c2016-02-02 15:37:51 -07001408 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1409 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001410 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001411
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001412 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1413 demo->uniform_data.buffer_info.offset = 0;
1414 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001415}
1416
Karl Schultze4dc75c2016-02-02 15:37:51 -07001417static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001418 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001419 [0] =
1420 {
1421 .binding = 0,
1422 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1423 .descriptorCount = 1,
1424 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1425 .pImmutableSamplers = NULL,
1426 },
1427 [1] =
1428 {
1429 .binding = 1,
1430 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1431 .descriptorCount = DEMO_TEXTURE_COUNT,
1432 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1433 .pImmutableSamplers = NULL,
1434 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001435 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001436 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001437 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001438 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001439 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001440 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001441 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001442 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443
Karl Schultze4dc75c2016-02-02 15:37:51 -07001444 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1445 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001446 assert(!err);
1447
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001448 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001449 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1450 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001451 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001452 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001453 };
1454
Karl Schultze4dc75c2016-02-02 15:37:51 -07001455 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001456 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001458}
1459
Karl Schultze4dc75c2016-02-02 15:37:51 -07001460static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001461 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001462 [0] =
1463 {
1464 .format = demo->format,
1465 .samples = VK_SAMPLE_COUNT_1_BIT,
1466 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1467 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1468 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1469 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1470 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1471 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1472 },
1473 [1] =
1474 {
1475 .format = demo->depth.format,
1476 .samples = VK_SAMPLE_COUNT_1_BIT,
1477 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1478 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1479 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1480 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1481 .initialLayout =
1482 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1483 .finalLayout =
1484 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1485 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001486 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001487 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001488 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001489 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001490 const VkAttachmentReference depth_reference = {
1491 .attachment = 1,
1492 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1493 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001494 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001495 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1496 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001497 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001498 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001499 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001500 .pColorAttachments = &color_reference,
1501 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001502 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001503 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001504 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001505 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001506 const VkRenderPassCreateInfo rp_info = {
1507 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1508 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001509 .attachmentCount = 2,
1510 .pAttachments = attachments,
1511 .subpassCount = 1,
1512 .pSubpasses = &subpass,
1513 .dependencyCount = 0,
1514 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001515 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001516 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001517
Chia-I Wu63636062015-10-26 21:10:41 +08001518 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001519 assert(!err);
1520}
1521
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001522//TODO: Merge shader reading
1523#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001524static VkShaderModule
1525demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001526 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001527 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001528 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001529
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001530 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1531 moduleCreateInfo.pNext = NULL;
1532
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001533 moduleCreateInfo.codeSize = size;
1534 moduleCreateInfo.pCode = code;
1535 moduleCreateInfo.flags = 0;
1536 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1537 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001538
1539 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540}
1541
Karl Schultze4dc75c2016-02-02 15:37:51 -07001542char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001543 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001544 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001545 void *shader_code;
1546
1547 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001548 if (!fp)
1549 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001550
1551 fseek(fp, 0L, SEEK_END);
1552 size = ftell(fp);
1553
1554 fseek(fp, 0L, SEEK_SET);
1555
1556 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001557 retval = fread(shader_code, size, 1, fp);
1558 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001559
1560 *psize = size;
1561
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001562 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001563 return shader_code;
1564}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001565#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001566
Karl Schultze4dc75c2016-02-02 15:37:51 -07001567static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001568#ifdef __ANDROID__
1569 VkShaderModuleCreateInfo sh_info = {};
1570 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1571
1572#include "cube.vert.h"
1573 sh_info.codeSize = sizeof(cube_vert);
1574 sh_info.pCode = cube_vert;
1575 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1576 assert(!err);
1577#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001578 void *vertShaderCode;
1579 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001580
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001581 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1582
Karl Schultze4dc75c2016-02-02 15:37:51 -07001583 demo->vert_shader_module =
1584 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001585
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001586 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001587#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001588
1589 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001590}
1591
Karl Schultze4dc75c2016-02-02 15:37:51 -07001592static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001593#ifdef __ANDROID__
1594 VkShaderModuleCreateInfo sh_info = {};
1595 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1596
1597#include "cube.frag.h"
1598 sh_info.codeSize = sizeof(cube_frag);
1599 sh_info.pCode = cube_frag;
1600 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1601 assert(!err);
1602#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001603 void *fragShaderCode;
1604 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001605
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001606 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001607
Karl Schultze4dc75c2016-02-02 15:37:51 -07001608 demo->frag_shader_module =
1609 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001610
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001611 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001612#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001613
1614 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001615}
1616
Karl Schultze4dc75c2016-02-02 15:37:51 -07001617static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001618 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001619 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001620 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001621 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001622 VkPipelineRasterizationStateCreateInfo rs;
1623 VkPipelineColorBlendStateCreateInfo cb;
1624 VkPipelineDepthStencilStateCreateInfo ds;
1625 VkPipelineViewportStateCreateInfo vp;
1626 VkPipelineMultisampleStateCreateInfo ms;
1627 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1628 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001629 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630
Piers Daniellba839d12015-09-29 13:01:09 -06001631 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1632 memset(&dynamicState, 0, sizeof dynamicState);
1633 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1634 dynamicState.pDynamicStates = dynamicStateEnables;
1635
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001637 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001638 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001640 memset(&vi, 0, sizeof(vi));
1641 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1642
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001643 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001644 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001645 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001646
1647 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001648 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1649 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001650 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001651 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001652 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001653 rs.rasterizerDiscardEnable = VK_FALSE;
1654 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001655 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001656
1657 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001658 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1659 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001660 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001661 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001662 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001663 cb.attachmentCount = 1;
1664 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001665
Tony Barbour29645d02015-01-16 14:27:35 -07001666 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001667 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001668 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001669 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1670 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001671 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001672 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1673 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001674
1675 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001676 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001677 ds.depthTestEnable = VK_TRUE;
1678 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001679 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001680 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001681 ds.back.failOp = VK_STENCIL_OP_KEEP;
1682 ds.back.passOp = VK_STENCIL_OP_KEEP;
1683 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001684 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001685 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686
Tony Barbour29645d02015-01-16 14:27:35 -07001687 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001688 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001689 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001690 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001692 // Two stages: vs and fs
1693 pipeline.stageCount = 2;
1694 VkPipelineShaderStageCreateInfo shaderStages[2];
1695 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1696
Karl Schultze4dc75c2016-02-02 15:37:51 -07001697 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1698 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001699 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001700 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001701
Karl Schultze4dc75c2016-02-02 15:37:51 -07001702 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1703 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001704 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001705 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001706
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001707 memset(&pipelineCache, 0, sizeof(pipelineCache));
1708 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709
Karl Schultze4dc75c2016-02-02 15:37:51 -07001710 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1711 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001712 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001713
Karl Schultze4dc75c2016-02-02 15:37:51 -07001714 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001715 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001716 pipeline.pRasterizationState = &rs;
1717 pipeline.pColorBlendState = &cb;
1718 pipeline.pMultisampleState = &ms;
1719 pipeline.pViewportState = &vp;
1720 pipeline.pDepthStencilState = &ds;
1721 pipeline.pStages = shaderStages;
1722 pipeline.renderPass = demo->render_pass;
1723 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001724
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001725 pipeline.renderPass = demo->render_pass;
1726
Karl Schultze4dc75c2016-02-02 15:37:51 -07001727 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1728 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729 assert(!err);
1730
Chia-I Wu63636062015-10-26 21:10:41 +08001731 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1732 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733}
1734
Karl Schultze4dc75c2016-02-02 15:37:51 -07001735static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001736 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001737 [0] =
1738 {
1739 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1740 .descriptorCount = 1,
1741 },
1742 [1] =
1743 {
1744 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1745 .descriptorCount = DEMO_TEXTURE_COUNT,
1746 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001747 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001748 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001749 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001750 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001751 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001752 .poolSizeCount = 2,
1753 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001754 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001755 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001756
Karl Schultze4dc75c2016-02-02 15:37:51 -07001757 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1758 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001759 assert(!err);
1760}
1761
Karl Schultze4dc75c2016-02-02 15:37:51 -07001762static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001763 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001764 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001765 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001766 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001767
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001768 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001769 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001770 .pNext = NULL,
1771 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001772 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001773 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001774 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001775 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001776
Chia-I Wuae721ba2015-05-25 16:27:55 +08001777 memset(&tex_descs, 0, sizeof(tex_descs));
1778 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001779 tex_descs[i].sampler = demo->textures[i].sampler;
1780 tex_descs[i].imageView = demo->textures[i].view;
1781 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001782 }
1783
1784 memset(&writes, 0, sizeof(writes));
1785
1786 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001787 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001788 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001789 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001790 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001791
1792 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001793 writes[1].dstSet = demo->desc_set;
1794 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001795 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001796 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001797 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001798
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001799 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001800}
1801
Karl Schultze4dc75c2016-02-02 15:37:51 -07001802static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001803 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001804 attachments[1] = demo->depth.view;
1805
Chia-I Wuf973e322015-07-08 13:34:24 +08001806 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001807 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1808 .pNext = NULL,
1809 .renderPass = demo->render_pass,
1810 .attachmentCount = 2,
1811 .pAttachments = attachments,
1812 .width = demo->width,
1813 .height = demo->height,
1814 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001815 };
1816 VkResult U_ASSERT_ONLY err;
1817 uint32_t i;
1818
Karl Schultze4dc75c2016-02-02 15:37:51 -07001819 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1820 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001821 assert(demo->framebuffers);
1822
1823 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001824 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001825 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1826 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001827 assert(!err);
1828 }
1829}
1830
Karl Schultze4dc75c2016-02-02 15:37:51 -07001831static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001832 VkResult U_ASSERT_ONLY err;
1833
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001834 const VkCommandPoolCreateInfo cmd_pool_info = {
1835 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001836 .pNext = NULL,
1837 .queueFamilyIndex = demo->graphics_queue_node_index,
1838 .flags = 0,
1839 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001840 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1841 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001842 assert(!err);
1843
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001844 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001845 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001846 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001847 .commandPool = demo->cmd_pool,
1848 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001849 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001850 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851
1852 demo_prepare_buffers(demo);
1853 demo_prepare_depth(demo);
1854 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001855 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001857 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001858 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001859 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001860
Ian Elliotta0781972015-08-21 15:09:33 -06001861 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001862 err =
1863 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001864 assert(!err);
1865 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001866
Chia-I Wu63ea9262015-03-26 13:14:16 +08001867 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001868 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001869
Chia-I Wuf973e322015-07-08 13:34:24 +08001870 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001871
Ian Elliotta0781972015-08-21 15:09:33 -06001872 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001873 demo->current_buffer = i;
1874 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1875 }
1876
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001877 /*
1878 * Prepare functions above may generate pipeline commands
1879 * that need to be flushed before beginning the render loop.
1880 */
1881 demo_flush_init_cmd(demo);
1882
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001883 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001884 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001885}
1886
Karl Schultze4dc75c2016-02-02 15:37:51 -07001887static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001888 uint32_t i;
1889
1890 demo->prepared = false;
1891
Tony Barbourd8e504f2015-10-08 14:26:24 -06001892 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001893 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001894 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001895 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001896 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001897
Chia-I Wu63636062015-10-26 21:10:41 +08001898 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1899 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1900 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1901 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1902 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001903
1904 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001905 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1906 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1907 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1908 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001909 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001910 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001911
Chia-I Wu63636062015-10-26 21:10:41 +08001912 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1913 vkDestroyImage(demo->device, demo->depth.image, NULL);
1914 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001915
Chia-I Wu63636062015-10-26 21:10:41 +08001916 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1917 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001918
Ian Elliotta0781972015-08-21 15:09:33 -06001919 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001920 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001921 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1922 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001923 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001924 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001925
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001926 free(demo->queue_props);
1927
Chia-I Wu63636062015-10-26 21:10:41 +08001928 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1929 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001930 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001931 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001932 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001933 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001934 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001935
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001936#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001937 if (demo->use_xlib) {
1938 XDestroyWindow(demo->display, demo->xlib_window);
1939 XCloseDisplay(demo->display);
1940 } else {
1941 xcb_destroy_window(demo->connection, demo->xcb_window);
1942 xcb_disconnect(demo->connection);
1943 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001944 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001945#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001946 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001947 xcb_disconnect(demo->connection);
1948 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001949#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
1950 wl_shell_surface_destroy(demo->shell_surface);
1951 wl_surface_destroy(demo->window);
1952 wl_shell_destroy(demo->shell);
1953 wl_compositor_destroy(demo->compositor);
1954 wl_registry_destroy(demo->registry);
1955 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001956#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001957}
1958
Karl Schultze4dc75c2016-02-02 15:37:51 -07001959static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001960 uint32_t i;
1961
Mike Stroyanbb60b382015-10-28 09:46:57 -06001962 // Don't react to resize until after first initialization.
1963 if (!demo->prepared) {
1964 return;
1965 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001966 // In order to properly resize the window, we must re-create the swapchain
1967 // AND redo the command buffers, etc.
1968 //
1969 // First, perform part of the demo_cleanup() function:
1970 demo->prepared = false;
1971
1972 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001973 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001974 }
1975 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001976 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001977
Chia-I Wu63636062015-10-26 21:10:41 +08001978 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1979 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1980 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1981 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1982 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001983
1984 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001985 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1986 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1987 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1988 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001989 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001990
Chia-I Wu63636062015-10-26 21:10:41 +08001991 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1992 vkDestroyImage(demo->device, demo->depth.image, NULL);
1993 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001994
Chia-I Wu63636062015-10-26 21:10:41 +08001995 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1996 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001997
1998 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001999 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002000 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2001 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002002 }
Chia-I Wu63636062015-10-26 21:10:41 +08002003 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002004 free(demo->buffers);
2005
Ian Elliottcf074d32015-10-16 18:02:43 -06002006 // Second, re-perform the demo_prepare() function, which will re-create the
2007 // swapchain:
2008 demo_prepare(demo);
2009}
2010
David Pinedo2bb7c932015-06-18 17:03:14 -06002011// On MS-Windows, make this a global, so it's available to WndProc()
2012struct demo demo;
2013
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002014#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002015static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002016 if (!demo->prepared)
2017 return;
Ian Elliott639ca472015-04-16 15:23:05 -06002018 // Wait for work to finish before updating MVP.
2019 vkDeviceWaitIdle(demo->device);
2020 demo_update_data_buffer(demo);
2021
2022 demo_draw(demo);
2023
2024 // Wait for work to finish before updating MVP.
2025 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06002026
David Pinedo2bb7c932015-06-18 17:03:14 -06002027 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002028 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002029 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002030 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002031}
Ian Elliott639ca472015-04-16 15:23:05 -06002032
2033// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002034LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2035 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002036 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002037 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002038 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002039 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002040 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002041 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002042 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002043 // Resize the application to the new window size, except when
2044 // it was minimized. Vulkan doesn't support images or swapchains
2045 // with width=0 and height=0.
2046 if (wParam != SIZE_MINIMIZED) {
2047 demo.width = lParam & 0xffff;
2048 demo.height = lParam & 0xffff0000 >> 16;
2049 demo_resize(&demo);
2050 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002051 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002052 default:
2053 break;
2054 }
2055 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2056}
2057
Karl Schultze4dc75c2016-02-02 15:37:51 -07002058static void demo_create_window(struct demo *demo) {
2059 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002060
2061 // Initialize the window class structure:
2062 win_class.cbSize = sizeof(WNDCLASSEX);
2063 win_class.style = CS_HREDRAW | CS_VREDRAW;
2064 win_class.lpfnWndProc = WndProc;
2065 win_class.cbClsExtra = 0;
2066 win_class.cbWndExtra = 0;
2067 win_class.hInstance = demo->connection; // hInstance
2068 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2069 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2070 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2071 win_class.lpszMenuName = NULL;
2072 win_class.lpszClassName = demo->name;
2073 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2074 // Register window class:
2075 if (!RegisterClassEx(&win_class)) {
2076 // It didn't work, so try to give a useful error:
2077 printf("Unexpected error trying to start the application!\n");
2078 fflush(stdout);
2079 exit(1);
2080 }
2081 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002082 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002083 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002084 demo->window = CreateWindowEx(0,
2085 demo->name, // class name
2086 demo->name, // app name
2087 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002088 WS_VISIBLE | WS_SYSMENU,
2089 100, 100, // x/y coords
2090 wr.right - wr.left, // width
2091 wr.bottom - wr.top, // height
2092 NULL, // handle to parent
2093 NULL, // handle to menu
2094 demo->connection, // hInstance
2095 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002096 if (!demo->window) {
2097 // It didn't work, so try to give a useful error:
2098 printf("Cannot create a window in which to draw!\n");
2099 fflush(stdout);
2100 exit(1);
2101 }
2102}
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 Lobodzinskiaaab5c02016-03-17 15:08:18 -06002425 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Karl Schultz635272d2016-03-07 17:54:07 -07002426 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis1398c712016-03-09 16:12:48 -07002427 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
Karl Schultz7c50ad62016-04-01 12:07:03 -06002428 "VK_LAYER_LUNARG_swapchain", "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 Barbourda2bad52016-01-22 14:36:40 -07002815 .queueFamilyIndex = demo->graphics_queue_node_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++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002910 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2911 if (graphicsQueueNodeIndex == UINT32_MAX) {
2912 graphicsQueueNodeIndex = i;
2913 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002914
Ian Elliottaaae5352015-07-06 14:27:58 -06002915 if (supportsPresent[i] == VK_TRUE) {
2916 graphicsQueueNodeIndex = i;
2917 presentQueueNodeIndex = i;
2918 break;
2919 }
2920 }
2921 }
2922 if (presentQueueNodeIndex == UINT32_MAX) {
2923 // If didn't find a queue that supports both graphics and present, then
2924 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002925 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002926 if (supportsPresent[i] == VK_TRUE) {
2927 presentQueueNodeIndex = i;
2928 break;
2929 }
2930 }
2931 }
2932 free(supportsPresent);
2933
2934 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002935 if (graphicsQueueNodeIndex == UINT32_MAX ||
2936 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002937 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002938 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002939 }
2940
2941 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002942 // synchronization, and appropriate tracking for QueueSubmit.
2943 // NOTE: While it is possible for an application to use a separate graphics
2944 // and a present queues, this demo program assumes it is only using
2945 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002946 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2947 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002948 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002949 }
2950
2951 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002952
Tony Barbourda2bad52016-01-22 14:36:40 -07002953 demo_create_device(demo);
2954
2955 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2956 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2957 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2958 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2959 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2960
Karl Schultze4dc75c2016-02-02 15:37:51 -07002961 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2962 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002963
Ian Elliottaaae5352015-07-06 14:27:58 -06002964 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002965 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002966 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002967 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002968 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002969 VkSurfaceFormatKHR *surfFormats =
2970 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002971 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002972 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002973 assert(!err);
2974 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2975 // the surface has no preferred format. Otherwise, at least one
2976 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002977 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002978 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002979 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002980 assert(formatCount >= 1);
2981 demo->format = surfFormats[0].format;
2982 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002983 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002984
David Pinedo2bb7c932015-06-18 17:03:14 -06002985 demo->quit = false;
2986 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002987
2988 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002989 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002990}
2991
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002992#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2993static void registry_handle_global(void *data, struct wl_registry *registry,
2994 uint32_t name, const char *interface,
2995 uint32_t version UNUSED) {
2996 struct demo *demo = data;
2997 if (strcmp(interface, "wl_compositor") == 0) {
2998 demo->compositor =
2999 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3000 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3001 * tp xdg_shell */
3002 } else if (strcmp(interface, "wl_shell") == 0) {
3003 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3004 }
3005}
3006
3007static void registry_handle_global_remove(void *data UNUSED,
3008 struct wl_registry *registry UNUSED,
3009 uint32_t name UNUSED) {}
3010
3011static const struct wl_registry_listener registry_listener = {
3012 registry_handle_global, registry_handle_global_remove};
3013#endif
3014
Karl Schultze4dc75c2016-02-02 15:37:51 -07003015static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003016#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003017 const xcb_setup_t *setup;
3018 xcb_screen_iterator_t iter;
3019 int scr;
3020
3021 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06003022 if (demo->connection == NULL) {
3023 printf("Cannot find a compatible Vulkan installable client driver "
3024 "(ICD).\nExiting ...\n");
3025 fflush(stdout);
3026 exit(1);
3027 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003028
3029 setup = xcb_get_setup(demo->connection);
3030 iter = xcb_setup_roots_iterator(setup);
3031 while (scr-- > 0)
3032 xcb_screen_next(&iter);
3033
3034 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003035#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3036 demo->display = wl_display_connect(NULL);
3037
3038 if (demo->display == NULL) {
3039 printf("Cannot find a compatible Vulkan installable client driver "
3040 "(ICD).\nExiting ...\n");
3041 fflush(stdout);
3042 exit(1);
3043 }
3044
3045 demo->registry = wl_display_get_registry(demo->display);
3046 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3047 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003048#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003049}
3050
Karl Schultze4dc75c2016-02-02 15:37:51 -07003051static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003052 vec3 eye = {0.0f, 3.0f, 5.0f};
3053 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003054 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003055
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003056 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003057 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003058
Piers Daniell735ee532015-02-23 16:23:13 -07003059 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003060 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003061 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003062 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003063 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003064 if (strcmp(argv[i], "--break") == 0) {
3065 demo->use_break = true;
3066 continue;
3067 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003068 if (strcmp(argv[i], "--validate") == 0) {
3069 demo->validate = true;
3070 continue;
3071 }
Tony Barbour2593b182016-04-19 10:57:58 -06003072 if (strcmp(argv[i], "--xlib") == 0) {
3073 demo->use_xlib = true;
3074 continue;
3075 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003076 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3077 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3078 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003079 i++;
3080 continue;
3081 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003082 if (strcmp(argv[i], "--suppress_popups") == 0) {
3083 demo->suppress_popups = true;
3084 continue;
3085 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003086
Karl Schultze4dc75c2016-02-02 15:37:51 -07003087 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
lenny-lunargfbe22392016-06-06 11:07:53 -06003088 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003089 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003090 fflush(stderr);
3091 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003092 }
3093
Tony Barbour2593b182016-04-19 10:57:58 -06003094 if (!demo->use_xlib)
3095 demo_init_connection(demo);
3096
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003097 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003098
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003099 demo->width = 500;
3100 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003101
3102 demo->spin_angle = 0.01f;
3103 demo->spin_increment = 0.01f;
3104 demo->pause = false;
3105
Karl Schultze4dc75c2016-02-02 15:37:51 -07003106 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3107 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003108 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3109 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003110}
3111
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003112#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003113// Include header required for parsing the command line options.
3114#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003115
Karl Schultze4dc75c2016-02-02 15:37:51 -07003116int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3117 int nCmdShow) {
3118 MSG msg; // message
3119 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003120 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003121 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003122
Karl Schultze4dc75c2016-02-02 15:37:51 -07003123 // Use the CommandLine functions to get the command line arguments.
3124 // Unfortunately, Microsoft outputs
3125 // this information as wide characters for Unicode, and we simply want the
3126 // Ascii version to be compatible
3127 // with the non-Windows side. So, we have to convert the information to
3128 // Ascii character strings.
3129 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3130 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003131 argc = 0;
3132 }
3133
Karl Schultze4dc75c2016-02-02 15:37:51 -07003134 if (argc > 0) {
3135 argv = (char **)malloc(sizeof(char *) * argc);
3136 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003137 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003138 } else {
3139 for (int iii = 0; iii < argc; iii++) {
3140 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003141 size_t numConverted = 0;
3142
Karl Schultze4dc75c2016-02-02 15:37:51 -07003143 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3144 if (argv[iii] != NULL) {
3145 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3146 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003147 }
3148 }
3149 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003150 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003151 argv = NULL;
3152 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003153
3154 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003155
3156 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003157 if (argc > 0 && argv != NULL) {
3158 for (int iii = 0; iii < argc; iii++) {
3159 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003160 free(argv[iii]);
3161 }
3162 }
3163 free(argv);
3164 }
3165
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003166 demo.connection = hInstance;
3167 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003168 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003169 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003170
3171 demo_prepare(&demo);
3172
Karl Schultze4dc75c2016-02-02 15:37:51 -07003173 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003174
3175 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003176 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003177 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003178 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003179 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003180 done = true; // if found, quit app
3181 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003182 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003183 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003184 DispatchMessage(&msg);
3185 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003186 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003187 }
3188
3189 demo_cleanup(&demo);
3190
Karl Schultze4dc75c2016-02-02 15:37:51 -07003191 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003192}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003193#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3194#include <android/log.h>
3195#include <android_native_app_glue.h>
3196static bool initialized = false;
3197static bool active = false;
3198struct demo demo;
3199
3200static int32_t processInput(struct android_app* app, AInputEvent* event) {
3201 return 0;
3202}
3203
3204static void processCommand(struct android_app* app, int32_t cmd) {
3205 switch(cmd) {
3206 case APP_CMD_INIT_WINDOW: {
3207 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003208 // We're getting a new window. If the app is starting up, we
3209 // need to initialize. If the app has already been
3210 // initialized, that means that we lost our previous window,
3211 // which means that we have a lot of work to do. At a minimum,
3212 // we need to destroy the swapchain and surface associated with
3213 // the old window, and create a new surface and swapchain.
3214 // However, since there are a lot of other objects/state that
3215 // is tied to the swapchain, it's easiest to simply cleanup and
3216 // start over (i.e. use a brute-force approach of re-starting
3217 // the app)
3218 if (demo.prepared) {
3219 demo_cleanup(&demo);
3220 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003221 demo_init(&demo, 0, NULL);
3222 demo.window = (void*)app->window;
3223 demo_init_vk_swapchain(&demo);
3224 demo_prepare(&demo);
3225 initialized = true;
3226 }
3227 break;
3228 }
3229 case APP_CMD_GAINED_FOCUS: {
3230 active = true;
3231 break;
3232 }
3233 case APP_CMD_LOST_FOCUS: {
3234 active = false;
3235 break;
3236 }
3237 }
3238}
3239
3240void android_main(struct android_app *app)
3241{
3242 app_dummy();
3243
Cody Northrop8707a6e2016-04-26 19:59:19 -06003244#ifdef ANDROID
3245 int vulkanSupport = InitVulkan();
3246 if (vulkanSupport == 0)
3247 return;
3248#endif
3249
Ian Elliottf05d5d12016-05-17 12:03:35 -06003250 demo.prepared = false;
3251
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003252 app->onAppCmd = processCommand;
3253 app->onInputEvent = processInput;
3254
3255 while(1) {
3256 int events;
3257 struct android_poll_source* source;
3258 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3259 if (source) {
3260 source->process(app, source);
3261 }
3262
3263 if (app->destroyRequested != 0) {
3264 demo_cleanup(&demo);
3265 return;
3266 }
3267 }
3268 if (initialized && active) {
3269 demo_run(&demo);
3270 }
3271 }
3272
3273}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003274#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003275int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003276 struct demo demo;
3277
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003278 demo_init(&demo, argc, argv);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003279#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003280 if (demo.use_xlib)
3281 demo_create_xlib_window(&demo);
3282 else
3283 demo_create_xcb_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003284#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3285 demo_create_window(&demo);
3286#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003287
Tony Barbourcc69f452015-10-08 13:59:42 -06003288 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003289
3290 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003291
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003292#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003293 if (demo.use_xlib)
3294 demo_run_xlib(&demo);
3295 else
3296 demo_run_xcb(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003297#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3298 demo_run(&demo);
3299#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003300
3301 demo_cleanup(&demo);
3302
Karl Schultz0218c002016-03-22 17:06:13 -06003303 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003304}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003305#endif