blob: 386b61370b4755a09ab1068422ed424012dfef05 [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>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090032#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060033#include <X11/Xutil.h>
34#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060035
Ian Elliott639ca472015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060038#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060039#endif // _WIN32
40
Cody Northrop8707a6e2016-04-26 19:59:19 -060041#ifdef ANDROID
42#include "vulkan_wrapper.h"
43#else
David Pinedoc5193c12015-11-06 12:54:48 -070044#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060045#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070046
David Pinedo541526f2015-11-24 09:00:24 -070047#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048#include "linmath.h"
49
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090062#if defined(__GNUC__)
63#define UNUSED __attribute__((unused))
64#else
65#define UNUSED
66#endif
67
Ian Elliott07264132015-04-28 11:35:02 -060068#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060069bool in_callback = false;
Karl Schultze4dc75c2016-02-02 15:37:51 -070070#define ERR_EXIT(err_msg, err_class) \
71 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060072 if (!demo->suppress_popups) \
73 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070074 exit(1); \
75 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060076
Michael Lentinec6cde4b2016-04-18 13:20:45 -050077#elif defined __ANDROID__
78#include <android/log.h>
79#define ERR_EXIT(err_msg, err_class) \
80 do { \
81 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
82 exit(1); \
83 } while (0)
84#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070085#define ERR_EXIT(err_msg, err_class) \
86 do { \
87 printf(err_msg); \
88 fflush(stdout); \
89 exit(1); \
90 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050091#endif
Ian Elliott07264132015-04-28 11:35:02 -060092
Karl Schultze4dc75c2016-02-02 15:37:51 -070093#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
94 { \
95 demo->fp##entrypoint = \
96 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
97 if (demo->fp##entrypoint == NULL) { \
98 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
99 "vkGetInstanceProcAddr Failure"); \
100 } \
101 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600102
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600103static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
104
Karl Schultze4dc75c2016-02-02 15:37:51 -0700105#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
106 { \
107 if (!g_gdpa) \
108 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
109 demo->inst, "vkGetDeviceProcAddr"); \
110 demo->fp##entrypoint = \
111 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
112 if (demo->fp##entrypoint == NULL) { \
113 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
114 "vkGetDeviceProcAddr Failure"); \
115 } \
116 }
Ian Elliott673898b2015-06-22 15:07:49 -0600117
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600118/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700119 * structure to track all objects related to a texture.
120 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600121struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600122 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700123
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600124 VkImage image;
125 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600126
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800127 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500128 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600129 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700130 int32_t tex_width, tex_height;
131};
132
Karl Schultze4dc75c2016-02-02 15:37:51 -0700133static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600134
Karl Schultz0218c002016-03-22 17:06:13 -0600135static int validation_error = 0;
136
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600137struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600138 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700139 float mvp[4][4];
140 float position[12 * 3][4];
141 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142};
143
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600144struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600145 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700146 float mvp[4][4];
147 float position[12 * 3][4];
148 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600150
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600151//--------------------------------------------------------------------------------------
152// Mesh and VertexFormat Data
153//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700154// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600155static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 -1.0f,-1.0f, 1.0f,
158 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800159 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 -1.0f, 1.0f,-1.0f,
161 -1.0f,-1.0f,-1.0f,
162
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f, 1.0f,-1.0f,
165 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 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,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600175 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f, 1.0f,-1.0f, // +Y 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, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f, 1.0f, 1.0f,
186 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,
189 1.0f, 1.0f,-1.0f,
190
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600193 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,
196 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600197};
198
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600199static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700200 0.0f, 1.0f, // -X side
201 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800203 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600204 0.0f, 0.0f,
205 0.0f, 1.0f,
206
Rene Lindsay76867e82016-07-29 10:49:11 -0700207 1.0f, 1.0f, // -Z side
208 0.0f, 0.0f,
209 0.0f, 1.0f,
210 1.0f, 1.0f,
211 1.0f, 0.0f,
212 0.0f, 0.0f,
213
214 1.0f, 0.0f, // -Y side
215 1.0f, 1.0f,
216 0.0f, 1.0f,
217 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600218 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220
Rene Lindsay76867e82016-07-29 10:49:11 -0700221 1.0f, 0.0f, // +Y side
222 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700225 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600226 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227
Rene Lindsay76867e82016-07-29 10:49:11 -0700228 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800229 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700230 0.0f, 1.0f,
231 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700234
235 0.0f, 0.0f, // +Z side
236 0.0f, 1.0f,
237 1.0f, 0.0f,
238 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600239 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700240 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600241};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700242// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600243
Karl Schultze4dc75c2016-02-02 15:37:51 -0700244void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600245 int i;
246
247 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
250 }
251 printf("\n");
252 fflush(stdout);
253}
254
Karl Schultze4dc75c2016-02-02 15:37:51 -0700255void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600256 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700257 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600258 printf("\n");
259 fflush(stdout);
260}
261
Karl Schultze4dc75c2016-02-02 15:37:51 -0700262VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600263BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
264 uint64_t srcObject, size_t location, int32_t msgCode,
265 const char *pLayerPrefix, const char *pMsg,
266 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700267#ifndef WIN32
268 raise(SIGTRAP);
269#else
270 DebugBreak();
271#endif
272
273 return false;
274}
275
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600276typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600277 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800278 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600279 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600280} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600281
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600282struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500283#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600284#define APP_NAME_STR_LEN 80
285 HINSTANCE connection; // hInstance - Windows Instance
286 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700287 HWND window; // hWnd - window handle
Rene Lindsayb9403da2016-07-01 18:00:30 -0600288 POINT minsize; // minimum window size
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600289#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600290 Display* display;
291 Window xlib_window;
292 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600293
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600294 xcb_connection_t *connection;
295 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600296 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800297 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900298#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
299 struct wl_display *display;
300 struct wl_registry *registry;
301 struct wl_compositor *compositor;
302 struct wl_surface *window;
303 struct wl_shell *shell;
304 struct wl_shell_surface *shell_surface;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500305#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
306 ANativeWindow* window;
307#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700308 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600309 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700310 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600311 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600313 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600314 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600316 VkQueue graphics_queue;
317 VkQueue present_queue;
318 uint32_t graphics_queue_family_index;
319 uint32_t present_queue_family_index;
Tony Barbource7524e2016-07-28 12:01:45 -0600320 VkSemaphore image_acquired_semaphore;
321 VkSemaphore draw_complete_semaphore;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600322 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600323 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600324 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325
Tony Barbourda2bad52016-01-22 14:36:40 -0700326 uint32_t enabled_extension_count;
327 uint32_t enabled_layer_count;
328 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600329 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700330
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600333 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
Karl Schultze4dc75c2016-02-02 15:37:51 -0700335 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
336 fpGetPhysicalDeviceSurfaceSupportKHR;
337 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
338 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
339 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
340 fpGetPhysicalDeviceSurfaceFormatsKHR;
341 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
342 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600343 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
344 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
345 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
346 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
347 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600348 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600349 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600350 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800352 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
354 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600355 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600356
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600357 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800358 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500359 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600360 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361 } depth;
362
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600363 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
365 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800367 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500368 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600369 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600370 } uniform_data;
371
Karl Schultze4dc75c2016-02-02 15:37:51 -0700372 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500373 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600375 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800376 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600379 mat4x4 projection_matrix;
380 mat4x4 view_matrix;
381 mat4x4 model_matrix;
382
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600383 float spin_angle;
384 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600385 bool pause;
386
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600387 VkShaderModule vert_shader_module;
388 VkShaderModule frag_shader_module;
389
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600390 VkDescriptorPool desc_pool;
391 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800392
Tony Barbourd8e504f2015-10-08 14:26:24 -0600393 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800394
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600396 int32_t curFrame;
397 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600398 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600399 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600400 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700401 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
402 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
403 VkDebugReportCallbackEXT msg_callback;
404 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600405
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600406 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600407 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600408};
409
lenny-lunargfbe22392016-06-06 11:07:53 -0600410VKAPI_ATTR VkBool32 VKAPI_CALL
411dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
412 uint64_t srcObject, size_t location, int32_t msgCode,
413 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
414 char *message = (char *)malloc(strlen(pMsg) + 100);
415
416 assert(message);
417
418 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
419 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
420 pMsg);
421 validation_error = 1;
422 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
423 // We know that we're submitting queues without fences, ignore this
424 // warning
425 if (strstr(pMsg,
426 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
427 return false;
428 }
429 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
430 pMsg);
431 validation_error = 1;
432 } else {
433 validation_error = 1;
434 return false;
435 }
436
437#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -0600438 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600439 struct demo *demo = (struct demo*) pUserData;
440 if (!demo->suppress_popups)
441 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600442 in_callback = false;
lenny-lunargfbe22392016-06-06 11:07:53 -0600443#else
444 printf("%s\n", message);
445 fflush(stdout);
446#endif
447 free(message);
448
449 /*
450 * false indicates that layer should not bail-out of an
451 * API call that had validation failures. This may mean that the
452 * app dies inside the driver due to invalid parameter(s).
453 * That's what would happen without validation layers, so we'll
454 * keep that behavior here.
455 */
456 return false;
457}
458
Ian Elliottcf074d32015-10-16 18:02:43 -0600459// Forward declaration:
460static void demo_resize(struct demo *demo);
461
Karl Schultze4dc75c2016-02-02 15:37:51 -0700462static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
463 VkFlags requirements_mask,
464 uint32_t *typeIndex) {
465 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600466 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700467 if ((typeBits & 1) == 1) {
468 // Type is available, does it match user properties?
469 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
470 requirements_mask) == requirements_mask) {
471 *typeIndex = i;
472 return true;
473 }
474 }
475 typeBits >>= 1;
476 }
477 // No memory types matched, return failure
478 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600479}
480
Karl Schultze4dc75c2016-02-02 15:37:51 -0700481static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600482 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600483
Tony Barbource7524e2016-07-28 12:01:45 -0600484 // This function could get called twice if the texture uses a staging buffer
485 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600486 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487 return;
488
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600490 assert(!err);
491
Tony Barbource7524e2016-07-28 12:01:45 -0600492 VkFence fence;
493 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
494 .pNext = NULL,
495 .flags = 0};
496 vkCreateFence(demo->device, &fence_ci, NULL, &fence);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700497 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700498 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
499 .pNext = NULL,
500 .waitSemaphoreCount = 0,
501 .pWaitSemaphores = NULL,
502 .pWaitDstStageMask = NULL,
503 .commandBufferCount = 1,
504 .pCommandBuffers = cmd_bufs,
505 .signalSemaphoreCount = 0,
506 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600507
Tony Barbource7524e2016-07-28 12:01:45 -0600508 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509 assert(!err);
510
Tony Barbource7524e2016-07-28 12:01:45 -0600511 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600512 assert(!err);
513
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600514 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600515 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600517}
518
Karl Schultze4dc75c2016-02-02 15:37:51 -0700519static void demo_set_image_layout(struct demo *demo, VkImage image,
520 VkImageAspectFlags aspectMask,
521 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700522 VkImageLayout new_image_layout,
523 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600524 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800527 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800528 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800530 .commandPool = demo->cmd_pool,
531 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700532 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 };
534
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800535 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600536 assert(!err);
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700537 VkCommandBufferBeginInfo cmd_buf_info = {
538 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
539 .pNext = NULL,
540 .flags = 0,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700541 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700542 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600543 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600544 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600545 }
546
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600547 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600548 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700550 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800551 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552 .oldLayout = old_image_layout,
553 .newLayout = new_image_layout,
554 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700555 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600556
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800557 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600558 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800559 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600560 }
561
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700562 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700563 image_memory_barrier.dstAccessMask =
564 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700565 }
566
567 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700568 image_memory_barrier.dstAccessMask =
569 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700570 }
571
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600572 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600573 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700574 image_memory_barrier.dstAccessMask =
575 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600576 }
577
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600578 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600579
Tony Barbour50bbca42015-06-29 16:20:35 -0600580 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
581 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582
Karl Schultze4dc75c2016-02-02 15:37:51 -0700583 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
584 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585}
586
Karl Schultze4dc75c2016-02-02 15:37:51 -0700587static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700588 const VkCommandBufferBeginInfo cmd_buf_info = {
589 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
590 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600591 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700592 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700593 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800594 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700595 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
596 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800597 };
598 const VkRenderPassBeginInfo rp_begin = {
599 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
600 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800601 .renderPass = demo->render_pass,
602 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800603 .renderArea.offset.x = 0,
604 .renderArea.offset.y = 0,
605 .renderArea.extent.width = demo->width,
606 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600607 .clearValueCount = 2,
608 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700609 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800610 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600611
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600612 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613 assert(!err);
614
Tony Barbour5c5b1632016-04-26 11:13:48 -0600615 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
616 // happens to the previous contents of the image
617 VkImageMemoryBarrier image_memory_barrier = {
618 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
619 .pNext = NULL,
620 .srcAccessMask = 0,
621 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
622 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
623 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
624 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
625 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
626 .image = demo->buffers[demo->current_buffer].image,
627 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
628
Tony Barbource7524e2016-07-28 12:01:45 -0600629 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
630 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
631 NULL, 0, NULL, 1, &image_memory_barrier);
Tony Barbour5c5b1632016-04-26 11:13:48 -0600632
Chia-I Wuf051d262015-10-27 19:25:11 +0800633 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800634
Karl Schultze4dc75c2016-02-02 15:37:51 -0700635 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
636 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
637 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
638 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600639 VkViewport viewport;
640 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700641 viewport.height = (float)demo->height;
642 viewport.width = (float)demo->width;
643 viewport.minDepth = (float)0.0f;
644 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700645 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600646
647 VkRect2D scissor;
648 memset(&scissor, 0, sizeof(scissor));
649 scissor.extent.width = demo->width;
650 scissor.extent.height = demo->height;
651 scissor.offset.x = 0;
652 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700653 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600654 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600655 // Note that ending the renderpass changes the image's layout from
656 // COLOR_ATTACHEMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800657 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600658
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600659 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600660 assert(!err);
661}
662
Karl Schultze4dc75c2016-02-02 15:37:51 -0700663void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600664 mat4x4 MVP, Model, VP;
665 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600666 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600667 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600668
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600669 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600670
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600671 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600672 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700673 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
674 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600675 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600676
Karl Schultze4dc75c2016-02-02 15:37:51 -0700677 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
678 demo->uniform_data.mem_alloc.allocationSize, 0,
679 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600680 assert(!err);
681
Karl Schultze4dc75c2016-02-02 15:37:51 -0700682 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600683
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600684 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600685}
686
Karl Schultze4dc75c2016-02-02 15:37:51 -0700687static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600688 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600689
Ian Elliottaaae5352015-07-06 14:27:58 -0600690 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbource7524e2016-07-28 12:01:45 -0600692 demo->image_acquired_semaphore, (VkFence)0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600693 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600694 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
695 // demo->swapchain is out of date (e.g. the window was resized) and
696 // must be recreated:
697 demo_resize(demo);
698 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600699 return;
700 } else if (err == VK_SUBOPTIMAL_KHR) {
701 // demo->swapchain is not as optimal as it could be, but the platform's
702 // presentation engine will still present the image correctly.
703 } else {
704 assert(!err);
705 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600706
Tony Barbource7524e2016-07-28 12:01:45 -0600707 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600708 // that the image won't be rendered to until the presentation
709 // engine has fully released ownership to the application, and it is
710 // okay to render to the image.
Ian Elliott3b55a122016-08-03 14:57:11 -0600711 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700712 VkPipelineStageFlags pipe_stage_flags =
Tony Barbource7524e2016-07-28 12:01:45 -0600713 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
714 VkSubmitInfo submit_info = {
715 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
716 .pNext = NULL,
717 .waitSemaphoreCount = 1,
718 .pWaitSemaphores = &demo->image_acquired_semaphore,
719 .pWaitDstStageMask = &pipe_stage_flags,
720 .commandBufferCount = 1,
721 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
722 .signalSemaphoreCount = 1,
723 .pSignalSemaphores = &demo->draw_complete_semaphore};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600724
Tony Barboura2a67812016-07-18 13:21:06 -0600725 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600726 assert(!err);
727
Ian Elliotta0781972015-08-21 15:09:33 -0600728 VkPresentInfoKHR present = {
729 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600730 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600731 .waitSemaphoreCount = 1,
732 .pWaitSemaphores = &demo->draw_complete_semaphore,
Ian Elliotta0781972015-08-21 15:09:33 -0600733 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700734 .pSwapchains = &demo->swapchain,
735 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600736 };
737
Tony Barboura2a67812016-07-18 13:21:06 -0600738 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600739 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
740 // demo->swapchain is out of date (e.g. the window was resized) and
741 // must be recreated:
742 demo_resize(demo);
743 } else if (err == VK_SUBOPTIMAL_KHR) {
744 // demo->swapchain is not as optimal as it could be, but the platform's
745 // presentation engine will still present the image correctly.
746 } else {
747 assert(!err);
748 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600749}
750
Karl Schultze4dc75c2016-02-02 15:37:51 -0700751static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600752 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600753 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600754
Ian Elliottb08e0d92015-11-20 11:55:46 -0700755 // Check the surface capabilities and formats
756 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700757 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
758 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600759 assert(!err);
760
Ian Elliott8528eff2015-08-07 15:56:59 -0600761 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700762 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
763 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600764 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600765 VkPresentModeKHR *presentModes =
766 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600767 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700768 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
769 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600770 assert(!err);
771
Ian Elliotta0781972015-08-21 15:09:33 -0600772 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600773 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
774 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
775 // If the surface size is undefined, the size is set to the size
776 // of the images requested, which must fit within the minimum and
777 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -0600778 swapchainExtent.width = demo->width;
779 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -0600780
781 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
782 swapchainExtent.width = surfCapabilities.minImageExtent.width;
783 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
784 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
785 }
786
787 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
788 swapchainExtent.height = surfCapabilities.minImageExtent.height;
789 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
790 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
791 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700792 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600793 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700794 swapchainExtent = surfCapabilities.currentExtent;
795 demo->width = surfCapabilities.currentExtent.width;
796 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600797 }
798
799 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600800 // tearing mode. If not, try IMMEDIATE which will usually be available,
801 // and is fastest (though it tears). If not, fall back to FIFO which is
802 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600803 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600804 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600805 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
806 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 break;
808 }
Ian Elliotta0781972015-08-21 15:09:33 -0600809 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
810 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
811 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600812 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600813 }
814
Ian Elliottcf7f7482016-08-19 03:46:58 -0600815 // Determine the number of VkImage's to use in the swap chain.
816 // Application desires to only acquire 1 image at a time (which is
817 // "surfCapabilities.minImageCount").
818 uint32_t desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
819 // If maxImageCount is 0, we can ask for as many images as we want;
820 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -0700821 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -0600822 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600823 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -0600824 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600825 }
826
Tony Barbour9fd6d352015-09-16 15:01:32 -0600827 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700828 if (surfCapabilities.supportedTransforms &
829 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700830 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600831 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700832 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600833 }
834
Tony Barboura2a67812016-07-18 13:21:06 -0600835 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600836 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800837 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700838 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600839 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800840 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600841 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700842 .imageExtent =
843 {
844 .width = swapchainExtent.width, .height = swapchainExtent.height,
845 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700846 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600847 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700848 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700849 .imageArrayLayers = 1,
850 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
851 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600852 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600853 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600854 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600855 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600856 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600857 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -0600858 uint32_t queueFamilyIndices[2] = {(uint32_t) demo->graphics_queue_family_index, (uint32_t) demo->present_queue_family_index};
859 if (demo->graphics_queue_family_index != demo->present_queue_family_index)
860 {
861 // If the graphics and present queues are from different queue families, we either have to
862 // explicitly transfer ownership of images between the queues, or we have to create the swapchain
863 // with imageSharingMode as VK_SHARING_MODE_CONCURRENT
864 swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
865 swapchain_ci.queueFamilyIndexCount = 2;
866 swapchain_ci.pQueueFamilyIndices = queueFamilyIndices;
867 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600868
Tony Barboura2a67812016-07-18 13:21:06 -0600869 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700870 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800871 assert(!err);
872
Ian Elliottcf074d32015-10-16 18:02:43 -0600873 // If we just re-created an existing swapchain, we should destroy the old
874 // swapchain at this point.
875 // Note: destroying the swapchain also cleans up all its associated
876 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800877 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700878 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600879 }
880
881 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600882 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600883 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800884
Karl Schultze4dc75c2016-02-02 15:37:51 -0700885 VkImage *swapchainImages =
886 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600887 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600888 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600889 &demo->swapchainImageCount,
890 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600891 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600892
Karl Schultze4dc75c2016-02-02 15:37:51 -0700893 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
894 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600895 assert(demo->buffers);
896
Ian Elliotta0781972015-08-21 15:09:33 -0600897 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600898 VkImageViewCreateInfo color_image_view = {
899 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600900 .pNext = NULL,
901 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700902 .components =
903 {
904 .r = VK_COMPONENT_SWIZZLE_R,
905 .g = VK_COMPONENT_SWIZZLE_G,
906 .b = VK_COMPONENT_SWIZZLE_B,
907 .a = VK_COMPONENT_SWIZZLE_A,
908 },
909 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
910 .baseMipLevel = 0,
911 .levelCount = 1,
912 .baseArrayLayer = 0,
913 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600914 .viewType = VK_IMAGE_VIEW_TYPE_2D,
915 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600916 };
917
Ian Elliotta0781972015-08-21 15:09:33 -0600918 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600919
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600920 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600921
Karl Schultze4dc75c2016-02-02 15:37:51 -0700922 err = vkCreateImageView(demo->device, &color_image_view, NULL,
923 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600924 assert(!err);
925 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700926
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500927
Mark Young04fd3d82016-01-25 14:43:50 -0700928 if (NULL != presentModes) {
929 free(presentModes);
930 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600931}
932
Karl Schultze4dc75c2016-02-02 15:37:51 -0700933static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600934 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600935 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600936 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600937 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600938 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600939 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700940 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600941 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600942 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800943 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600944 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600945 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600946 .flags = 0,
947 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200948
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600949 VkImageViewCreateInfo view = {
950 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600951 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800952 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600953 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700954 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
955 .baseMipLevel = 0,
956 .levelCount = 1,
957 .baseArrayLayer = 0,
958 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600960 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600962
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500963 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600964 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600965 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600966
967 demo->depth.format = depth_format;
968
969 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700970 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971 assert(!err);
972
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600974 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600975
Chia-I Wuf48700b2015-11-10 16:21:09 +0800976 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200977 demo->depth.mem_alloc.pNext = NULL;
978 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
979 demo->depth.mem_alloc.memoryTypeIndex = 0;
980
Karl Schultze4dc75c2016-02-02 15:37:51 -0700981 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
982 0, /* No requirements */
983 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600984 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500986 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700987 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
988 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500989 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500991 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700992 err =
993 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500994 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600995
Karl Schultze4dc75c2016-02-02 15:37:51 -0700996 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
997 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700998 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
999 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001000
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001001 /* create image view */
1002 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001003 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001005}
1006
Tony Barbour1a86d032015-09-21 15:17:33 -06001007/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001008bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001010#ifdef __ANDROID__
1011#include <lunarg.ppm.h>
1012 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001013 cPtr = (char*)lunarg_ppm;
1014 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001015 return false;
1016 }
1017 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001018 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001019 if (rgba_data == NULL) {
1020 return true;
1021 }
1022 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001023 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001024 return false;
1025 }
1026 while(strncmp(cPtr++, "\n", 1));
1027
1028 for (int y = 0; y < *height; y++) {
1029 uint8_t *rowPtr = rgba_data;
1030 for (int x = 0; x < *width; x++) {
1031 memcpy(rowPtr, cPtr, 3);
1032 rowPtr[3] = 255; /* Alpha of 1 */
1033 rowPtr += 4;
1034 cPtr += 3;
1035 }
1036 rgba_data += layout->rowPitch;
1037 }
1038
1039 return true;
1040#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001041 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001042 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001043
Tony Barbour1a86d032015-09-21 15:17:33 -06001044 if (!fPtr)
1045 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001046
Tony Barbour1a86d032015-09-21 15:17:33 -06001047 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001048 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1049 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001050 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001051 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001052
Tony Barbour1a86d032015-09-21 15:17:33 -06001053 do {
1054 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001055 if (cPtr == NULL) {
1056 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001057 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001058 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001059 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001060
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001061 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001062 if (rgba_data == NULL) {
1063 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001064 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001065 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001066 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001067 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001068 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1069 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001070 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001071 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001072
Karl Schultze4dc75c2016-02-02 15:37:51 -07001073 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001074 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001075 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001076 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001077 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001078 rowPtr[3] = 255; /* Alpha of 1 */
1079 rowPtr += 4;
1080 }
1081 rgba_data += layout->rowPitch;
1082 }
1083 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001084 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001085#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001086}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001087
Karl Schultze4dc75c2016-02-02 15:37:51 -07001088static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001089 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001090 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001091 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001092 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001093 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001094 int32_t tex_width;
1095 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001096 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001097 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001098
Karl Schultze4dc75c2016-02-02 15:37:51 -07001099 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001100 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001101 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001102
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001103 tex_obj->tex_width = tex_width;
1104 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001105
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001106 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001107 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001108 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001109 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001110 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001111 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001112 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001113 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001114 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001116 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001118 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001120
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001121 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001122
Karl Schultze4dc75c2016-02-02 15:37:51 -07001123 err =
1124 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 assert(!err);
1126
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001127 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001128
Chia-I Wuf48700b2015-11-10 16:21:09 +08001129 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001130 tex_obj->mem_alloc.pNext = NULL;
1131 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1132 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133
Karl Schultze4dc75c2016-02-02 15:37:51 -07001134 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1135 required_props,
1136 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001137 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001138
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001139 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001140 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001141 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001142 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001144 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001145 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001146 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001147
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001148 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001149 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001150 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001152 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001153 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001154 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155 void *data;
1156
Karl Schultze4dc75c2016-02-02 15:37:51 -07001157 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1158 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159
Karl Schultze4dc75c2016-02-02 15:37:51 -07001160 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1161 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162 assert(!err);
1163
1164 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1165 fprintf(stderr, "Error loading texture: %s\n", filename);
1166 }
1167
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001168 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001169 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001170
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001171 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001172 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001173 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1174 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001175 /* setting the image layout does not reference the actual memory so no need
1176 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001177}
1178
Karl Schultze4dc75c2016-02-02 15:37:51 -07001179static void demo_destroy_texture_image(struct demo *demo,
1180 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001181 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001182 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1183 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184}
1185
Karl Schultze4dc75c2016-02-02 15:37:51 -07001186static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001187 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001188 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001189 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001190
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001191 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001192
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001193 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001194 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195
Karl Schultze4dc75c2016-02-02 15:37:51 -07001196 if ((props.linearTilingFeatures &
1197 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1198 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001200 demo_prepare_texture_image(
1201 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1202 VK_IMAGE_USAGE_SAMPLED_BIT,
1203 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1204 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205 } else if (props.optimalTilingFeatures &
1206 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001207 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001208 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001209
1210 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001211 demo_prepare_texture_image(
1212 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1213 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1214 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1215 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216
Karl Schultze4dc75c2016-02-02 15:37:51 -07001217 demo_prepare_texture_image(
1218 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1219 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1220 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001221
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001222 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001223 VK_IMAGE_ASPECT_COLOR_BIT,
1224 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001225 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1226 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001227
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001228 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001229 VK_IMAGE_ASPECT_COLOR_BIT,
1230 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001231 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1232 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001233
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001234 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001235 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1236 .srcOffset = {0, 0, 0},
1237 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1238 .dstOffset = {0, 0, 0},
1239 .extent = {staging_texture.tex_width,
1240 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001241 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001242 vkCmdCopyImage(
1243 demo->cmd, staging_texture.image,
1244 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1245 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
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 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001250 demo->textures[i].imageLayout,
1251 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001253 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001254
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001255 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001256 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001257 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1258 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001259 }
1260
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001261 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001262 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001263 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001264 .magFilter = VK_FILTER_NEAREST,
1265 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001266 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001267 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1268 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1269 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001270 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001271 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001272 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001273 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274 .minLod = 0.0f,
1275 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001276 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001277 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001278 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001279
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001280 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001281 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001282 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001283 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001284 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001285 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001286 .components =
1287 {
1288 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1289 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1290 },
1291 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001292 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001294
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001295 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001296 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001297 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001298 assert(!err);
1299
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001300 /* create image view */
1301 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001302 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001303 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001304 assert(!err);
1305 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307
Karl Schultze4dc75c2016-02-02 15:37:51 -07001308void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001309 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001310 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001311 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001312 int i;
1313 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001314 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001315 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001316 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001317
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001318 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001319 mat4x4_mul(MVP, VP, demo->model_matrix);
1320 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001322
Karl Schultze4dc75c2016-02-02 15:37:51 -07001323 for (i = 0; i < 12 * 3; i++) {
1324 data.position[i][0] = g_vertex_buffer_data[i * 3];
1325 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1326 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001327 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001328 data.attr[i][0] = g_uv_buffer_data[2 * i];
1329 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001330 data.attr[i][2] = 0;
1331 data.attr[i][3] = 0;
1332 }
1333
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001334 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001335 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001336 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001337 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001338 err =
1339 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001340 assert(!err);
1341
Karl Schultze4dc75c2016-02-02 15:37:51 -07001342 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1343 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001344
Chia-I Wuf48700b2015-11-10 16:21:09 +08001345 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001346 demo->uniform_data.mem_alloc.pNext = NULL;
1347 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1348 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1349
Karl Schultze4dc75c2016-02-02 15:37:51 -07001350 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001351 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1352 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001353 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001354 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001355
Karl Schultze4dc75c2016-02-02 15:37:51 -07001356 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1357 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001358 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001359
Karl Schultze4dc75c2016-02-02 15:37:51 -07001360 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1361 demo->uniform_data.mem_alloc.allocationSize, 0,
1362 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001363 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001364
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001365 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001366
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001367 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001368
Karl Schultze4dc75c2016-02-02 15:37:51 -07001369 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1370 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001371 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001372
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001373 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1374 demo->uniform_data.buffer_info.offset = 0;
1375 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001376}
1377
Karl Schultze4dc75c2016-02-02 15:37:51 -07001378static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001379 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001380 [0] =
1381 {
1382 .binding = 0,
1383 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1384 .descriptorCount = 1,
1385 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1386 .pImmutableSamplers = NULL,
1387 },
1388 [1] =
1389 {
1390 .binding = 1,
1391 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1392 .descriptorCount = DEMO_TEXTURE_COUNT,
1393 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1394 .pImmutableSamplers = NULL,
1395 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001396 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001397 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001398 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001399 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001400 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001401 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001402 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001403 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404
Karl Schultze4dc75c2016-02-02 15:37:51 -07001405 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1406 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001407 assert(!err);
1408
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001409 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001410 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1411 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001412 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001413 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001414 };
1415
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001417 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419}
1420
Karl Schultze4dc75c2016-02-02 15:37:51 -07001421static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001422 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001423 [0] =
1424 {
1425 .format = demo->format,
1426 .samples = VK_SAMPLE_COUNT_1_BIT,
1427 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1428 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1429 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1430 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1431 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Tony Barbour98390392016-07-28 10:50:13 -06001432 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001433 },
1434 [1] =
1435 {
1436 .format = demo->depth.format,
1437 .samples = VK_SAMPLE_COUNT_1_BIT,
1438 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1439 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1440 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1441 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1442 .initialLayout =
1443 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1444 .finalLayout =
1445 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1446 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001447 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001448 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001449 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001450 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001451 const VkAttachmentReference depth_reference = {
1452 .attachment = 1,
1453 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1454 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001455 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001456 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1457 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001458 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001459 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001460 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001461 .pColorAttachments = &color_reference,
1462 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001463 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001464 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001465 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001466 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001467 const VkRenderPassCreateInfo rp_info = {
1468 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1469 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001470 .attachmentCount = 2,
1471 .pAttachments = attachments,
1472 .subpassCount = 1,
1473 .pSubpasses = &subpass,
1474 .dependencyCount = 0,
1475 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001476 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001477 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001478
Chia-I Wu63636062015-10-26 21:10:41 +08001479 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001480 assert(!err);
1481}
1482
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001483//TODO: Merge shader reading
1484#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001485static VkShaderModule
1486demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001487 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001488 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001489 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001491 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1492 moduleCreateInfo.pNext = NULL;
1493
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001494 moduleCreateInfo.codeSize = size;
1495 moduleCreateInfo.pCode = code;
1496 moduleCreateInfo.flags = 0;
1497 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1498 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001499
1500 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501}
1502
Karl Schultze4dc75c2016-02-02 15:37:51 -07001503char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001504 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001505 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001506 void *shader_code;
1507
1508 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001509 if (!fp)
1510 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001511
1512 fseek(fp, 0L, SEEK_END);
1513 size = ftell(fp);
1514
1515 fseek(fp, 0L, SEEK_SET);
1516
1517 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001518 retval = fread(shader_code, size, 1, fp);
1519 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001520
1521 *psize = size;
1522
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001523 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001524 return shader_code;
1525}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001526#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001527
Karl Schultze4dc75c2016-02-02 15:37:51 -07001528static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001529#ifdef __ANDROID__
1530 VkShaderModuleCreateInfo sh_info = {};
1531 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1532
1533#include "cube.vert.h"
1534 sh_info.codeSize = sizeof(cube_vert);
1535 sh_info.pCode = cube_vert;
1536 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1537 assert(!err);
1538#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001539 void *vertShaderCode;
1540 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001541
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001542 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1543
Karl Schultze4dc75c2016-02-02 15:37:51 -07001544 demo->vert_shader_module =
1545 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001546
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001547 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001548#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001549
1550 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001551}
1552
Karl Schultze4dc75c2016-02-02 15:37:51 -07001553static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001554#ifdef __ANDROID__
1555 VkShaderModuleCreateInfo sh_info = {};
1556 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1557
1558#include "cube.frag.h"
1559 sh_info.codeSize = sizeof(cube_frag);
1560 sh_info.pCode = cube_frag;
1561 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1562 assert(!err);
1563#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001564 void *fragShaderCode;
1565 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001566
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001567 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001568
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 demo->frag_shader_module =
1570 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001571
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001572 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001573#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001574
1575 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001576}
1577
Karl Schultze4dc75c2016-02-02 15:37:51 -07001578static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001579 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001580 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001581 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001582 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001583 VkPipelineRasterizationStateCreateInfo rs;
1584 VkPipelineColorBlendStateCreateInfo cb;
1585 VkPipelineDepthStencilStateCreateInfo ds;
1586 VkPipelineViewportStateCreateInfo vp;
1587 VkPipelineMultisampleStateCreateInfo ms;
1588 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1589 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001590 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001591
Piers Daniellba839d12015-09-29 13:01:09 -06001592 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1593 memset(&dynamicState, 0, sizeof dynamicState);
1594 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1595 dynamicState.pDynamicStates = dynamicStateEnables;
1596
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001598 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001599 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001600
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001601 memset(&vi, 0, sizeof(vi));
1602 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1603
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001604 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001605 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001606 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001607
1608 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001609 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1610 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001611 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001612 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001613 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001614 rs.rasterizerDiscardEnable = VK_FALSE;
1615 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001616 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617
1618 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001619 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1620 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001621 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001622 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001623 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001624 cb.attachmentCount = 1;
1625 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001626
Tony Barbour29645d02015-01-16 14:27:35 -07001627 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001628 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001629 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001630 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1631 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001632 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001633 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1634 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001635
1636 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001637 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001638 ds.depthTestEnable = VK_TRUE;
1639 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001640 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001641 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001642 ds.back.failOp = VK_STENCIL_OP_KEEP;
1643 ds.back.passOp = VK_STENCIL_OP_KEEP;
1644 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001645 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001646 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001647
Tony Barbour29645d02015-01-16 14:27:35 -07001648 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001649 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001650 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001651 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001652
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001653 // Two stages: vs and fs
1654 pipeline.stageCount = 2;
1655 VkPipelineShaderStageCreateInfo shaderStages[2];
1656 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1657
Karl Schultze4dc75c2016-02-02 15:37:51 -07001658 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1659 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001660 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001662
Karl Schultze4dc75c2016-02-02 15:37:51 -07001663 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1664 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001665 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001666 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001667
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001668 memset(&pipelineCache, 0, sizeof(pipelineCache));
1669 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001670
Karl Schultze4dc75c2016-02-02 15:37:51 -07001671 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1672 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001673 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001674
Karl Schultze4dc75c2016-02-02 15:37:51 -07001675 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001676 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001677 pipeline.pRasterizationState = &rs;
1678 pipeline.pColorBlendState = &cb;
1679 pipeline.pMultisampleState = &ms;
1680 pipeline.pViewportState = &vp;
1681 pipeline.pDepthStencilState = &ds;
1682 pipeline.pStages = shaderStages;
1683 pipeline.renderPass = demo->render_pass;
1684 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001685
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001686 pipeline.renderPass = demo->render_pass;
1687
Karl Schultze4dc75c2016-02-02 15:37:51 -07001688 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1689 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690 assert(!err);
1691
Chia-I Wu63636062015-10-26 21:10:41 +08001692 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1693 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694}
1695
Karl Schultze4dc75c2016-02-02 15:37:51 -07001696static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001697 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001698 [0] =
1699 {
1700 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1701 .descriptorCount = 1,
1702 },
1703 [1] =
1704 {
1705 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1706 .descriptorCount = DEMO_TEXTURE_COUNT,
1707 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001708 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001709 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001710 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001711 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001712 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001713 .poolSizeCount = 2,
1714 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001715 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001716 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001717
Karl Schultze4dc75c2016-02-02 15:37:51 -07001718 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1719 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001720 assert(!err);
1721}
1722
Karl Schultze4dc75c2016-02-02 15:37:51 -07001723static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001724 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001725 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001726 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001727 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001728
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001729 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001730 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001731 .pNext = NULL,
1732 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001733 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001734 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001735 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001736 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001737
Chia-I Wuae721ba2015-05-25 16:27:55 +08001738 memset(&tex_descs, 0, sizeof(tex_descs));
1739 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001740 tex_descs[i].sampler = demo->textures[i].sampler;
1741 tex_descs[i].imageView = demo->textures[i].view;
1742 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001743 }
1744
1745 memset(&writes, 0, sizeof(writes));
1746
1747 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001748 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001749 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001750 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001751 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001752
1753 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001754 writes[1].dstSet = demo->desc_set;
1755 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001756 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001757 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001758 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001759
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001760 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001761}
1762
Karl Schultze4dc75c2016-02-02 15:37:51 -07001763static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001764 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001765 attachments[1] = demo->depth.view;
1766
Chia-I Wuf973e322015-07-08 13:34:24 +08001767 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001768 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1769 .pNext = NULL,
1770 .renderPass = demo->render_pass,
1771 .attachmentCount = 2,
1772 .pAttachments = attachments,
1773 .width = demo->width,
1774 .height = demo->height,
1775 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001776 };
1777 VkResult U_ASSERT_ONLY err;
1778 uint32_t i;
1779
Karl Schultze4dc75c2016-02-02 15:37:51 -07001780 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1781 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001782 assert(demo->framebuffers);
1783
1784 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001785 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001786 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1787 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001788 assert(!err);
1789 }
1790}
1791
Karl Schultze4dc75c2016-02-02 15:37:51 -07001792static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001793 VkResult U_ASSERT_ONLY err;
1794
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001795 const VkCommandPoolCreateInfo cmd_pool_info = {
1796 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001797 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06001798 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06001799 .flags = 0,
1800 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001801 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1802 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001803 assert(!err);
1804
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001805 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001806 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001807 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001808 .commandPool = demo->cmd_pool,
1809 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001810 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001811 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001812
1813 demo_prepare_buffers(demo);
1814 demo_prepare_depth(demo);
1815 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001816 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001818 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001819 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001821
Ian Elliotta0781972015-08-21 15:09:33 -06001822 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001823 err =
1824 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001825 assert(!err);
1826 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001827
Chia-I Wu63ea9262015-03-26 13:14:16 +08001828 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001829 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001830
Chia-I Wuf973e322015-07-08 13:34:24 +08001831 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001832
Ian Elliotta0781972015-08-21 15:09:33 -06001833 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001834 demo->current_buffer = i;
1835 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1836 }
1837
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001838 /*
1839 * Prepare functions above may generate pipeline commands
1840 * that need to be flushed before beginning the render loop.
1841 */
1842 demo_flush_init_cmd(demo);
1843
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001844 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001845 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001846}
1847
Karl Schultze4dc75c2016-02-02 15:37:51 -07001848static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001849 uint32_t i;
1850
1851 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06001852 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001853
Tony Barbourd8e504f2015-10-08 14:26:24 -06001854 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001855 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001856 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001857 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001858 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001859
Chia-I Wu63636062015-10-26 21:10:41 +08001860 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1861 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1862 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1863 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1864 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001865
1866 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001867 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1868 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1869 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1870 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001871 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001872 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001873
Chia-I Wu63636062015-10-26 21:10:41 +08001874 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1875 vkDestroyImage(demo->device, demo->depth.image, NULL);
1876 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001877
Chia-I Wu63636062015-10-26 21:10:41 +08001878 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1879 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001880
Ian Elliotta0781972015-08-21 15:09:33 -06001881 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001882 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001883 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1884 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001885 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001886 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001887
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001888 free(demo->queue_props);
1889
Chia-I Wu63636062015-10-26 21:10:41 +08001890 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbource7524e2016-07-28 12:01:45 -06001891 vkDestroySemaphore(demo->device, demo->image_acquired_semaphore, NULL);
1892 vkDestroySemaphore(demo->device, demo->draw_complete_semaphore, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001893 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001894 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001895 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001896 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001897 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001898 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001899
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001900#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001901 if (demo->use_xlib) {
1902 XDestroyWindow(demo->display, demo->xlib_window);
1903 XCloseDisplay(demo->display);
1904 } else {
1905 xcb_destroy_window(demo->connection, demo->xcb_window);
1906 xcb_disconnect(demo->connection);
1907 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001908 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001909#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001910 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001911 xcb_disconnect(demo->connection);
1912 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001913#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
1914 wl_shell_surface_destroy(demo->shell_surface);
1915 wl_surface_destroy(demo->window);
1916 wl_shell_destroy(demo->shell);
1917 wl_compositor_destroy(demo->compositor);
1918 wl_registry_destroy(demo->registry);
1919 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001920#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001921}
1922
Karl Schultze4dc75c2016-02-02 15:37:51 -07001923static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001924 uint32_t i;
1925
Mike Stroyanbb60b382015-10-28 09:46:57 -06001926 // Don't react to resize until after first initialization.
1927 if (!demo->prepared) {
1928 return;
1929 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001930 // In order to properly resize the window, we must re-create the swapchain
1931 // AND redo the command buffers, etc.
1932 //
1933 // First, perform part of the demo_cleanup() function:
1934 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06001935 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06001936
1937 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001938 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001939 }
1940 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001941 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001942
Chia-I Wu63636062015-10-26 21:10:41 +08001943 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1944 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1945 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1946 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1947 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001948
1949 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001950 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1951 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1952 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1953 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001954 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001955
Chia-I Wu63636062015-10-26 21:10:41 +08001956 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1957 vkDestroyImage(demo->device, demo->depth.image, NULL);
1958 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001959
Chia-I Wu63636062015-10-26 21:10:41 +08001960 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1961 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001962
1963 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001964 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001965 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1966 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001967 }
Chia-I Wu63636062015-10-26 21:10:41 +08001968 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001969 free(demo->buffers);
1970
Ian Elliottcf074d32015-10-16 18:02:43 -06001971 // Second, re-perform the demo_prepare() function, which will re-create the
1972 // swapchain:
1973 demo_prepare(demo);
1974}
1975
David Pinedo2bb7c932015-06-18 17:03:14 -06001976// On MS-Windows, make this a global, so it's available to WndProc()
1977struct demo demo;
1978
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001979#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07001980static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001981 if (!demo->prepared)
1982 return;
Tony Barbource7524e2016-07-28 12:01:45 -06001983
Ian Elliott639ca472015-04-16 15:23:05 -06001984 demo_update_data_buffer(demo);
Ian Elliott639ca472015-04-16 15:23:05 -06001985 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06001986 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001987 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06001988 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06001989 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001990}
Ian Elliott639ca472015-04-16 15:23:05 -06001991
1992// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001993LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1994 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001995 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06001996 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001997 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001998 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06001999 // The validation callback calls MessageBox which can generate paint
2000 // events - don't make more Vulkan calls if we got here from the
2001 // callback
2002 if (!in_callback) {
2003 demo_run(&demo);
2004 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002005 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002006 case WM_GETMINMAXINFO: // set window's minimum size
2007 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2008 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002009 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002010 // Resize the application to the new window size, except when
2011 // it was minimized. Vulkan doesn't support images or swapchains
2012 // with width=0 and height=0.
2013 if (wParam != SIZE_MINIMIZED) {
2014 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002015 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002016 demo_resize(&demo);
2017 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002018 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002019 default:
2020 break;
2021 }
2022 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2023}
2024
Karl Schultze4dc75c2016-02-02 15:37:51 -07002025static void demo_create_window(struct demo *demo) {
2026 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002027
2028 // Initialize the window class structure:
2029 win_class.cbSize = sizeof(WNDCLASSEX);
2030 win_class.style = CS_HREDRAW | CS_VREDRAW;
2031 win_class.lpfnWndProc = WndProc;
2032 win_class.cbClsExtra = 0;
2033 win_class.cbWndExtra = 0;
2034 win_class.hInstance = demo->connection; // hInstance
2035 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2036 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2037 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2038 win_class.lpszMenuName = NULL;
2039 win_class.lpszClassName = demo->name;
2040 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2041 // Register window class:
2042 if (!RegisterClassEx(&win_class)) {
2043 // It didn't work, so try to give a useful error:
2044 printf("Unexpected error trying to start the application!\n");
2045 fflush(stdout);
2046 exit(1);
2047 }
2048 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002049 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002050 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002051 demo->window = CreateWindowEx(0,
2052 demo->name, // class name
2053 demo->name, // app name
2054 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002055 WS_VISIBLE | WS_SYSMENU,
2056 100, 100, // x/y coords
2057 wr.right - wr.left, // width
2058 wr.bottom - wr.top, // height
2059 NULL, // handle to parent
2060 NULL, // handle to menu
2061 demo->connection, // hInstance
2062 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002063 if (!demo->window) {
2064 // It didn't work, so try to give a useful error:
2065 printf("Cannot create a window in which to draw!\n");
2066 fflush(stdout);
2067 exit(1);
2068 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002069 // Window client area size must be at least 1 pixel high, to prevent crash.
2070 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2071 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002072}
Tony Barbour8295ac42016-08-08 11:04:17 -06002073#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002074static void demo_create_xlib_window(struct demo *demo) {
2075
2076 demo->display = XOpenDisplay(NULL);
2077 long visualMask = VisualScreenMask;
2078 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002079 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002080 vInfoTemplate.screen = DefaultScreen(demo->display);
2081 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2082 &vInfoTemplate, &numberOfVisuals);
2083
2084 Colormap colormap = XCreateColormap(
2085 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2086 visualInfo->visual, AllocNone);
2087
Rene Lindsay11612722016-06-13 17:20:39 -06002088 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002089 windowAttributes.colormap = colormap;
2090 windowAttributes.background_pixel = 0xFFFFFFFF;
2091 windowAttributes.border_pixel = 0;
2092 windowAttributes.event_mask =
2093 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2094
2095 demo->xlib_window = XCreateWindow(
2096 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2097 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2098 visualInfo->visual,
2099 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2100
2101 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2102 XMapWindow(demo->display, demo->xlib_window);
2103 XFlush(demo->display);
2104 demo->xlib_wm_delete_window =
2105 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2106}
2107static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2108 switch(event->type) {
2109 case ClientMessage:
2110 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2111 demo->quit = true;
2112 break;
2113 case KeyPress:
2114 switch (event->xkey.keycode) {
2115 case 0x9: // Escape
2116 demo->quit = true;
2117 break;
2118 case 0x71: // left arrow key
2119 demo->spin_angle += demo->spin_increment;
2120 break;
2121 case 0x72: // right arrow key
2122 demo->spin_angle -= demo->spin_increment;
2123 break;
2124 case 0x41:
2125 demo->pause = !demo->pause;
2126 break;
2127 }
2128 break;
2129 case ConfigureNotify:
2130 if ((demo->width != event->xconfigure.width) ||
2131 (demo->height != event->xconfigure.height)) {
2132 demo->width = event->xconfigure.width;
2133 demo->height = event->xconfigure.height;
2134 demo_resize(demo);
2135 }
2136 break;
2137 default:
2138 break;
2139 }
2140
2141}
2142
2143static void demo_run_xlib(struct demo *demo) {
2144
2145 while (!demo->quit) {
2146 XEvent event;
2147
2148 if (demo->pause) {
2149 XNextEvent(demo->display, &event);
2150 demo_handle_xlib_event(demo, &event);
2151 } else {
2152 while (XPending(demo->display) > 0) {
2153 XNextEvent(demo->display, &event);
2154 demo_handle_xlib_event(demo, &event);
2155 }
2156 }
2157
Tony Barbour2593b182016-04-19 10:57:58 -06002158 demo_update_data_buffer(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002159 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002160 demo->curFrame++;
2161 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2162 demo->quit = true;
2163 }
2164}
Tony Barbour8295ac42016-08-08 11:04:17 -06002165#endif // VK_USE_PLATFORM_XLIB_KHR
2166#ifdef VK_USE_PLATFORM_XCB_KHR
Tony Barbour2593b182016-04-19 10:57:58 -06002167static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002168 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002169 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002170 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002171 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002172 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002173 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002174 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002175 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2176 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002177 demo->quit = true;
2178 }
2179 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002180 case XCB_KEY_RELEASE: {
2181 const xcb_key_release_event_t *key =
2182 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002183
Karl Schultze4dc75c2016-02-02 15:37:51 -07002184 switch (key->detail) {
2185 case 0x9: // Escape
2186 demo->quit = true;
2187 break;
2188 case 0x71: // left arrow key
2189 demo->spin_angle += demo->spin_increment;
2190 break;
2191 case 0x72: // right arrow key
2192 demo->spin_angle -= demo->spin_increment;
2193 break;
2194 case 0x41:
2195 demo->pause = !demo->pause;
2196 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002197 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002198 } break;
2199 case XCB_CONFIGURE_NOTIFY: {
2200 const xcb_configure_notify_event_t *cfg =
2201 (const xcb_configure_notify_event_t *)event;
2202 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002203 demo->width = cfg->width;
2204 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002205 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002206 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002207 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002208 default:
2209 break;
2210 }
2211}
2212
Tony Barbour2593b182016-04-19 10:57:58 -06002213static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002214 xcb_flush(demo->connection);
2215
2216 while (!demo->quit) {
2217 xcb_generic_event_t *event;
2218
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002219 if (demo->pause) {
2220 event = xcb_wait_for_event(demo->connection);
2221 } else {
2222 event = xcb_poll_for_event(demo->connection);
Tony Barbour9b26b5a2016-08-05 14:55:20 -06002223 while(event) {
2224 demo_handle_xcb_event(demo, event);
2225 free(event);
2226 event = xcb_poll_for_event(demo->connection);
2227 }
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002228 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002229
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002230 demo_update_data_buffer(demo);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002231 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002232 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002233 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002234 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002235 }
2236}
2237
Tony Barbour2593b182016-04-19 10:57:58 -06002238static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239 uint32_t value_mask, value_list[32];
2240
Tony Barbour2593b182016-04-19 10:57:58 -06002241 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002242
2243 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2244 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002245 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002246 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002247
Tony Barbour2593b182016-04-19 10:57:58 -06002248 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002249 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2250 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2251 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002252
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002253 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002254 xcb_intern_atom_cookie_t cookie =
2255 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2256 xcb_intern_atom_reply_t *reply =
2257 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002258
Karl Schultze4dc75c2016-02-02 15:37:51 -07002259 xcb_intern_atom_cookie_t cookie2 =
2260 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2261 demo->atom_wm_delete_window =
2262 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002263
Tony Barbour2593b182016-04-19 10:57:58 -06002264 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002265 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002266 &(*demo->atom_wm_delete_window).atom);
2267 free(reply);
2268
Tony Barbour2593b182016-04-19 10:57:58 -06002269 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002270
Karl Schultze4dc75c2016-02-02 15:37:51 -07002271 // Force the x/y coordinates to 100,100 results are identical in consecutive
2272 // runs
2273 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002274 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002275 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002276}
Tony Barbour6b131662016-08-25 13:14:45 -06002277// VK_USE_PLATFORM_XCB_KHR
2278#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002279static void demo_run(struct demo *demo) {
2280 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002281 demo_update_data_buffer(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002282 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002283 demo->curFrame++;
2284 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2285 demo->quit = true;
2286 }
2287}
2288
2289static void handle_ping(void *data UNUSED,
2290 struct wl_shell_surface *shell_surface,
2291 uint32_t serial) {
2292 wl_shell_surface_pong(shell_surface, serial);
2293}
2294
2295static void handle_configure(void *data UNUSED,
2296 struct wl_shell_surface *shell_surface UNUSED,
2297 uint32_t edges UNUSED, int32_t width UNUSED,
2298 int32_t height UNUSED) {}
2299
2300static void handle_popup_done(void *data UNUSED,
2301 struct wl_shell_surface *shell_surface UNUSED) {}
2302
2303static const struct wl_shell_surface_listener shell_surface_listener = {
2304 handle_ping, handle_configure, handle_popup_done};
2305
2306static void demo_create_window(struct demo *demo) {
2307 demo->window = wl_compositor_create_surface(demo->compositor);
2308 if (!demo->window) {
2309 printf("Can not create wayland_surface from compositor!\n");
2310 fflush(stdout);
2311 exit(1);
2312 }
2313
2314 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2315 if (!demo->shell_surface) {
2316 printf("Can not get shell_surface from wayland_surface!\n");
2317 fflush(stdout);
2318 exit(1);
2319 }
2320 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2321 demo);
2322 wl_shell_surface_set_toplevel(demo->shell_surface);
2323 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2324}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002325#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2326static void demo_run(struct demo *demo) {
2327 if (!demo->prepared)
2328 return;
2329
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002330 demo_update_data_buffer(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002331 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002332 demo->curFrame++;
2333}
2334#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002335
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002336/*
2337 * Return 1 (true) if all layer names specified in check_names
2338 * can be found in given layer properties.
2339 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002340static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002341 uint32_t layer_count,
2342 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002343 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002344 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002345 for (uint32_t j = 0; j < layer_count; j++) {
2346 if (!strcmp(check_names[i], layers[j].layerName)) {
2347 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002348 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002349 }
2350 }
2351 if (!found) {
2352 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2353 return 0;
2354 }
2355 }
2356 return 1;
2357}
2358
Karl Schultze4dc75c2016-02-02 15:37:51 -07002359static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002360 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002361 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002362 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002363 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002364 char **instance_validation_layers = NULL;
2365 demo->enabled_extension_count = 0;
2366 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002367
Karl Schultz7c50ad62016-04-01 12:07:03 -06002368 char *instance_validation_layers_alt1[] = {
2369 "VK_LAYER_LUNARG_standard_validation"
2370 };
2371
2372 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskia2afb382016-06-29 14:01:14 -06002373 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2374 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image",
2375 "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain",
2376 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002377 };
2378
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002379 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002380 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002381 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002382
Karl Schultz7c50ad62016-04-01 12:07:03 -06002383 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002384 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002385
Karl Schultz7c50ad62016-04-01 12:07:03 -06002386 instance_validation_layers = instance_validation_layers_alt1;
2387 if (instance_layer_count > 0) {
2388 VkLayerProperties *instance_layers =
2389 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2390 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2391 instance_layers);
2392 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002393
Karl Schultz7c50ad62016-04-01 12:07:03 -06002394
2395 validation_found = demo_check_layers(
2396 ARRAY_SIZE(instance_validation_layers_alt1),
2397 instance_validation_layers, instance_layer_count,
2398 instance_layers);
2399 if (validation_found) {
2400 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002401 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2402 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002403 } else {
2404 // use alternative set of validation layers
2405 instance_validation_layers = instance_validation_layers_alt2;
2406 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2407 validation_found = demo_check_layers(
2408 ARRAY_SIZE(instance_validation_layers_alt2),
2409 instance_validation_layers, instance_layer_count,
2410 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002411 validation_layer_count =
2412 ARRAY_SIZE(instance_validation_layers_alt2);
2413 for (uint32_t i = 0; i < validation_layer_count; i++) {
2414 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002415 }
2416 }
2417 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002418 }
Dustin Graves7c287352016-01-27 13:48:06 -07002419
Karl Schultz7c50ad62016-04-01 12:07:03 -06002420 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002421 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002422 "required validation layer.\n\n"
2423 "Please look at the Getting Started guide for additional "
2424 "information.\n",
2425 "vkCreateInstance Failure");
2426 }
Dustin Graves7c287352016-01-27 13:48:06 -07002427 }
2428
2429 /* Look for instance extensions */
2430 VkBool32 surfaceExtFound = 0;
2431 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002432#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002433 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002434#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002435 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002436
Karl Schultze4dc75c2016-02-02 15:37:51 -07002437 err = vkEnumerateInstanceExtensionProperties(
2438 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002439 assert(!err);
2440
Dustin Graves7c287352016-01-27 13:48:06 -07002441 if (instance_extension_count > 0) {
2442 VkExtensionProperties *instance_extensions =
2443 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2444 err = vkEnumerateInstanceExtensionProperties(
2445 NULL, &instance_extension_count, instance_extensions);
2446 assert(!err);
2447 for (uint32_t i = 0; i < instance_extension_count; i++) {
2448 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2449 instance_extensions[i].extensionName)) {
2450 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002451 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002452 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002453 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002454#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002455 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2456 instance_extensions[i].extensionName)) {
2457 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002458 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002459 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2460 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002461#endif
2462#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002463 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2464 instance_extensions[i].extensionName)) {
2465 platformSurfaceExtFound = 1;
2466 xlibSurfaceExtFound = 1;
2467 demo->extension_names[demo->enabled_extension_count++] =
2468 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2469 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002470#endif
2471#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002472 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2473 instance_extensions[i].extensionName)) {
2474 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002475 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002476 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2477 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002478#endif
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002479#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
2480 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
2481 instance_extensions[i].extensionName)) {
2482 platformSurfaceExtFound = 1;
2483 demo->extension_names[demo->enabled_extension_count++] =
2484 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
2485 }
2486#endif
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002487#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002488 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2489 instance_extensions[i].extensionName)) {
2490 platformSurfaceExtFound = 1;
2491 demo->extension_names[demo->enabled_extension_count++] =
2492 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2493 }
2494#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002495 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2496 instance_extensions[i].extensionName)) {
2497 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002498 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002499 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2500 }
2501 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002502 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002503 }
Dustin Graves7c287352016-01-27 13:48:06 -07002504
2505 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002506 }
Dustin Graves7c287352016-01-27 13:48:06 -07002507
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002508 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002509 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2510 "the " VK_KHR_SURFACE_EXTENSION_NAME
2511 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002512 "Vulkan installable client driver (ICD) installed?\nPlease "
2513 "look at the Getting Started guide for additional "
2514 "information.\n",
2515 "vkCreateInstance Failure");
2516 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002517 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002518#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002519 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2520 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2521 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002522 "Vulkan installable client driver (ICD) installed?\nPlease "
2523 "look at the Getting Started guide for additional "
2524 "information.\n",
2525 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002526#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002527 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2528 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2529 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002530 "Vulkan installable client driver (ICD) installed?\nPlease "
2531 "look at the Getting Started guide for additional "
2532 "information.\n",
2533 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002534#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2535 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2536 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2537 " extension.\n\nDo you have a compatible "
2538 "Vulkan installable client driver (ICD) installed?\nPlease "
2539 "look at the Getting Started guide for additional "
2540 "information.\n",
2541 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002542#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2543 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2544 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2545 " extension.\n\nDo you have a compatible "
2546 "Vulkan installable client driver (ICD) installed?\nPlease "
2547 "look at the Getting Started guide for additional "
2548 "information.\n",
2549 "vkCreateInstance Failure");
2550#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002551 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002552#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002553 if (demo->use_xlib && !xlibSurfaceExtFound) {
2554 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2555 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2556 " extension.\n\nDo you have a compatible "
2557 "Vulkan installable client driver (ICD) installed?\nPlease "
2558 "look at the Getting Started guide for additional "
2559 "information.\n",
2560 "vkCreateInstance Failure");
2561 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002562#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002563 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002564 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002565 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002566 .pApplicationName = APP_SHORT_NAME,
2567 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002568 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002569 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002570 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002571 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002572 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002573 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002574 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002575 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002576 .enabledLayerCount = demo->enabled_layer_count,
2577 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2578 .enabledExtensionCount = demo->enabled_extension_count,
2579 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002580 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002581
2582 /*
2583 * This is info for a temp callback to use during CreateInstance.
2584 * After the instance is created, we use the instance-based
2585 * function to register the final callback.
2586 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002587 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002588 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002589 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2590 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002591 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002592 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002593 dbgCreateInfo.flags =
2594 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2595 inst_info.pNext = &dbgCreateInfo;
2596 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002597
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002598 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002599
Chia-I Wu63636062015-10-26 21:10:41 +08002600 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002601 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2602 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002603 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002604 "additional information.\n",
2605 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002606 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002607 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002608 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002609 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002610 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002611 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2612 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002613 "the Getting Started guide for additional information.\n",
2614 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002615 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002616
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002617 /* Make initial call to query gpu_count, then second call for gpu info*/
2618 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2619 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002620
2621 if (gpu_count > 0) {
2622 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2623 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2624 assert(!err);
2625 /* For cube demo we just grab the first physical device */
2626 demo->gpu = physical_devices[0];
2627 free(physical_devices);
2628 } else {
2629 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2630 "Do you have a compatible Vulkan installable client driver (ICD) "
2631 "installed?\nPlease look at the Getting Started guide for "
2632 "additional information.\n",
2633 "vkEnumeratePhysicalDevices Failure");
2634 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002635
Dustin Graves7c287352016-01-27 13:48:06 -07002636 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002637 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002638 VkBool32 swapchainExtFound = 0;
2639 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002640 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002641
Karl Schultze4dc75c2016-02-02 15:37:51 -07002642 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2643 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002644 assert(!err);
2645
Dustin Graves7c287352016-01-27 13:48:06 -07002646 if (device_extension_count > 0) {
2647 VkExtensionProperties *device_extensions =
2648 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2649 err = vkEnumerateDeviceExtensionProperties(
2650 demo->gpu, NULL, &device_extension_count, device_extensions);
2651 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002652
Dustin Graves7c287352016-01-27 13:48:06 -07002653 for (uint32_t i = 0; i < device_extension_count; i++) {
2654 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2655 device_extensions[i].extensionName)) {
2656 swapchainExtFound = 1;
2657 demo->extension_names[demo->enabled_extension_count++] =
2658 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2659 }
2660 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002661 }
Dustin Graves7c287352016-01-27 13:48:06 -07002662
2663 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002664 }
Dustin Graves7c287352016-01-27 13:48:06 -07002665
Tony Barbourcc69f452015-10-08 13:59:42 -06002666 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002667 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2668 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2669 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002670 "Vulkan installable client driver (ICD) installed?\nPlease "
2671 "look at the Getting Started guide for additional "
2672 "information.\n",
2673 "vkCreateInstance Failure");
2674 }
2675
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002676 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002677 demo->CreateDebugReportCallback =
2678 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2679 demo->inst, "vkCreateDebugReportCallbackEXT");
2680 demo->DestroyDebugReportCallback =
2681 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2682 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002683 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002684 ERR_EXIT(
2685 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2686 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002687 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002688 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002689 ERR_EXIT(
2690 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2691 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002692 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002693 demo->DebugReportMessage =
2694 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2695 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002696 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002697 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002698 "vkGetProcAddr Failure");
2699 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002700
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002701 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002702 PFN_vkDebugReportCallbackEXT callback;
2703 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002704 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002705 dbgCreateInfo.pNext = NULL;
2706 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002707 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002708 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002709 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002710 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2711 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002712 switch (err) {
2713 case VK_SUCCESS:
2714 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002715 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002716 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2717 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002718 break;
2719 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002720 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2721 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002722 break;
2723 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002724 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002725 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002726
2727 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06002728 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
2729 &demo->queue_family_count, NULL);
2730 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002731
Karl Schultze4dc75c2016-02-02 15:37:51 -07002732 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06002733 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
2734 vkGetPhysicalDeviceQueueFamilyProperties(
2735 demo->gpu, &demo->queue_family_count, demo->queue_props);
2736
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002737 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002738 // If app has specific feature requirements it should check supported
2739 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002740 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002741 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002742
Tony Barbourda2bad52016-01-22 14:36:40 -07002743 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2744 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2745 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2746 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2747 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2748}
2749
Karl Schultze4dc75c2016-02-02 15:37:51 -07002750static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002751 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002752 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002753 const VkDeviceQueueCreateInfo queue = {
2754 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2755 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002756 .queueFamilyIndex = demo->graphics_queue_family_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002757 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002758 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002759
2760 VkDeviceCreateInfo device = {
2761 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2762 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002763 .queueCreateInfoCount = 1,
2764 .pQueueCreateInfos = &queue,
Karl Schultz5b423ea2016-06-20 19:08:43 -06002765 .enabledLayerCount = 0,
2766 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002767 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002768 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2769 .pEnabledFeatures =
2770 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002771 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002772
Chia-I Wu63636062015-10-26 21:10:41 +08002773 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002774 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002775}
2776
Karl Schultze4dc75c2016-02-02 15:37:51 -07002777static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002778 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002779 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002780
Karl Schultze4dc75c2016-02-02 15:37:51 -07002781// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002782#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002783 VkWin32SurfaceCreateInfoKHR createInfo;
2784 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2785 createInfo.pNext = NULL;
2786 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002787 createInfo.hinstance = demo->connection;
2788 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002789
Karl Schultze4dc75c2016-02-02 15:37:51 -07002790 err =
2791 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002792#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2793 VkWaylandSurfaceCreateInfoKHR createInfo;
2794 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2795 createInfo.pNext = NULL;
2796 createInfo.flags = 0;
2797 createInfo.display = demo->display;
2798 createInfo.surface = demo->window;
2799
2800 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
2801 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002802#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2803 VkAndroidSurfaceCreateInfoKHR createInfo;
2804 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2805 createInfo.pNext = NULL;
2806 createInfo.flags = 0;
2807 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002808
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002809 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2810#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002811 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002812#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002813 VkXlibSurfaceCreateInfoKHR createInfo;
2814 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2815 createInfo.pNext = NULL;
2816 createInfo.flags = 0;
2817 createInfo.dpy = demo->display;
2818 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002819
Tony Barbour2593b182016-04-19 10:57:58 -06002820 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2821 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002822#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002823 }
2824 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002825#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002826 VkXcbSurfaceCreateInfoKHR createInfo;
2827 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2828 createInfo.pNext = NULL;
2829 createInfo.flags = 0;
2830 createInfo.connection = demo->connection;
2831 createInfo.window = demo->xcb_window;
2832
2833 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002834#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002835 }
Tony Barbour2593b182016-04-19 10:57:58 -06002836 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002837
Tony Barbourcc69f452015-10-08 13:59:42 -06002838 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002839 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06002840 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
2841 for (i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002842 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002843 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002844 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002845
2846 // Search for a graphics and a present queue in the array of queue
2847 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06002848 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
2849 uint32_t presentQueueFamilyIndex = UINT32_MAX;
2850 for (i = 0; i < demo->queue_family_count; i++) {
2851 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2852 if (graphicsQueueFamilyIndex == UINT32_MAX) {
2853 graphicsQueueFamilyIndex = i;
2854 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002855
Tony Barbourab2a0362016-09-06 11:40:12 -06002856 if (supportsPresent[i] == VK_TRUE) {
2857 graphicsQueueFamilyIndex = i;
2858 presentQueueFamilyIndex = i;
2859 break;
2860 }
2861 }
2862 }
2863
2864 if (presentQueueFamilyIndex == UINT32_MAX) {
2865 // If didn't find a queue that supports both graphics and present, then
2866 // find a separate present queue.
2867 for (size_t i = 0; i < demo->queue_family_count; ++i) {
2868 if (supportsPresent[i] == VK_TRUE) {
2869 presentQueueFamilyIndex = i;
2870 break;
2871 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002872 }
2873 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002874
2875 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06002876 if (graphicsQueueFamilyIndex == UINT32_MAX ||
2877 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06002878 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002879 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002880 }
2881
Tony Barbourab2a0362016-09-06 11:40:12 -06002882 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
2883 demo->present_queue_family_index = presentQueueFamilyIndex;
2884 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002885
Tony Barbourda2bad52016-01-22 14:36:40 -07002886 demo_create_device(demo);
2887
2888 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2889 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2890 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2891 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2892 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2893
Tony Barboura2a67812016-07-18 13:21:06 -06002894 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
2895 &demo->graphics_queue);
2896
2897 if (demo->graphics_queue_family_index == demo->present_queue_family_index) {
2898 demo->present_queue = demo->graphics_queue;
2899 } else {
2900 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
2901 &demo->present_queue);
2902 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002903
Ian Elliottaaae5352015-07-06 14:27:58 -06002904 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002905 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002906 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002907 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002908 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002909 VkSurfaceFormatKHR *surfFormats =
2910 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002911 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002912 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002913 assert(!err);
2914 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2915 // the surface has no preferred format. Otherwise, at least one
2916 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002917 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002918 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002919 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002920 assert(formatCount >= 1);
2921 demo->format = surfFormats[0].format;
2922 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002923 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002924
David Pinedo2bb7c932015-06-18 17:03:14 -06002925 demo->quit = false;
2926 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002927
Tony Barbource7524e2016-07-28 12:01:45 -06002928 // Create semaphores to synchronize acquiring presentable buffers before
2929 // rendering and waiting for drawing to be complete before presenting
2930 VkSemaphoreCreateInfo semaphoreCreateInfo = {
2931 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
2932 .pNext = NULL,
2933 .flags = 0,
2934 };
2935 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
2936 &demo->image_acquired_semaphore);
2937 assert(!err);
2938
2939 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
2940 &demo->draw_complete_semaphore);
2941 assert(!err);
2942
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002943 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002944 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002945}
2946
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002947#if defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR)
2948static void registry_handle_global(void *data, struct wl_registry *registry,
2949 uint32_t name, const char *interface,
2950 uint32_t version UNUSED) {
2951 struct demo *demo = data;
2952 if (strcmp(interface, "wl_compositor") == 0) {
2953 demo->compositor =
2954 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
2955 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
2956 * tp xdg_shell */
2957 } else if (strcmp(interface, "wl_shell") == 0) {
2958 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
2959 }
2960}
2961
2962static void registry_handle_global_remove(void *data UNUSED,
2963 struct wl_registry *registry UNUSED,
2964 uint32_t name UNUSED) {}
2965
2966static const struct wl_registry_listener registry_listener = {
2967 registry_handle_global, registry_handle_global_remove};
2968#endif
2969
Karl Schultze4dc75c2016-02-02 15:37:51 -07002970static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002971#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002972 const xcb_setup_t *setup;
2973 xcb_screen_iterator_t iter;
2974 int scr;
2975
2976 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06002977 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06002978 printf("Cannot find a compatible Vulkan installable client driver "
2979 "(ICD).\nExiting ...\n");
2980 fflush(stdout);
2981 exit(1);
2982 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002983
2984 setup = xcb_get_setup(demo->connection);
2985 iter = xcb_setup_roots_iterator(setup);
2986 while (scr-- > 0)
2987 xcb_screen_next(&iter);
2988
2989 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002990#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2991 demo->display = wl_display_connect(NULL);
2992
2993 if (demo->display == NULL) {
2994 printf("Cannot find a compatible Vulkan installable client driver "
2995 "(ICD).\nExiting ...\n");
2996 fflush(stdout);
2997 exit(1);
2998 }
2999
3000 demo->registry = wl_display_get_registry(demo->display);
3001 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3002 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003003#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003004}
3005
Karl Schultze4dc75c2016-02-02 15:37:51 -07003006static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003007 vec3 eye = {0.0f, 3.0f, 5.0f};
3008 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003009 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003010
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003011 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06003012 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003013
Piers Daniell735ee532015-02-23 16:23:13 -07003014 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003015 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003016 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003017 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003018 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003019 if (strcmp(argv[i], "--break") == 0) {
3020 demo->use_break = true;
3021 continue;
3022 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003023 if (strcmp(argv[i], "--validate") == 0) {
3024 demo->validate = true;
3025 continue;
3026 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003027#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003028 if (strcmp(argv[i], "--xlib") == 0) {
3029 demo->use_xlib = true;
3030 continue;
3031 }
Tony Barbour8295ac42016-08-08 11:04:17 -06003032#endif
Karl Schultze4dc75c2016-02-02 15:37:51 -07003033 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3034 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3035 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003036 i++;
3037 continue;
3038 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003039 if (strcmp(argv[i], "--suppress_popups") == 0) {
3040 demo->suppress_popups = true;
3041 continue;
3042 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003043
Karl Schultze4dc75c2016-02-02 15:37:51 -07003044 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
Tony Barbour8295ac42016-08-08 11:04:17 -06003045#if defined(VK_USE_PLATFORM_XLIB_KHR)
3046 "[--xlib] "
3047#endif
lenny-lunargfbe22392016-06-06 11:07:53 -06003048 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07003049 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06003050 fflush(stderr);
3051 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003052 }
3053
Tony Barbour2593b182016-04-19 10:57:58 -06003054 if (!demo->use_xlib)
3055 demo_init_connection(demo);
3056
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003057 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003058
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003059 demo->width = 500;
3060 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003061
3062 demo->spin_angle = 0.01f;
3063 demo->spin_increment = 0.01f;
3064 demo->pause = false;
3065
Karl Schultze4dc75c2016-02-02 15:37:51 -07003066 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3067 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003068 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3069 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003070
3071 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003072}
3073
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003074#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003075// Include header required for parsing the command line options.
3076#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003077
Karl Schultze4dc75c2016-02-02 15:37:51 -07003078int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3079 int nCmdShow) {
3080 MSG msg; // message
3081 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003082 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003083 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003084
Karl Schultze4dc75c2016-02-02 15:37:51 -07003085 // Use the CommandLine functions to get the command line arguments.
3086 // Unfortunately, Microsoft outputs
3087 // this information as wide characters for Unicode, and we simply want the
3088 // Ascii version to be compatible
3089 // with the non-Windows side. So, we have to convert the information to
3090 // Ascii character strings.
3091 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3092 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003093 argc = 0;
3094 }
3095
Karl Schultze4dc75c2016-02-02 15:37:51 -07003096 if (argc > 0) {
3097 argv = (char **)malloc(sizeof(char *) * argc);
3098 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003099 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003100 } else {
3101 for (int iii = 0; iii < argc; iii++) {
3102 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003103 size_t numConverted = 0;
3104
Karl Schultze4dc75c2016-02-02 15:37:51 -07003105 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3106 if (argv[iii] != NULL) {
3107 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3108 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003109 }
3110 }
3111 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003112 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003113 argv = NULL;
3114 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003115
3116 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003117
3118 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003119 if (argc > 0 && argv != NULL) {
3120 for (int iii = 0; iii < argc; iii++) {
3121 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003122 free(argv[iii]);
3123 }
3124 }
3125 free(argv);
3126 }
3127
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003128 demo.connection = hInstance;
3129 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003130 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003131 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003132
3133 demo_prepare(&demo);
3134
Karl Schultze4dc75c2016-02-02 15:37:51 -07003135 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003136
3137 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003138 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003139 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003140 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003141 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003142 done = true; // if found, quit app
3143 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003144 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003145 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003146 DispatchMessage(&msg);
3147 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003148 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003149 }
3150
3151 demo_cleanup(&demo);
3152
Karl Schultze4dc75c2016-02-02 15:37:51 -07003153 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003154}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003155#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3156#include <android/log.h>
3157#include <android_native_app_glue.h>
3158static bool initialized = false;
3159static bool active = false;
3160struct demo demo;
3161
3162static int32_t processInput(struct android_app* app, AInputEvent* event) {
3163 return 0;
3164}
3165
3166static void processCommand(struct android_app* app, int32_t cmd) {
3167 switch(cmd) {
3168 case APP_CMD_INIT_WINDOW: {
3169 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003170 // We're getting a new window. If the app is starting up, we
3171 // need to initialize. If the app has already been
3172 // initialized, that means that we lost our previous window,
3173 // which means that we have a lot of work to do. At a minimum,
3174 // we need to destroy the swapchain and surface associated with
3175 // the old window, and create a new surface and swapchain.
3176 // However, since there are a lot of other objects/state that
3177 // is tied to the swapchain, it's easiest to simply cleanup and
3178 // start over (i.e. use a brute-force approach of re-starting
3179 // the app)
3180 if (demo.prepared) {
3181 demo_cleanup(&demo);
3182 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003183 demo_init(&demo, 0, NULL);
3184 demo.window = (void*)app->window;
3185 demo_init_vk_swapchain(&demo);
3186 demo_prepare(&demo);
3187 initialized = true;
3188 }
3189 break;
3190 }
3191 case APP_CMD_GAINED_FOCUS: {
3192 active = true;
3193 break;
3194 }
3195 case APP_CMD_LOST_FOCUS: {
3196 active = false;
3197 break;
3198 }
3199 }
3200}
3201
3202void android_main(struct android_app *app)
3203{
3204 app_dummy();
3205
Cody Northrop8707a6e2016-04-26 19:59:19 -06003206#ifdef ANDROID
3207 int vulkanSupport = InitVulkan();
3208 if (vulkanSupport == 0)
3209 return;
3210#endif
3211
Ian Elliottf05d5d12016-05-17 12:03:35 -06003212 demo.prepared = false;
3213
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003214 app->onAppCmd = processCommand;
3215 app->onInputEvent = processInput;
3216
3217 while(1) {
3218 int events;
3219 struct android_poll_source* source;
3220 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3221 if (source) {
3222 source->process(app, source);
3223 }
3224
3225 if (app->destroyRequested != 0) {
3226 demo_cleanup(&demo);
3227 return;
3228 }
3229 }
3230 if (initialized && active) {
3231 demo_run(&demo);
3232 }
3233 }
3234
3235}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003236#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003237int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003238 struct demo demo;
3239
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003240 demo_init(&demo, argc, argv);
Tony Barbour8295ac42016-08-08 11:04:17 -06003241#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003242 if (demo.use_xlib)
3243 demo_create_xlib_window(&demo);
3244 else
3245 demo_create_xcb_window(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003246#elif defined(VK_USE_PLATFORM_XCB_KHR)
3247 demo_create_xcb_window(&demo);
3248#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3249 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003250#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3251 demo_create_window(&demo);
3252#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003253
Tony Barbourcc69f452015-10-08 13:59:42 -06003254 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003255
3256 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003257
Tony Barbour8295ac42016-08-08 11:04:17 -06003258#if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003259 if (demo.use_xlib)
3260 demo_run_xlib(&demo);
3261 else
3262 demo_run_xcb(&demo);
Tony Barbour8295ac42016-08-08 11:04:17 -06003263#elif defined(VK_USE_PLATFORM_XCB_KHR)
3264 demo_run_xcb(&demo);
3265#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3266 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003267#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3268 demo_run(&demo);
3269#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003270
3271 demo_cleanup(&demo);
3272
Karl Schultz0218c002016-03-22 17:06:13 -06003273 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003274}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003275#endif