blob: 608a81cbeba64c207d0107d0138962e53ebbe725 [file] [log] [blame]
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001 /*
Karl Schultze4dc75c2016-02-02 15:37:51 -07002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
Ian Elliotta748eaf2015-04-28 15:50:36 -06005 *
Jon Ashburn7b44e362016-04-19 11:30:31 -06006 * 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
Ian Elliotta748eaf2015-04-28 15:50:36 -06009 *
Jon Ashburn7b44e362016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Ian Elliotta748eaf2015-04-28 15:50:36 -060011 *
Jon Ashburn7b44e362016-04-19 11:30:31 -060012 * 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.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060017 *
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>
Ian Elliotta748eaf2015-04-28 15:50:36 -060022 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070023
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070030#include <signal.h>
Tony Barbour2593b182016-04-19 10:57:58 -060031#ifdef __linux__
32#include <X11/Xutil.h>
33#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060034
Ian Elliott639ca472015-04-16 15:23:05 -060035#ifdef _WIN32
36#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060037#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060038#endif // _WIN32
39
Cody Northrop8707a6e2016-04-26 19:59:19 -060040#ifdef ANDROID
41#include "vulkan_wrapper.h"
42#else
David Pinedoc5193c12015-11-06 12:54:48 -070043#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060044#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070045
David Pinedo541526f2015-11-24 09:00:24 -070046#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070062#define ERR_EXIT(err_msg, err_class) \
63 do { \
lenny-lunargfbe22392016-06-06 11:07:53 -060064 if (!demo->suppress_popups) \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070066 exit(1); \
67 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060068
Michael Lentinec6cde4b2016-04-18 13:20:45 -050069#elif defined __ANDROID__
70#include <android/log.h>
71#define ERR_EXIT(err_msg, err_class) \
72 do { \
73 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
74 exit(1); \
75 } while (0)
76#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070077#define ERR_EXIT(err_msg, err_class) \
78 do { \
79 printf(err_msg); \
80 fflush(stdout); \
81 exit(1); \
82 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050083#endif
Ian Elliott07264132015-04-28 11:35:02 -060084
Karl Schultze4dc75c2016-02-02 15:37:51 -070085#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
86 { \
87 demo->fp##entrypoint = \
88 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
89 if (demo->fp##entrypoint == NULL) { \
90 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
91 "vkGetInstanceProcAddr Failure"); \
92 } \
93 }
Ian Elliottaaae5352015-07-06 14:27:58 -060094
Jon Ashburn7d8342c2015-10-08 15:58:23 -060095static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
96
Karl Schultze4dc75c2016-02-02 15:37:51 -070097#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
98 { \
99 if (!g_gdpa) \
100 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
101 demo->inst, "vkGetDeviceProcAddr"); \
102 demo->fp##entrypoint = \
103 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
104 if (demo->fp##entrypoint == NULL) { \
105 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
106 "vkGetDeviceProcAddr Failure"); \
107 } \
108 }
Ian Elliott673898b2015-06-22 15:07:49 -0600109
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600110/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700111 * structure to track all objects related to a texture.
112 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600113struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600114 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700115
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600116 VkImage image;
117 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600118
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800119 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500120 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600121 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700122 int32_t tex_width, tex_height;
123};
124
Karl Schultze4dc75c2016-02-02 15:37:51 -0700125static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600126
Karl Schultz0218c002016-03-22 17:06:13 -0600127static int validation_error = 0;
128
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600129struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600130 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700131 float mvp[4][4];
132 float position[12 * 3][4];
133 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600134};
135
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600136struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600137 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700138 float mvp[4][4];
139 float position[12 * 3][4];
140 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600141};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600142
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600143//--------------------------------------------------------------------------------------
144// Mesh and VertexFormat Data
145//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700146// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600147struct Vertex
148{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600149 float posX, posY, posZ, posW; // Position data
150 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600151};
152
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600153struct VertexPosTex
154{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600155 float posX, posY, posZ, posW; // Position data
156 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600157};
158
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600159#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600160#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600161
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600162static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f, // -X 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,
168 -1.0f,-1.0f,-1.0f,
169
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 1.0f, 1.0f,-1.0f,
172 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 -1.0f, 1.0f,-1.0f,
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,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600179 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600182 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 -1.0f, 1.0f, 1.0f,
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,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600189 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 1.0f, 1.0f, 1.0f,
193 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800194 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600195 1.0f,-1.0f,-1.0f,
196 1.0f, 1.0f,-1.0f,
197
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600200 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800201 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600202 1.0f,-1.0f, 1.0f,
203 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600204};
205
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600206static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800207 0.0f, 0.0f, // -X side
208 1.0f, 0.0f,
209 1.0f, 1.0f,
210 1.0f, 1.0f,
211 0.0f, 1.0f,
212 0.0f, 0.0f,
213
214 1.0f, 0.0f, // -Z side
215 0.0f, 1.0f,
216 0.0f, 0.0f,
217 1.0f, 0.0f,
218 1.0f, 1.0f,
219 0.0f, 1.0f,
220
221 1.0f, 1.0f, // -Y side
222 1.0f, 0.0f,
223 0.0f, 0.0f,
224 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 0.0f, 0.0f,
226 0.0f, 1.0f,
227
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600229 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800230 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600231 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600233 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 1.0f, // +X side
236 0.0f, 1.0f,
237 0.0f, 0.0f,
238 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600239 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600240 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600241
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800242 0.0f, 1.0f, // +Z side
243 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600244 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600245 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800246 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600247 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600248};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700249// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600250
Karl Schultze4dc75c2016-02-02 15:37:51 -0700251void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600252 int i;
253
254 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700255 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600256 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
257 }
258 printf("\n");
259 fflush(stdout);
260}
261
Karl Schultze4dc75c2016-02-02 15:37:51 -0700262void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600263 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700264 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600265 printf("\n");
266 fflush(stdout);
267}
268
Karl Schultze4dc75c2016-02-02 15:37:51 -0700269VKAPI_ATTR VkBool32 VKAPI_CALL
Karl Schultz0c3c1512016-03-25 15:35:18 -0600270BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
271 uint64_t srcObject, size_t location, int32_t msgCode,
272 const char *pLayerPrefix, const char *pMsg,
273 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700274#ifndef WIN32
275 raise(SIGTRAP);
276#else
277 DebugBreak();
278#endif
279
280 return false;
281}
282
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600283typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600284 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800285 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600286 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600287} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600288
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600289struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500290#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600291#define APP_NAME_STR_LEN 80
292 HINSTANCE connection; // hInstance - Windows Instance
293 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700294 HWND window; // hWnd - window handle
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600295#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600296 Display* display;
297 Window xlib_window;
298 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600299
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600300 xcb_connection_t *connection;
301 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600302 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800303 xcb_intern_atom_reply_t *atom_wm_delete_window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500304#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
305 ANativeWindow* window;
306#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700307 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600308 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700309 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600310 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600312 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600313 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600314 VkDevice device;
315 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700316 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600317 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600318 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600319 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600320
Tony Barbourda2bad52016-01-22 14:36:40 -0700321 uint32_t enabled_extension_count;
322 uint32_t enabled_layer_count;
323 char *extension_names[64];
324 char *device_validation_layers[64];
325
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600328 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Karl Schultze4dc75c2016-02-02 15:37:51 -0700330 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
331 fpGetPhysicalDeviceSurfaceSupportKHR;
332 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
333 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
334 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
335 fpGetPhysicalDeviceSurfaceFormatsKHR;
336 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
337 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600338 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
339 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
340 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
341 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
342 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600343 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600344 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600345 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800347 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600348
349 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600350 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600352 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800353 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500354 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600355 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600356 } depth;
357
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600358 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600359
360 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600361 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800362 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500363 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600364 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600365 } uniform_data;
366
Karl Schultze4dc75c2016-02-02 15:37:51 -0700367 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500368 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600370 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800371 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600372 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600374 mat4x4 projection_matrix;
375 mat4x4 view_matrix;
376 mat4x4 model_matrix;
377
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600378 float spin_angle;
379 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600380 bool pause;
381
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600382 VkShaderModule vert_shader_module;
383 VkShaderModule frag_shader_module;
384
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 VkDescriptorPool desc_pool;
386 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800387
Tony Barbourd8e504f2015-10-08 14:26:24 -0600388 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800389
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600391 int32_t curFrame;
392 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600393 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600394 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600395 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700396 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
397 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
398 VkDebugReportCallbackEXT msg_callback;
399 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600400
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600401 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600402 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403};
404
lenny-lunargfbe22392016-06-06 11:07:53 -0600405VKAPI_ATTR VkBool32 VKAPI_CALL
406dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
407 uint64_t srcObject, size_t location, int32_t msgCode,
408 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
409 char *message = (char *)malloc(strlen(pMsg) + 100);
410
411 assert(message);
412
413 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
414 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
415 pMsg);
416 validation_error = 1;
417 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
418 // We know that we're submitting queues without fences, ignore this
419 // warning
420 if (strstr(pMsg,
421 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
422 return false;
423 }
424 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
425 pMsg);
426 validation_error = 1;
427 } else {
428 validation_error = 1;
429 return false;
430 }
431
432#ifdef _WIN32
433 struct demo *demo = (struct demo*) pUserData;
434 if (!demo->suppress_popups)
435 MessageBox(NULL, message, "Alert", MB_OK);
436#else
437 printf("%s\n", message);
438 fflush(stdout);
439#endif
440 free(message);
441
442 /*
443 * false indicates that layer should not bail-out of an
444 * API call that had validation failures. This may mean that the
445 * app dies inside the driver due to invalid parameter(s).
446 * That's what would happen without validation layers, so we'll
447 * keep that behavior here.
448 */
449 return false;
450}
451
Ian Elliottcf074d32015-10-16 18:02:43 -0600452// Forward declaration:
453static void demo_resize(struct demo *demo);
454
Karl Schultze4dc75c2016-02-02 15:37:51 -0700455static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
456 VkFlags requirements_mask,
457 uint32_t *typeIndex) {
458 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600459 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700460 if ((typeBits & 1) == 1) {
461 // Type is available, does it match user properties?
462 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
463 requirements_mask) == requirements_mask) {
464 *typeIndex = i;
465 return true;
466 }
467 }
468 typeBits >>= 1;
469 }
470 // No memory types matched, return failure
471 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600472}
473
Karl Schultze4dc75c2016-02-02 15:37:51 -0700474static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600475 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600477 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600478 return;
479
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 assert(!err);
482
Karl Schultze4dc75c2016-02-02 15:37:51 -0700483 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800484 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700485 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
486 .pNext = NULL,
487 .waitSemaphoreCount = 0,
488 .pWaitSemaphores = NULL,
489 .pWaitDstStageMask = NULL,
490 .commandBufferCount = 1,
491 .pCommandBuffers = cmd_bufs,
492 .signalSemaphoreCount = 0,
493 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600495 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 assert(!err);
497
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600498 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499 assert(!err);
500
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600501 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503}
504
Karl Schultze4dc75c2016-02-02 15:37:51 -0700505static void demo_set_image_layout(struct demo *demo, VkImage image,
506 VkImageAspectFlags aspectMask,
507 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700508 VkImageLayout new_image_layout,
509 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600510 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600512 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800513 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800514 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800516 .commandPool = demo->cmd_pool,
517 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700518 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600519 };
520
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800521 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600522 assert(!err);
523
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700524 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
525 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800527 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600528 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800529 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800530 .occlusionQueryEnable = VK_FALSE,
531 .queryFlags = 0,
532 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700534 VkCommandBufferBeginInfo cmd_buf_info = {
535 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
536 .pNext = NULL,
537 .flags = 0,
538 .pInheritanceInfo = &cmd_buf_hinfo,
539 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600540 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600541 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600542 }
543
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600544 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600545 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600546 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700547 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800548 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549 .oldLayout = old_image_layout,
550 .newLayout = new_image_layout,
551 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700552 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800554 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600555 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800556 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557 }
558
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700559 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700560 image_memory_barrier.dstAccessMask =
561 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700562 }
563
564 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700565 image_memory_barrier.dstAccessMask =
566 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700567 }
568
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600569 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600570 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700571 image_memory_barrier.dstAccessMask =
572 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600573 }
574
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600575 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600576
Tony Barbour50bbca42015-06-29 16:20:35 -0600577 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
578 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600579
Karl Schultze4dc75c2016-02-02 15:37:51 -0700580 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
581 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582}
583
Karl Schultze4dc75c2016-02-02 15:37:51 -0700584static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700585 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
586 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600587 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800588 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600589 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800590 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800591 .occlusionQueryEnable = VK_FALSE,
592 .queryFlags = 0,
593 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700594 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700595 const VkCommandBufferBeginInfo cmd_buf_info = {
596 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
597 .pNext = NULL,
598 .flags = 0,
599 .pInheritanceInfo = &cmd_buf_hinfo,
600 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800601 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700602 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
603 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800604 };
605 const VkRenderPassBeginInfo rp_begin = {
606 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
607 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800608 .renderPass = demo->render_pass,
609 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800610 .renderArea.offset.x = 0,
611 .renderArea.offset.y = 0,
612 .renderArea.extent.width = demo->width,
613 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600614 .clearValueCount = 2,
615 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700616 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800617 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600618
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600619 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600620 assert(!err);
621
Tony Barbour5c5b1632016-04-26 11:13:48 -0600622 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
623 // happens to the previous contents of the image
624 VkImageMemoryBarrier image_memory_barrier = {
625 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
626 .pNext = NULL,
627 .srcAccessMask = 0,
628 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
629 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
630 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
631 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
632 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
633 .image = demo->buffers[demo->current_buffer].image,
634 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
635
636 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
637 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
638 NULL, 1, &image_memory_barrier);
639
Chia-I Wuf051d262015-10-27 19:25:11 +0800640 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800641
Karl Schultze4dc75c2016-02-02 15:37:51 -0700642 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
643 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
644 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
645 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600646 VkViewport viewport;
647 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700648 viewport.height = (float)demo->height;
649 viewport.width = (float)demo->width;
650 viewport.minDepth = (float)0.0f;
651 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700652 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600653
654 VkRect2D scissor;
655 memset(&scissor, 0, sizeof(scissor));
656 scissor.extent.width = demo->width;
657 scissor.extent.height = demo->height;
658 scissor.offset.x = 0;
659 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700660 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600661 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800662 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600663
Tony Barbour15a4ef62015-10-21 10:14:48 -0600664 VkImageMemoryBarrier prePresentBarrier = {
665 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
666 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800667 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700668 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600669 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700670 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600671 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800672 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700673 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600674
675 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
676 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700677 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
678 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
679 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600680
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600681 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600682 assert(!err);
683}
684
Karl Schultze4dc75c2016-02-02 15:37:51 -0700685void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600686 mat4x4 MVP, Model, VP;
687 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600688 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600689 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600690
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600691 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600693 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600694 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700695 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
696 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600697 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600698
Karl Schultze4dc75c2016-02-02 15:37:51 -0700699 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
700 demo->uniform_data.mem_alloc.allocationSize, 0,
701 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600702 assert(!err);
703
Karl Schultze4dc75c2016-02-02 15:37:51 -0700704 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600705
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600706 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600707}
708
Karl Schultze4dc75c2016-02-02 15:37:51 -0700709static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600710 VkResult U_ASSERT_ONLY err;
Tony Barbour416c6502016-06-20 10:30:23 -0600711 VkSemaphore imageAcquiredSemaphore;
712 VkSemaphoreCreateInfo imageAcquiredSemaphoreCreateInfo = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600713 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
714 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600715 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600716 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800717 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600718
Tony Barbour416c6502016-06-20 10:30:23 -0600719 err = vkCreateSemaphore(demo->device, &imageAcquiredSemaphoreCreateInfo,
720 NULL, &imageAcquiredSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 assert(!err);
722
723 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700724 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barbour416c6502016-06-20 10:30:23 -0600725 imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700726 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600727 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600728 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
729 // demo->swapchain is out of date (e.g. the window was resized) and
730 // must be recreated:
731 demo_resize(demo);
732 demo_draw(demo);
Tony Barbour416c6502016-06-20 10:30:23 -0600733 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600734 return;
735 } else if (err == VK_SUBOPTIMAL_KHR) {
736 // demo->swapchain is not as optimal as it could be, but the platform's
737 // presentation engine will still present the image correctly.
738 } else {
739 assert(!err);
740 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600741
Tony Barbour15a4ef62015-10-21 10:14:48 -0600742 demo_flush_init_cmd(demo);
743
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600744 // Wait for the present complete semaphore to be signaled to ensure
745 // that the image won't be rendered to until the presentation
746 // engine has fully released ownership to the application, and it is
747 // okay to render to the image.
748
Karl Schultze4dc75c2016-02-02 15:37:51 -0700749 VkPipelineStageFlags pipe_stage_flags =
750 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
751 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
752 .pNext = NULL,
753 .waitSemaphoreCount = 1,
Tony Barbour416c6502016-06-20 10:30:23 -0600754 .pWaitSemaphores = &imageAcquiredSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700755 .pWaitDstStageMask = &pipe_stage_flags,
756 .commandBufferCount = 1,
757 .pCommandBuffers =
758 &demo->buffers[demo->current_buffer].cmd,
759 .signalSemaphoreCount = 0,
760 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600761
762 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600763 assert(!err);
764
Ian Elliotta0781972015-08-21 15:09:33 -0600765 VkPresentInfoKHR present = {
766 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600767 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600768 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700769 .pSwapchains = &demo->swapchain,
770 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600771 };
772
Karl Schultze4dc75c2016-02-02 15:37:51 -0700773 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600774 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600775 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
776 // demo->swapchain is out of date (e.g. the window was resized) and
777 // must be recreated:
778 demo_resize(demo);
779 } else if (err == VK_SUBOPTIMAL_KHR) {
780 // demo->swapchain is not as optimal as it could be, but the platform's
781 // presentation engine will still present the image correctly.
782 } else {
783 assert(!err);
784 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600785
Chia-I Wucbb564e2015-04-16 22:02:10 +0800786 err = vkQueueWaitIdle(demo->queue);
787 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600788
Tony Barbour416c6502016-06-20 10:30:23 -0600789 vkDestroySemaphore(demo->device, imageAcquiredSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600790}
791
Karl Schultze4dc75c2016-02-02 15:37:51 -0700792static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600793 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600794 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600795
Ian Elliottb08e0d92015-11-20 11:55:46 -0700796 // Check the surface capabilities and formats
797 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700798 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
799 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600800 assert(!err);
801
Ian Elliott8528eff2015-08-07 15:56:59 -0600802 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700803 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
804 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600805 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600806 VkPresentModeKHR *presentModes =
807 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600808 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700809 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
810 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600811 assert(!err);
812
Ian Elliotta0781972015-08-21 15:09:33 -0600813 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600814 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700815 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600816 // If the surface size is undefined, the size is set to
817 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600818 swapchainExtent.width = demo->width;
819 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700820 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600821 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700822 swapchainExtent = surfCapabilities.currentExtent;
823 demo->width = surfCapabilities.currentExtent.width;
824 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600825 }
826
827 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600828 // tearing mode. If not, try IMMEDIATE which will usually be available,
829 // and is fastest (though it tears). If not, fall back to FIFO which is
830 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600831 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600832 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600833 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
834 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600835 break;
836 }
Ian Elliotta0781972015-08-21 15:09:33 -0600837 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
838 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
839 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600840 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600841 }
842
843 // Determine the number of VkImage's to use in the swap chain (we desire to
844 // own only 1 image at a time, besides the images being displayed and
845 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700846 uint32_t desiredNumberOfSwapchainImages =
847 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700848 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700849 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600850 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700851 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600852 }
853
Tony Barbour9fd6d352015-09-16 15:01:32 -0600854 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700855 if (surfCapabilities.supportedTransforms &
856 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700857 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600858 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700859 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600860 }
861
Ian Elliottcf074d32015-10-16 18:02:43 -0600862 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600863 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800864 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700865 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600866 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800867 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600868 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700869 .imageExtent =
870 {
871 .width = swapchainExtent.width, .height = swapchainExtent.height,
872 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700873 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600874 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700875 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700876 .imageArrayLayers = 1,
877 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
878 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600879 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600880 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600881 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600882 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600883 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600884 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600885
Karl Schultze4dc75c2016-02-02 15:37:51 -0700886 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
887 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800888 assert(!err);
889
Ian Elliottcf074d32015-10-16 18:02:43 -0600890 // If we just re-created an existing swapchain, we should destroy the old
891 // swapchain at this point.
892 // Note: destroying the swapchain also cleans up all its associated
893 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800894 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700895 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600896 }
897
898 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600899 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600900 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800901
Karl Schultze4dc75c2016-02-02 15:37:51 -0700902 VkImage *swapchainImages =
903 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600904 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600905 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600906 &demo->swapchainImageCount,
907 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600908 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600909
Karl Schultze4dc75c2016-02-02 15:37:51 -0700910 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
911 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600912 assert(demo->buffers);
913
Ian Elliotta0781972015-08-21 15:09:33 -0600914 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600915 VkImageViewCreateInfo color_image_view = {
916 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600917 .pNext = NULL,
918 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700919 .components =
920 {
921 .r = VK_COMPONENT_SWIZZLE_R,
922 .g = VK_COMPONENT_SWIZZLE_G,
923 .b = VK_COMPONENT_SWIZZLE_B,
924 .a = VK_COMPONENT_SWIZZLE_A,
925 },
926 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
927 .baseMipLevel = 0,
928 .levelCount = 1,
929 .baseArrayLayer = 0,
930 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600931 .viewType = VK_IMAGE_VIEW_TYPE_2D,
932 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600933 };
934
Ian Elliotta0781972015-08-21 15:09:33 -0600935 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600936
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600937 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600938
Karl Schultze4dc75c2016-02-02 15:37:51 -0700939 err = vkCreateImageView(demo->device, &color_image_view, NULL,
940 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600941 assert(!err);
942 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700943
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500944
Mark Young04fd3d82016-01-25 14:43:50 -0700945 if (NULL != presentModes) {
946 free(presentModes);
947 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600948}
949
Karl Schultze4dc75c2016-02-02 15:37:51 -0700950static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600951 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600952 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600953 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600955 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700957 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600958 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600959 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800960 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600961 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600962 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600963 .flags = 0,
964 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200965
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600966 VkImageViewCreateInfo view = {
967 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600968 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800969 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600970 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700971 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
972 .baseMipLevel = 0,
973 .levelCount = 1,
974 .baseArrayLayer = 0,
975 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600976 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600977 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600979
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500980 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600981 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600982 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983
984 demo->depth.format = depth_format;
985
986 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700987 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600988 assert(!err);
989
Karl Schultze4dc75c2016-02-02 15:37:51 -0700990 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600991 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600992
Chia-I Wuf48700b2015-11-10 16:21:09 +0800993 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200994 demo->depth.mem_alloc.pNext = NULL;
995 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
996 demo->depth.mem_alloc.memoryTypeIndex = 0;
997
Karl Schultze4dc75c2016-02-02 15:37:51 -0700998 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
999 0, /* No requirements */
1000 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001001 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001002
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001003 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001004 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1005 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001006 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001007
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001008 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 err =
1010 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001011 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001012
Karl Schultze4dc75c2016-02-02 15:37:51 -07001013 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1014 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001015 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1016 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001017
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018 /* create image view */
1019 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001020 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001021 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001022}
1023
Tony Barbour1a86d032015-09-21 15:17:33 -06001024/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001025bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001026 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001027#ifdef __ANDROID__
1028#include <lunarg.ppm.h>
1029 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001030 cPtr = (char*)lunarg_ppm;
1031 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001032 return false;
1033 }
1034 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001035 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001036 if (rgba_data == NULL) {
1037 return true;
1038 }
1039 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001040 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001041 return false;
1042 }
1043 while(strncmp(cPtr++, "\n", 1));
1044
1045 for (int y = 0; y < *height; y++) {
1046 uint8_t *rowPtr = rgba_data;
1047 for (int x = 0; x < *width; x++) {
1048 memcpy(rowPtr, cPtr, 3);
1049 rowPtr[3] = 255; /* Alpha of 1 */
1050 rowPtr += 4;
1051 cPtr += 3;
1052 }
1053 rgba_data += layout->rowPitch;
1054 }
1055
1056 return true;
1057#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001058 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001059 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001060
Tony Barbour1a86d032015-09-21 15:17:33 -06001061 if (!fPtr)
1062 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001063
Tony Barbour1a86d032015-09-21 15:17:33 -06001064 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001065 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1066 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001067 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001068 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001069
Tony Barbour1a86d032015-09-21 15:17:33 -06001070 do {
1071 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001072 if (cPtr == NULL) {
1073 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001074 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001075 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001076 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001077
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001078 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001079 if (rgba_data == NULL) {
1080 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001081 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001082 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001083 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001084 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001085 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1086 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001087 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001088 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001089
Karl Schultze4dc75c2016-02-02 15:37:51 -07001090 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001091 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001092 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001093 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001094 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001095 rowPtr[3] = 255; /* Alpha of 1 */
1096 rowPtr += 4;
1097 }
1098 rgba_data += layout->rowPitch;
1099 }
1100 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001101 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001102#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001103}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001104
Karl Schultze4dc75c2016-02-02 15:37:51 -07001105static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001106 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001107 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001108 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001109 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001110 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001111 int32_t tex_width;
1112 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001113 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001114 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115
Karl Schultze4dc75c2016-02-02 15:37:51 -07001116 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001117 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001118 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001120 tex_obj->tex_width = tex_width;
1121 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001122
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001123 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001124 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001126 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001128 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001130 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001131 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001133 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001135 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001136 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001138 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139
Karl Schultze4dc75c2016-02-02 15:37:51 -07001140 err =
1141 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142 assert(!err);
1143
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001144 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001145
Chia-I Wuf48700b2015-11-10 16:21:09 +08001146 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001147 tex_obj->mem_alloc.pNext = NULL;
1148 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1149 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001150
Karl Schultze4dc75c2016-02-02 15:37:51 -07001151 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1152 required_props,
1153 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001154 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001155
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001156 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001157 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001158 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001159 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001161 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001162 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001163 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001165 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001166 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001167 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001169 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001171 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172 void *data;
1173
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1175 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001176
Karl Schultze4dc75c2016-02-02 15:37:51 -07001177 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1178 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001179 assert(!err);
1180
1181 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1182 fprintf(stderr, "Error loading texture: %s\n", filename);
1183 }
1184
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001185 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001186 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001187
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001188 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001189 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001190 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1191 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001192 /* setting the image layout does not reference the actual memory so no need
1193 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001194}
1195
Karl Schultze4dc75c2016-02-02 15:37:51 -07001196static void demo_destroy_texture_image(struct demo *demo,
1197 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001198 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001199 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1200 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201}
1202
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001204 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001205 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001206 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001207
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001208 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001209
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001211 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001212
Karl Schultze4dc75c2016-02-02 15:37:51 -07001213 if ((props.linearTilingFeatures &
1214 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1215 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001217 demo_prepare_texture_image(
1218 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1219 VK_IMAGE_USAGE_SAMPLED_BIT,
1220 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1221 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001222 } else if (props.optimalTilingFeatures &
1223 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001224 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001225 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226
1227 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001228 demo_prepare_texture_image(
1229 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1230 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1231 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1232 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001233
Karl Schultze4dc75c2016-02-02 15:37:51 -07001234 demo_prepare_texture_image(
1235 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1236 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1237 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001239 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001240 VK_IMAGE_ASPECT_COLOR_BIT,
1241 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001242 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1243 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001244
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001245 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001246 VK_IMAGE_ASPECT_COLOR_BIT,
1247 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001248 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1249 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001250
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001251 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001252 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1253 .srcOffset = {0, 0, 0},
1254 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1255 .dstOffset = {0, 0, 0},
1256 .extent = {staging_texture.tex_width,
1257 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001258 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001259 vkCmdCopyImage(
1260 demo->cmd, staging_texture.image,
1261 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1262 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001263
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001264 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001265 VK_IMAGE_ASPECT_COLOR_BIT,
1266 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001267 demo->textures[i].imageLayout,
1268 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001269
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001270 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001271
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001272 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001273 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001274 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1275 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001276 }
1277
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001278 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001279 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001280 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001281 .magFilter = VK_FILTER_NEAREST,
1282 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001283 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001284 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1285 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1286 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001287 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001288 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001289 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001290 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001291 .minLod = 0.0f,
1292 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001293 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001294 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001295 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001296
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001297 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001298 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001299 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001300 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001301 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001302 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001303 .components =
1304 {
1305 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1306 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1307 },
1308 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001309 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001311
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001312 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001313 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001314 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315 assert(!err);
1316
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317 /* create image view */
1318 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001319 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001320 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001321 assert(!err);
1322 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001323}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001324
Karl Schultze4dc75c2016-02-02 15:37:51 -07001325void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001326 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001327 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001328 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001329 int i;
1330 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001331 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001332 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001333 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001334
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001335 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001336 mat4x4_mul(MVP, VP, demo->model_matrix);
1337 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001338 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001339
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340 for (i = 0; i < 12 * 3; i++) {
1341 data.position[i][0] = g_vertex_buffer_data[i * 3];
1342 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1343 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001344 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001345 data.attr[i][0] = g_uv_buffer_data[2 * i];
1346 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001347 data.attr[i][2] = 0;
1348 data.attr[i][3] = 0;
1349 }
1350
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001351 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001352 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001353 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001354 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001355 err =
1356 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001357 assert(!err);
1358
Karl Schultze4dc75c2016-02-02 15:37:51 -07001359 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1360 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001361
Chia-I Wuf48700b2015-11-10 16:21:09 +08001362 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001363 demo->uniform_data.mem_alloc.pNext = NULL;
1364 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1365 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1366
Karl Schultze4dc75c2016-02-02 15:37:51 -07001367 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001368 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1369 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001370 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001371 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001372
Karl Schultze4dc75c2016-02-02 15:37:51 -07001373 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1374 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001375 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001376
Karl Schultze4dc75c2016-02-02 15:37:51 -07001377 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1378 demo->uniform_data.mem_alloc.allocationSize, 0,
1379 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001380 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001381
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001382 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001383
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001384 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001385
Karl Schultze4dc75c2016-02-02 15:37:51 -07001386 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1387 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001388 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001389
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001390 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1391 demo->uniform_data.buffer_info.offset = 0;
1392 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001393}
1394
Karl Schultze4dc75c2016-02-02 15:37:51 -07001395static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001396 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001397 [0] =
1398 {
1399 .binding = 0,
1400 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1401 .descriptorCount = 1,
1402 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1403 .pImmutableSamplers = NULL,
1404 },
1405 [1] =
1406 {
1407 .binding = 1,
1408 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1409 .descriptorCount = DEMO_TEXTURE_COUNT,
1410 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1411 .pImmutableSamplers = NULL,
1412 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001413 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001414 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001415 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001416 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001417 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001418 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001419 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001420 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421
Karl Schultze4dc75c2016-02-02 15:37:51 -07001422 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1423 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001424 assert(!err);
1425
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001426 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001427 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1428 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001429 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001430 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001431 };
1432
Karl Schultze4dc75c2016-02-02 15:37:51 -07001433 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001434 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001436}
1437
Karl Schultze4dc75c2016-02-02 15:37:51 -07001438static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001439 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001440 [0] =
1441 {
1442 .format = demo->format,
1443 .samples = VK_SAMPLE_COUNT_1_BIT,
1444 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1445 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1446 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1447 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1448 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1449 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1450 },
1451 [1] =
1452 {
1453 .format = demo->depth.format,
1454 .samples = VK_SAMPLE_COUNT_1_BIT,
1455 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1456 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1457 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1458 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1459 .initialLayout =
1460 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1461 .finalLayout =
1462 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1463 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001464 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001465 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001466 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001467 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001468 const VkAttachmentReference depth_reference = {
1469 .attachment = 1,
1470 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1471 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001472 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001473 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1474 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001475 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001476 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001477 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001478 .pColorAttachments = &color_reference,
1479 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001480 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001481 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001482 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001483 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001484 const VkRenderPassCreateInfo rp_info = {
1485 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1486 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001487 .attachmentCount = 2,
1488 .pAttachments = attachments,
1489 .subpassCount = 1,
1490 .pSubpasses = &subpass,
1491 .dependencyCount = 0,
1492 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001493 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001494 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001495
Chia-I Wu63636062015-10-26 21:10:41 +08001496 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001497 assert(!err);
1498}
1499
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001500//TODO: Merge shader reading
1501#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001502static VkShaderModule
1503demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001504 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001505 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001506 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001507
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001508 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1509 moduleCreateInfo.pNext = NULL;
1510
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001511 moduleCreateInfo.codeSize = size;
1512 moduleCreateInfo.pCode = code;
1513 moduleCreateInfo.flags = 0;
1514 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1515 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001516
1517 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001518}
1519
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001521 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001522 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001523 void *shader_code;
1524
1525 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001526 if (!fp)
1527 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001528
1529 fseek(fp, 0L, SEEK_END);
1530 size = ftell(fp);
1531
1532 fseek(fp, 0L, SEEK_SET);
1533
1534 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001535 retval = fread(shader_code, size, 1, fp);
1536 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001537
1538 *psize = size;
1539
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001540 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001541 return shader_code;
1542}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001543#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001544
Karl Schultze4dc75c2016-02-02 15:37:51 -07001545static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001546#ifdef __ANDROID__
1547 VkShaderModuleCreateInfo sh_info = {};
1548 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1549
1550#include "cube.vert.h"
1551 sh_info.codeSize = sizeof(cube_vert);
1552 sh_info.pCode = cube_vert;
1553 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1554 assert(!err);
1555#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001556 void *vertShaderCode;
1557 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001558
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001559 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1560
Karl Schultze4dc75c2016-02-02 15:37:51 -07001561 demo->vert_shader_module =
1562 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001563
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001564 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001565#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001566
1567 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568}
1569
Karl Schultze4dc75c2016-02-02 15:37:51 -07001570static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001571#ifdef __ANDROID__
1572 VkShaderModuleCreateInfo sh_info = {};
1573 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1574
1575#include "cube.frag.h"
1576 sh_info.codeSize = sizeof(cube_frag);
1577 sh_info.pCode = cube_frag;
1578 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1579 assert(!err);
1580#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001581 void *fragShaderCode;
1582 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001583
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001584 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001585
Karl Schultze4dc75c2016-02-02 15:37:51 -07001586 demo->frag_shader_module =
1587 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001588
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001589 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001590#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001591
1592 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593}
1594
Karl Schultze4dc75c2016-02-02 15:37:51 -07001595static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001596 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001597 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001598 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001599 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001600 VkPipelineRasterizationStateCreateInfo rs;
1601 VkPipelineColorBlendStateCreateInfo cb;
1602 VkPipelineDepthStencilStateCreateInfo ds;
1603 VkPipelineViewportStateCreateInfo vp;
1604 VkPipelineMultisampleStateCreateInfo ms;
1605 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1606 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001607 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001608
Piers Daniellba839d12015-09-29 13:01:09 -06001609 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1610 memset(&dynamicState, 0, sizeof dynamicState);
1611 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1612 dynamicState.pDynamicStates = dynamicStateEnables;
1613
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001615 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001616 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001618 memset(&vi, 0, sizeof(vi));
1619 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1620
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001621 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001622 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001623 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001624
1625 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001626 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1627 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001628 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001629 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001630 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001631 rs.rasterizerDiscardEnable = VK_FALSE;
1632 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001633 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001634
1635 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001636 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1637 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001638 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001639 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001640 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001641 cb.attachmentCount = 1;
1642 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001643
Tony Barbour29645d02015-01-16 14:27:35 -07001644 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001645 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001646 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001647 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1648 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001649 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001650 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1651 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001652
1653 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001654 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001655 ds.depthTestEnable = VK_TRUE;
1656 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001657 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001658 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001659 ds.back.failOp = VK_STENCIL_OP_KEEP;
1660 ds.back.passOp = VK_STENCIL_OP_KEEP;
1661 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001662 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001663 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001664
Tony Barbour29645d02015-01-16 14:27:35 -07001665 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001666 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001667 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001668 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001670 // Two stages: vs and fs
1671 pipeline.stageCount = 2;
1672 VkPipelineShaderStageCreateInfo shaderStages[2];
1673 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1674
Karl Schultze4dc75c2016-02-02 15:37:51 -07001675 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1676 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001677 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001678 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001679
Karl Schultze4dc75c2016-02-02 15:37:51 -07001680 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1681 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001682 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001683 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001684
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001685 memset(&pipelineCache, 0, sizeof(pipelineCache));
1686 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687
Karl Schultze4dc75c2016-02-02 15:37:51 -07001688 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1689 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001690 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001691
Karl Schultze4dc75c2016-02-02 15:37:51 -07001692 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001693 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001694 pipeline.pRasterizationState = &rs;
1695 pipeline.pColorBlendState = &cb;
1696 pipeline.pMultisampleState = &ms;
1697 pipeline.pViewportState = &vp;
1698 pipeline.pDepthStencilState = &ds;
1699 pipeline.pStages = shaderStages;
1700 pipeline.renderPass = demo->render_pass;
1701 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001702
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001703 pipeline.renderPass = demo->render_pass;
1704
Karl Schultze4dc75c2016-02-02 15:37:51 -07001705 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1706 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707 assert(!err);
1708
Chia-I Wu63636062015-10-26 21:10:41 +08001709 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1710 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711}
1712
Karl Schultze4dc75c2016-02-02 15:37:51 -07001713static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001714 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001715 [0] =
1716 {
1717 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1718 .descriptorCount = 1,
1719 },
1720 [1] =
1721 {
1722 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1723 .descriptorCount = DEMO_TEXTURE_COUNT,
1724 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001725 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001726 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001727 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001728 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001729 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001730 .poolSizeCount = 2,
1731 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001732 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001733 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001734
Karl Schultze4dc75c2016-02-02 15:37:51 -07001735 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1736 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001737 assert(!err);
1738}
1739
Karl Schultze4dc75c2016-02-02 15:37:51 -07001740static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001741 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001742 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001743 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001744 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001745
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001746 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001747 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001748 .pNext = NULL,
1749 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001750 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001751 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001752 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001753 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001754
Chia-I Wuae721ba2015-05-25 16:27:55 +08001755 memset(&tex_descs, 0, sizeof(tex_descs));
1756 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001757 tex_descs[i].sampler = demo->textures[i].sampler;
1758 tex_descs[i].imageView = demo->textures[i].view;
1759 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001760 }
1761
1762 memset(&writes, 0, sizeof(writes));
1763
1764 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001765 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001766 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001767 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001768 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001769
1770 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001771 writes[1].dstSet = demo->desc_set;
1772 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001773 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001774 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001775 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001776
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001777 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001778}
1779
Karl Schultze4dc75c2016-02-02 15:37:51 -07001780static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001781 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001782 attachments[1] = demo->depth.view;
1783
Chia-I Wuf973e322015-07-08 13:34:24 +08001784 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001785 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1786 .pNext = NULL,
1787 .renderPass = demo->render_pass,
1788 .attachmentCount = 2,
1789 .pAttachments = attachments,
1790 .width = demo->width,
1791 .height = demo->height,
1792 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001793 };
1794 VkResult U_ASSERT_ONLY err;
1795 uint32_t i;
1796
Karl Schultze4dc75c2016-02-02 15:37:51 -07001797 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1798 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001799 assert(demo->framebuffers);
1800
1801 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001802 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001803 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1804 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001805 assert(!err);
1806 }
1807}
1808
Karl Schultze4dc75c2016-02-02 15:37:51 -07001809static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001810 VkResult U_ASSERT_ONLY err;
1811
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001812 const VkCommandPoolCreateInfo cmd_pool_info = {
1813 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001814 .pNext = NULL,
1815 .queueFamilyIndex = demo->graphics_queue_node_index,
1816 .flags = 0,
1817 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001818 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1819 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001820 assert(!err);
1821
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001822 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001823 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001824 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001825 .commandPool = demo->cmd_pool,
1826 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001827 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001828 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001829
1830 demo_prepare_buffers(demo);
1831 demo_prepare_depth(demo);
1832 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001833 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001834
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001835 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001836 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001837 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838
Ian Elliotta0781972015-08-21 15:09:33 -06001839 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001840 err =
1841 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001842 assert(!err);
1843 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001844
Chia-I Wu63ea9262015-03-26 13:14:16 +08001845 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001846 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001847
Chia-I Wuf973e322015-07-08 13:34:24 +08001848 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001849
Ian Elliotta0781972015-08-21 15:09:33 -06001850 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001851 demo->current_buffer = i;
1852 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1853 }
1854
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001855 /*
1856 * Prepare functions above may generate pipeline commands
1857 * that need to be flushed before beginning the render loop.
1858 */
1859 demo_flush_init_cmd(demo);
1860
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001861 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001862 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001863}
1864
Karl Schultze4dc75c2016-02-02 15:37:51 -07001865static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001866 uint32_t i;
1867
1868 demo->prepared = false;
1869
Tony Barbourd8e504f2015-10-08 14:26:24 -06001870 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001871 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001872 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001873 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001874 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001875
Chia-I Wu63636062015-10-26 21:10:41 +08001876 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1877 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1878 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1879 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1880 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001881
1882 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001883 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1884 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1885 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1886 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001887 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001888 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001889
Chia-I Wu63636062015-10-26 21:10:41 +08001890 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1891 vkDestroyImage(demo->device, demo->depth.image, NULL);
1892 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001893
Chia-I Wu63636062015-10-26 21:10:41 +08001894 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1895 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001896
Ian Elliotta0781972015-08-21 15:09:33 -06001897 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001898 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001899 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1900 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001901 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001902 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001903
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001904 free(demo->queue_props);
1905
Chia-I Wu63636062015-10-26 21:10:41 +08001906 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1907 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001908 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001909 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001910 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001911 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001912 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001913
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001914#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001915 if (demo->use_xlib) {
1916 XDestroyWindow(demo->display, demo->xlib_window);
1917 XCloseDisplay(demo->display);
1918 } else {
1919 xcb_destroy_window(demo->connection, demo->xcb_window);
1920 xcb_disconnect(demo->connection);
1921 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001922 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001923#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001924 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001925 xcb_disconnect(demo->connection);
1926 free(demo->atom_wm_delete_window);
1927#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001928}
1929
Karl Schultze4dc75c2016-02-02 15:37:51 -07001930static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001931 uint32_t i;
1932
Mike Stroyanbb60b382015-10-28 09:46:57 -06001933 // Don't react to resize until after first initialization.
1934 if (!demo->prepared) {
1935 return;
1936 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001937 // In order to properly resize the window, we must re-create the swapchain
1938 // AND redo the command buffers, etc.
1939 //
1940 // First, perform part of the demo_cleanup() function:
1941 demo->prepared = false;
1942
1943 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001944 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001945 }
1946 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001947 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001948
Chia-I Wu63636062015-10-26 21:10:41 +08001949 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1950 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1951 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1952 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1953 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001954
1955 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001956 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1957 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1958 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1959 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001960 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001961
Chia-I Wu63636062015-10-26 21:10:41 +08001962 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1963 vkDestroyImage(demo->device, demo->depth.image, NULL);
1964 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001965
Chia-I Wu63636062015-10-26 21:10:41 +08001966 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1967 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001968
1969 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001970 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001971 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1972 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001973 }
Chia-I Wu63636062015-10-26 21:10:41 +08001974 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001975 free(demo->buffers);
1976
Ian Elliottcf074d32015-10-16 18:02:43 -06001977 // Second, re-perform the demo_prepare() function, which will re-create the
1978 // swapchain:
1979 demo_prepare(demo);
1980}
1981
David Pinedo2bb7c932015-06-18 17:03:14 -06001982// On MS-Windows, make this a global, so it's available to WndProc()
1983struct demo demo;
1984
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001985#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07001986static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001987 if (!demo->prepared)
1988 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001989 // Wait for work to finish before updating MVP.
1990 vkDeviceWaitIdle(demo->device);
1991 demo_update_data_buffer(demo);
1992
1993 demo_draw(demo);
1994
1995 // Wait for work to finish before updating MVP.
1996 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001997
David Pinedo2bb7c932015-06-18 17:03:14 -06001998 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001999 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002000 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002001 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002002}
Ian Elliott639ca472015-04-16 15:23:05 -06002003
2004// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002005LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2006 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002007 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002008 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002009 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002010 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002011 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002012 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002013 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002014 // Resize the application to the new window size, except when
2015 // it was minimized. Vulkan doesn't support images or swapchains
2016 // with width=0 and height=0.
2017 if (wParam != SIZE_MINIMIZED) {
2018 demo.width = lParam & 0xffff;
2019 demo.height = lParam & 0xffff0000 >> 16;
2020 demo_resize(&demo);
2021 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002022 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002023 default:
2024 break;
2025 }
2026 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2027}
2028
Karl Schultze4dc75c2016-02-02 15:37:51 -07002029static void demo_create_window(struct demo *demo) {
2030 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002031
2032 // Initialize the window class structure:
2033 win_class.cbSize = sizeof(WNDCLASSEX);
2034 win_class.style = CS_HREDRAW | CS_VREDRAW;
2035 win_class.lpfnWndProc = WndProc;
2036 win_class.cbClsExtra = 0;
2037 win_class.cbWndExtra = 0;
2038 win_class.hInstance = demo->connection; // hInstance
2039 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2040 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2041 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2042 win_class.lpszMenuName = NULL;
2043 win_class.lpszClassName = demo->name;
2044 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2045 // Register window class:
2046 if (!RegisterClassEx(&win_class)) {
2047 // It didn't work, so try to give a useful error:
2048 printf("Unexpected error trying to start the application!\n");
2049 fflush(stdout);
2050 exit(1);
2051 }
2052 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002053 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002054 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002055 demo->window = CreateWindowEx(0,
2056 demo->name, // class name
2057 demo->name, // app name
2058 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002059 WS_VISIBLE | WS_SYSMENU,
2060 100, 100, // x/y coords
2061 wr.right - wr.left, // width
2062 wr.bottom - wr.top, // height
2063 NULL, // handle to parent
2064 NULL, // handle to menu
2065 demo->connection, // hInstance
2066 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002067 if (!demo->window) {
2068 // It didn't work, so try to give a useful error:
2069 printf("Cannot create a window in which to draw!\n");
2070 fflush(stdout);
2071 exit(1);
2072 }
2073}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002074#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002075static void demo_create_xlib_window(struct demo *demo) {
2076
2077 demo->display = XOpenDisplay(NULL);
2078 long visualMask = VisualScreenMask;
2079 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002080 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002081 vInfoTemplate.screen = DefaultScreen(demo->display);
2082 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2083 &vInfoTemplate, &numberOfVisuals);
2084
2085 Colormap colormap = XCreateColormap(
2086 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2087 visualInfo->visual, AllocNone);
2088
Rene Lindsay11612722016-06-13 17:20:39 -06002089 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002090 windowAttributes.colormap = colormap;
2091 windowAttributes.background_pixel = 0xFFFFFFFF;
2092 windowAttributes.border_pixel = 0;
2093 windowAttributes.event_mask =
2094 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2095
2096 demo->xlib_window = XCreateWindow(
2097 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2098 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2099 visualInfo->visual,
2100 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2101
2102 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2103 XMapWindow(demo->display, demo->xlib_window);
2104 XFlush(demo->display);
2105 demo->xlib_wm_delete_window =
2106 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2107}
2108static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2109 switch(event->type) {
2110 case ClientMessage:
2111 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2112 demo->quit = true;
2113 break;
2114 case KeyPress:
2115 switch (event->xkey.keycode) {
2116 case 0x9: // Escape
2117 demo->quit = true;
2118 break;
2119 case 0x71: // left arrow key
2120 demo->spin_angle += demo->spin_increment;
2121 break;
2122 case 0x72: // right arrow key
2123 demo->spin_angle -= demo->spin_increment;
2124 break;
2125 case 0x41:
2126 demo->pause = !demo->pause;
2127 break;
2128 }
2129 break;
2130 case ConfigureNotify:
2131 if ((demo->width != event->xconfigure.width) ||
2132 (demo->height != event->xconfigure.height)) {
2133 demo->width = event->xconfigure.width;
2134 demo->height = event->xconfigure.height;
2135 demo_resize(demo);
2136 }
2137 break;
2138 default:
2139 break;
2140 }
2141
2142}
2143
2144static void demo_run_xlib(struct demo *demo) {
2145
2146 while (!demo->quit) {
2147 XEvent event;
2148
2149 if (demo->pause) {
2150 XNextEvent(demo->display, &event);
2151 demo_handle_xlib_event(demo, &event);
2152 } else {
2153 while (XPending(demo->display) > 0) {
2154 XNextEvent(demo->display, &event);
2155 demo_handle_xlib_event(demo, &event);
2156 }
2157 }
2158
2159 // Wait for work to finish before updating MVP.
2160 vkDeviceWaitIdle(demo->device);
2161 demo_update_data_buffer(demo);
2162
2163 demo_draw(demo);
2164
2165 // Wait for work to finish before updating MVP.
2166 vkDeviceWaitIdle(demo->device);
2167 demo->curFrame++;
2168 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2169 demo->quit = true;
2170 }
2171}
2172
2173static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002174 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002175 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002176 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002177 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002178 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002179 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002180 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002181 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2182 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002183 demo->quit = true;
2184 }
2185 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 case XCB_KEY_RELEASE: {
2187 const xcb_key_release_event_t *key =
2188 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002189
Karl Schultze4dc75c2016-02-02 15:37:51 -07002190 switch (key->detail) {
2191 case 0x9: // Escape
2192 demo->quit = true;
2193 break;
2194 case 0x71: // left arrow key
2195 demo->spin_angle += demo->spin_increment;
2196 break;
2197 case 0x72: // right arrow key
2198 demo->spin_angle -= demo->spin_increment;
2199 break;
2200 case 0x41:
2201 demo->pause = !demo->pause;
2202 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002203 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002204 } break;
2205 case XCB_CONFIGURE_NOTIFY: {
2206 const xcb_configure_notify_event_t *cfg =
2207 (const xcb_configure_notify_event_t *)event;
2208 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002209 demo->width = cfg->width;
2210 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002211 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002212 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002213 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002214 default:
2215 break;
2216 }
2217}
2218
Tony Barbour2593b182016-04-19 10:57:58 -06002219static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002220 xcb_flush(demo->connection);
2221
2222 while (!demo->quit) {
2223 xcb_generic_event_t *event;
2224
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002225 if (demo->pause) {
2226 event = xcb_wait_for_event(demo->connection);
2227 } else {
2228 event = xcb_poll_for_event(demo->connection);
2229 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002230 if (event) {
Tony Barbour2593b182016-04-19 10:57:58 -06002231 demo_handle_xcb_event(demo, event);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002232 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002233 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002234
2235 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002236 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002237 demo_update_data_buffer(demo);
2238
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002239 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002240
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002241 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002242 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002243 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002244 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002245 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002246 }
2247}
2248
Tony Barbour2593b182016-04-19 10:57:58 -06002249static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002250 uint32_t value_mask, value_list[32];
2251
Tony Barbour2593b182016-04-19 10:57:58 -06002252 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002253
2254 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2255 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002256 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002257 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002258
Tony Barbour2593b182016-04-19 10:57:58 -06002259 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002260 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2261 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2262 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002263
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002264 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002265 xcb_intern_atom_cookie_t cookie =
2266 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2267 xcb_intern_atom_reply_t *reply =
2268 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002269
Karl Schultze4dc75c2016-02-02 15:37:51 -07002270 xcb_intern_atom_cookie_t cookie2 =
2271 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2272 demo->atom_wm_delete_window =
2273 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002274
Tony Barbour2593b182016-04-19 10:57:58 -06002275 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002276 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002277 &(*demo->atom_wm_delete_window).atom);
2278 free(reply);
2279
Tony Barbour2593b182016-04-19 10:57:58 -06002280 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002281
Karl Schultze4dc75c2016-02-02 15:37:51 -07002282 // Force the x/y coordinates to 100,100 results are identical in consecutive
2283 // runs
2284 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002285 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002286 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002287}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002288#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2289static void demo_run(struct demo *demo) {
2290 if (!demo->prepared)
2291 return;
2292
2293 // Wait for work to finish before updating MVP.
2294 vkDeviceWaitIdle(demo->device);
2295 demo_update_data_buffer(demo);
2296
2297 demo_draw(demo);
2298
2299 // Wait for work to finish before updating MVP.
2300 vkDeviceWaitIdle(demo->device);
2301
2302 demo->curFrame++;
2303}
2304#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002305
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002306/*
2307 * Return 1 (true) if all layer names specified in check_names
2308 * can be found in given layer properties.
2309 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002310static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002311 uint32_t layer_count,
2312 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002313 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002314 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002315 for (uint32_t j = 0; j < layer_count; j++) {
2316 if (!strcmp(check_names[i], layers[j].layerName)) {
2317 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002318 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002319 }
2320 }
2321 if (!found) {
2322 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2323 return 0;
2324 }
2325 }
2326 return 1;
2327}
2328
Karl Schultze4dc75c2016-02-02 15:37:51 -07002329static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002330 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002331 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002332 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002333 uint32_t device_validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002334 char **instance_validation_layers = NULL;
2335 demo->enabled_extension_count = 0;
2336 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002337
Karl Schultz7c50ad62016-04-01 12:07:03 -06002338 char *instance_validation_layers_alt1[] = {
2339 "VK_LAYER_LUNARG_standard_validation"
2340 };
2341
2342 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskiaaab5c02016-03-17 15:08:18 -06002343 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Karl Schultz635272d2016-03-07 17:54:07 -07002344 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis1398c712016-03-09 16:12:48 -07002345 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
Karl Schultz7c50ad62016-04-01 12:07:03 -06002346 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002347 };
2348
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002349 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002350 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002351 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002352
Karl Schultz7c50ad62016-04-01 12:07:03 -06002353 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002354 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002355
Karl Schultz7c50ad62016-04-01 12:07:03 -06002356 instance_validation_layers = instance_validation_layers_alt1;
2357 if (instance_layer_count > 0) {
2358 VkLayerProperties *instance_layers =
2359 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2360 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2361 instance_layers);
2362 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002363
Karl Schultz7c50ad62016-04-01 12:07:03 -06002364
2365 validation_found = demo_check_layers(
2366 ARRAY_SIZE(instance_validation_layers_alt1),
2367 instance_validation_layers, instance_layer_count,
2368 instance_layers);
2369 if (validation_found) {
2370 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
2371 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2372 device_validation_layer_count = 1;
2373 } else {
2374 // use alternative set of validation layers
2375 instance_validation_layers = instance_validation_layers_alt2;
2376 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2377 validation_found = demo_check_layers(
2378 ARRAY_SIZE(instance_validation_layers_alt2),
2379 instance_validation_layers, instance_layer_count,
2380 instance_layers);
2381 device_validation_layer_count =
2382 ARRAY_SIZE(instance_validation_layers_alt2);
2383 for (uint32_t i = 0; i < device_validation_layer_count; i++) {
2384 demo->device_validation_layers[i] =
2385 instance_validation_layers[i];
2386 }
2387 }
2388 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002389 }
Dustin Graves7c287352016-01-27 13:48:06 -07002390
Karl Schultz7c50ad62016-04-01 12:07:03 -06002391 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002392 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002393 "required validation layer.\n\n"
2394 "Please look at the Getting Started guide for additional "
2395 "information.\n",
2396 "vkCreateInstance Failure");
2397 }
Dustin Graves7c287352016-01-27 13:48:06 -07002398 }
2399
2400 /* Look for instance extensions */
2401 VkBool32 surfaceExtFound = 0;
2402 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002403#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002404 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002405#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002406 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002407
Karl Schultze4dc75c2016-02-02 15:37:51 -07002408 err = vkEnumerateInstanceExtensionProperties(
2409 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002410 assert(!err);
2411
Dustin Graves7c287352016-01-27 13:48:06 -07002412 if (instance_extension_count > 0) {
2413 VkExtensionProperties *instance_extensions =
2414 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2415 err = vkEnumerateInstanceExtensionProperties(
2416 NULL, &instance_extension_count, instance_extensions);
2417 assert(!err);
2418 for (uint32_t i = 0; i < instance_extension_count; i++) {
2419 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2420 instance_extensions[i].extensionName)) {
2421 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002422 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002423 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002424 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002425#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002426 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2427 instance_extensions[i].extensionName)) {
2428 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002429 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002430 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2431 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002432#endif
2433#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002434 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2435 instance_extensions[i].extensionName)) {
2436 platformSurfaceExtFound = 1;
2437 xlibSurfaceExtFound = 1;
2438 demo->extension_names[demo->enabled_extension_count++] =
2439 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2440 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002441#endif
2442#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002443 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2444 instance_extensions[i].extensionName)) {
2445 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002446 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002447 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2448 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002449#endif
2450#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002451 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2452 instance_extensions[i].extensionName)) {
2453 platformSurfaceExtFound = 1;
2454 demo->extension_names[demo->enabled_extension_count++] =
2455 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2456 }
2457#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002458 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2459 instance_extensions[i].extensionName)) {
2460 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002461 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002462 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2463 }
2464 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002465 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002466 }
Dustin Graves7c287352016-01-27 13:48:06 -07002467
2468 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002469 }
Dustin Graves7c287352016-01-27 13:48:06 -07002470
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002471 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002472 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2473 "the " VK_KHR_SURFACE_EXTENSION_NAME
2474 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002475 "Vulkan installable client driver (ICD) installed?\nPlease "
2476 "look at the Getting Started guide for additional "
2477 "information.\n",
2478 "vkCreateInstance Failure");
2479 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002480 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002481#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002482 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2483 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2484 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002485 "Vulkan installable client driver (ICD) installed?\nPlease "
2486 "look at the Getting Started guide for additional "
2487 "information.\n",
2488 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002489#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002490 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2491 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2492 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002493 "Vulkan installable client driver (ICD) installed?\nPlease "
2494 "look at the Getting Started guide for additional "
2495 "information.\n",
2496 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002497#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2498 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2499 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2500 " extension.\n\nDo you have a compatible "
2501 "Vulkan installable client driver (ICD) installed?\nPlease "
2502 "look at the Getting Started guide for additional "
2503 "information.\n",
2504 "vkCreateInstance Failure");
2505#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002506 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002507#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002508 if (demo->use_xlib && !xlibSurfaceExtFound) {
2509 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2510 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2511 " extension.\n\nDo you have a compatible "
2512 "Vulkan installable client driver (ICD) installed?\nPlease "
2513 "look at the Getting Started guide for additional "
2514 "information.\n",
2515 "vkCreateInstance Failure");
2516 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002517#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002518 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002519 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002520 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002521 .pApplicationName = APP_SHORT_NAME,
2522 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002523 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002524 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002525 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002526 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002527 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002528 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002529 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002530 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002531 .enabledLayerCount = demo->enabled_layer_count,
2532 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2533 .enabledExtensionCount = demo->enabled_extension_count,
2534 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002535 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002536
2537 /*
2538 * This is info for a temp callback to use during CreateInstance.
2539 * After the instance is created, we use the instance-based
2540 * function to register the final callback.
2541 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002542 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002543 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002544 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2545 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002546 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
lenny-lunargfbe22392016-06-06 11:07:53 -06002547 dbgCreateInfo.pUserData = demo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002548 dbgCreateInfo.flags =
2549 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2550 inst_info.pNext = &dbgCreateInfo;
2551 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002552
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002553 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002554
Chia-I Wu63636062015-10-26 21:10:41 +08002555 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002556 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2557 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002558 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002559 "additional information.\n",
2560 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002561 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002562 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002563 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002564 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002565 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002566 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2567 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002568 "the Getting Started guide for additional information.\n",
2569 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002570 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002571
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002572 /* Make initial call to query gpu_count, then second call for gpu info*/
2573 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2574 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002575
2576 if (gpu_count > 0) {
2577 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2578 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2579 assert(!err);
2580 /* For cube demo we just grab the first physical device */
2581 demo->gpu = physical_devices[0];
2582 free(physical_devices);
2583 } else {
2584 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2585 "Do you have a compatible Vulkan installable client driver (ICD) "
2586 "installed?\nPlease look at the Getting Started guide for "
2587 "additional information.\n",
2588 "vkEnumeratePhysicalDevices Failure");
2589 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002590
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002591 /* Look for validation layers */
2592 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002593 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002594 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002595 err =
2596 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002597 assert(!err);
2598
Dustin Graves7c287352016-01-27 13:48:06 -07002599 if (device_layer_count > 0) {
2600 VkLayerProperties *device_layers =
2601 malloc(sizeof(VkLayerProperties) * device_layer_count);
2602 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2603 device_layers);
2604 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002605
Dustin Graves7c287352016-01-27 13:48:06 -07002606 if (demo->validate) {
2607 validation_found = demo_check_layers(device_validation_layer_count,
2608 demo->device_validation_layers,
2609 device_layer_count,
2610 device_layers);
2611 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002612 }
Dustin Graves7c287352016-01-27 13:48:06 -07002613
2614 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002615 }
2616
Dustin Graves7c287352016-01-27 13:48:06 -07002617 if (demo->validate && !validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002618 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find "
Dustin Graves7c287352016-01-27 13:48:06 -07002619 "a required validation layer.\n\n"
2620 "Please look at the Getting Started guide for additional "
2621 "information.\n",
2622 "vkCreateDevice Failure");
2623 }
2624
2625 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002626 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002627 VkBool32 swapchainExtFound = 0;
2628 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002629 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002630
Karl Schultze4dc75c2016-02-02 15:37:51 -07002631 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2632 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002633 assert(!err);
2634
Dustin Graves7c287352016-01-27 13:48:06 -07002635 if (device_extension_count > 0) {
2636 VkExtensionProperties *device_extensions =
2637 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2638 err = vkEnumerateDeviceExtensionProperties(
2639 demo->gpu, NULL, &device_extension_count, device_extensions);
2640 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002641
Dustin Graves7c287352016-01-27 13:48:06 -07002642 for (uint32_t i = 0; i < device_extension_count; i++) {
2643 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2644 device_extensions[i].extensionName)) {
2645 swapchainExtFound = 1;
2646 demo->extension_names[demo->enabled_extension_count++] =
2647 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2648 }
2649 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002650 }
Dustin Graves7c287352016-01-27 13:48:06 -07002651
2652 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002653 }
Dustin Graves7c287352016-01-27 13:48:06 -07002654
Tony Barbourcc69f452015-10-08 13:59:42 -06002655 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002656 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2657 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2658 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002659 "Vulkan installable client driver (ICD) installed?\nPlease "
2660 "look at the Getting Started guide for additional "
2661 "information.\n",
2662 "vkCreateInstance Failure");
2663 }
2664
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002665 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002666 demo->CreateDebugReportCallback =
2667 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2668 demo->inst, "vkCreateDebugReportCallbackEXT");
2669 demo->DestroyDebugReportCallback =
2670 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2671 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002672 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002673 ERR_EXIT(
2674 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2675 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002676 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002677 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002678 ERR_EXIT(
2679 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2680 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002681 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002682 demo->DebugReportMessage =
2683 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2684 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002685 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002686 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002687 "vkGetProcAddr Failure");
2688 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002689
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002690 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002691 PFN_vkDebugReportCallbackEXT callback;
2692 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002693 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002694 dbgCreateInfo.pNext = NULL;
2695 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06002696 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002697 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002698 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002699 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2700 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002701 switch (err) {
2702 case VK_SUCCESS:
2703 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002704 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002705 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2706 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002707 break;
2708 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002709 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2710 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002711 break;
2712 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002713 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002714 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002715
2716 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002717 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2718 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002719 assert(demo->queue_count >= 1);
2720
Karl Schultze4dc75c2016-02-02 15:37:51 -07002721 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2722 demo->queue_count * sizeof(VkQueueFamilyProperties));
2723 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2724 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002725 // Find a queue that supports gfx
2726 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002727 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2728 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002729 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2730 break;
2731 }
2732 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002733 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002734 // If app has specific feature requirements it should check supported
2735 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002736 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002737 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002738
Tony Barbourda2bad52016-01-22 14:36:40 -07002739 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2740 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2741 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2742 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2743 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2744}
2745
Karl Schultze4dc75c2016-02-02 15:37:51 -07002746static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002747 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002748 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002749 const VkDeviceQueueCreateInfo queue = {
2750 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2751 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002752 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002753 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002754 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002755
2756 VkDeviceCreateInfo device = {
2757 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2758 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002759 .queueCreateInfoCount = 1,
2760 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002761 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002762 .ppEnabledLayerNames =
2763 (const char *const *)((demo->validate)
2764 ? demo->device_validation_layers
2765 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002766 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002767 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2768 .pEnabledFeatures =
2769 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002770 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002771
Chia-I Wu63636062015-10-26 21:10:41 +08002772 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002773 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002774}
2775
Karl Schultze4dc75c2016-02-02 15:37:51 -07002776static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002777 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002778 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002779
Karl Schultze4dc75c2016-02-02 15:37:51 -07002780// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002781#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002782 VkWin32SurfaceCreateInfoKHR createInfo;
2783 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2784 createInfo.pNext = NULL;
2785 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002786 createInfo.hinstance = demo->connection;
2787 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002788
Karl Schultze4dc75c2016-02-02 15:37:51 -07002789 err =
2790 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002791#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2792 VkAndroidSurfaceCreateInfoKHR createInfo;
2793 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2794 createInfo.pNext = NULL;
2795 createInfo.flags = 0;
2796 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002797
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002798 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2799#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002800 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002801#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002802 VkXlibSurfaceCreateInfoKHR createInfo;
2803 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2804 createInfo.pNext = NULL;
2805 createInfo.flags = 0;
2806 createInfo.dpy = demo->display;
2807 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002808
Tony Barbour2593b182016-04-19 10:57:58 -06002809 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2810 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002811#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002812 }
2813 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002814#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002815 VkXcbSurfaceCreateInfoKHR createInfo;
2816 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2817 createInfo.pNext = NULL;
2818 createInfo.flags = 0;
2819 createInfo.connection = demo->connection;
2820 createInfo.window = demo->xcb_window;
2821
2822 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002823#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002824 }
Tony Barbour2593b182016-04-19 10:57:58 -06002825 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002826
Tony Barbourcc69f452015-10-08 13:59:42 -06002827 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002828 VkBool32 *supportsPresent =
2829 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002830 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002831 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002832 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002833 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002834
2835 // Search for a graphics and a present queue in the array of queue
2836 // families, try to find one that supports both
2837 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002838 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002839 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002840 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2841 if (graphicsQueueNodeIndex == UINT32_MAX) {
2842 graphicsQueueNodeIndex = i;
2843 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002844
Ian Elliottaaae5352015-07-06 14:27:58 -06002845 if (supportsPresent[i] == VK_TRUE) {
2846 graphicsQueueNodeIndex = i;
2847 presentQueueNodeIndex = i;
2848 break;
2849 }
2850 }
2851 }
2852 if (presentQueueNodeIndex == UINT32_MAX) {
2853 // If didn't find a queue that supports both graphics and present, then
2854 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002855 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002856 if (supportsPresent[i] == VK_TRUE) {
2857 presentQueueNodeIndex = i;
2858 break;
2859 }
2860 }
2861 }
2862 free(supportsPresent);
2863
2864 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002865 if (graphicsQueueNodeIndex == UINT32_MAX ||
2866 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002867 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002868 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002869 }
2870
2871 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002872 // synchronization, and appropriate tracking for QueueSubmit.
2873 // NOTE: While it is possible for an application to use a separate graphics
2874 // and a present queues, this demo program assumes it is only using
2875 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002876 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2877 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002878 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002879 }
2880
2881 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002882
Tony Barbourda2bad52016-01-22 14:36:40 -07002883 demo_create_device(demo);
2884
2885 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2886 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2887 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2888 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2889 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2890
Karl Schultze4dc75c2016-02-02 15:37:51 -07002891 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2892 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002893
Ian Elliottaaae5352015-07-06 14:27:58 -06002894 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002895 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002896 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002897 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002898 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002899 VkSurfaceFormatKHR *surfFormats =
2900 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002901 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002902 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002903 assert(!err);
2904 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2905 // the surface has no preferred format. Otherwise, at least one
2906 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002907 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002908 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002909 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002910 assert(formatCount >= 1);
2911 demo->format = surfFormats[0].format;
2912 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002913 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002914
David Pinedo2bb7c932015-06-18 17:03:14 -06002915 demo->quit = false;
2916 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002917
2918 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002919 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002920}
2921
Karl Schultze4dc75c2016-02-02 15:37:51 -07002922static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002923#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002924 const xcb_setup_t *setup;
2925 xcb_screen_iterator_t iter;
2926 int scr;
2927
2928 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002929 if (demo->connection == NULL) {
2930 printf("Cannot find a compatible Vulkan installable client driver "
2931 "(ICD).\nExiting ...\n");
2932 fflush(stdout);
2933 exit(1);
2934 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002935
2936 setup = xcb_get_setup(demo->connection);
2937 iter = xcb_setup_roots_iterator(setup);
2938 while (scr-- > 0)
2939 xcb_screen_next(&iter);
2940
2941 demo->screen = iter.data;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002942#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002943}
2944
Karl Schultze4dc75c2016-02-02 15:37:51 -07002945static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002946 vec3 eye = {0.0f, 3.0f, 5.0f};
2947 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002948 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002949
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002950 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002951 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002952
Piers Daniell735ee532015-02-23 16:23:13 -07002953 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002954 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002955 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002956 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002957 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002958 if (strcmp(argv[i], "--break") == 0) {
2959 demo->use_break = true;
2960 continue;
2961 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002962 if (strcmp(argv[i], "--validate") == 0) {
2963 demo->validate = true;
2964 continue;
2965 }
Tony Barbour2593b182016-04-19 10:57:58 -06002966 if (strcmp(argv[i], "--xlib") == 0) {
2967 demo->use_xlib = true;
2968 continue;
2969 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002970 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2971 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2972 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002973 i++;
2974 continue;
2975 }
lenny-lunargfbe22392016-06-06 11:07:53 -06002976 if (strcmp(argv[i], "--suppress_popups") == 0) {
2977 demo->suppress_popups = true;
2978 continue;
2979 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002980
Karl Schultze4dc75c2016-02-02 15:37:51 -07002981 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
lenny-lunargfbe22392016-06-06 11:07:53 -06002982 "[--c <framecount>] [--suppress_popups]\n",
Karl Schultze4dc75c2016-02-02 15:37:51 -07002983 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002984 fflush(stderr);
2985 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002986 }
2987
Tony Barbour2593b182016-04-19 10:57:58 -06002988 if (!demo->use_xlib)
2989 demo_init_connection(demo);
2990
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002991 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002992
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002993 demo->width = 500;
2994 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002995
2996 demo->spin_angle = 0.01f;
2997 demo->spin_increment = 0.01f;
2998 demo->pause = false;
2999
Karl Schultze4dc75c2016-02-02 15:37:51 -07003000 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3001 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003002 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3003 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003004}
3005
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003006#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003007// Include header required for parsing the command line options.
3008#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003009
Karl Schultze4dc75c2016-02-02 15:37:51 -07003010int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3011 int nCmdShow) {
3012 MSG msg; // message
3013 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003014 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003015 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003016
Karl Schultze4dc75c2016-02-02 15:37:51 -07003017 // Use the CommandLine functions to get the command line arguments.
3018 // Unfortunately, Microsoft outputs
3019 // this information as wide characters for Unicode, and we simply want the
3020 // Ascii version to be compatible
3021 // with the non-Windows side. So, we have to convert the information to
3022 // Ascii character strings.
3023 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3024 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003025 argc = 0;
3026 }
3027
Karl Schultze4dc75c2016-02-02 15:37:51 -07003028 if (argc > 0) {
3029 argv = (char **)malloc(sizeof(char *) * argc);
3030 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003031 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003032 } else {
3033 for (int iii = 0; iii < argc; iii++) {
3034 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003035 size_t numConverted = 0;
3036
Karl Schultze4dc75c2016-02-02 15:37:51 -07003037 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3038 if (argv[iii] != NULL) {
3039 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3040 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003041 }
3042 }
3043 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003044 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003045 argv = NULL;
3046 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003047
3048 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003049
3050 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003051 if (argc > 0 && argv != NULL) {
3052 for (int iii = 0; iii < argc; iii++) {
3053 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003054 free(argv[iii]);
3055 }
3056 }
3057 free(argv);
3058 }
3059
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003060 demo.connection = hInstance;
3061 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003062 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003063 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003064
3065 demo_prepare(&demo);
3066
Karl Schultze4dc75c2016-02-02 15:37:51 -07003067 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003068
3069 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003070 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003071 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003072 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003073 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003074 done = true; // if found, quit app
3075 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003076 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003077 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003078 DispatchMessage(&msg);
3079 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003080 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003081 }
3082
3083 demo_cleanup(&demo);
3084
Karl Schultze4dc75c2016-02-02 15:37:51 -07003085 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003086}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003087#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3088#include <android/log.h>
3089#include <android_native_app_glue.h>
3090static bool initialized = false;
3091static bool active = false;
3092struct demo demo;
3093
3094static int32_t processInput(struct android_app* app, AInputEvent* event) {
3095 return 0;
3096}
3097
3098static void processCommand(struct android_app* app, int32_t cmd) {
3099 switch(cmd) {
3100 case APP_CMD_INIT_WINDOW: {
3101 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003102 // We're getting a new window. If the app is starting up, we
3103 // need to initialize. If the app has already been
3104 // initialized, that means that we lost our previous window,
3105 // which means that we have a lot of work to do. At a minimum,
3106 // we need to destroy the swapchain and surface associated with
3107 // the old window, and create a new surface and swapchain.
3108 // However, since there are a lot of other objects/state that
3109 // is tied to the swapchain, it's easiest to simply cleanup and
3110 // start over (i.e. use a brute-force approach of re-starting
3111 // the app)
3112 if (demo.prepared) {
3113 demo_cleanup(&demo);
3114 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003115 demo_init(&demo, 0, NULL);
3116 demo.window = (void*)app->window;
3117 demo_init_vk_swapchain(&demo);
3118 demo_prepare(&demo);
3119 initialized = true;
3120 }
3121 break;
3122 }
3123 case APP_CMD_GAINED_FOCUS: {
3124 active = true;
3125 break;
3126 }
3127 case APP_CMD_LOST_FOCUS: {
3128 active = false;
3129 break;
3130 }
3131 }
3132}
3133
3134void android_main(struct android_app *app)
3135{
3136 app_dummy();
3137
Cody Northrop8707a6e2016-04-26 19:59:19 -06003138#ifdef ANDROID
3139 int vulkanSupport = InitVulkan();
3140 if (vulkanSupport == 0)
3141 return;
3142#endif
3143
Ian Elliottf05d5d12016-05-17 12:03:35 -06003144 demo.prepared = false;
3145
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003146 app->onAppCmd = processCommand;
3147 app->onInputEvent = processInput;
3148
3149 while(1) {
3150 int events;
3151 struct android_poll_source* source;
3152 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3153 if (source) {
3154 source->process(app, source);
3155 }
3156
3157 if (app->destroyRequested != 0) {
3158 demo_cleanup(&demo);
3159 return;
3160 }
3161 }
3162 if (initialized && active) {
3163 demo_run(&demo);
3164 }
3165 }
3166
3167}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003168#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003169int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003170 struct demo demo;
3171
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003172 demo_init(&demo, argc, argv);
Tony Barbour2593b182016-04-19 10:57:58 -06003173 if (demo.use_xlib)
3174 demo_create_xlib_window(&demo);
3175 else
3176 demo_create_xcb_window(&demo);
3177
Tony Barbourcc69f452015-10-08 13:59:42 -06003178 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003179
3180 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003181
3182 if (demo.use_xlib)
3183 demo_run_xlib(&demo);
3184 else
3185 demo_run_xcb(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003186
3187 demo_cleanup(&demo);
3188
Karl Schultz0218c002016-03-22 17:06:13 -06003189 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003190}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003191#endif