blob: 5b262888b50b28437be5ff02c05f46fbbc1215b9 [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);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700538 VkCommandBufferBeginInfo cmd_buf_info = {
539 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
540 .pNext = NULL,
541 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700542 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700543 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600544 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600545 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600546 }
547
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600548 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600549 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700551 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800552 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553 .oldLayout = old_image_layout,
554 .newLayout = new_image_layout,
555 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700556 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800558 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800560 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600561 }
562
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700563 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700564 image_memory_barrier.dstAccessMask =
565 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700566 }
567
568 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700569 image_memory_barrier.dstAccessMask =
570 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700571 }
572
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600573 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600574 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700575 image_memory_barrier.dstAccessMask =
576 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600577 }
578
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600579 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600580
Tony Barbour50bbca42015-06-29 16:20:35 -0600581 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
582 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600583
Karl Schultze4dc75c2016-02-02 15:37:51 -0700584 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
585 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600586}
587
Karl Schultze4dc75c2016-02-02 15:37:51 -0700588static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700589 const VkCommandBufferBeginInfo cmd_buf_info = {
590 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
591 .pNext = NULL,
592 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700593 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700594 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800595 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700596 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
597 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800598 };
599 const VkRenderPassBeginInfo rp_begin = {
600 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
601 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800602 .renderPass = demo->render_pass,
603 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800604 .renderArea.offset.x = 0,
605 .renderArea.offset.y = 0,
606 .renderArea.extent.width = demo->width,
607 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600608 .clearValueCount = 2,
609 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700610 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800611 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600613 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614 assert(!err);
615
Tony Barbour5c5b1632016-04-26 11:13:48 -0600616 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
617 // happens to the previous contents of the image
618 VkImageMemoryBarrier image_memory_barrier = {
619 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
620 .pNext = NULL,
621 .srcAccessMask = 0,
622 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
623 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
624 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
625 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
626 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
627 .image = demo->buffers[demo->current_buffer].image,
628 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
629
630 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
631 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
632 NULL, 1, &image_memory_barrier);
633
Chia-I Wuf051d262015-10-27 19:25:11 +0800634 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800635
Karl Schultze4dc75c2016-02-02 15:37:51 -0700636 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
637 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
638 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
639 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600640 VkViewport viewport;
641 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700642 viewport.height = (float)demo->height;
643 viewport.width = (float)demo->width;
644 viewport.minDepth = (float)0.0f;
645 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700646 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600647
648 VkRect2D scissor;
649 memset(&scissor, 0, sizeof(scissor));
650 scissor.extent.width = demo->width;
651 scissor.extent.height = demo->height;
652 scissor.offset.x = 0;
653 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700654 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600655 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800656 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657
Tony Barbour15a4ef62015-10-21 10:14:48 -0600658 VkImageMemoryBarrier prePresentBarrier = {
659 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
660 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800661 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700662 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600663 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700664 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600665 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800666 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700667 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600668
669 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
670 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700671 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
672 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
673 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600674
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600675 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600676 assert(!err);
677}
678
Karl Schultze4dc75c2016-02-02 15:37:51 -0700679void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600680 mat4x4 MVP, Model, VP;
681 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600682 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600683 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600684
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600685 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600687 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600688 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700689 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
690 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600691 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600692
Karl Schultze4dc75c2016-02-02 15:37:51 -0700693 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
694 demo->uniform_data.mem_alloc.allocationSize, 0,
695 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600696 assert(!err);
697
Karl Schultze4dc75c2016-02-02 15:37:51 -0700698 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600699
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600700 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701}
702
Karl Schultze4dc75c2016-02-02 15:37:51 -0700703static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600704 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600705 VkSemaphore imageAcquiredSemaphore, drawCompleteSemaphore;
706 VkSemaphoreCreateInfo semaphoreCreateInfo = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600707 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
708 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600709 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600710 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800711 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600712
Tony Barboure35e8aa2016-06-20 10:44:08 -0600713 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
Tony Barbour416c6502016-06-20 10:30:23 -0600714 NULL, &imageAcquiredSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600715 assert(!err);
716
Tony Barboure35e8aa2016-06-20 10:44:08 -0600717 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo,
718 NULL, &drawCompleteSemaphore);
719 assert(!err);
720
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700722 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour416c6502016-06-20 10:30:23 -0600723 imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700724 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600725 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600726 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
727 // demo->swapchain is out of date (e.g. the window was resized) and
728 // must be recreated:
729 demo_resize(demo);
730 demo_draw(demo);
Tony Barbour416c6502016-06-20 10:30:23 -0600731 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600732 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600733 return;
734 } else if (err == VK_SUBOPTIMAL_KHR) {
735 // demo->swapchain is not as optimal as it could be, but the platform's
736 // presentation engine will still present the image correctly.
737 } else {
738 assert(!err);
739 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600740
Tony Barbour15a4ef62015-10-21 10:14:48 -0600741 demo_flush_init_cmd(demo);
742
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600743 // Wait for the present complete semaphore to be signaled to ensure
744 // that the image won't be rendered to until the presentation
745 // engine has fully released ownership to the application, and it is
746 // okay to render to the image.
747
Karl Schultze4dc75c2016-02-02 15:37:51 -0700748 VkPipelineStageFlags pipe_stage_flags =
749 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
750 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
751 .pNext = NULL,
752 .waitSemaphoreCount = 1,
Tony Barbour416c6502016-06-20 10:30:23 -0600753 .pWaitSemaphores = &imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700754 .pWaitDstStageMask = &pipe_stage_flags,
755 .commandBufferCount = 1,
756 .pCommandBuffers =
757 &demo->buffers[demo->current_buffer].cmd,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600758 .signalSemaphoreCount = 1,
759 .pSignalSemaphores = &drawCompleteSemaphore};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600760
761 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600762 assert(!err);
763
Ian Elliotta0781972015-08-21 15:09:33 -0600764 VkPresentInfoKHR present = {
765 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600766 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600767 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700768 .pSwapchains = &demo->swapchain,
769 .pImageIndices = &demo->current_buffer,
Tony Barboure35e8aa2016-06-20 10:44:08 -0600770 .waitSemaphoreCount = 1,
771 .pWaitSemaphores = &drawCompleteSemaphore,
Ian Elliottaaae5352015-07-06 14:27:58 -0600772 };
773
Karl Schultze4dc75c2016-02-02 15:37:51 -0700774 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600775 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600776 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
777 // demo->swapchain is out of date (e.g. the window was resized) and
778 // must be recreated:
779 demo_resize(demo);
780 } else if (err == VK_SUBOPTIMAL_KHR) {
781 // demo->swapchain is not as optimal as it could be, but the platform's
782 // presentation engine will still present the image correctly.
783 } else {
784 assert(!err);
785 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600786
Chia-I Wucbb564e2015-04-16 22:02:10 +0800787 err = vkQueueWaitIdle(demo->queue);
788 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600789
Tony Barbour416c6502016-06-20 10:30:23 -0600790 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Tony Barboure35e8aa2016-06-20 10:44:08 -0600791 vkDestroySemaphore(demo->device, drawCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600792}
793
Karl Schultze4dc75c2016-02-02 15:37:51 -0700794static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600795 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600796 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600797
Ian Elliottb08e0d92015-11-20 11:55:46 -0700798 // Check the surface capabilities and formats
799 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700800 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
801 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600802 assert(!err);
803
Ian Elliott8528eff2015-08-07 15:56:59 -0600804 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700805 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
806 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600808 VkPresentModeKHR *presentModes =
809 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600810 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700811 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
812 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600813 assert(!err);
814
Ian Elliotta0781972015-08-21 15:09:33 -0600815 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600816 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700817 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600818 // If the surface size is undefined, the size is set to
819 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600820 swapchainExtent.width = demo->width;
821 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700822 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600823 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700824 swapchainExtent = surfCapabilities.currentExtent;
825 demo->width = surfCapabilities.currentExtent.width;
826 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600827 }
828
829 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600830 // tearing mode. If not, try IMMEDIATE which will usually be available,
831 // and is fastest (though it tears). If not, fall back to FIFO which is
832 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600833 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600834 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600835 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
836 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600837 break;
838 }
Ian Elliotta0781972015-08-21 15:09:33 -0600839 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
840 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
841 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600842 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600843 }
844
845 // Determine the number of VkImage's to use in the swap chain (we desire to
846 // own only 1 image at a time, besides the images being displayed and
847 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700848 uint32_t desiredNumberOfSwapchainImages =
849 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700850 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700851 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600852 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700853 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600854 }
855
Tony Barbour9fd6d352015-09-16 15:01:32 -0600856 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700857 if (surfCapabilities.supportedTransforms &
858 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700859 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600860 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700861 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600862 }
863
Ian Elliottcf074d32015-10-16 18:02:43 -0600864 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600865 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800866 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700867 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600868 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800869 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600870 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700871 .imageExtent =
872 {
873 .width = swapchainExtent.width, .height = swapchainExtent.height,
874 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700875 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600876 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700877 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700878 .imageArrayLayers = 1,
879 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
880 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600881 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600882 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600883 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600884 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600885 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600886 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600887
Karl Schultze4dc75c2016-02-02 15:37:51 -0700888 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
889 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800890 assert(!err);
891
Ian Elliottcf074d32015-10-16 18:02:43 -0600892 // If we just re-created an existing swapchain, we should destroy the old
893 // swapchain at this point.
894 // Note: destroying the swapchain also cleans up all its associated
895 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800896 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700897 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600898 }
899
900 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600901 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600902 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800903
Karl Schultze4dc75c2016-02-02 15:37:51 -0700904 VkImage *swapchainImages =
905 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600906 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600907 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600908 &demo->swapchainImageCount,
909 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600910 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600911
Karl Schultze4dc75c2016-02-02 15:37:51 -0700912 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
913 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600914 assert(demo->buffers);
915
Ian Elliotta0781972015-08-21 15:09:33 -0600916 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600917 VkImageViewCreateInfo color_image_view = {
918 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600919 .pNext = NULL,
920 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700921 .components =
922 {
923 .r = VK_COMPONENT_SWIZZLE_R,
924 .g = VK_COMPONENT_SWIZZLE_G,
925 .b = VK_COMPONENT_SWIZZLE_B,
926 .a = VK_COMPONENT_SWIZZLE_A,
927 },
928 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
929 .baseMipLevel = 0,
930 .levelCount = 1,
931 .baseArrayLayer = 0,
932 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600933 .viewType = VK_IMAGE_VIEW_TYPE_2D,
934 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600935 };
936
Ian Elliotta0781972015-08-21 15:09:33 -0600937 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600938
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600939 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600940
Karl Schultze4dc75c2016-02-02 15:37:51 -0700941 err = vkCreateImageView(demo->device, &color_image_view, NULL,
942 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600943 assert(!err);
944 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700945
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500946
Mark Young04fd3d82016-01-25 14:43:50 -0700947 if (NULL != presentModes) {
948 free(presentModes);
949 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950}
951
Karl Schultze4dc75c2016-02-02 15:37:51 -0700952static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600953 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600954 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600955 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600957 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600958 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700959 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600960 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600961 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800962 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600963 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600964 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600965 .flags = 0,
966 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200967
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600968 VkImageViewCreateInfo view = {
969 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600970 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800971 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600972 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
974 .baseMipLevel = 0,
975 .levelCount = 1,
976 .baseArrayLayer = 0,
977 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600979 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600980 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600981
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500982 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600983 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600984 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985
986 demo->depth.format = depth_format;
987
988 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700989 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990 assert(!err);
991
Karl Schultze4dc75c2016-02-02 15:37:51 -0700992 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600993 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600994
Chia-I Wuf48700b2015-11-10 16:21:09 +0800995 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200996 demo->depth.mem_alloc.pNext = NULL;
997 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
998 demo->depth.mem_alloc.memoryTypeIndex = 0;
999
Karl Schultze4dc75c2016-02-02 15:37:51 -07001000 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1001 0, /* No requirements */
1002 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001003 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001005 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001006 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1007 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001008 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001009
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001010 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001011 err =
1012 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001013 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014
Karl Schultze4dc75c2016-02-02 15:37:51 -07001015 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1016 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001017 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1018 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001019
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001020 /* create image view */
1021 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001022 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001023 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001024}
1025
Tony Barbour1a86d032015-09-21 15:17:33 -06001026/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001027bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001028 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001029#ifdef __ANDROID__
1030#include <lunarg.ppm.h>
1031 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001032 cPtr = (char*)lunarg_ppm;
1033 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001034 return false;
1035 }
1036 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001037 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001038 if (rgba_data == NULL) {
1039 return true;
1040 }
1041 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001042 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001043 return false;
1044 }
1045 while(strncmp(cPtr++, "\n", 1));
1046
1047 for (int y = 0; y < *height; y++) {
1048 uint8_t *rowPtr = rgba_data;
1049 for (int x = 0; x < *width; x++) {
1050 memcpy(rowPtr, cPtr, 3);
1051 rowPtr[3] = 255; /* Alpha of 1 */
1052 rowPtr += 4;
1053 cPtr += 3;
1054 }
1055 rgba_data += layout->rowPitch;
1056 }
1057
1058 return true;
1059#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001060 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001061 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001062
Tony Barbour1a86d032015-09-21 15:17:33 -06001063 if (!fPtr)
1064 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001065
Tony Barbour1a86d032015-09-21 15:17:33 -06001066 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001067 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1068 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001069 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001070 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001071
Tony Barbour1a86d032015-09-21 15:17:33 -06001072 do {
1073 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001074 if (cPtr == NULL) {
1075 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001076 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001077 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001078 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001079
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001080 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001081 if (rgba_data == NULL) {
1082 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001083 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001084 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001085 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001086 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001087 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1088 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001089 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001090 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001091
Karl Schultze4dc75c2016-02-02 15:37:51 -07001092 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001093 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001094 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001095 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001096 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001097 rowPtr[3] = 255; /* Alpha of 1 */
1098 rowPtr += 4;
1099 }
1100 rgba_data += layout->rowPitch;
1101 }
1102 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001103 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001104#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001105}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001106
Karl Schultze4dc75c2016-02-02 15:37:51 -07001107static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001108 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001109 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001110 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001111 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001112 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001113 int32_t tex_width;
1114 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001115 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001116 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117
Karl Schultze4dc75c2016-02-02 15:37:51 -07001118 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001119 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001120 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001122 tex_obj->tex_width = tex_width;
1123 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001124
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001125 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001126 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001128 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001130 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001131 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001132 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001133 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001135 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001136 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001137 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001140 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001141
Karl Schultze4dc75c2016-02-02 15:37:51 -07001142 err =
1143 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144 assert(!err);
1145
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001146 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001147
Chia-I Wuf48700b2015-11-10 16:21:09 +08001148 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001149 tex_obj->mem_alloc.pNext = NULL;
1150 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1151 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001152
Karl Schultze4dc75c2016-02-02 15:37:51 -07001153 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1154 required_props,
1155 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001156 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001157
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001158 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001159 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001160 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001161 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001163 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001164 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001165 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001167 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001168 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001169 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001171 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001173 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001174 void *data;
1175
Karl Schultze4dc75c2016-02-02 15:37:51 -07001176 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1177 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178
Karl Schultze4dc75c2016-02-02 15:37:51 -07001179 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1180 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001181 assert(!err);
1182
1183 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1184 fprintf(stderr, "Error loading texture: %s\n", filename);
1185 }
1186
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001187 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001188 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001189
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001190 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001191 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001192 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1193 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001194 /* setting the image layout does not reference the actual memory so no need
1195 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001196}
1197
Karl Schultze4dc75c2016-02-02 15:37:51 -07001198static void demo_destroy_texture_image(struct demo *demo,
1199 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001200 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001201 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1202 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001203}
1204
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001206 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001207 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001208 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001210 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001211
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001213 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001214
Karl Schultze4dc75c2016-02-02 15:37:51 -07001215 if ((props.linearTilingFeatures &
1216 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1217 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001218 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001219 demo_prepare_texture_image(
1220 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1221 VK_IMAGE_USAGE_SAMPLED_BIT,
1222 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1223 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001224 } else if (props.optimalTilingFeatures &
1225 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001227 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001228
1229 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001230 demo_prepare_texture_image(
1231 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1232 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1233 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1234 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001235
Karl Schultze4dc75c2016-02-02 15:37:51 -07001236 demo_prepare_texture_image(
1237 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1238 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1239 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001241 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001242 VK_IMAGE_ASPECT_COLOR_BIT,
1243 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001244 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1245 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001246
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001247 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001248 VK_IMAGE_ASPECT_COLOR_BIT,
1249 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001250 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1251 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001253 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001254 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1255 .srcOffset = {0, 0, 0},
1256 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1257 .dstOffset = {0, 0, 0},
1258 .extent = {staging_texture.tex_width,
1259 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001260 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001261 vkCmdCopyImage(
1262 demo->cmd, staging_texture.image,
1263 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1264 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001265
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001266 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001267 VK_IMAGE_ASPECT_COLOR_BIT,
1268 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001269 demo->textures[i].imageLayout,
1270 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001271
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001272 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001274 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001275 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001276 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1277 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001278 }
1279
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001280 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001281 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001282 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001283 .magFilter = VK_FILTER_NEAREST,
1284 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001285 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001286 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1287 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1288 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001289 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001290 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001291 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001292 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293 .minLod = 0.0f,
1294 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001295 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001296 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001297 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001298
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001299 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001300 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001301 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001302 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001303 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001304 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001305 .components =
1306 {
1307 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1308 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1309 },
1310 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001311 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001312 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001313
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001314 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001315 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001316 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317 assert(!err);
1318
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001319 /* create image view */
1320 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001321 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001322 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001323 assert(!err);
1324 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001325}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001326
Karl Schultze4dc75c2016-02-02 15:37:51 -07001327void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001328 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001329 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001330 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001331 int i;
1332 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001333 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001334 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001335 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001336
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001337 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001338 mat4x4_mul(MVP, VP, demo->model_matrix);
1339 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001341
Karl Schultze4dc75c2016-02-02 15:37:51 -07001342 for (i = 0; i < 12 * 3; i++) {
1343 data.position[i][0] = g_vertex_buffer_data[i * 3];
1344 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1345 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001346 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001347 data.attr[i][0] = g_uv_buffer_data[2 * i];
1348 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001349 data.attr[i][2] = 0;
1350 data.attr[i][3] = 0;
1351 }
1352
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001353 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001354 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001355 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001356 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001357 err =
1358 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001359 assert(!err);
1360
Karl Schultze4dc75c2016-02-02 15:37:51 -07001361 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1362 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001363
Chia-I Wuf48700b2015-11-10 16:21:09 +08001364 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001365 demo->uniform_data.mem_alloc.pNext = NULL;
1366 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1367 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1368
Karl Schultze4dc75c2016-02-02 15:37:51 -07001369 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001370 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1371 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001372 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001373 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001374
Karl Schultze4dc75c2016-02-02 15:37:51 -07001375 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1376 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001377 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001378
Karl Schultze4dc75c2016-02-02 15:37:51 -07001379 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1380 demo->uniform_data.mem_alloc.allocationSize, 0,
1381 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001382 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001383
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001384 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001385
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001386 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001387
Karl Schultze4dc75c2016-02-02 15:37:51 -07001388 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1389 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001390 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001391
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001392 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1393 demo->uniform_data.buffer_info.offset = 0;
1394 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001395}
1396
Karl Schultze4dc75c2016-02-02 15:37:51 -07001397static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001398 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001399 [0] =
1400 {
1401 .binding = 0,
1402 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1403 .descriptorCount = 1,
1404 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1405 .pImmutableSamplers = NULL,
1406 },
1407 [1] =
1408 {
1409 .binding = 1,
1410 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1411 .descriptorCount = DEMO_TEXTURE_COUNT,
1412 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1413 .pImmutableSamplers = NULL,
1414 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001415 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001416 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001417 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001418 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001419 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001420 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001421 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001422 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423
Karl Schultze4dc75c2016-02-02 15:37:51 -07001424 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1425 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001426 assert(!err);
1427
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001428 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001429 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1430 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001431 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001432 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001433 };
1434
Karl Schultze4dc75c2016-02-02 15:37:51 -07001435 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001436 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438}
1439
Karl Schultze4dc75c2016-02-02 15:37:51 -07001440static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001441 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001442 [0] =
1443 {
1444 .format = demo->format,
1445 .samples = VK_SAMPLE_COUNT_1_BIT,
1446 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1447 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1448 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1449 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1450 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1451 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1452 },
1453 [1] =
1454 {
1455 .format = demo->depth.format,
1456 .samples = VK_SAMPLE_COUNT_1_BIT,
1457 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1458 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1459 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1460 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1461 .initialLayout =
1462 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1463 .finalLayout =
1464 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1465 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001466 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001467 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001469 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001470 const VkAttachmentReference depth_reference = {
1471 .attachment = 1,
1472 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1473 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001474 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001475 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1476 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001477 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001478 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001479 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001480 .pColorAttachments = &color_reference,
1481 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001482 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001483 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001484 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001485 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001486 const VkRenderPassCreateInfo rp_info = {
1487 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1488 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001489 .attachmentCount = 2,
1490 .pAttachments = attachments,
1491 .subpassCount = 1,
1492 .pSubpasses = &subpass,
1493 .dependencyCount = 0,
1494 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001495 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001496 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001497
Chia-I Wu63636062015-10-26 21:10:41 +08001498 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001499 assert(!err);
1500}
1501
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001502//TODO: Merge shader reading
1503#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001504static VkShaderModule
1505demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001506 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001507 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001508 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001509
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001510 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1511 moduleCreateInfo.pNext = NULL;
1512
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001513 moduleCreateInfo.codeSize = size;
1514 moduleCreateInfo.pCode = code;
1515 moduleCreateInfo.flags = 0;
1516 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1517 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001518
1519 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520}
1521
Karl Schultze4dc75c2016-02-02 15:37:51 -07001522char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001523 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001524 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001525 void *shader_code;
1526
1527 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001528 if (!fp)
1529 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001530
1531 fseek(fp, 0L, SEEK_END);
1532 size = ftell(fp);
1533
1534 fseek(fp, 0L, SEEK_SET);
1535
1536 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001537 retval = fread(shader_code, size, 1, fp);
1538 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001539
1540 *psize = size;
1541
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001542 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001543 return shader_code;
1544}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001545#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001546
Karl Schultze4dc75c2016-02-02 15:37:51 -07001547static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001548#ifdef __ANDROID__
1549 VkShaderModuleCreateInfo sh_info = {};
1550 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1551
1552#include "cube.vert.h"
1553 sh_info.codeSize = sizeof(cube_vert);
1554 sh_info.pCode = cube_vert;
1555 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1556 assert(!err);
1557#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001558 void *vertShaderCode;
1559 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001560
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001561 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1562
Karl Schultze4dc75c2016-02-02 15:37:51 -07001563 demo->vert_shader_module =
1564 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001565
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001566 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001567#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001568
1569 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570}
1571
Karl Schultze4dc75c2016-02-02 15:37:51 -07001572static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001573#ifdef __ANDROID__
1574 VkShaderModuleCreateInfo sh_info = {};
1575 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1576
1577#include "cube.frag.h"
1578 sh_info.codeSize = sizeof(cube_frag);
1579 sh_info.pCode = cube_frag;
1580 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1581 assert(!err);
1582#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001583 void *fragShaderCode;
1584 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001585
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001586 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001587
Karl Schultze4dc75c2016-02-02 15:37:51 -07001588 demo->frag_shader_module =
1589 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001590
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001591 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001592#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001593
1594 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001595}
1596
Karl Schultze4dc75c2016-02-02 15:37:51 -07001597static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001598 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001599 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001600 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001601 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001602 VkPipelineRasterizationStateCreateInfo rs;
1603 VkPipelineColorBlendStateCreateInfo cb;
1604 VkPipelineDepthStencilStateCreateInfo ds;
1605 VkPipelineViewportStateCreateInfo vp;
1606 VkPipelineMultisampleStateCreateInfo ms;
1607 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1608 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001609 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001610
Piers Daniellba839d12015-09-29 13:01:09 -06001611 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1612 memset(&dynamicState, 0, sizeof dynamicState);
1613 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1614 dynamicState.pDynamicStates = dynamicStateEnables;
1615
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001616 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001617 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001618 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001619
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001620 memset(&vi, 0, sizeof(vi));
1621 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1622
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001623 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001624 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001625 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001626
1627 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001628 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1629 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001630 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001631 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001632 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001633 rs.rasterizerDiscardEnable = VK_FALSE;
1634 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001635 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636
1637 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001638 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1639 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001640 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001641 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001642 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001643 cb.attachmentCount = 1;
1644 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001645
Tony Barbour29645d02015-01-16 14:27:35 -07001646 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001647 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001648 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001649 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1650 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001651 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001652 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1653 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001654
1655 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001656 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001657 ds.depthTestEnable = VK_TRUE;
1658 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001659 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001660 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001661 ds.back.failOp = VK_STENCIL_OP_KEEP;
1662 ds.back.passOp = VK_STENCIL_OP_KEEP;
1663 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001664 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001665 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001666
Tony Barbour29645d02015-01-16 14:27:35 -07001667 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001668 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001669 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001670 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001671
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001672 // Two stages: vs and fs
1673 pipeline.stageCount = 2;
1674 VkPipelineShaderStageCreateInfo shaderStages[2];
1675 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1676
Karl Schultze4dc75c2016-02-02 15:37:51 -07001677 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1678 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001679 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001680 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001681
Karl Schultze4dc75c2016-02-02 15:37:51 -07001682 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1683 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001684 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001685 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001686
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001687 memset(&pipelineCache, 0, sizeof(pipelineCache));
1688 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001689
Karl Schultze4dc75c2016-02-02 15:37:51 -07001690 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1691 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001692 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001693
Karl Schultze4dc75c2016-02-02 15:37:51 -07001694 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001695 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001696 pipeline.pRasterizationState = &rs;
1697 pipeline.pColorBlendState = &cb;
1698 pipeline.pMultisampleState = &ms;
1699 pipeline.pViewportState = &vp;
1700 pipeline.pDepthStencilState = &ds;
1701 pipeline.pStages = shaderStages;
1702 pipeline.renderPass = demo->render_pass;
1703 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001704
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001705 pipeline.renderPass = demo->render_pass;
1706
Karl Schultze4dc75c2016-02-02 15:37:51 -07001707 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1708 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709 assert(!err);
1710
Chia-I Wu63636062015-10-26 21:10:41 +08001711 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1712 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001713}
1714
Karl Schultze4dc75c2016-02-02 15:37:51 -07001715static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001716 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001717 [0] =
1718 {
1719 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1720 .descriptorCount = 1,
1721 },
1722 [1] =
1723 {
1724 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1725 .descriptorCount = DEMO_TEXTURE_COUNT,
1726 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001727 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001728 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001729 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001730 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001731 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001732 .poolSizeCount = 2,
1733 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001734 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001735 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001736
Karl Schultze4dc75c2016-02-02 15:37:51 -07001737 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1738 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001739 assert(!err);
1740}
1741
Karl Schultze4dc75c2016-02-02 15:37:51 -07001742static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001743 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001744 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001745 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001746 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001747
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001748 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001749 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001750 .pNext = NULL,
1751 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001752 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001753 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001754 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001755 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001756
Chia-I Wuae721ba2015-05-25 16:27:55 +08001757 memset(&tex_descs, 0, sizeof(tex_descs));
1758 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001759 tex_descs[i].sampler = demo->textures[i].sampler;
1760 tex_descs[i].imageView = demo->textures[i].view;
1761 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001762 }
1763
1764 memset(&writes, 0, sizeof(writes));
1765
1766 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001767 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001768 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001769 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001770 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001771
1772 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001773 writes[1].dstSet = demo->desc_set;
1774 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001775 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001776 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001777 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001778
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001779 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001780}
1781
Karl Schultze4dc75c2016-02-02 15:37:51 -07001782static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001783 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001784 attachments[1] = demo->depth.view;
1785
Chia-I Wuf973e322015-07-08 13:34:24 +08001786 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001787 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1788 .pNext = NULL,
1789 .renderPass = demo->render_pass,
1790 .attachmentCount = 2,
1791 .pAttachments = attachments,
1792 .width = demo->width,
1793 .height = demo->height,
1794 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001795 };
1796 VkResult U_ASSERT_ONLY err;
1797 uint32_t i;
1798
Karl Schultze4dc75c2016-02-02 15:37:51 -07001799 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1800 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001801 assert(demo->framebuffers);
1802
1803 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001804 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001805 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1806 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001807 assert(!err);
1808 }
1809}
1810
Karl Schultze4dc75c2016-02-02 15:37:51 -07001811static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001812 VkResult U_ASSERT_ONLY err;
1813
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001814 const VkCommandPoolCreateInfo cmd_pool_info = {
1815 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001816 .pNext = NULL,
1817 .queueFamilyIndex = demo->graphics_queue_node_index,
1818 .flags = 0,
1819 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001820 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1821 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001822 assert(!err);
1823
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001824 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001825 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001826 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001827 .commandPool = demo->cmd_pool,
1828 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001829 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001831
1832 demo_prepare_buffers(demo);
1833 demo_prepare_depth(demo);
1834 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001835 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001836
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001837 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001838 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001839 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001840
Ian Elliotta0781972015-08-21 15:09:33 -06001841 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001842 err =
1843 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001844 assert(!err);
1845 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001846
Chia-I Wu63ea9262015-03-26 13:14:16 +08001847 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001848 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001849
Chia-I Wuf973e322015-07-08 13:34:24 +08001850 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001851
Ian Elliotta0781972015-08-21 15:09:33 -06001852 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001853 demo->current_buffer = i;
1854 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1855 }
1856
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001857 /*
1858 * Prepare functions above may generate pipeline commands
1859 * that need to be flushed before beginning the render loop.
1860 */
1861 demo_flush_init_cmd(demo);
1862
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001863 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001864 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865}
1866
Karl Schultze4dc75c2016-02-02 15:37:51 -07001867static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001868 uint32_t i;
1869
1870 demo->prepared = false;
1871
Tony Barbourd8e504f2015-10-08 14:26:24 -06001872 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001873 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001874 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001875 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001876 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001877
Chia-I Wu63636062015-10-26 21:10:41 +08001878 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1879 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1880 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1881 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1882 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001883
1884 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001885 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1886 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1887 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1888 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001889 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001890 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001891
Chia-I Wu63636062015-10-26 21:10:41 +08001892 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1893 vkDestroyImage(demo->device, demo->depth.image, NULL);
1894 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001895
Chia-I Wu63636062015-10-26 21:10:41 +08001896 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1897 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001898
Ian Elliotta0781972015-08-21 15:09:33 -06001899 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001900 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001901 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1902 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001903 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001904 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001905
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001906 free(demo->queue_props);
1907
Chia-I Wu63636062015-10-26 21:10:41 +08001908 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1909 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001910 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001911 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001912 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001913 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001914 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001915
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001916#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001917 if (demo->use_xlib) {
1918 XDestroyWindow(demo->display, demo->xlib_window);
1919 XCloseDisplay(demo->display);
1920 } else {
1921 xcb_destroy_window(demo->connection, demo->xcb_window);
1922 xcb_disconnect(demo->connection);
1923 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001924 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001925#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001926 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001927 xcb_disconnect(demo->connection);
1928 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001929#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
1930 wl_shell_surface_destroy(demo->shell_surface);
1931 wl_surface_destroy(demo->window);
1932 wl_shell_destroy(demo->shell);
1933 wl_compositor_destroy(demo->compositor);
1934 wl_registry_destroy(demo->registry);
1935 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001936#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001937}
1938
Karl Schultze4dc75c2016-02-02 15:37:51 -07001939static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001940 uint32_t i;
1941
Mike Stroyanbb60b382015-10-28 09:46:57 -06001942 // Don't react to resize until after first initialization.
1943 if (!demo->prepared) {
1944 return;
1945 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001946 // In order to properly resize the window, we must re-create the swapchain
1947 // AND redo the command buffers, etc.
1948 //
1949 // First, perform part of the demo_cleanup() function:
1950 demo->prepared = false;
1951
1952 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001953 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001954 }
1955 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001956 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001957
Chia-I Wu63636062015-10-26 21:10:41 +08001958 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1959 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1960 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1961 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1962 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001963
1964 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001965 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1966 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1967 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1968 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001969 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001970
Chia-I Wu63636062015-10-26 21:10:41 +08001971 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1972 vkDestroyImage(demo->device, demo->depth.image, NULL);
1973 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001974
Chia-I Wu63636062015-10-26 21:10:41 +08001975 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1976 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001977
1978 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001979 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001980 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1981 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001982 }
Chia-I Wu63636062015-10-26 21:10:41 +08001983 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001984 free(demo->buffers);
1985
Ian Elliottcf074d32015-10-16 18:02:43 -06001986 // Second, re-perform the demo_prepare() function, which will re-create the
1987 // swapchain:
1988 demo_prepare(demo);
1989}
1990
David Pinedo2bb7c932015-06-18 17:03:14 -06001991// On MS-Windows, make this a global, so it's available to WndProc()
1992struct demo demo;
1993
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001994#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07001995static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001996 if (!demo->prepared)
1997 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001998 // Wait for work to finish before updating MVP.
1999 vkDeviceWaitIdle(demo->device);
2000 demo_update_data_buffer(demo);
2001
2002 demo_draw(demo);
2003
2004 // Wait for work to finish before updating MVP.
2005 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06002006
David Pinedo2bb7c932015-06-18 17:03:14 -06002007 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002008 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002009 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002010 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002011}
Ian Elliott639ca472015-04-16 15:23:05 -06002012
2013// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002014LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2015 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002016 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002017 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002018 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002019 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002020 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002021 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002022 case WM_GETMINMAXINFO: // set window's minimum size
2023 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2024 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002025 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002026 // Resize the application to the new window size, except when
2027 // it was minimized. Vulkan doesn't support images or swapchains
2028 // with width=0 and height=0.
2029 if (wParam != SIZE_MINIMIZED) {
2030 demo.width = lParam & 0xffff;
2031 demo.height = lParam & 0xffff0000 >> 16;
2032 demo_resize(&demo);
2033 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002034 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002035 default:
2036 break;
2037 }
2038 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2039}
2040
Karl Schultze4dc75c2016-02-02 15:37:51 -07002041static void demo_create_window(struct demo *demo) {
2042 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002043
2044 // Initialize the window class structure:
2045 win_class.cbSize = sizeof(WNDCLASSEX);
2046 win_class.style = CS_HREDRAW | CS_VREDRAW;
2047 win_class.lpfnWndProc = WndProc;
2048 win_class.cbClsExtra = 0;
2049 win_class.cbWndExtra = 0;
2050 win_class.hInstance = demo->connection; // hInstance
2051 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2052 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2053 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2054 win_class.lpszMenuName = NULL;
2055 win_class.lpszClassName = demo->name;
2056 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2057 // Register window class:
2058 if (!RegisterClassEx(&win_class)) {
2059 // It didn't work, so try to give a useful error:
2060 printf("Unexpected error trying to start the application!\n");
2061 fflush(stdout);
2062 exit(1);
2063 }
2064 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002065 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002066 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002067 demo->window = CreateWindowEx(0,
2068 demo->name, // class name
2069 demo->name, // app name
2070 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002071 WS_VISIBLE | WS_SYSMENU,
2072 100, 100, // x/y coords
2073 wr.right - wr.left, // width
2074 wr.bottom - wr.top, // height
2075 NULL, // handle to parent
2076 NULL, // handle to menu
2077 demo->connection, // hInstance
2078 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002079 if (!demo->window) {
2080 // It didn't work, so try to give a useful error:
2081 printf("Cannot create a window in which to draw!\n");
2082 fflush(stdout);
2083 exit(1);
2084 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002085 // Window client area size must be at least 1 pixel high, to prevent crash.
2086 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2087 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002088}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002089#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002090static void demo_create_xlib_window(struct demo *demo) {
2091
2092 demo->display = XOpenDisplay(NULL);
2093 long visualMask = VisualScreenMask;
2094 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002095 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002096 vInfoTemplate.screen = DefaultScreen(demo->display);
2097 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2098 &vInfoTemplate, &numberOfVisuals);
2099
2100 Colormap colormap = XCreateColormap(
2101 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2102 visualInfo->visual, AllocNone);
2103
Rene Lindsay11612722016-06-13 17:20:39 -06002104 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002105 windowAttributes.colormap = colormap;
2106 windowAttributes.background_pixel = 0xFFFFFFFF;
2107 windowAttributes.border_pixel = 0;
2108 windowAttributes.event_mask =
2109 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2110
2111 demo->xlib_window = XCreateWindow(
2112 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2113 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2114 visualInfo->visual,
2115 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2116
2117 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2118 XMapWindow(demo->display, demo->xlib_window);
2119 XFlush(demo->display);
2120 demo->xlib_wm_delete_window =
2121 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2122}
2123static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2124 switch(event->type) {
2125 case ClientMessage:
2126 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2127 demo->quit = true;
2128 break;
2129 case KeyPress:
2130 switch (event->xkey.keycode) {
2131 case 0x9: // Escape
2132 demo->quit = true;
2133 break;
2134 case 0x71: // left arrow key
2135 demo->spin_angle += demo->spin_increment;
2136 break;
2137 case 0x72: // right arrow key
2138 demo->spin_angle -= demo->spin_increment;
2139 break;
2140 case 0x41:
2141 demo->pause = !demo->pause;
2142 break;
2143 }
2144 break;
2145 case ConfigureNotify:
2146 if ((demo->width != event->xconfigure.width) ||
2147 (demo->height != event->xconfigure.height)) {
2148 demo->width = event->xconfigure.width;
2149 demo->height = event->xconfigure.height;
2150 demo_resize(demo);
2151 }
2152 break;
2153 default:
2154 break;
2155 }
2156
2157}
2158
2159static void demo_run_xlib(struct demo *demo) {
2160
2161 while (!demo->quit) {
2162 XEvent event;
2163
2164 if (demo->pause) {
2165 XNextEvent(demo->display, &event);
2166 demo_handle_xlib_event(demo, &event);
2167 } else {
2168 while (XPending(demo->display) > 0) {
2169 XNextEvent(demo->display, &event);
2170 demo_handle_xlib_event(demo, &event);
2171 }
2172 }
2173
2174 // Wait for work to finish before updating MVP.
2175 vkDeviceWaitIdle(demo->device);
2176 demo_update_data_buffer(demo);
2177
2178 demo_draw(demo);
2179
2180 // Wait for work to finish before updating MVP.
2181 vkDeviceWaitIdle(demo->device);
2182 demo->curFrame++;
2183 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2184 demo->quit = true;
2185 }
2186}
2187
2188static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002189 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002190 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002191 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002192 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002193 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002194 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002195 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002196 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2197 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002198 demo->quit = true;
2199 }
2200 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002201 case XCB_KEY_RELEASE: {
2202 const xcb_key_release_event_t *key =
2203 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002204
Karl Schultze4dc75c2016-02-02 15:37:51 -07002205 switch (key->detail) {
2206 case 0x9: // Escape
2207 demo->quit = true;
2208 break;
2209 case 0x71: // left arrow key
2210 demo->spin_angle += demo->spin_increment;
2211 break;
2212 case 0x72: // right arrow key
2213 demo->spin_angle -= demo->spin_increment;
2214 break;
2215 case 0x41:
2216 demo->pause = !demo->pause;
2217 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002218 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002219 } break;
2220 case XCB_CONFIGURE_NOTIFY: {
2221 const xcb_configure_notify_event_t *cfg =
2222 (const xcb_configure_notify_event_t *)event;
2223 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002224 demo->width = cfg->width;
2225 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002226 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002227 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002228 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002229 default:
2230 break;
2231 }
2232}
2233
Tony Barbour2593b182016-04-19 10:57:58 -06002234static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002235 xcb_flush(demo->connection);
2236
2237 while (!demo->quit) {
2238 xcb_generic_event_t *event;
2239
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002240 if (demo->pause) {
2241 event = xcb_wait_for_event(demo->connection);
2242 } else {
2243 event = xcb_poll_for_event(demo->connection);
2244 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002245 if (event) {
Tony Barbour2593b182016-04-19 10:57:58 -06002246 demo_handle_xcb_event(demo, event);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002247 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002248 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002249
2250 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002251 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002252 demo_update_data_buffer(demo);
2253
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002254 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002255
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002256 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002257 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002258 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002259 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002260 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002261 }
2262}
2263
Tony Barbour2593b182016-04-19 10:57:58 -06002264static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002265 uint32_t value_mask, value_list[32];
2266
Tony Barbour2593b182016-04-19 10:57:58 -06002267 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002268
2269 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2270 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002271 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002272 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002273
Tony Barbour2593b182016-04-19 10:57:58 -06002274 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002275 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2276 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2277 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002278
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002279 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002280 xcb_intern_atom_cookie_t cookie =
2281 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2282 xcb_intern_atom_reply_t *reply =
2283 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002284
Karl Schultze4dc75c2016-02-02 15:37:51 -07002285 xcb_intern_atom_cookie_t cookie2 =
2286 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2287 demo->atom_wm_delete_window =
2288 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002289
Tony Barbour2593b182016-04-19 10:57:58 -06002290 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002291 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002292 &(*demo->atom_wm_delete_window).atom);
2293 free(reply);
2294
Tony Barbour2593b182016-04-19 10:57:58 -06002295 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002296
Karl Schultze4dc75c2016-02-02 15:37:51 -07002297 // Force the x/y coordinates to 100,100 results are identical in consecutive
2298 // runs
2299 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002300 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002301 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002302}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002303#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2304static void demo_run(struct demo *demo) {
2305 while (!demo->quit) {
2306 // Wait for work to finish before updating MVP.
2307 vkDeviceWaitIdle(demo->device);
2308 demo_update_data_buffer(demo);
2309
2310 demo_draw(demo);
2311
2312 // Wait for work to finish before updating MVP.
2313 vkDeviceWaitIdle(demo->device);
2314 demo->curFrame++;
2315 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2316 demo->quit = true;
2317 }
2318}
2319
2320static void handle_ping(void *data UNUSED,
2321 struct wl_shell_surface *shell_surface,
2322 uint32_t serial) {
2323 wl_shell_surface_pong(shell_surface, serial);
2324}
2325
2326static void handle_configure(void *data UNUSED,
2327 struct wl_shell_surface *shell_surface UNUSED,
2328 uint32_t edges UNUSED, int32_t width UNUSED,
2329 int32_t height UNUSED) {}
2330
2331static void handle_popup_done(void *data UNUSED,
2332 struct wl_shell_surface *shell_surface UNUSED) {}
2333
2334static const struct wl_shell_surface_listener shell_surface_listener = {
2335 handle_ping, handle_configure, handle_popup_done};
2336
2337static void demo_create_window(struct demo *demo) {
2338 demo->window = wl_compositor_create_surface(demo->compositor);
2339 if (!demo->window) {
2340 printf("Can not create wayland_surface from compositor!\n");
2341 fflush(stdout);
2342 exit(1);
2343 }
2344
2345 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2346 if (!demo->shell_surface) {
2347 printf("Can not get shell_surface from wayland_surface!\n");
2348 fflush(stdout);
2349 exit(1);
2350 }
2351 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2352 demo);
2353 wl_shell_surface_set_toplevel(demo->shell_surface);
2354 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2355}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002356#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2357static void demo_run(struct demo *demo) {
2358 if (!demo->prepared)
2359 return;
2360
2361 // Wait for work to finish before updating MVP.
2362 vkDeviceWaitIdle(demo->device);
2363 demo_update_data_buffer(demo);
2364
2365 demo_draw(demo);
2366
2367 // Wait for work to finish before updating MVP.
2368 vkDeviceWaitIdle(demo->device);
2369
2370 demo->curFrame++;
2371}
2372#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002373
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002374/*
2375 * Return 1 (true) if all layer names specified in check_names
2376 * can be found in given layer properties.
2377 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002378static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002379 uint32_t layer_count,
2380 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002381 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002382 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002383 for (uint32_t j = 0; j < layer_count; j++) {
2384 if (!strcmp(check_names[i], layers[j].layerName)) {
2385 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002386 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002387 }
2388 }
2389 if (!found) {
2390 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2391 return 0;
2392 }
2393 }
2394 return 1;
2395}
2396
Karl Schultze4dc75c2016-02-02 15:37:51 -07002397static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002398 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002399 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002400 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002401 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002402 char **instance_validation_layers = NULL;
2403 demo->enabled_extension_count = 0;
2404 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002405
Karl Schultz7c50ad62016-04-01 12:07:03 -06002406 char *instance_validation_layers_alt1[] = {
2407 "VK_LAYER_LUNARG_standard_validation"
2408 };
2409
2410 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002411 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2412 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2413 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2414 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002415 };
2416
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002417 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002418 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002419 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002420
Karl Schultz7c50ad62016-04-01 12:07:03 -06002421 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002422 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002423
Karl Schultz7c50ad62016-04-01 12:07:03 -06002424 instance_validation_layers = instance_validation_layers_alt1;
2425 if (instance_layer_count > 0) {
2426 VkLayerProperties *instance_layers =
2427 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2428 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2429 instance_layers);
2430 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002431
Karl Schultz7c50ad62016-04-01 12:07:03 -06002432
2433 validation_found = demo_check_layers(
2434 ARRAY_SIZE(instance_validation_layers_alt1),
2435 instance_validation_layers, instance_layer_count,
2436 instance_layers);
2437 if (validation_found) {
2438 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002439 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2440 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002441 } else {
2442 // use alternative set of validation layers
2443 instance_validation_layers = instance_validation_layers_alt2;
2444 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2445 validation_found = demo_check_layers(
2446 ARRAY_SIZE(instance_validation_layers_alt2),
2447 instance_validation_layers, instance_layer_count,
2448 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002449 validation_layer_count =
2450 ARRAY_SIZE(instance_validation_layers_alt2);
2451 for (uint32_t i = 0; i < validation_layer_count; i++) {
2452 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002453 }
2454 }
2455 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002456 }
Dustin Graves7c287352016-01-27 13:48:06 -07002457
Karl Schultz7c50ad62016-04-01 12:07:03 -06002458 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002459 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002460 "required validation layer.\n\n"
2461 "Please look at the Getting Started guide for additional "
2462 "information.\n",
2463 "vkCreateInstance Failure");
2464 }
Dustin Graves7c287352016-01-27 13:48:06 -07002465 }
2466
2467 /* Look for instance extensions */
2468 VkBool32 surfaceExtFound = 0;
2469 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002470#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002471 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002472#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002473 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002474
Karl Schultze4dc75c2016-02-02 15:37:51 -07002475 err = vkEnumerateInstanceExtensionProperties(
2476 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002477 assert(!err);
2478
Dustin Graves7c287352016-01-27 13:48:06 -07002479 if (instance_extension_count > 0) {
2480 VkExtensionProperties *instance_extensions =
2481 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2482 err = vkEnumerateInstanceExtensionProperties(
2483 NULL, &instance_extension_count, instance_extensions);
2484 assert(!err);
2485 for (uint32_t i = 0; i < instance_extension_count; i++) {
2486 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2487 instance_extensions[i].extensionName)) {
2488 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002489 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002490 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002491 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002492#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002493 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2494 instance_extensions[i].extensionName)) {
2495 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002496 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002497 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2498 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002499#endif
2500#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002501 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2502 instance_extensions[i].extensionName)) {
2503 platformSurfaceExtFound = 1;
2504 xlibSurfaceExtFound = 1;
2505 demo->extension_names[demo->enabled_extension_count++] =
2506 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2507 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002508#endif
2509#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002510 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2511 instance_extensions[i].extensionName)) {
2512 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002513 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002514 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2515 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002516#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002517#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2518 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2519 instance_extensions[i].extensionName)) {
2520 platformSurfaceExtFound = 1;
2521 demo->extension_names[demo->enabled_extension_count++] =
2522 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2523 }
2524#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002525#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002526 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2527 instance_extensions[i].extensionName)) {
2528 platformSurfaceExtFound = 1;
2529 demo->extension_names[demo->enabled_extension_count++] =
2530 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2531 }
2532#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002533 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2534 instance_extensions[i].extensionName)) {
2535 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002536 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002537 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2538 }
2539 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002540 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002541 }
Dustin Graves7c287352016-01-27 13:48:06 -07002542
2543 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002544 }
Dustin Graves7c287352016-01-27 13:48:06 -07002545
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002546 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002547 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2548 "the " VK_KHR_SURFACE_EXTENSION_NAME
2549 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002550 "Vulkan installable client driver (ICD) installed?\nPlease "
2551 "look at the Getting Started guide for additional "
2552 "information.\n",
2553 "vkCreateInstance Failure");
2554 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002555 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002556#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002557 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2558 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2559 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002560 "Vulkan installable client driver (ICD) installed?\nPlease "
2561 "look at the Getting Started guide for additional "
2562 "information.\n",
2563 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002564#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002565 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2566 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2567 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002568 "Vulkan installable client driver (ICD) installed?\nPlease "
2569 "look at the Getting Started guide for additional "
2570 "information.\n",
2571 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002572#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2573 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2574 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2575 " extension.\n\nDo you have a compatible "
2576 "Vulkan installable client driver (ICD) installed?\nPlease "
2577 "look at the Getting Started guide for additional "
2578 "information.\n",
2579 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002580#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2581 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2582 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2583 " extension.\n\nDo you have a compatible "
2584 "Vulkan installable client driver (ICD) installed?\nPlease "
2585 "look at the Getting Started guide for additional "
2586 "information.\n",
2587 "vkCreateInstance Failure");
2588#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002589 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002590#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002591 if (demo->use_xlib && !xlibSurfaceExtFound) {
2592 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2593 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2594 " extension.\n\nDo you have a compatible "
2595 "Vulkan installable client driver (ICD) installed?\nPlease "
2596 "look at the Getting Started guide for additional "
2597 "information.\n",
2598 "vkCreateInstance Failure");
2599 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002600#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002601 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002602 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002603 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002604 .pApplicationName = APP_SHORT_NAME,
2605 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002606 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002607 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002608 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002609 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002610 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002611 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002612 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002613 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002614 .enabledLayerCount = demo->enabled_layer_count,
2615 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2616 .enabledExtensionCount = demo->enabled_extension_count,
2617 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002618 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002619
2620 /*
2621 * This is info for a temp callback to use during CreateInstance.
2622 * After the instance is created, we use the instance-based
2623 * function to register the final callback.
2624 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002625 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002626 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002627 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2628 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002629 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002630 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002631 dbgCreateInfo.flags =
2632 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2633 inst_info.pNext = &dbgCreateInfo;
2634 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002635
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002636 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002637
Chia-I Wu63636062015-10-26 21:10:41 +08002638 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002639 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2640 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002641 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002642 "additional information.\n",
2643 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002644 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002645 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002646 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002647 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002648 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002649 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2650 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002651 "the Getting Started guide for additional information.\n",
2652 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002653 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002654
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002655 /* Make initial call to query gpu_count, then second call for gpu info*/
2656 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2657 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002658
2659 if (gpu_count > 0) {
2660 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2661 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2662 assert(!err);
2663 /* For cube demo we just grab the first physical device */
2664 demo->gpu = physical_devices[0];
2665 free(physical_devices);
2666 } else {
2667 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2668 "Do you have a compatible Vulkan installable client driver (ICD) "
2669 "installed?\nPlease look at the Getting Started guide for "
2670 "additional information.\n",
2671 "vkEnumeratePhysicalDevices Failure");
2672 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002673
Dustin Graves7c287352016-01-27 13:48:06 -07002674 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002675 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002676 VkBool32 swapchainExtFound = 0;
2677 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002678 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002679
Karl Schultze4dc75c2016-02-02 15:37:51 -07002680 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2681 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002682 assert(!err);
2683
Dustin Graves7c287352016-01-27 13:48:06 -07002684 if (device_extension_count > 0) {
2685 VkExtensionProperties *device_extensions =
2686 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2687 err = vkEnumerateDeviceExtensionProperties(
2688 demo->gpu, NULL, &device_extension_count, device_extensions);
2689 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002690
Dustin Graves7c287352016-01-27 13:48:06 -07002691 for (uint32_t i = 0; i < device_extension_count; i++) {
2692 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2693 device_extensions[i].extensionName)) {
2694 swapchainExtFound = 1;
2695 demo->extension_names[demo->enabled_extension_count++] =
2696 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2697 }
2698 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002699 }
Dustin Graves7c287352016-01-27 13:48:06 -07002700
2701 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002702 }
Dustin Graves7c287352016-01-27 13:48:06 -07002703
Tony Barbourcc69f452015-10-08 13:59:42 -06002704 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002705 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2706 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2707 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002708 "Vulkan installable client driver (ICD) installed?\nPlease "
2709 "look at the Getting Started guide for additional "
2710 "information.\n",
2711 "vkCreateInstance Failure");
2712 }
2713
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002714 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002715 demo->CreateDebugReportCallback =
2716 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2717 demo->inst, "vkCreateDebugReportCallbackEXT");
2718 demo->DestroyDebugReportCallback =
2719 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2720 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002721 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002722 ERR_EXIT(
2723 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2724 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002725 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002726 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002727 ERR_EXIT(
2728 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2729 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002730 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002731 demo->DebugReportMessage =
2732 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2733 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002734 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002735 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002736 "vkGetProcAddr Failure");
2737 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002738
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002739 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002740 PFN_vkDebugReportCallbackEXT callback;
2741 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002742 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002743 dbgCreateInfo.pNext = NULL;
2744 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002745 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002746 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002747 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002748 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2749 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002750 switch (err) {
2751 case VK_SUCCESS:
2752 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002753 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002754 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2755 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002756 break;
2757 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002758 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2759 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002760 break;
2761 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002762 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002763 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002764
2765 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002766 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2767 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002768 assert(demo->queue_count >= 1);
2769
Karl Schultze4dc75c2016-02-02 15:37:51 -07002770 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2771 demo->queue_count * sizeof(VkQueueFamilyProperties));
2772 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2773 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002774 // Find a queue that supports gfx
2775 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002776 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2777 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002778 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2779 break;
2780 }
2781 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002782 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002783 // If app has specific feature requirements it should check supported
2784 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002785 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002786 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002787
Tony Barbourda2bad52016-01-22 14:36:40 -07002788 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2789 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2790 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2791 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2792 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2793}
2794
Karl Schultze4dc75c2016-02-02 15:37:51 -07002795static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002796 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002797 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002798 const VkDeviceQueueCreateInfo queue = {
2799 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2800 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002801 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002802 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002803 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002804
2805 VkDeviceCreateInfo device = {
2806 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2807 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002808 .queueCreateInfoCount = 1,
2809 .pQueueCreateInfos = &queue,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002810 .enabledLayerCount = 0,
2811 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002812 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002813 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2814 .pEnabledFeatures =
2815 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002816 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002817
Chia-I Wu63636062015-10-26 21:10:41 +08002818 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002819 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002820}
2821
Karl Schultze4dc75c2016-02-02 15:37:51 -07002822static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002823 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002824 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002825
Karl Schultze4dc75c2016-02-02 15:37:51 -07002826// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002827#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002828 VkWin32SurfaceCreateInfoKHR createInfo;
2829 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2830 createInfo.pNext = NULL;
2831 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002832 createInfo.hinstance = demo->connection;
2833 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002834
Karl Schultze4dc75c2016-02-02 15:37:51 -07002835 err =
2836 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002837#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2838 VkWaylandSurfaceCreateInfoKHR createInfo;
2839 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2840 createInfo.pNext = NULL;
2841 createInfo.flags = 0;
2842 createInfo.display = demo->display;
2843 createInfo.surface = demo->window;
2844
2845 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2846 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002847#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2848 VkAndroidSurfaceCreateInfoKHR createInfo;
2849 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2850 createInfo.pNext = NULL;
2851 createInfo.flags = 0;
2852 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002853
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002854 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2855#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002856 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002857#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002858 VkXlibSurfaceCreateInfoKHR createInfo;
2859 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2860 createInfo.pNext = NULL;
2861 createInfo.flags = 0;
2862 createInfo.dpy = demo->display;
2863 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002864
Tony Barbour2593b182016-04-19 10:57:58 -06002865 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2866 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002867#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002868 }
2869 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002870#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002871 VkXcbSurfaceCreateInfoKHR createInfo;
2872 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2873 createInfo.pNext = NULL;
2874 createInfo.flags = 0;
2875 createInfo.connection = demo->connection;
2876 createInfo.window = demo->xcb_window;
2877
2878 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002879#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002880 }
Tony Barbour2593b182016-04-19 10:57:58 -06002881 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002882
Tony Barbourcc69f452015-10-08 13:59:42 -06002883 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002884 VkBool32 *supportsPresent =
2885 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002886 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002887 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002888 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002889 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002890
2891 // Search for a graphics and a present queue in the array of queue
2892 // families, try to find one that supports both
2893 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002894 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002895 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002896 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2897 if (graphicsQueueNodeIndex == UINT32_MAX) {
2898 graphicsQueueNodeIndex = i;
2899 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002900
Ian Elliottaaae5352015-07-06 14:27:58 -06002901 if (supportsPresent[i] == VK_TRUE) {
2902 graphicsQueueNodeIndex = i;
2903 presentQueueNodeIndex = i;
2904 break;
2905 }
2906 }
2907 }
2908 if (presentQueueNodeIndex == UINT32_MAX) {
2909 // If didn't find a queue that supports both graphics and present, then
2910 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002911 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002912 if (supportsPresent[i] == VK_TRUE) {
2913 presentQueueNodeIndex = i;
2914 break;
2915 }
2916 }
2917 }
2918 free(supportsPresent);
2919
2920 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002921 if (graphicsQueueNodeIndex == UINT32_MAX ||
2922 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002923 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002924 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002925 }
2926
2927 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002928 // synchronization, and appropriate tracking for QueueSubmit.
2929 // NOTE: While it is possible for an application to use a separate graphics
2930 // and a present queues, this demo program assumes it is only using
2931 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002932 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2933 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002934 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002935 }
2936
2937 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002938
Tony Barbourda2bad52016-01-22 14:36:40 -07002939 demo_create_device(demo);
2940
2941 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2942 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2943 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2944 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2945 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2946
Karl Schultze4dc75c2016-02-02 15:37:51 -07002947 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2948 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002949
Ian Elliottaaae5352015-07-06 14:27:58 -06002950 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002951 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002952 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002953 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002954 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002955 VkSurfaceFormatKHR *surfFormats =
2956 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002957 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002958 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002959 assert(!err);
2960 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2961 // the surface has no preferred format. Otherwise, at least one
2962 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002963 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002964 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002965 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002966 assert(formatCount >= 1);
2967 demo->format = surfFormats[0].format;
2968 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002969 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002970
David Pinedo2bb7c932015-06-18 17:03:14 -06002971 demo->quit = false;
2972 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002973
2974 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002975 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002976}
2977
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002978#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2979static void registry_handle_global(void *data, struct wl_registry *registry,
2980 uint32_t name, const char *interface,
2981 uint32_t version UNUSED) {
2982 struct demo *demo = data;
2983 if (strcmp(interface, "wl_compositor") == 0) {
2984 demo->compositor =
2985 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
2986 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
2987 * tp xdg_shell */
2988 } else if (strcmp(interface, "wl_shell") == 0) {
2989 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
2990 }
2991}
2992
2993static void registry_handle_global_remove(void *data UNUSED,
2994 struct wl_registry *registry UNUSED,
2995 uint32_t name UNUSED) {}
2996
2997static const struct wl_registry_listener registry_listener = {
2998 registry_handle_global, registry_handle_global_remove};
2999#endif
3000
Karl Schultze4dc75c2016-02-02 15:37:51 -07003001static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003002#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003003 const xcb_setup_t *setup;
3004 xcb_screen_iterator_t iter;
3005 int scr;
3006
3007 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06003008 if (demo->connection == NULL) {
3009 printf("Cannot find a compatible Vulkan installable client driver "
3010 "(ICD).\nExiting ...\n");
3011 fflush(stdout);
3012 exit(1);
3013 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003014
3015 setup = xcb_get_setup(demo->connection);
3016 iter = xcb_setup_roots_iterator(setup);
3017 while (scr-- > 0)
3018 xcb_screen_next(&iter);
3019
3020 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003021#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3022 demo->display = wl_display_connect(NULL);
3023
3024 if (demo->display == NULL) {
3025 printf("Cannot find a compatible Vulkan installable client driver "
3026 "(ICD).\nExiting ...\n");
3027 fflush(stdout);
3028 exit(1);
3029 }
3030
3031 demo->registry = wl_display_get_registry(demo->display);
3032 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3033 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003034#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003035}
3036
Karl Schultze4dc75c2016-02-02 15:37:51 -07003037static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003038 vec3 eye = {0.0f, 3.0f, 5.0f};
3039 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003040 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003041
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003042 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003043 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003044
Piers Daniell735ee532015-02-23 16:23:13 -07003045 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003046 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003047 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003048 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003049 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003050 if (strcmp(argv[i], "--break") == 0) {
3051 demo->use_break = true;
3052 continue;
3053 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003054 if (strcmp(argv[i], "--validate") == 0) {
3055 demo->validate = true;
3056 continue;
3057 }
Tony Barbour2593b182016-04-19 10:57:58 -06003058 if (strcmp(argv[i], "--xlib") == 0) {
3059 demo->use_xlib = true;
3060 continue;
3061 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003062 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3063 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3064 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003065 i++;
3066 continue;
3067 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003068 if (strcmp(argv[i], "--suppress_popups") == 0) {
3069 demo->suppress_popups = true;
3070 continue;
3071 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003072
Karl Schultze4dc75c2016-02-02 15:37:51 -07003073 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
lenny-lunargfbe22392016-06-06 11:07:53 -06003074 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003075 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003076 fflush(stderr);
3077 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003078 }
3079
Tony Barbour2593b182016-04-19 10:57:58 -06003080 if (!demo->use_xlib)
3081 demo_init_connection(demo);
3082
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003083 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003084
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003085 demo->width = 500;
3086 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003087
3088 demo->spin_angle = 0.01f;
3089 demo->spin_increment = 0.01f;
3090 demo->pause = false;
3091
Karl Schultze4dc75c2016-02-02 15:37:51 -07003092 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3093 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003094 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3095 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003096}
3097
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003098#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003099// Include header required for parsing the command line options.
3100#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003101
Karl Schultze4dc75c2016-02-02 15:37:51 -07003102int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3103 int nCmdShow) {
3104 MSG msg; // message
3105 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003106 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003107 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003108
Karl Schultze4dc75c2016-02-02 15:37:51 -07003109 // Use the CommandLine functions to get the command line arguments.
3110 // Unfortunately, Microsoft outputs
3111 // this information as wide characters for Unicode, and we simply want the
3112 // Ascii version to be compatible
3113 // with the non-Windows side. So, we have to convert the information to
3114 // Ascii character strings.
3115 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3116 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003117 argc = 0;
3118 }
3119
Karl Schultze4dc75c2016-02-02 15:37:51 -07003120 if (argc > 0) {
3121 argv = (char **)malloc(sizeof(char *) * argc);
3122 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003123 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003124 } else {
3125 for (int iii = 0; iii < argc; iii++) {
3126 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003127 size_t numConverted = 0;
3128
Karl Schultze4dc75c2016-02-02 15:37:51 -07003129 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3130 if (argv[iii] != NULL) {
3131 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3132 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003133 }
3134 }
3135 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003136 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003137 argv = NULL;
3138 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003139
3140 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003141
3142 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003143 if (argc > 0 && argv != NULL) {
3144 for (int iii = 0; iii < argc; iii++) {
3145 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003146 free(argv[iii]);
3147 }
3148 }
3149 free(argv);
3150 }
3151
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003152 demo.connection = hInstance;
3153 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003154 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003155 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003156
3157 demo_prepare(&demo);
3158
Karl Schultze4dc75c2016-02-02 15:37:51 -07003159 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003160
3161 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003162 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003163 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003164 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003165 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003166 done = true; // if found, quit app
3167 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003168 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003169 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003170 DispatchMessage(&msg);
3171 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003172 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003173 }
3174
3175 demo_cleanup(&demo);
3176
Karl Schultze4dc75c2016-02-02 15:37:51 -07003177 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003178}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003179#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3180#include <android/log.h>
3181#include <android_native_app_glue.h>
3182static bool initialized = false;
3183static bool active = false;
3184struct demo demo;
3185
3186static int32_t processInput(struct android_app* app, AInputEvent* event) {
3187 return 0;
3188}
3189
3190static void processCommand(struct android_app* app, int32_t cmd) {
3191 switch(cmd) {
3192 case APP_CMD_INIT_WINDOW: {
3193 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003194 // We're getting a new window. If the app is starting up, we
3195 // need to initialize. If the app has already been
3196 // initialized, that means that we lost our previous window,
3197 // which means that we have a lot of work to do. At a minimum,
3198 // we need to destroy the swapchain and surface associated with
3199 // the old window, and create a new surface and swapchain.
3200 // However, since there are a lot of other objects/state that
3201 // is tied to the swapchain, it's easiest to simply cleanup and
3202 // start over (i.e. use a brute-force approach of re-starting
3203 // the app)
3204 if (demo.prepared) {
3205 demo_cleanup(&demo);
3206 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003207 demo_init(&demo, 0, NULL);
3208 demo.window = (void*)app->window;
3209 demo_init_vk_swapchain(&demo);
3210 demo_prepare(&demo);
3211 initialized = true;
3212 }
3213 break;
3214 }
3215 case APP_CMD_GAINED_FOCUS: {
3216 active = true;
3217 break;
3218 }
3219 case APP_CMD_LOST_FOCUS: {
3220 active = false;
3221 break;
3222 }
3223 }
3224}
3225
3226void android_main(struct android_app *app)
3227{
3228 app_dummy();
3229
Cody Northrop8707a6e2016-04-26 19:59:19 -06003230#ifdef ANDROID
3231 int vulkanSupport = InitVulkan();
3232 if (vulkanSupport == 0)
3233 return;
3234#endif
3235
Ian Elliottf05d5d12016-05-17 12:03:35 -06003236 demo.prepared = false;
3237
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003238 app->onAppCmd = processCommand;
3239 app->onInputEvent = processInput;
3240
3241 while(1) {
3242 int events;
3243 struct android_poll_source* source;
3244 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3245 if (source) {
3246 source->process(app, source);
3247 }
3248
3249 if (app->destroyRequested != 0) {
3250 demo_cleanup(&demo);
3251 return;
3252 }
3253 }
3254 if (initialized && active) {
3255 demo_run(&demo);
3256 }
3257 }
3258
3259}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003260#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003261int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003262 struct demo demo;
3263
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003264 demo_init(&demo, argc, argv);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003265#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003266 if (demo.use_xlib)
3267 demo_create_xlib_window(&demo);
3268 else
3269 demo_create_xcb_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003270#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3271 demo_create_window(&demo);
3272#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003273
Tony Barbourcc69f452015-10-08 13:59:42 -06003274 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003275
3276 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003277
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003278#if defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003279 if (demo.use_xlib)
3280 demo_run_xlib(&demo);
3281 else
3282 demo_run_xcb(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003283#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3284 demo_run(&demo);
3285#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003286
3287 demo_cleanup(&demo);
3288
Karl Schultz0218c002016-03-22 17:06:13 -06003289 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003290}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003291#endif