blob: b4f3cdcc777f9a2a04a588de6f9545744ac9f57f [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
2* Copyright (c) 2015-2016 The Khronos Group Inc.
3* Copyright (c) 2015-2016 Valve Corporation
4* Copyright (c) 2015-2016 LunarG, Inc.
5*
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Author: Chia-I Wu <olv@lunarg.com>
19* Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20* Author: Ian Elliott <ian@LunarG.com>
21* Author: Jon Ashburn <jon@lunarg.com>
22* Author: Gwan-gyeong Mun <elongbug@gmail.com>
23*/
Karl Schultze4dc75c2016-02-02 15:37:51 -070024
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060025#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdbool.h>
30#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070031#include <signal.h>
Tony Barbour2593b182016-04-19 10:57:58 -060032#ifdef __linux__
33#include <X11/Xutil.h>
34#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060035
Ian Elliott639ca472015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060038#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060039#endif // _WIN32
40
Cody Northrop8707a6e2016-04-26 19:59:19 -060041#ifdef ANDROID
42#include "vulkan_wrapper.h"
43#else
David Pinedoc5193c12015-11-06 12:54:48 -070044#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060045#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070046
David Pinedo541526f2015-11-24 09:00:24 -070047#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048#include "linmath.h"
49
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090062#if defined(__GNUC__)
63#define UNUSED __attribute__((unused))
64#else
65#define UNUSED
66#endif
67
Ian Elliott07264132015-04-28 11:35:02 -060068#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070069#define ERR_EXIT(err_msg, err_class) \
70 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060071 if (!demo->suppress_popups) \
72 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070073 exit(1); \
74 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060075
Michael Lentinec6cde4b2016-04-18 13:20:45 -050076#elif defined __ANDROID__
77#include <android/log.h>
78#define ERR_EXIT(err_msg, err_class) \
79 do { \
80 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
81 exit(1); \
82 } while (0)
83#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070084#define ERR_EXIT(err_msg, err_class) \
85 do { \
86 printf(err_msg); \
87 fflush(stdout); \
88 exit(1); \
89 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050090#endif
Ian Elliott07264132015-04-28 11:35:02 -060091
Karl Schultze4dc75c2016-02-02 15:37:51 -070092#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
93 { \
94 demo->fp##entrypoint = \
95 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
96 if (demo->fp##entrypoint == NULL) { \
97 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
98 "vkGetInstanceProcAddr Failure"); \
99 } \
100 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600101
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600102static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
103
Karl Schultze4dc75c2016-02-02 15:37:51 -0700104#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
105 { \
106 if (!g_gdpa) \
107 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
108 demo->inst, "vkGetDeviceProcAddr"); \
109 demo->fp##entrypoint = \
110 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
111 if (demo->fp##entrypoint == NULL) { \
112 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
113 "vkGetDeviceProcAddr Failure"); \
114 } \
115 }
Ian Elliott673898b2015-06-22 15:07:49 -0600116
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600117/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700118 * structure to track all objects related to a texture.
119 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600120struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600121 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700122
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600123 VkImage image;
124 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600125
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800126 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500127 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600128 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700129 int32_t tex_width, tex_height;
130};
131
Karl Schultze4dc75c2016-02-02 15:37:51 -0700132static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600133
Karl Schultz0218c002016-03-22 17:06:13 -0600134static int validation_error = 0;
135
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600136struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600137 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700138 float mvp[4][4];
139 float position[12 * 3][4];
140 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600141};
142
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600143struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600144 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700145 float mvp[4][4];
146 float position[12 * 3][4];
147 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600148};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600149
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150//--------------------------------------------------------------------------------------
151// Mesh and VertexFormat Data
152//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700153// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600154struct Vertex
155{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600156 float posX, posY, posZ, posW; // Position data
157 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600158};
159
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600160struct VertexPosTex
161{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600162 float posX, posY, posZ, posW; // Position data
163 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600164};
165
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600166#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600167#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600168
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600169static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f,-1.0f, 1.0f,
172 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 -1.0f, 1.0f,-1.0f,
175 -1.0f,-1.0f,-1.0f,
176
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 1.0f,-1.0f,
179 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600182 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600189 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 -1.0f, 1.0f, 1.0f,
193 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800194 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600195 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600196 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600197
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 1.0f, 1.0f, 1.0f,
200 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800201 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600202 1.0f,-1.0f,-1.0f,
203 1.0f, 1.0f,-1.0f,
204
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800205 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600206 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600207 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600209 1.0f,-1.0f, 1.0f,
210 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600211};
212
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600213static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800214 0.0f, 0.0f, // -X side
215 1.0f, 0.0f,
216 1.0f, 1.0f,
217 1.0f, 1.0f,
218 0.0f, 1.0f,
219 0.0f, 0.0f,
220
221 1.0f, 0.0f, // -Z side
222 0.0f, 1.0f,
223 0.0f, 0.0f,
224 1.0f, 0.0f,
225 1.0f, 1.0f,
226 0.0f, 1.0f,
227
228 1.0f, 1.0f, // -Y side
229 1.0f, 0.0f,
230 0.0f, 0.0f,
231 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 0.0f, 0.0f,
233 0.0f, 1.0f,
234
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600236 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600238 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600239 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600240 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600241
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800242 1.0f, 1.0f, // +X side
243 0.0f, 1.0f,
244 0.0f, 0.0f,
245 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600246 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600247 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600248
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800249 0.0f, 1.0f, // +Z side
250 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600251 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600252 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800253 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600254 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600255};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700256// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600257
Karl Schultze4dc75c2016-02-02 15:37:51 -0700258void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600259 int i;
260
261 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700262 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600263 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
264 }
265 printf("\n");
266 fflush(stdout);
267}
268
Karl Schultze4dc75c2016-02-02 15:37:51 -0700269void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600270 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700271 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600272 printf("\n");
273 fflush(stdout);
274}
275
Karl Schultze4dc75c2016-02-02 15:37:51 -0700276VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600277BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
278 uint64_t srcObject, size_t location, int32_t msgCode,
279 const char *pLayerPrefix, const char *pMsg,
280 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700281#ifndef WIN32
282 raise(SIGTRAP);
283#else
284 DebugBreak();
285#endif
286
287 return false;
288}
289
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600290typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600291 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800292 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600293 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600294} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600295
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600296struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500297#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600298#define APP_NAME_STR_LEN 80
299 HINSTANCE connection; // hInstance - Windows Instance
300 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700301 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600302 POINT minsize; // minimum window size
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600303#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600304 Display* display;
305 Window xlib_window;
306 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600307
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600308 xcb_connection_t *connection;
309 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600310 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800311 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900312#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
313 struct wl_display *display;
314 struct wl_registry *registry;
315 struct wl_compositor *compositor;
316 struct wl_surface *window;
317 struct wl_shell *shell;
318 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500319#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
320 ANativeWindow* window;
321#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700322 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600323 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700324 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600325 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600328 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600329 VkDevice device;
330 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700331 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600332 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600333 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600334 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
Tony Barbourda2bad52016-01-22 14:36:40 -0700336 uint32_t enabled_extension_count;
337 uint32_t enabled_layer_count;
338 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600339 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700340
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600342 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600343 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344
Karl Schultze4dc75c2016-02-02 15:37:51 -0700345 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
346 fpGetPhysicalDeviceSurfaceSupportKHR;
347 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
348 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
349 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
350 fpGetPhysicalDeviceSurfaceFormatsKHR;
351 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
352 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600353 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
354 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
355 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
356 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
357 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600358 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600359 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600360 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800362 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
364 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600365 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800368 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500369 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600370 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371 } depth;
372
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
375 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800377 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500378 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600379 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600380 } uniform_data;
381
Karl Schultze4dc75c2016-02-02 15:37:51 -0700382 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500383 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600385 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800386 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600389 mat4x4 projection_matrix;
390 mat4x4 view_matrix;
391 mat4x4 model_matrix;
392
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600393 float spin_angle;
394 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600395 bool pause;
396
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600397 VkShaderModule vert_shader_module;
398 VkShaderModule frag_shader_module;
399
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkDescriptorPool desc_pool;
401 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800402
Tony Barbourd8e504f2015-10-08 14:26:24 -0600403 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800404
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600405 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600406 int32_t curFrame;
407 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600408 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600409 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600410 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700411 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
412 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
413 VkDebugReportCallbackEXT msg_callback;
414 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600415
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600416 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600417 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600418};
419
lenny-lunargfbe22392016-06-06 11:07:53 -0600420VKAPI_ATTR VkBool32 VKAPI_CALL
421dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
422 uint64_t srcObject, size_t location, int32_t msgCode,
423 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
424 char *message = (char *)malloc(strlen(pMsg) + 100);
425
426 assert(message);
427
428 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
429 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
430 pMsg);
431 validation_error = 1;
432 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
433 // We know that we're submitting queues without fences, ignore this
434 // warning
435 if (strstr(pMsg,
436 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
437 return false;
438 }
439 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
440 pMsg);
441 validation_error = 1;
442 } else {
443 validation_error = 1;
444 return false;
445 }
446
447#ifdef _WIN32
448 struct demo *demo = (struct demo*) pUserData;
449 if (!demo->suppress_popups)
450 MessageBox(NULL, message, "Alert", MB_OK);
451#else
452 printf("%s\n", message);
453 fflush(stdout);
454#endif
455 free(message);
456
457 /*
458 * false indicates that layer should not bail-out of an
459 * API call that had validation failures. This may mean that the
460 * app dies inside the driver due to invalid parameter(s).
461 * That's what would happen without validation layers, so we'll
462 * keep that behavior here.
463 */
464 return false;
465}
466
Ian Elliottcf074d32015-10-16 18:02:43 -0600467// Forward declaration:
468static void demo_resize(struct demo *demo);
469
Karl Schultze4dc75c2016-02-02 15:37:51 -0700470static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
471 VkFlags requirements_mask,
472 uint32_t *typeIndex) {
473 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600474 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700475 if ((typeBits & 1) == 1) {
476 // Type is available, does it match user properties?
477 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
478 requirements_mask) == requirements_mask) {
479 *typeIndex = i;
480 return true;
481 }
482 }
483 typeBits >>= 1;
484 }
485 // No memory types matched, return failure
486 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600487}
488
Karl Schultze4dc75c2016-02-02 15:37:51 -0700489static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600490 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600492 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600493 return;
494
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600495 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 assert(!err);
497
Karl Schultze4dc75c2016-02-02 15:37:51 -0700498 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800499 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700500 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
501 .pNext = NULL,
502 .waitSemaphoreCount = 0,
503 .pWaitSemaphores = NULL,
504 .pWaitDstStageMask = NULL,
505 .commandBufferCount = 1,
506 .pCommandBuffers = cmd_bufs,
507 .signalSemaphoreCount = 0,
508 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600510 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511 assert(!err);
512
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600513 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600514 assert(!err);
515
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600516 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518}
519
Karl Schultze4dc75c2016-02-02 15:37:51 -0700520static void demo_set_image_layout(struct demo *demo, VkImage image,
521 VkImageAspectFlags aspectMask,
522 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700523 VkImageLayout new_image_layout,
524 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600525 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800528 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800529 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800531 .commandPool = demo->cmd_pool,
532 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700533 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600534 };
535
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800536 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600537 assert(!err);
538
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700539 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
540 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600541 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800542 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600543 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800544 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800545 .occlusionQueryEnable = VK_FALSE,
546 .queryFlags = 0,
547 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600548 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700549 VkCommandBufferBeginInfo cmd_buf_info = {
550 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
551 .pNext = NULL,
552 .flags = 0,
553 .pInheritanceInfo = &cmd_buf_hinfo,
554 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600555 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600556 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557 }
558
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600559 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600560 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600561 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700562 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800563 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600564 .oldLayout = old_image_layout,
565 .newLayout = new_image_layout,
566 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700567 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600568
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800569 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600570 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800571 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600572 }
573
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700574 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700575 image_memory_barrier.dstAccessMask =
576 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700577 }
578
579 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700580 image_memory_barrier.dstAccessMask =
581 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700582 }
583
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600584 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700586 image_memory_barrier.dstAccessMask =
587 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600588 }
589
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600590 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600591
Tony Barbour50bbca42015-06-29 16:20:35 -0600592 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
593 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600594
Karl Schultze4dc75c2016-02-02 15:37:51 -0700595 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
596 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600597}
598
Karl Schultze4dc75c2016-02-02 15:37:51 -0700599static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700600 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
601 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600602 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800603 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600604 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800605 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800606 .occlusionQueryEnable = VK_FALSE,
607 .queryFlags = 0,
608 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700609 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700610 const VkCommandBufferBeginInfo cmd_buf_info = {
611 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
612 .pNext = NULL,
613 .flags = 0,
614 .pInheritanceInfo = &cmd_buf_hinfo,
615 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800616 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700617 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
618 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800619 };
620 const VkRenderPassBeginInfo rp_begin = {
621 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
622 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800623 .renderPass = demo->render_pass,
624 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800625 .renderArea.offset.x = 0,
626 .renderArea.offset.y = 0,
627 .renderArea.extent.width = demo->width,
628 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600629 .clearValueCount = 2,
630 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700631 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800632 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600633
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600634 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600635 assert(!err);
636
Tony Barbour5c5b1632016-04-26 11:13:48 -0600637 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
638 // happens to the previous contents of the image
639 VkImageMemoryBarrier image_memory_barrier = {
640 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
641 .pNext = NULL,
642 .srcAccessMask = 0,
643 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
644 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
645 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
646 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
647 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
648 .image = demo->buffers[demo->current_buffer].image,
649 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
650
651 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
652 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
653 NULL, 1, &image_memory_barrier);
654
Chia-I Wuf051d262015-10-27 19:25:11 +0800655 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800656
Karl Schultze4dc75c2016-02-02 15:37:51 -0700657 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
658 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
659 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
660 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600661 VkViewport viewport;
662 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700663 viewport.height = (float)demo->height;
664 viewport.width = (float)demo->width;
665 viewport.minDepth = (float)0.0f;
666 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700667 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600668
669 VkRect2D scissor;
670 memset(&scissor, 0, sizeof(scissor));
671 scissor.extent.width = demo->width;
672 scissor.extent.height = demo->height;
673 scissor.offset.x = 0;
674 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700675 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600676 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800677 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600678
Tony Barbour15a4ef62015-10-21 10:14:48 -0600679 VkImageMemoryBarrier prePresentBarrier = {
680 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
681 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800682 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700683 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600684 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700685 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600686 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800687 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700688 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600689
690 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
691 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700692 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
693 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
694 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600695
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600696 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697 assert(!err);
698}
699
Karl Schultze4dc75c2016-02-02 15:37:51 -0700700void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600701 mat4x4 MVP, Model, VP;
702 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600703 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600704 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600705
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600706 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600707
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600708 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600709 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700710 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
711 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600712 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600713
Karl Schultze4dc75c2016-02-02 15:37:51 -0700714 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
715 demo->uniform_data.mem_alloc.allocationSize, 0,
716 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600717 assert(!err);
718
Karl Schultze4dc75c2016-02-02 15:37:51 -0700719 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600721 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722}
723
Karl Schultze4dc75c2016-02-02 15:37:51 -0700724static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600725 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600726 VkSemaphore imageAcquiredSemaphore, drawCompleteSemaphore;
727 VkSemaphoreCreateInfo semaphoreCreateInfo = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600728 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
729 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600730 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600731 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800732 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733
Tony Barboure35e8aa2016-06-20 10:44:08 -0600734 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
Tony Barbour416c6502016-06-20 10:30:23 -0600735 NULL, &imageAcquiredSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600736 assert(!err);
737
Tony Barboure35e8aa2016-06-20 10:44:08 -0600738 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
739 NULL, &drawCompleteSemaphore);
740 assert(!err);
741
Ian Elliottaaae5352015-07-06 14:27:58 -0600742 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700743 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour416c6502016-06-20 10:30:23 -0600744 imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700745 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600746 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600747 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
748 // demo->swapchain is out of date (e.g. the window was resized) and
749 // must be recreated:
750 demo_resize(demo);
751 demo_draw(demo);
Tony Barbour416c6502016-06-20 10:30:23 -0600752 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600753 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600754 return;
755 } else if (err == VK_SUBOPTIMAL_KHR) {
756 // demo->swapchain is not as optimal as it could be, but the platform's
757 // presentation engine will still present the image correctly.
758 } else {
759 assert(!err);
760 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600761
Tony Barbour15a4ef62015-10-21 10:14:48 -0600762 demo_flush_init_cmd(demo);
763
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600764 // Wait for the present complete semaphore to be signaled to ensure
765 // that the image won't be rendered to until the presentation
766 // engine has fully released ownership to the application, and it is
767 // okay to render to the image.
768
Karl Schultze4dc75c2016-02-02 15:37:51 -0700769 VkPipelineStageFlags pipe_stage_flags =
770 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
771 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
772 .pNext = NULL,
773 .waitSemaphoreCount = 1,
Tony Barbour416c6502016-06-20 10:30:23 -0600774 .pWaitSemaphores = &imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700775 .pWaitDstStageMask = &pipe_stage_flags,
776 .commandBufferCount = 1,
777 .pCommandBuffers =
778 &demo->buffers[demo->current_buffer].cmd,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600779 .signalSemaphoreCount = 1,
780 .pSignalSemaphores = &drawCompleteSemaphore};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600781
782 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600783 assert(!err);
784
Ian Elliotta0781972015-08-21 15:09:33 -0600785 VkPresentInfoKHR present = {
786 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600787 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600788 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700789 .pSwapchains = &demo->swapchain,
790 .pImageIndices = &demo->current_buffer,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600791 .waitSemaphoreCount = 1,
792 .pWaitSemaphores = &drawCompleteSemaphore,
Ian Elliottaaae5352015-07-06 14:27:58 -0600793 };
794
Karl Schultze4dc75c2016-02-02 15:37:51 -0700795 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600796 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600797 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
798 // demo->swapchain is out of date (e.g. the window was resized) and
799 // must be recreated:
800 demo_resize(demo);
801 } else if (err == VK_SUBOPTIMAL_KHR) {
802 // demo->swapchain is not as optimal as it could be, but the platform's
803 // presentation engine will still present the image correctly.
804 } else {
805 assert(!err);
806 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600807
Chia-I Wucbb564e2015-04-16 22:02:10 +0800808 err = vkQueueWaitIdle(demo->queue);
809 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600810
Tony Barbour416c6502016-06-20 10:30:23 -0600811 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600812 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600813}
814
Karl Schultze4dc75c2016-02-02 15:37:51 -0700815static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600816 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600817 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600818
Ian Elliottb08e0d92015-11-20 11:55:46 -0700819 // Check the surface capabilities and formats
820 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700821 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
822 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600823 assert(!err);
824
Ian Elliott8528eff2015-08-07 15:56:59 -0600825 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700826 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
827 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600828 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600829 VkPresentModeKHR *presentModes =
830 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600831 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700832 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
833 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600834 assert(!err);
835
Ian Elliotta0781972015-08-21 15:09:33 -0600836 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600837 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700838 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600839 // If the surface size is undefined, the size is set to
840 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600841 swapchainExtent.width = demo->width;
842 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700843 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600844 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700845 swapchainExtent = surfCapabilities.currentExtent;
846 demo->width = surfCapabilities.currentExtent.width;
847 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600848 }
849
850 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600851 // tearing mode. If not, try IMMEDIATE which will usually be available,
852 // and is fastest (though it tears). If not, fall back to FIFO which is
853 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600854 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600855 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600856 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
857 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600858 break;
859 }
Ian Elliotta0781972015-08-21 15:09:33 -0600860 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
861 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
862 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600863 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600864 }
865
866 // Determine the number of VkImage's to use in the swap chain (we desire to
867 // own only 1 image at a time, besides the images being displayed and
868 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700869 uint32_t desiredNumberOfSwapchainImages =
870 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700871 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700872 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600873 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700874 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600875 }
876
Tony Barbour9fd6d352015-09-16 15:01:32 -0600877 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700878 if (surfCapabilities.supportedTransforms &
879 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700880 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600881 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700882 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600883 }
884
Ian Elliottcf074d32015-10-16 18:02:43 -0600885 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600886 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800887 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700888 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600889 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800890 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600891 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700892 .imageExtent =
893 {
894 .width = swapchainExtent.width, .height = swapchainExtent.height,
895 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700896 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600897 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700898 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700899 .imageArrayLayers = 1,
900 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
901 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600902 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600903 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600904 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600905 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600906 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600907 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600908
Karl Schultze4dc75c2016-02-02 15:37:51 -0700909 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
910 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800911 assert(!err);
912
Ian Elliottcf074d32015-10-16 18:02:43 -0600913 // If we just re-created an existing swapchain, we should destroy the old
914 // swapchain at this point.
915 // Note: destroying the swapchain also cleans up all its associated
916 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800917 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700918 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600919 }
920
921 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600922 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600923 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800924
Karl Schultze4dc75c2016-02-02 15:37:51 -0700925 VkImage *swapchainImages =
926 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600927 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600928 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600929 &demo->swapchainImageCount,
930 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600931 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600932
Karl Schultze4dc75c2016-02-02 15:37:51 -0700933 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
934 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600935 assert(demo->buffers);
936
Ian Elliotta0781972015-08-21 15:09:33 -0600937 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600938 VkImageViewCreateInfo color_image_view = {
939 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600940 .pNext = NULL,
941 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700942 .components =
943 {
944 .r = VK_COMPONENT_SWIZZLE_R,
945 .g = VK_COMPONENT_SWIZZLE_G,
946 .b = VK_COMPONENT_SWIZZLE_B,
947 .a = VK_COMPONENT_SWIZZLE_A,
948 },
949 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
950 .baseMipLevel = 0,
951 .levelCount = 1,
952 .baseArrayLayer = 0,
953 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600954 .viewType = VK_IMAGE_VIEW_TYPE_2D,
955 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956 };
957
Ian Elliotta0781972015-08-21 15:09:33 -0600958 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600960 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961
Karl Schultze4dc75c2016-02-02 15:37:51 -0700962 err = vkCreateImageView(demo->device, &color_image_view, NULL,
963 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600964 assert(!err);
965 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700966
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500967
Mark Young04fd3d82016-01-25 14:43:50 -0700968 if (NULL != presentModes) {
969 free(presentModes);
970 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971}
972
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600974 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600975 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600976 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600978 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600979 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700980 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600981 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600982 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800983 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600984 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600985 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600986 .flags = 0,
987 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200988
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600989 VkImageViewCreateInfo view = {
990 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800992 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600993 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700994 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
995 .baseMipLevel = 0,
996 .levelCount = 1,
997 .baseArrayLayer = 0,
998 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600999 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001000 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001001 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001002
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001003 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001004 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001005 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001006
1007 demo->depth.format = depth_format;
1008
1009 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001010 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001011 assert(!err);
1012
Karl Schultze4dc75c2016-02-02 15:37:51 -07001013 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001014 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001015
Chia-I Wuf48700b2015-11-10 16:21:09 +08001016 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001017 demo->depth.mem_alloc.pNext = NULL;
1018 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1019 demo->depth.mem_alloc.memoryTypeIndex = 0;
1020
Karl Schultze4dc75c2016-02-02 15:37:51 -07001021 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1022 0, /* No requirements */
1023 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001024 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001025
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001026 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001027 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1028 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001029 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001030
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001031 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001032 err =
1033 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001034 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035
Karl Schultze4dc75c2016-02-02 15:37:51 -07001036 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1037 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001038 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1039 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001040
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001041 /* create image view */
1042 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001043 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045}
1046
Tony Barbour1a86d032015-09-21 15:17:33 -06001047/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001049 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001050#ifdef __ANDROID__
1051#include <lunarg.ppm.h>
1052 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001053 cPtr = (char*)lunarg_ppm;
1054 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001055 return false;
1056 }
1057 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001058 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001059 if (rgba_data == NULL) {
1060 return true;
1061 }
1062 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001063 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001064 return false;
1065 }
1066 while(strncmp(cPtr++, "\n", 1));
1067
1068 for (int y = 0; y < *height; y++) {
1069 uint8_t *rowPtr = rgba_data;
1070 for (int x = 0; x < *width; x++) {
1071 memcpy(rowPtr, cPtr, 3);
1072 rowPtr[3] = 255; /* Alpha of 1 */
1073 rowPtr += 4;
1074 cPtr += 3;
1075 }
1076 rgba_data += layout->rowPitch;
1077 }
1078
1079 return true;
1080#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001081 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001082 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001083
Tony Barbour1a86d032015-09-21 15:17:33 -06001084 if (!fPtr)
1085 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001086
Tony Barbour1a86d032015-09-21 15:17:33 -06001087 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001088 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1089 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001090 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001091 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001092
Tony Barbour1a86d032015-09-21 15:17:33 -06001093 do {
1094 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001095 if (cPtr == NULL) {
1096 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001097 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001098 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001099 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001100
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001101 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001102 if (rgba_data == NULL) {
1103 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001104 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001105 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001106 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001107 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001108 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1109 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001110 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001111 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001112
Karl Schultze4dc75c2016-02-02 15:37:51 -07001113 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001114 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001115 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001116 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001117 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001118 rowPtr[3] = 255; /* Alpha of 1 */
1119 rowPtr += 4;
1120 }
1121 rgba_data += layout->rowPitch;
1122 }
1123 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001124 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001125#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001126}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001127
Karl Schultze4dc75c2016-02-02 15:37:51 -07001128static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001129 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001130 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001131 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001132 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001133 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001134 int32_t tex_width;
1135 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001136 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001137 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138
Karl Schultze4dc75c2016-02-02 15:37:51 -07001139 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001140 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001141 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001143 tex_obj->tex_width = tex_width;
1144 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001145
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001146 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001147 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001149 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001150 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001151 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001152 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001153 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001154 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001156 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001157 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001158 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001161 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162
Karl Schultze4dc75c2016-02-02 15:37:51 -07001163 err =
1164 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165 assert(!err);
1166
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001167 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001168
Chia-I Wuf48700b2015-11-10 16:21:09 +08001169 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001170 tex_obj->mem_alloc.pNext = NULL;
1171 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1172 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001173
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1175 required_props,
1176 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001177 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001178
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001179 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001180 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001181 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001182 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001183
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001184 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001185 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001186 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001187
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001188 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001189 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001190 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001191 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001192 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001193 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001194 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195 void *data;
1196
Karl Schultze4dc75c2016-02-02 15:37:51 -07001197 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1198 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199
Karl Schultze4dc75c2016-02-02 15:37:51 -07001200 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1201 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001202 assert(!err);
1203
1204 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1205 fprintf(stderr, "Error loading texture: %s\n", filename);
1206 }
1207
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001208 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001209 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001210
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001211 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001212 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001213 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1214 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001215 /* setting the image layout does not reference the actual memory so no need
1216 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217}
1218
Karl Schultze4dc75c2016-02-02 15:37:51 -07001219static void demo_destroy_texture_image(struct demo *demo,
1220 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001221 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001222 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1223 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001224}
1225
Karl Schultze4dc75c2016-02-02 15:37:51 -07001226static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001227 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001228 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001229 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001230
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001231 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001232
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001233 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001234 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001235
Karl Schultze4dc75c2016-02-02 15:37:51 -07001236 if ((props.linearTilingFeatures &
1237 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1238 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001239 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001240 demo_prepare_texture_image(
1241 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1242 VK_IMAGE_USAGE_SAMPLED_BIT,
1243 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1244 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001245 } else if (props.optimalTilingFeatures &
1246 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001247 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001248 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001249
1250 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001251 demo_prepare_texture_image(
1252 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1253 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1254 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1255 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001256
Karl Schultze4dc75c2016-02-02 15:37:51 -07001257 demo_prepare_texture_image(
1258 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1259 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1260 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001261
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001262 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001263 VK_IMAGE_ASPECT_COLOR_BIT,
1264 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001265 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1266 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001267
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001268 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001269 VK_IMAGE_ASPECT_COLOR_BIT,
1270 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001271 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1272 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001274 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001275 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1276 .srcOffset = {0, 0, 0},
1277 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1278 .dstOffset = {0, 0, 0},
1279 .extent = {staging_texture.tex_width,
1280 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001281 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001282 vkCmdCopyImage(
1283 demo->cmd, staging_texture.image,
1284 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1285 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001286
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001287 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001288 VK_IMAGE_ASPECT_COLOR_BIT,
1289 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001290 demo->textures[i].imageLayout,
1291 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001292
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001293 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001294
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001295 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001296 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001297 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1298 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001299 }
1300
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001301 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001302 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001303 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001304 .magFilter = VK_FILTER_NEAREST,
1305 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001306 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001307 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1308 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1309 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001311 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001312 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001313 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001314 .minLod = 0.0f,
1315 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001316 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001317 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001318 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001319
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001320 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001321 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001323 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001324 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001325 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001326 .components =
1327 {
1328 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1329 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1330 },
1331 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001332 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001333 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001334
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001336 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001337 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001338 assert(!err);
1339
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001340 /* create image view */
1341 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001342 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001343 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001344 assert(!err);
1345 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347
Karl Schultze4dc75c2016-02-02 15:37:51 -07001348void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001349 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001350 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001351 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001352 int i;
1353 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001354 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001355 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001356 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001357
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001358 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001359 mat4x4_mul(MVP, VP, demo->model_matrix);
1360 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001361 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001362
Karl Schultze4dc75c2016-02-02 15:37:51 -07001363 for (i = 0; i < 12 * 3; i++) {
1364 data.position[i][0] = g_vertex_buffer_data[i * 3];
1365 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1366 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001367 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001368 data.attr[i][0] = g_uv_buffer_data[2 * i];
1369 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001370 data.attr[i][2] = 0;
1371 data.attr[i][3] = 0;
1372 }
1373
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001374 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001375 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001376 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001377 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001378 err =
1379 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001380 assert(!err);
1381
Karl Schultze4dc75c2016-02-02 15:37:51 -07001382 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1383 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001384
Chia-I Wuf48700b2015-11-10 16:21:09 +08001385 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001386 demo->uniform_data.mem_alloc.pNext = NULL;
1387 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1388 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1389
Karl Schultze4dc75c2016-02-02 15:37:51 -07001390 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001391 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1392 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001393 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001394 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001395
Karl Schultze4dc75c2016-02-02 15:37:51 -07001396 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1397 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001398 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001399
Karl Schultze4dc75c2016-02-02 15:37:51 -07001400 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1401 demo->uniform_data.mem_alloc.allocationSize, 0,
1402 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001403 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001404
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001405 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001406
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001407 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001408
Karl Schultze4dc75c2016-02-02 15:37:51 -07001409 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1410 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001411 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001412
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001413 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1414 demo->uniform_data.buffer_info.offset = 0;
1415 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001416}
1417
Karl Schultze4dc75c2016-02-02 15:37:51 -07001418static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001419 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001420 [0] =
1421 {
1422 .binding = 0,
1423 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1424 .descriptorCount = 1,
1425 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1426 .pImmutableSamplers = NULL,
1427 },
1428 [1] =
1429 {
1430 .binding = 1,
1431 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1432 .descriptorCount = DEMO_TEXTURE_COUNT,
1433 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1434 .pImmutableSamplers = NULL,
1435 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001436 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001437 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001438 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001439 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001440 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001441 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001442 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001443 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444
Karl Schultze4dc75c2016-02-02 15:37:51 -07001445 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1446 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001447 assert(!err);
1448
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001449 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001450 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1451 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001452 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001453 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001454 };
1455
Karl Schultze4dc75c2016-02-02 15:37:51 -07001456 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001457 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001458 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459}
1460
Karl Schultze4dc75c2016-02-02 15:37:51 -07001461static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001462 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001463 [0] =
1464 {
1465 .format = demo->format,
1466 .samples = VK_SAMPLE_COUNT_1_BIT,
1467 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1468 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1469 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1470 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1471 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1472 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1473 },
1474 [1] =
1475 {
1476 .format = demo->depth.format,
1477 .samples = VK_SAMPLE_COUNT_1_BIT,
1478 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1479 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1480 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1481 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1482 .initialLayout =
1483 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1484 .finalLayout =
1485 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1486 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001487 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001488 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001489 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001490 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001491 const VkAttachmentReference depth_reference = {
1492 .attachment = 1,
1493 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1494 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001495 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001496 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1497 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001498 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001499 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001500 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001501 .pColorAttachments = &color_reference,
1502 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001503 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001504 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001505 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001506 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001507 const VkRenderPassCreateInfo rp_info = {
1508 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1509 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001510 .attachmentCount = 2,
1511 .pAttachments = attachments,
1512 .subpassCount = 1,
1513 .pSubpasses = &subpass,
1514 .dependencyCount = 0,
1515 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001516 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001517 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001518
Chia-I Wu63636062015-10-26 21:10:41 +08001519 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001520 assert(!err);
1521}
1522
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001523//TODO: Merge shader reading
1524#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001525static VkShaderModule
1526demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001527 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001528 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001529 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001530
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001531 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1532 moduleCreateInfo.pNext = NULL;
1533
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001534 moduleCreateInfo.codeSize = size;
1535 moduleCreateInfo.pCode = code;
1536 moduleCreateInfo.flags = 0;
1537 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1538 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001539
1540 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001541}
1542
Karl Schultze4dc75c2016-02-02 15:37:51 -07001543char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001544 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001545 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001546 void *shader_code;
1547
1548 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001549 if (!fp)
1550 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001551
1552 fseek(fp, 0L, SEEK_END);
1553 size = ftell(fp);
1554
1555 fseek(fp, 0L, SEEK_SET);
1556
1557 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001558 retval = fread(shader_code, size, 1, fp);
1559 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001560
1561 *psize = size;
1562
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001563 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001564 return shader_code;
1565}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001566#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001567
Karl Schultze4dc75c2016-02-02 15:37:51 -07001568static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001569#ifdef __ANDROID__
1570 VkShaderModuleCreateInfo sh_info = {};
1571 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1572
1573#include "cube.vert.h"
1574 sh_info.codeSize = sizeof(cube_vert);
1575 sh_info.pCode = cube_vert;
1576 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1577 assert(!err);
1578#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001579 void *vertShaderCode;
1580 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001581
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001582 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1583
Karl Schultze4dc75c2016-02-02 15:37:51 -07001584 demo->vert_shader_module =
1585 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001586
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001587 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001588#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001589
1590 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001591}
1592
Karl Schultze4dc75c2016-02-02 15:37:51 -07001593static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001594#ifdef __ANDROID__
1595 VkShaderModuleCreateInfo sh_info = {};
1596 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1597
1598#include "cube.frag.h"
1599 sh_info.codeSize = sizeof(cube_frag);
1600 sh_info.pCode = cube_frag;
1601 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1602 assert(!err);
1603#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001604 void *fragShaderCode;
1605 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001606
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001607 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001608
Karl Schultze4dc75c2016-02-02 15:37:51 -07001609 demo->frag_shader_module =
1610 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001611
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001612 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001613#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001614
1615 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001616}
1617
Karl Schultze4dc75c2016-02-02 15:37:51 -07001618static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001619 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001620 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001621 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001622 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001623 VkPipelineRasterizationStateCreateInfo rs;
1624 VkPipelineColorBlendStateCreateInfo cb;
1625 VkPipelineDepthStencilStateCreateInfo ds;
1626 VkPipelineViewportStateCreateInfo vp;
1627 VkPipelineMultisampleStateCreateInfo ms;
1628 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1629 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001630 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631
Piers Daniellba839d12015-09-29 13:01:09 -06001632 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1633 memset(&dynamicState, 0, sizeof dynamicState);
1634 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1635 dynamicState.pDynamicStates = dynamicStateEnables;
1636
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001637 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001638 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001639 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001640
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001641 memset(&vi, 0, sizeof(vi));
1642 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1643
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001644 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001645 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001646 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001647
1648 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001649 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1650 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001651 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001652 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001653 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001654 rs.rasterizerDiscardEnable = VK_FALSE;
1655 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001656 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001657
1658 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001659 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1660 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001661 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001662 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001663 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001664 cb.attachmentCount = 1;
1665 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001666
Tony Barbour29645d02015-01-16 14:27:35 -07001667 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001668 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001669 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001670 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1671 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001672 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001673 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1674 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001675
1676 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001677 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001678 ds.depthTestEnable = VK_TRUE;
1679 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001680 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001681 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001682 ds.back.failOp = VK_STENCIL_OP_KEEP;
1683 ds.back.passOp = VK_STENCIL_OP_KEEP;
1684 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001685 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001686 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687
Tony Barbour29645d02015-01-16 14:27:35 -07001688 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001689 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001690 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001691 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001692
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001693 // Two stages: vs and fs
1694 pipeline.stageCount = 2;
1695 VkPipelineShaderStageCreateInfo shaderStages[2];
1696 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1697
Karl Schultze4dc75c2016-02-02 15:37:51 -07001698 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1699 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001700 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001701 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001702
Karl Schultze4dc75c2016-02-02 15:37:51 -07001703 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1704 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001705 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001706 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001707
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001708 memset(&pipelineCache, 0, sizeof(pipelineCache));
1709 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710
Karl Schultze4dc75c2016-02-02 15:37:51 -07001711 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1712 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001713 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001714
Karl Schultze4dc75c2016-02-02 15:37:51 -07001715 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001716 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001717 pipeline.pRasterizationState = &rs;
1718 pipeline.pColorBlendState = &cb;
1719 pipeline.pMultisampleState = &ms;
1720 pipeline.pViewportState = &vp;
1721 pipeline.pDepthStencilState = &ds;
1722 pipeline.pStages = shaderStages;
1723 pipeline.renderPass = demo->render_pass;
1724 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001725
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001726 pipeline.renderPass = demo->render_pass;
1727
Karl Schultze4dc75c2016-02-02 15:37:51 -07001728 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1729 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730 assert(!err);
1731
Chia-I Wu63636062015-10-26 21:10:41 +08001732 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1733 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734}
1735
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001737 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001738 [0] =
1739 {
1740 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1741 .descriptorCount = 1,
1742 },
1743 [1] =
1744 {
1745 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1746 .descriptorCount = DEMO_TEXTURE_COUNT,
1747 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001748 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001749 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001750 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001751 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001752 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001753 .poolSizeCount = 2,
1754 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001755 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001756 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001757
Karl Schultze4dc75c2016-02-02 15:37:51 -07001758 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1759 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001760 assert(!err);
1761}
1762
Karl Schultze4dc75c2016-02-02 15:37:51 -07001763static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001764 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001765 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001766 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001767 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001768
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001769 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001770 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001771 .pNext = NULL,
1772 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001773 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001774 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001775 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001776 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001777
Chia-I Wuae721ba2015-05-25 16:27:55 +08001778 memset(&tex_descs, 0, sizeof(tex_descs));
1779 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001780 tex_descs[i].sampler = demo->textures[i].sampler;
1781 tex_descs[i].imageView = demo->textures[i].view;
1782 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001783 }
1784
1785 memset(&writes, 0, sizeof(writes));
1786
1787 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001788 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001789 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001790 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001791 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001792
1793 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001794 writes[1].dstSet = demo->desc_set;
1795 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001796 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001797 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001798 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001799
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001800 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001801}
1802
Karl Schultze4dc75c2016-02-02 15:37:51 -07001803static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001804 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001805 attachments[1] = demo->depth.view;
1806
Chia-I Wuf973e322015-07-08 13:34:24 +08001807 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001808 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1809 .pNext = NULL,
1810 .renderPass = demo->render_pass,
1811 .attachmentCount = 2,
1812 .pAttachments = attachments,
1813 .width = demo->width,
1814 .height = demo->height,
1815 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001816 };
1817 VkResult U_ASSERT_ONLY err;
1818 uint32_t i;
1819
Karl Schultze4dc75c2016-02-02 15:37:51 -07001820 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1821 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001822 assert(demo->framebuffers);
1823
1824 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001825 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001826 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1827 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001828 assert(!err);
1829 }
1830}
1831
Karl Schultze4dc75c2016-02-02 15:37:51 -07001832static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001833 VkResult U_ASSERT_ONLY err;
1834
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001835 const VkCommandPoolCreateInfo cmd_pool_info = {
1836 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001837 .pNext = NULL,
1838 .queueFamilyIndex = demo->graphics_queue_node_index,
1839 .flags = 0,
1840 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001841 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1842 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001843 assert(!err);
1844
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001845 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001846 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001847 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001848 .commandPool = demo->cmd_pool,
1849 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001850 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852
1853 demo_prepare_buffers(demo);
1854 demo_prepare_depth(demo);
1855 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001856 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001857
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001858 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001859 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001860 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001861
Ian Elliotta0781972015-08-21 15:09:33 -06001862 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001863 err =
1864 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001865 assert(!err);
1866 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001867
Chia-I Wu63ea9262015-03-26 13:14:16 +08001868 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001869 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001870
Chia-I Wuf973e322015-07-08 13:34:24 +08001871 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001872
Ian Elliotta0781972015-08-21 15:09:33 -06001873 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001874 demo->current_buffer = i;
1875 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1876 }
1877
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001878 /*
1879 * Prepare functions above may generate pipeline commands
1880 * that need to be flushed before beginning the render loop.
1881 */
1882 demo_flush_init_cmd(demo);
1883
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001884 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001885 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001886}
1887
Karl Schultze4dc75c2016-02-02 15:37:51 -07001888static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001889 uint32_t i;
1890
1891 demo->prepared = false;
1892
Tony Barbourd8e504f2015-10-08 14:26:24 -06001893 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001894 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001895 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001896 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001897 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001898
Chia-I Wu63636062015-10-26 21:10:41 +08001899 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1900 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1901 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1902 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1903 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001904
1905 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001906 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1907 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1908 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1909 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001910 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001911 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001912
Chia-I Wu63636062015-10-26 21:10:41 +08001913 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1914 vkDestroyImage(demo->device, demo->depth.image, NULL);
1915 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001916
Chia-I Wu63636062015-10-26 21:10:41 +08001917 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1918 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001919
Ian Elliotta0781972015-08-21 15:09:33 -06001920 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001921 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001922 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1923 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001924 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001925 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001926
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001927 free(demo->queue_props);
1928
Chia-I Wu63636062015-10-26 21:10:41 +08001929 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1930 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001931 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001932 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001933 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001934 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001935 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001936
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001937#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001938 if (demo->use_xlib) {
1939 XDestroyWindow(demo->display, demo->xlib_window);
1940 XCloseDisplay(demo->display);
1941 } else {
1942 xcb_destroy_window(demo->connection, demo->xcb_window);
1943 xcb_disconnect(demo->connection);
1944 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001945 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001946#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001947 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001948 xcb_disconnect(demo->connection);
1949 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001950#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
1951 wl_shell_surface_destroy(demo->shell_surface);
1952 wl_surface_destroy(demo->window);
1953 wl_shell_destroy(demo->shell);
1954 wl_compositor_destroy(demo->compositor);
1955 wl_registry_destroy(demo->registry);
1956 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001957#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001958}
1959
Karl Schultze4dc75c2016-02-02 15:37:51 -07001960static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001961 uint32_t i;
1962
Mike Stroyanbb60b382015-10-28 09:46:57 -06001963 // Don't react to resize until after first initialization.
1964 if (!demo->prepared) {
1965 return;
1966 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001967 // In order to properly resize the window, we must re-create the swapchain
1968 // AND redo the command buffers, etc.
1969 //
1970 // First, perform part of the demo_cleanup() function:
1971 demo->prepared = false;
1972
1973 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001974 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001975 }
1976 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001977 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001978
Chia-I Wu63636062015-10-26 21:10:41 +08001979 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1980 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1981 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1982 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1983 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001984
1985 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001986 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1987 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1988 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1989 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001990 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001991
Chia-I Wu63636062015-10-26 21:10:41 +08001992 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1993 vkDestroyImage(demo->device, demo->depth.image, NULL);
1994 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001995
Chia-I Wu63636062015-10-26 21:10:41 +08001996 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1997 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001998
1999 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002000 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002001 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
2002 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06002003 }
Chia-I Wu63636062015-10-26 21:10:41 +08002004 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002005 free(demo->buffers);
2006
Ian Elliottcf074d32015-10-16 18:02:43 -06002007 // Second, re-perform the demo_prepare() function, which will re-create the
2008 // swapchain:
2009 demo_prepare(demo);
2010}
2011
David Pinedo2bb7c932015-06-18 17:03:14 -06002012// On MS-Windows, make this a global, so it's available to WndProc()
2013struct demo demo;
2014
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002015#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002016static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002017 if (!demo->prepared)
2018 return;
Ian Elliott639ca472015-04-16 15:23:05 -06002019 // Wait for work to finish before updating MVP.
2020 vkDeviceWaitIdle(demo->device);
2021 demo_update_data_buffer(demo);
2022
2023 demo_draw(demo);
2024
2025 // Wait for work to finish before updating MVP.
2026 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06002027
David Pinedo2bb7c932015-06-18 17:03:14 -06002028 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002029 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002030 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002031 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002032}
Ian Elliott639ca472015-04-16 15:23:05 -06002033
2034// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002035LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2036 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002037 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002038 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002039 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002040 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002041 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002042 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002043 case WM_GETMINMAXINFO: // set window's minimum size
2044 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2045 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002046 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002047 // Resize the application to the new window size, except when
2048 // it was minimized. Vulkan doesn't support images or swapchains
2049 // with width=0 and height=0.
2050 if (wParam != SIZE_MINIMIZED) {
2051 demo.width = lParam & 0xffff;
2052 demo.height = lParam & 0xffff0000 >> 16;
2053 demo_resize(&demo);
2054 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002055 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002056 default:
2057 break;
2058 }
2059 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2060}
2061
Karl Schultze4dc75c2016-02-02 15:37:51 -07002062static void demo_create_window(struct demo *demo) {
2063 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002064
2065 // Initialize the window class structure:
2066 win_class.cbSize = sizeof(WNDCLASSEX);
2067 win_class.style = CS_HREDRAW | CS_VREDRAW;
2068 win_class.lpfnWndProc = WndProc;
2069 win_class.cbClsExtra = 0;
2070 win_class.cbWndExtra = 0;
2071 win_class.hInstance = demo->connection; // hInstance
2072 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2073 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2074 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2075 win_class.lpszMenuName = NULL;
2076 win_class.lpszClassName = demo->name;
2077 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2078 // Register window class:
2079 if (!RegisterClassEx(&win_class)) {
2080 // It didn't work, so try to give a useful error:
2081 printf("Unexpected error trying to start the application!\n");
2082 fflush(stdout);
2083 exit(1);
2084 }
2085 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002086 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002087 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002088 demo->window = CreateWindowEx(0,
2089 demo->name, // class name
2090 demo->name, // app name
2091 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002092 WS_VISIBLE | WS_SYSMENU,
2093 100, 100, // x/y coords
2094 wr.right - wr.left, // width
2095 wr.bottom - wr.top, // height
2096 NULL, // handle to parent
2097 NULL, // handle to menu
2098 demo->connection, // hInstance
2099 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002100 if (!demo->window) {
2101 // It didn't work, so try to give a useful error:
2102 printf("Cannot create a window in which to draw!\n");
2103 fflush(stdout);
2104 exit(1);
2105 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002106 // Window client area size must be at least 1 pixel high, to prevent crash.
2107 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2108 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002109}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002110#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002111static void demo_create_xlib_window(struct demo *demo) {
2112
2113 demo->display = XOpenDisplay(NULL);
2114 long visualMask = VisualScreenMask;
2115 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002116 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002117 vInfoTemplate.screen = DefaultScreen(demo->display);
2118 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2119 &vInfoTemplate, &numberOfVisuals);
2120
2121 Colormap colormap = XCreateColormap(
2122 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2123 visualInfo->visual, AllocNone);
2124
Rene Lindsay11612722016-06-13 17:20:39 -06002125 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002126 windowAttributes.colormap = colormap;
2127 windowAttributes.background_pixel = 0xFFFFFFFF;
2128 windowAttributes.border_pixel = 0;
2129 windowAttributes.event_mask =
2130 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2131
2132 demo->xlib_window = XCreateWindow(
2133 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2134 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2135 visualInfo->visual,
2136 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2137
2138 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2139 XMapWindow(demo->display, demo->xlib_window);
2140 XFlush(demo->display);
2141 demo->xlib_wm_delete_window =
2142 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2143}
2144static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2145 switch(event->type) {
2146 case ClientMessage:
2147 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2148 demo->quit = true;
2149 break;
2150 case KeyPress:
2151 switch (event->xkey.keycode) {
2152 case 0x9: // Escape
2153 demo->quit = true;
2154 break;
2155 case 0x71: // left arrow key
2156 demo->spin_angle += demo->spin_increment;
2157 break;
2158 case 0x72: // right arrow key
2159 demo->spin_angle -= demo->spin_increment;
2160 break;
2161 case 0x41:
2162 demo->pause = !demo->pause;
2163 break;
2164 }
2165 break;
2166 case ConfigureNotify:
2167 if ((demo->width != event->xconfigure.width) ||
2168 (demo->height != event->xconfigure.height)) {
2169 demo->width = event->xconfigure.width;
2170 demo->height = event->xconfigure.height;
2171 demo_resize(demo);
2172 }
2173 break;
2174 default:
2175 break;
2176 }
2177
2178}
2179
2180static void demo_run_xlib(struct demo *demo) {
2181
2182 while (!demo->quit) {
2183 XEvent event;
2184
2185 if (demo->pause) {
2186 XNextEvent(demo->display, &event);
2187 demo_handle_xlib_event(demo, &event);
2188 } else {
2189 while (XPending(demo->display) > 0) {
2190 XNextEvent(demo->display, &event);
2191 demo_handle_xlib_event(demo, &event);
2192 }
2193 }
2194
2195 // Wait for work to finish before updating MVP.
2196 vkDeviceWaitIdle(demo->device);
2197 demo_update_data_buffer(demo);
2198
2199 demo_draw(demo);
2200
2201 // Wait for work to finish before updating MVP.
2202 vkDeviceWaitIdle(demo->device);
2203 demo->curFrame++;
2204 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2205 demo->quit = true;
2206 }
2207}
2208
2209static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002210 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002211 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002212 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002213 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002214 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002215 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002216 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002217 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2218 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002219 demo->quit = true;
2220 }
2221 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002222 case XCB_KEY_RELEASE: {
2223 const xcb_key_release_event_t *key =
2224 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002225
Karl Schultze4dc75c2016-02-02 15:37:51 -07002226 switch (key->detail) {
2227 case 0x9: // Escape
2228 demo->quit = true;
2229 break;
2230 case 0x71: // left arrow key
2231 demo->spin_angle += demo->spin_increment;
2232 break;
2233 case 0x72: // right arrow key
2234 demo->spin_angle -= demo->spin_increment;
2235 break;
2236 case 0x41:
2237 demo->pause = !demo->pause;
2238 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002240 } break;
2241 case XCB_CONFIGURE_NOTIFY: {
2242 const xcb_configure_notify_event_t *cfg =
2243 (const xcb_configure_notify_event_t *)event;
2244 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002245 demo->width = cfg->width;
2246 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002247 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002248 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002249 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002250 default:
2251 break;
2252 }
2253}
2254
Tony Barbour2593b182016-04-19 10:57:58 -06002255static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002256 xcb_flush(demo->connection);
2257
2258 while (!demo->quit) {
2259 xcb_generic_event_t *event;
2260
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002261 if (demo->pause) {
2262 event = xcb_wait_for_event(demo->connection);
2263 } else {
2264 event = xcb_poll_for_event(demo->connection);
2265 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002266 if (event) {
Tony Barbour2593b182016-04-19 10:57:58 -06002267 demo_handle_xcb_event(demo, event);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002268 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002269 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002270
2271 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002272 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002273 demo_update_data_buffer(demo);
2274
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002275 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002276
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002277 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002278 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002279 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002280 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002281 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002282 }
2283}
2284
Tony Barbour2593b182016-04-19 10:57:58 -06002285static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286 uint32_t value_mask, value_list[32];
2287
Tony Barbour2593b182016-04-19 10:57:58 -06002288 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002289
2290 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2291 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002292 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002293 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002294
Tony Barbour2593b182016-04-19 10:57:58 -06002295 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002296 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2297 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2298 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002299
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002300 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002301 xcb_intern_atom_cookie_t cookie =
2302 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2303 xcb_intern_atom_reply_t *reply =
2304 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002305
Karl Schultze4dc75c2016-02-02 15:37:51 -07002306 xcb_intern_atom_cookie_t cookie2 =
2307 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2308 demo->atom_wm_delete_window =
2309 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002310
Tony Barbour2593b182016-04-19 10:57:58 -06002311 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002312 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002313 &(*demo->atom_wm_delete_window).atom);
2314 free(reply);
2315
Tony Barbour2593b182016-04-19 10:57:58 -06002316 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002317
Karl Schultze4dc75c2016-02-02 15:37:51 -07002318 // Force the x/y coordinates to 100,100 results are identical in consecutive
2319 // runs
2320 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002321 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002322 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002323}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002324#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2325static void demo_run(struct demo *demo) {
2326 while (!demo->quit) {
2327 // Wait for work to finish before updating MVP.
2328 vkDeviceWaitIdle(demo->device);
2329 demo_update_data_buffer(demo);
2330
2331 demo_draw(demo);
2332
2333 // Wait for work to finish before updating MVP.
2334 vkDeviceWaitIdle(demo->device);
2335 demo->curFrame++;
2336 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2337 demo->quit = true;
2338 }
2339}
2340
2341static void handle_ping(void *data UNUSED,
2342 struct wl_shell_surface *shell_surface,
2343 uint32_t serial) {
2344 wl_shell_surface_pong(shell_surface, serial);
2345}
2346
2347static void handle_configure(void *data UNUSED,
2348 struct wl_shell_surface *shell_surface UNUSED,
2349 uint32_t edges UNUSED, int32_t width UNUSED,
2350 int32_t height UNUSED) {}
2351
2352static void handle_popup_done(void *data UNUSED,
2353 struct wl_shell_surface *shell_surface UNUSED) {}
2354
2355static const struct wl_shell_surface_listener shell_surface_listener = {
2356 handle_ping, handle_configure, handle_popup_done};
2357
2358static void demo_create_window(struct demo *demo) {
2359 demo->window = wl_compositor_create_surface(demo->compositor);
2360 if (!demo->window) {
2361 printf("Can not create wayland_surface from compositor!\n");
2362 fflush(stdout);
2363 exit(1);
2364 }
2365
2366 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2367 if (!demo->shell_surface) {
2368 printf("Can not get shell_surface from wayland_surface!\n");
2369 fflush(stdout);
2370 exit(1);
2371 }
2372 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2373 demo);
2374 wl_shell_surface_set_toplevel(demo->shell_surface);
2375 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2376}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002377#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2378static void demo_run(struct demo *demo) {
2379 if (!demo->prepared)
2380 return;
2381
2382 // Wait for work to finish before updating MVP.
2383 vkDeviceWaitIdle(demo->device);
2384 demo_update_data_buffer(demo);
2385
2386 demo_draw(demo);
2387
2388 // Wait for work to finish before updating MVP.
2389 vkDeviceWaitIdle(demo->device);
2390
2391 demo->curFrame++;
2392}
2393#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002394
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002395/*
2396 * Return 1 (true) if all layer names specified in check_names
2397 * can be found in given layer properties.
2398 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002399static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002400 uint32_t layer_count,
2401 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002402 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002403 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002404 for (uint32_t j = 0; j < layer_count; j++) {
2405 if (!strcmp(check_names[i], layers[j].layerName)) {
2406 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002407 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002408 }
2409 }
2410 if (!found) {
2411 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2412 return 0;
2413 }
2414 }
2415 return 1;
2416}
2417
Karl Schultze4dc75c2016-02-02 15:37:51 -07002418static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002419 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002420 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002421 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002422 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002423 char **instance_validation_layers = NULL;
2424 demo->enabled_extension_count = 0;
2425 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002426
Karl Schultz7c50ad62016-04-01 12:07:03 -06002427 char *instance_validation_layers_alt1[] = {
2428 "VK_LAYER_LUNARG_standard_validation"
2429 };
2430
2431 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002432 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2433 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2434 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2435 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002436 };
2437
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002438 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002439 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002440 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002441
Karl Schultz7c50ad62016-04-01 12:07:03 -06002442 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002443 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002444
Karl Schultz7c50ad62016-04-01 12:07:03 -06002445 instance_validation_layers = instance_validation_layers_alt1;
2446 if (instance_layer_count > 0) {
2447 VkLayerProperties *instance_layers =
2448 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2449 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2450 instance_layers);
2451 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002452
Karl Schultz7c50ad62016-04-01 12:07:03 -06002453
2454 validation_found = demo_check_layers(
2455 ARRAY_SIZE(instance_validation_layers_alt1),
2456 instance_validation_layers, instance_layer_count,
2457 instance_layers);
2458 if (validation_found) {
2459 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002460 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2461 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002462 } else {
2463 // use alternative set of validation layers
2464 instance_validation_layers = instance_validation_layers_alt2;
2465 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2466 validation_found = demo_check_layers(
2467 ARRAY_SIZE(instance_validation_layers_alt2),
2468 instance_validation_layers, instance_layer_count,
2469 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002470 validation_layer_count =
2471 ARRAY_SIZE(instance_validation_layers_alt2);
2472 for (uint32_t i = 0; i < validation_layer_count; i++) {
2473 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002474 }
2475 }
2476 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002477 }
Dustin Graves7c287352016-01-27 13:48:06 -07002478
Karl Schultz7c50ad62016-04-01 12:07:03 -06002479 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002480 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002481 "required validation layer.\n\n"
2482 "Please look at the Getting Started guide for additional "
2483 "information.\n",
2484 "vkCreateInstance Failure");
2485 }
Dustin Graves7c287352016-01-27 13:48:06 -07002486 }
2487
2488 /* Look for instance extensions */
2489 VkBool32 surfaceExtFound = 0;
2490 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002491#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002492 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002493#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002494 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002495
Karl Schultze4dc75c2016-02-02 15:37:51 -07002496 err = vkEnumerateInstanceExtensionProperties(
2497 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002498 assert(!err);
2499
Dustin Graves7c287352016-01-27 13:48:06 -07002500 if (instance_extension_count > 0) {
2501 VkExtensionProperties *instance_extensions =
2502 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2503 err = vkEnumerateInstanceExtensionProperties(
2504 NULL, &instance_extension_count, instance_extensions);
2505 assert(!err);
2506 for (uint32_t i = 0; i < instance_extension_count; i++) {
2507 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2508 instance_extensions[i].extensionName)) {
2509 surfaceExtFound = 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_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002512 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002513#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002514 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2515 instance_extensions[i].extensionName)) {
2516 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002517 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002518 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2519 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002520#endif
2521#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002522 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2523 instance_extensions[i].extensionName)) {
2524 platformSurfaceExtFound = 1;
2525 xlibSurfaceExtFound = 1;
2526 demo->extension_names[demo->enabled_extension_count++] =
2527 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2528 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002529#endif
2530#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002531 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2532 instance_extensions[i].extensionName)) {
2533 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002534 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002535 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2536 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002537#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002538#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2539 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2540 instance_extensions[i].extensionName)) {
2541 platformSurfaceExtFound = 1;
2542 demo->extension_names[demo->enabled_extension_count++] =
2543 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2544 }
2545#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002546#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002547 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2548 instance_extensions[i].extensionName)) {
2549 platformSurfaceExtFound = 1;
2550 demo->extension_names[demo->enabled_extension_count++] =
2551 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2552 }
2553#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002554 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2555 instance_extensions[i].extensionName)) {
2556 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002557 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002558 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2559 }
2560 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002561 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002562 }
Dustin Graves7c287352016-01-27 13:48:06 -07002563
2564 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002565 }
Dustin Graves7c287352016-01-27 13:48:06 -07002566
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002567 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002568 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2569 "the " VK_KHR_SURFACE_EXTENSION_NAME
2570 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002571 "Vulkan installable client driver (ICD) installed?\nPlease "
2572 "look at the Getting Started guide for additional "
2573 "information.\n",
2574 "vkCreateInstance Failure");
2575 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002576 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002577#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002578 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2579 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2580 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002581 "Vulkan installable client driver (ICD) installed?\nPlease "
2582 "look at the Getting Started guide for additional "
2583 "information.\n",
2584 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002585#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002586 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2587 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2588 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002589 "Vulkan installable client driver (ICD) installed?\nPlease "
2590 "look at the Getting Started guide for additional "
2591 "information.\n",
2592 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002593#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2594 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2595 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2596 " extension.\n\nDo you have a compatible "
2597 "Vulkan installable client driver (ICD) installed?\nPlease "
2598 "look at the Getting Started guide for additional "
2599 "information.\n",
2600 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002601#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2602 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2603 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2604 " extension.\n\nDo you have a compatible "
2605 "Vulkan installable client driver (ICD) installed?\nPlease "
2606 "look at the Getting Started guide for additional "
2607 "information.\n",
2608 "vkCreateInstance Failure");
2609#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002610 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002611#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002612 if (demo->use_xlib && !xlibSurfaceExtFound) {
2613 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2614 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2615 " extension.\n\nDo you have a compatible "
2616 "Vulkan installable client driver (ICD) installed?\nPlease "
2617 "look at the Getting Started guide for additional "
2618 "information.\n",
2619 "vkCreateInstance Failure");
2620 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002621#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002622 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002623 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002624 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002625 .pApplicationName = APP_SHORT_NAME,
2626 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002627 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002628 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002629 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002630 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002631 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002632 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002633 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002634 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002635 .enabledLayerCount = demo->enabled_layer_count,
2636 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2637 .enabledExtensionCount = demo->enabled_extension_count,
2638 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002639 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002640
2641 /*
2642 * This is info for a temp callback to use during CreateInstance.
2643 * After the instance is created, we use the instance-based
2644 * function to register the final callback.
2645 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002646 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002647 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002648 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2649 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002650 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002651 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002652 dbgCreateInfo.flags =
2653 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2654 inst_info.pNext = &dbgCreateInfo;
2655 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002656
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002657 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002658
Chia-I Wu63636062015-10-26 21:10:41 +08002659 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002660 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2661 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002662 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002663 "additional information.\n",
2664 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002665 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002666 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002667 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002668 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002669 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002670 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2671 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002672 "the Getting Started guide for additional information.\n",
2673 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002674 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002675
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002676 /* Make initial call to query gpu_count, then second call for gpu info*/
2677 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2678 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002679
2680 if (gpu_count > 0) {
2681 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2682 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2683 assert(!err);
2684 /* For cube demo we just grab the first physical device */
2685 demo->gpu = physical_devices[0];
2686 free(physical_devices);
2687 } else {
2688 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2689 "Do you have a compatible Vulkan installable client driver (ICD) "
2690 "installed?\nPlease look at the Getting Started guide for "
2691 "additional information.\n",
2692 "vkEnumeratePhysicalDevices Failure");
2693 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002694
Dustin Graves7c287352016-01-27 13:48:06 -07002695 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002696 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002697 VkBool32 swapchainExtFound = 0;
2698 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002699 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002700
Karl Schultze4dc75c2016-02-02 15:37:51 -07002701 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2702 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002703 assert(!err);
2704
Dustin Graves7c287352016-01-27 13:48:06 -07002705 if (device_extension_count > 0) {
2706 VkExtensionProperties *device_extensions =
2707 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2708 err = vkEnumerateDeviceExtensionProperties(
2709 demo->gpu, NULL, &device_extension_count, device_extensions);
2710 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002711
Dustin Graves7c287352016-01-27 13:48:06 -07002712 for (uint32_t i = 0; i < device_extension_count; i++) {
2713 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2714 device_extensions[i].extensionName)) {
2715 swapchainExtFound = 1;
2716 demo->extension_names[demo->enabled_extension_count++] =
2717 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2718 }
2719 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002720 }
Dustin Graves7c287352016-01-27 13:48:06 -07002721
2722 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002723 }
Dustin Graves7c287352016-01-27 13:48:06 -07002724
Tony Barbourcc69f452015-10-08 13:59:42 -06002725 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002726 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2727 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2728 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002729 "Vulkan installable client driver (ICD) installed?\nPlease "
2730 "look at the Getting Started guide for additional "
2731 "information.\n",
2732 "vkCreateInstance Failure");
2733 }
2734
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002735 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002736 demo->CreateDebugReportCallback =
2737 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2738 demo->inst, "vkCreateDebugReportCallbackEXT");
2739 demo->DestroyDebugReportCallback =
2740 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2741 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002742 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002743 ERR_EXIT(
2744 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2745 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002746 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002747 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002748 ERR_EXIT(
2749 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2750 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002751 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002752 demo->DebugReportMessage =
2753 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2754 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002755 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002756 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002757 "vkGetProcAddr Failure");
2758 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002759
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002760 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002761 PFN_vkDebugReportCallbackEXT callback;
2762 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002763 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002764 dbgCreateInfo.pNext = NULL;
2765 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002766 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002767 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002768 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002769 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2770 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002771 switch (err) {
2772 case VK_SUCCESS:
2773 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002774 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002775 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2776 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002777 break;
2778 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002779 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2780 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002781 break;
2782 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002783 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002784 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002785
2786 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002787 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2788 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002789 assert(demo->queue_count >= 1);
2790
Karl Schultze4dc75c2016-02-02 15:37:51 -07002791 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2792 demo->queue_count * sizeof(VkQueueFamilyProperties));
2793 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2794 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002795 // Find a queue that supports gfx
2796 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002797 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2798 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002799 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2800 break;
2801 }
2802 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002803 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002804 // If app has specific feature requirements it should check supported
2805 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002806 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002807 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002808
Tony Barbourda2bad52016-01-22 14:36:40 -07002809 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2810 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2811 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2812 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2813 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2814}
2815
Karl Schultze4dc75c2016-02-02 15:37:51 -07002816static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002817 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002818 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002819 const VkDeviceQueueCreateInfo queue = {
2820 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2821 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002822 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002823 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002824 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002825
2826 VkDeviceCreateInfo device = {
2827 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2828 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002829 .queueCreateInfoCount = 1,
2830 .pQueueCreateInfos = &queue,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002831 .enabledLayerCount = 0,
2832 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002833 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002834 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2835 .pEnabledFeatures =
2836 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002837 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002838
Chia-I Wu63636062015-10-26 21:10:41 +08002839 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002840 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002841}
2842
Karl Schultze4dc75c2016-02-02 15:37:51 -07002843static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002844 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002845 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002846
Karl Schultze4dc75c2016-02-02 15:37:51 -07002847// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002848#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002849 VkWin32SurfaceCreateInfoKHR createInfo;
2850 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2851 createInfo.pNext = NULL;
2852 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002853 createInfo.hinstance = demo->connection;
2854 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002855
Karl Schultze4dc75c2016-02-02 15:37:51 -07002856 err =
2857 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002858#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2859 VkWaylandSurfaceCreateInfoKHR createInfo;
2860 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2861 createInfo.pNext = NULL;
2862 createInfo.flags = 0;
2863 createInfo.display = demo->display;
2864 createInfo.surface = demo->window;
2865
2866 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2867 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002868#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2869 VkAndroidSurfaceCreateInfoKHR createInfo;
2870 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2871 createInfo.pNext = NULL;
2872 createInfo.flags = 0;
2873 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002874
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002875 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2876#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002877 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002878#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002879 VkXlibSurfaceCreateInfoKHR createInfo;
2880 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2881 createInfo.pNext = NULL;
2882 createInfo.flags = 0;
2883 createInfo.dpy = demo->display;
2884 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002885
Tony Barbour2593b182016-04-19 10:57:58 -06002886 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2887 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002888#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002889 }
2890 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002891#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002892 VkXcbSurfaceCreateInfoKHR createInfo;
2893 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2894 createInfo.pNext = NULL;
2895 createInfo.flags = 0;
2896 createInfo.connection = demo->connection;
2897 createInfo.window = demo->xcb_window;
2898
2899 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002900#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002901 }
Tony Barbour2593b182016-04-19 10:57:58 -06002902 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002903
Tony Barbourcc69f452015-10-08 13:59:42 -06002904 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002905 VkBool32 *supportsPresent =
2906 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002907 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002908 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002909 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002910 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002911
2912 // Search for a graphics and a present queue in the array of queue
2913 // families, try to find one that supports both
2914 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002915 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002916 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002917 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2918 if (graphicsQueueNodeIndex == UINT32_MAX) {
2919 graphicsQueueNodeIndex = i;
2920 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002921
Ian Elliottaaae5352015-07-06 14:27:58 -06002922 if (supportsPresent[i] == VK_TRUE) {
2923 graphicsQueueNodeIndex = i;
2924 presentQueueNodeIndex = i;
2925 break;
2926 }
2927 }
2928 }
2929 if (presentQueueNodeIndex == UINT32_MAX) {
2930 // If didn't find a queue that supports both graphics and present, then
2931 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002932 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002933 if (supportsPresent[i] == VK_TRUE) {
2934 presentQueueNodeIndex = i;
2935 break;
2936 }
2937 }
2938 }
2939 free(supportsPresent);
2940
2941 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002942 if (graphicsQueueNodeIndex == UINT32_MAX ||
2943 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002944 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002945 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002946 }
2947
2948 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002949 // synchronization, and appropriate tracking for QueueSubmit.
2950 // NOTE: While it is possible for an application to use a separate graphics
2951 // and a present queues, this demo program assumes it is only using
2952 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002953 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2954 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002955 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002956 }
2957
2958 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002959
Tony Barbourda2bad52016-01-22 14:36:40 -07002960 demo_create_device(demo);
2961
2962 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2963 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2964 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2965 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2966 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2967
Karl Schultze4dc75c2016-02-02 15:37:51 -07002968 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2969 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002970
Ian Elliottaaae5352015-07-06 14:27:58 -06002971 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002972 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002973 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002974 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002975 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002976 VkSurfaceFormatKHR *surfFormats =
2977 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002978 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002979 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002980 assert(!err);
2981 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2982 // the surface has no preferred format. Otherwise, at least one
2983 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002984 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002985 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002986 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002987 assert(formatCount >= 1);
2988 demo->format = surfFormats[0].format;
2989 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002990 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002991
David Pinedo2bb7c932015-06-18 17:03:14 -06002992 demo->quit = false;
2993 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002994
2995 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002996 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002997}
2998
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002999#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
3000static void registry_handle_global(void *data, struct wl_registry *registry,
3001 uint32_t name, const char *interface,
3002 uint32_t version UNUSED) {
3003 struct demo *demo = data;
3004 if (strcmp(interface, "wl_compositor") == 0) {
3005 demo->compositor =
3006 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3007 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3008 * tp xdg_shell */
3009 } else if (strcmp(interface, "wl_shell") == 0) {
3010 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3011 }
3012}
3013
3014static void registry_handle_global_remove(void *data UNUSED,
3015 struct wl_registry *registry UNUSED,
3016 uint32_t name UNUSED) {}
3017
3018static const struct wl_registry_listener registry_listener = {
3019 registry_handle_global, registry_handle_global_remove};
3020#endif
3021
Karl Schultze4dc75c2016-02-02 15:37:51 -07003022static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003023#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003024 const xcb_setup_t *setup;
3025 xcb_screen_iterator_t iter;
3026 int scr;
3027
3028 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06003029 if (demo->connection == NULL) {
3030 printf("Cannot find a compatible Vulkan installable client driver "
3031 "(ICD).\nExiting ...\n");
3032 fflush(stdout);
3033 exit(1);
3034 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003035
3036 setup = xcb_get_setup(demo->connection);
3037 iter = xcb_setup_roots_iterator(setup);
3038 while (scr-- > 0)
3039 xcb_screen_next(&iter);
3040
3041 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003042#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3043 demo->display = wl_display_connect(NULL);
3044
3045 if (demo->display == NULL) {
3046 printf("Cannot find a compatible Vulkan installable client driver "
3047 "(ICD).\nExiting ...\n");
3048 fflush(stdout);
3049 exit(1);
3050 }
3051
3052 demo->registry = wl_display_get_registry(demo->display);
3053 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3054 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003055#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003056}
3057
Karl Schultze4dc75c2016-02-02 15:37:51 -07003058static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003059 vec3 eye = {0.0f, 3.0f, 5.0f};
3060 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003061 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003062
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003063 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003064 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003065
Piers Daniell735ee532015-02-23 16:23:13 -07003066 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003067 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003068 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003069 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003070 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003071 if (strcmp(argv[i], "--break") == 0) {
3072 demo->use_break = true;
3073 continue;
3074 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003075 if (strcmp(argv[i], "--validate") == 0) {
3076 demo->validate = true;
3077 continue;
3078 }
Tony Barbour2593b182016-04-19 10:57:58 -06003079 if (strcmp(argv[i], "--xlib") == 0) {
3080 demo->use_xlib = true;
3081 continue;
3082 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003083 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3084 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3085 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003086 i++;
3087 continue;
3088 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003089 if (strcmp(argv[i], "--suppress_popups") == 0) {
3090 demo->suppress_popups = true;
3091 continue;
3092 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003093
Karl Schultze4dc75c2016-02-02 15:37:51 -07003094 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
lenny-lunargfbe22392016-06-06 11:07:53 -06003095 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003096 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003097 fflush(stderr);
3098 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003099 }
3100
Tony Barbour2593b182016-04-19 10:57:58 -06003101 if (!demo->use_xlib)
3102 demo_init_connection(demo);
3103
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003104 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003105
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003106 demo->width = 500;
3107 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003108
3109 demo->spin_angle = 0.01f;
3110 demo->spin_increment = 0.01f;
3111 demo->pause = false;
3112
Karl Schultze4dc75c2016-02-02 15:37:51 -07003113 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3114 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003115 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3116 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003117}
3118
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003119#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003120// Include header required for parsing the command line options.
3121#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003122
Karl Schultze4dc75c2016-02-02 15:37:51 -07003123int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3124 int nCmdShow) {
3125 MSG msg; // message
3126 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003127 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003128 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003129
Karl Schultze4dc75c2016-02-02 15:37:51 -07003130 // Use the CommandLine functions to get the command line arguments.
3131 // Unfortunately, Microsoft outputs
3132 // this information as wide characters for Unicode, and we simply want the
3133 // Ascii version to be compatible
3134 // with the non-Windows side. So, we have to convert the information to
3135 // Ascii character strings.
3136 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3137 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003138 argc = 0;
3139 }
3140
Karl Schultze4dc75c2016-02-02 15:37:51 -07003141 if (argc > 0) {
3142 argv = (char **)malloc(sizeof(char *) * argc);
3143 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003144 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003145 } else {
3146 for (int iii = 0; iii < argc; iii++) {
3147 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003148 size_t numConverted = 0;
3149
Karl Schultze4dc75c2016-02-02 15:37:51 -07003150 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3151 if (argv[iii] != NULL) {
3152 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3153 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003154 }
3155 }
3156 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003157 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003158 argv = NULL;
3159 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003160
3161 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003162
3163 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003164 if (argc > 0 && argv != NULL) {
3165 for (int iii = 0; iii < argc; iii++) {
3166 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003167 free(argv[iii]);
3168 }
3169 }
3170 free(argv);
3171 }
3172
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003173 demo.connection = hInstance;
3174 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003175 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003176 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003177
3178 demo_prepare(&demo);
3179
Karl Schultze4dc75c2016-02-02 15:37:51 -07003180 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003181
3182 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003183 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003184 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003185 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003186 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003187 done = true; // if found, quit app
3188 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003189 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003190 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003191 DispatchMessage(&msg);
3192 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003193 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003194 }
3195
3196 demo_cleanup(&demo);
3197
Karl Schultze4dc75c2016-02-02 15:37:51 -07003198 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003199}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003200#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3201#include <android/log.h>
3202#include <android_native_app_glue.h>
3203static bool initialized = false;
3204static bool active = false;
3205struct demo demo;
3206
3207static int32_t processInput(struct android_app* app, AInputEvent* event) {
3208 return 0;
3209}
3210
3211static void processCommand(struct android_app* app, int32_t cmd) {
3212 switch(cmd) {
3213 case APP_CMD_INIT_WINDOW: {
3214 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003215 // We're getting a new window. If the app is starting up, we
3216 // need to initialize. If the app has already been
3217 // initialized, that means that we lost our previous window,
3218 // which means that we have a lot of work to do. At a minimum,
3219 // we need to destroy the swapchain and surface associated with
3220 // the old window, and create a new surface and swapchain.
3221 // However, since there are a lot of other objects/state that
3222 // is tied to the swapchain, it's easiest to simply cleanup and
3223 // start over (i.e. use a brute-force approach of re-starting
3224 // the app)
3225 if (demo.prepared) {
3226 demo_cleanup(&demo);
3227 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003228 demo_init(&demo, 0, NULL);
3229 demo.window = (void*)app->window;
3230 demo_init_vk_swapchain(&demo);
3231 demo_prepare(&demo);
3232 initialized = true;
3233 }
3234 break;
3235 }
3236 case APP_CMD_GAINED_FOCUS: {
3237 active = true;
3238 break;
3239 }
3240 case APP_CMD_LOST_FOCUS: {
3241 active = false;
3242 break;
3243 }
3244 }
3245}
3246
3247void android_main(struct android_app *app)
3248{
3249 app_dummy();
3250
Cody Northrop8707a6e2016-04-26 19:59:19 -06003251#ifdef ANDROID
3252 int vulkanSupport = InitVulkan();
3253 if (vulkanSupport == 0)
3254 return;
3255#endif
3256
Ian Elliottf05d5d12016-05-17 12:03:35 -06003257 demo.prepared = false;
3258
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003259 app->onAppCmd = processCommand;
3260 app->onInputEvent = processInput;
3261
3262 while(1) {
3263 int events;
3264 struct android_poll_source* source;
3265 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3266 if (source) {
3267 source->process(app, source);
3268 }
3269
3270 if (app->destroyRequested != 0) {
3271 demo_cleanup(&demo);
3272 return;
3273 }
3274 }
3275 if (initialized && active) {
3276 demo_run(&demo);
3277 }
3278 }
3279
3280}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003281#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003282int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003283 struct demo demo;
3284
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003285 demo_init(&demo, argc, argv);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003286#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003287 if (demo.use_xlib)
3288 demo_create_xlib_window(&demo);
3289 else
3290 demo_create_xcb_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003291#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3292 demo_create_window(&demo);
3293#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003294
Tony Barbourcc69f452015-10-08 13:59:42 -06003295 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003296
3297 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003298
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003299#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003300 if (demo.use_xlib)
3301 demo_run_xlib(&demo);
3302 else
3303 demo_run_xcb(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003304#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3305 demo_run(&demo);
3306#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003307
3308 demo_cleanup(&demo);
3309
Karl Schultz0218c002016-03-22 17:06:13 -06003310 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003311}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003312#endif