blob: e8da31edc4a17fd5fef1b388d9992af3ce7fef45 [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 { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060067
Michael Lentinec6cde4b2016-04-18 13:20:45 -050068#elif defined __ANDROID__
69#include <android/log.h>
70#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
73 exit(1); \
74 } while (0)
75#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070076#define ERR_EXIT(err_msg, err_class) \
77 do { \
78 printf(err_msg); \
79 fflush(stdout); \
80 exit(1); \
81 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050082#endif
Ian Elliott07264132015-04-28 11:35:02 -060083
Karl Schultze4dc75c2016-02-02 15:37:51 -070084#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
85 { \
86 demo->fp##entrypoint = \
87 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
88 if (demo->fp##entrypoint == NULL) { \
89 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
90 "vkGetInstanceProcAddr Failure"); \
91 } \
92 }
Ian Elliottaaae5352015-07-06 14:27:58 -060093
Jon Ashburn7d8342c2015-10-08 15:58:23 -060094static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
95
Karl Schultze4dc75c2016-02-02 15:37:51 -070096#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
97 { \
98 if (!g_gdpa) \
99 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
100 demo->inst, "vkGetDeviceProcAddr"); \
101 demo->fp##entrypoint = \
102 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
103 if (demo->fp##entrypoint == NULL) { \
104 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
105 "vkGetDeviceProcAddr Failure"); \
106 } \
107 }
Ian Elliott673898b2015-06-22 15:07:49 -0600108
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600109/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700110 * structure to track all objects related to a texture.
111 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600112struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600113 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700114
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600115 VkImage image;
116 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600117
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800118 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500119 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600120 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700121 int32_t tex_width, tex_height;
122};
123
Karl Schultze4dc75c2016-02-02 15:37:51 -0700124static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600125
Karl Schultz0218c002016-03-22 17:06:13 -0600126static int validation_error = 0;
127
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600128struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700130 float mvp[4][4];
131 float position[12 * 3][4];
132 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600133};
134
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600135struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600136 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700137 float mvp[4][4];
138 float position[12 * 3][4];
139 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600140};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600141
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600142//--------------------------------------------------------------------------------------
143// Mesh and VertexFormat Data
144//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700145// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146struct Vertex
147{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600148 float posX, posY, posZ, posW; // Position data
149 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150};
151
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600152struct VertexPosTex
153{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600154 float posX, posY, posZ, posW; // Position data
155 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600156};
157
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600158#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600159#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600160
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600161static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 -1.0f,-1.0f, 1.0f,
164 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 -1.0f, 1.0f,-1.0f,
167 -1.0f,-1.0f,-1.0f,
168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f, 1.0f,-1.0f,
171 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600178 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600181 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 -1.0f, 1.0f, 1.0f,
185 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600188 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 1.0f, 1.0f, 1.0f,
192 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600194 1.0f,-1.0f,-1.0f,
195 1.0f, 1.0f,-1.0f,
196
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600199 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800200 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600201 1.0f,-1.0f, 1.0f,
202 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600203};
204
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600205static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 0.0f, 0.0f, // -X side
207 1.0f, 0.0f,
208 1.0f, 1.0f,
209 1.0f, 1.0f,
210 0.0f, 1.0f,
211 0.0f, 0.0f,
212
213 1.0f, 0.0f, // -Z side
214 0.0f, 1.0f,
215 0.0f, 0.0f,
216 1.0f, 0.0f,
217 1.0f, 1.0f,
218 0.0f, 1.0f,
219
220 1.0f, 1.0f, // -Y side
221 1.0f, 0.0f,
222 0.0f, 0.0f,
223 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 0.0f, 0.0f,
225 0.0f, 1.0f,
226
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600228 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800229 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 1.0f, // +X side
235 0.0f, 1.0f,
236 0.0f, 0.0f,
237 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600238 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600239 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600240
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800241 0.0f, 1.0f, // +Z side
242 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600243 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600244 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800245 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600246 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249
Karl Schultze4dc75c2016-02-02 15:37:51 -0700250void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600251 int i;
252
253 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700254 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600255 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
256 }
257 printf("\n");
258 fflush(stdout);
259}
260
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600262 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700263 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600264 printf("\n");
265 fflush(stdout);
266}
267
Karl Schultze4dc75c2016-02-02 15:37:51 -0700268VKAPI_ATTR VkBool32 VKAPI_CALL
269dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
270 uint64_t srcObject, size_t location, int32_t msgCode,
271 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
272 char *message = (char *)malloc(strlen(pMsg) + 100);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600273
Karl Schultze4dc75c2016-02-02 15:37:51 -0700274 assert(message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600275
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700276 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700277 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
278 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600279 validation_error = 1;
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -0700280 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700281 // We know that we're submitting queues without fences, ignore this
282 // warning
283 if (strstr(pMsg,
284 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600285 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600286 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700287 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
288 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600289 validation_error = 1;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600290 } else {
Karl Schultz0218c002016-03-22 17:06:13 -0600291 validation_error = 1;
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600292 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600293 }
294
295#ifdef _WIN32
296 MessageBox(NULL, message, "Alert", MB_OK);
297#else
Karl Schultze4dc75c2016-02-02 15:37:51 -0700298 printf("%s\n", message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600299 fflush(stdout);
300#endif
301 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600302
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600303 /*
304 * false indicates that layer should not bail-out of an
305 * API call that had validation failures. This may mean that the
306 * app dies inside the driver due to invalid parameter(s).
307 * That's what would happen without validation layers, so we'll
308 * keep that behavior here.
309 */
310 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600311}
312
Karl Schultz0c3c1512016-03-25 15:35:18 -0600313VKAPI_ATTR VkBool32 VKAPI_CALL
314BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
315 uint64_t srcObject, size_t location, int32_t msgCode,
316 const char *pLayerPrefix, const char *pMsg,
317 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700318#ifndef WIN32
319 raise(SIGTRAP);
320#else
321 DebugBreak();
322#endif
323
324 return false;
325}
326
Ian Elliotta0781972015-08-21 15:09:33 -0600327typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600328 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800329 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600330 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600331} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600332
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500334#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600335#define APP_NAME_STR_LEN 80
336 HINSTANCE connection; // hInstance - Windows Instance
337 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700338 HWND window; // hWnd - window handle
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500339#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600340 Display* display;
341 Window xlib_window;
342 Atom xlib_wm_delete_window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500343#elif defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344 xcb_connection_t *connection;
345 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600346 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800347 xcb_intern_atom_reply_t *atom_wm_delete_window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500348#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
349 ANativeWindow* window;
350#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700351 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600352 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700353 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600354 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600357 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkDevice device;
359 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700360 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600361 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600362 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600363 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
Tony Barbourda2bad52016-01-22 14:36:40 -0700365 uint32_t enabled_extension_count;
366 uint32_t enabled_layer_count;
367 char *extension_names[64];
368 char *device_validation_layers[64];
369
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600371 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600372 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Karl Schultze4dc75c2016-02-02 15:37:51 -0700374 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
375 fpGetPhysicalDeviceSurfaceSupportKHR;
376 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
377 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
378 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
379 fpGetPhysicalDeviceSurfaceFormatsKHR;
380 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
381 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600382 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
383 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
384 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
385 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
386 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600387 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600388 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600389 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800391 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600392
393 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600396 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800397 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500398 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600399 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400 } depth;
401
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403
404 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800406 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500407 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600408 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600409 } uniform_data;
410
Karl Schultze4dc75c2016-02-02 15:37:51 -0700411 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500412 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600413 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600414 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800415 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600416 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600417
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600418 mat4x4 projection_matrix;
419 mat4x4 view_matrix;
420 mat4x4 model_matrix;
421
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600422 float spin_angle;
423 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600424 bool pause;
425
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600426 VkShaderModule vert_shader_module;
427 VkShaderModule frag_shader_module;
428
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429 VkDescriptorPool desc_pool;
430 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800431
Tony Barbourd8e504f2015-10-08 14:26:24 -0600432 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800433
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600434 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600435 int32_t curFrame;
436 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600437 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600438 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700439 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
440 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
441 VkDebugReportCallbackEXT msg_callback;
442 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600443
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600444 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600445 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600446};
447
Ian Elliottcf074d32015-10-16 18:02:43 -0600448// Forward declaration:
449static void demo_resize(struct demo *demo);
450
Karl Schultze4dc75c2016-02-02 15:37:51 -0700451static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
452 VkFlags requirements_mask,
453 uint32_t *typeIndex) {
454 // Search memtypes to find first index with those properties
455 for (uint32_t i = 0; i < 32; i++) {
456 if ((typeBits & 1) == 1) {
457 // Type is available, does it match user properties?
458 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
459 requirements_mask) == requirements_mask) {
460 *typeIndex = i;
461 return true;
462 }
463 }
464 typeBits >>= 1;
465 }
466 // No memory types matched, return failure
467 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600468}
469
Karl Schultze4dc75c2016-02-02 15:37:51 -0700470static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600471 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600473 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474 return;
475
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 assert(!err);
478
Karl Schultze4dc75c2016-02-02 15:37:51 -0700479 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800480 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700481 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
482 .pNext = NULL,
483 .waitSemaphoreCount = 0,
484 .pWaitSemaphores = NULL,
485 .pWaitDstStageMask = NULL,
486 .commandBufferCount = 1,
487 .pCommandBuffers = cmd_bufs,
488 .signalSemaphoreCount = 0,
489 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600490
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600491 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 assert(!err);
493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 assert(!err);
496
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600497 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600498 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499}
500
Karl Schultze4dc75c2016-02-02 15:37:51 -0700501static void demo_set_image_layout(struct demo *demo, VkImage image,
502 VkImageAspectFlags aspectMask,
503 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700504 VkImageLayout new_image_layout,
505 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600506 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600507
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600508 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800509 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800510 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800512 .commandPool = demo->cmd_pool,
513 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700514 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515 };
516
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800517 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518 assert(!err);
519
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700520 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
521 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600522 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800523 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600524 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800525 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800526 .occlusionQueryEnable = VK_FALSE,
527 .queryFlags = 0,
528 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700530 VkCommandBufferBeginInfo cmd_buf_info = {
531 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
532 .pNext = NULL,
533 .flags = 0,
534 .pInheritanceInfo = &cmd_buf_hinfo,
535 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600536 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600537 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600538 }
539
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600540 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600542 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700543 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800544 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600545 .oldLayout = old_image_layout,
546 .newLayout = new_image_layout,
547 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700548 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800550 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800552 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553 }
554
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700555 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700556 image_memory_barrier.dstAccessMask =
557 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700558 }
559
560 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700561 image_memory_barrier.dstAccessMask =
562 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700563 }
564
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600565 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600566 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700567 image_memory_barrier.dstAccessMask =
568 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600569 }
570
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600571 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600572
Tony Barbour50bbca42015-06-29 16:20:35 -0600573 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
574 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600575
Karl Schultze4dc75c2016-02-02 15:37:51 -0700576 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
577 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600578}
579
Karl Schultze4dc75c2016-02-02 15:37:51 -0700580static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700581 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
582 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600583 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800584 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600585 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800586 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800587 .occlusionQueryEnable = VK_FALSE,
588 .queryFlags = 0,
589 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700590 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700591 const VkCommandBufferBeginInfo cmd_buf_info = {
592 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
593 .pNext = NULL,
594 .flags = 0,
595 .pInheritanceInfo = &cmd_buf_hinfo,
596 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800597 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700598 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
599 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800600 };
601 const VkRenderPassBeginInfo rp_begin = {
602 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
603 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800604 .renderPass = demo->render_pass,
605 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800606 .renderArea.offset.x = 0,
607 .renderArea.offset.y = 0,
608 .renderArea.extent.width = demo->width,
609 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600610 .clearValueCount = 2,
611 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700612 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800613 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600615 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 assert(!err);
617
Tony Barbour5c5b1632016-04-26 11:13:48 -0600618 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
619 // happens to the previous contents of the image
620 VkImageMemoryBarrier image_memory_barrier = {
621 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
622 .pNext = NULL,
623 .srcAccessMask = 0,
624 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
625 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
626 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
627 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
628 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
629 .image = demo->buffers[demo->current_buffer].image,
630 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
631
632 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
633 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
634 NULL, 1, &image_memory_barrier);
635
Chia-I Wuf051d262015-10-27 19:25:11 +0800636 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800637
Karl Schultze4dc75c2016-02-02 15:37:51 -0700638 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
639 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
640 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
641 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600642 VkViewport viewport;
643 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700644 viewport.height = (float)demo->height;
645 viewport.width = (float)demo->width;
646 viewport.minDepth = (float)0.0f;
647 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700648 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600649
650 VkRect2D scissor;
651 memset(&scissor, 0, sizeof(scissor));
652 scissor.extent.width = demo->width;
653 scissor.extent.height = demo->height;
654 scissor.offset.x = 0;
655 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700656 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600657 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800658 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659
Tony Barbour15a4ef62015-10-21 10:14:48 -0600660 VkImageMemoryBarrier prePresentBarrier = {
661 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
662 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800663 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700664 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600665 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700666 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600667 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800668 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700669 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600670
671 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
672 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700673 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
674 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
675 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600676
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600677 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600678 assert(!err);
679}
680
Karl Schultze4dc75c2016-02-02 15:37:51 -0700681void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600682 mat4x4 MVP, Model, VP;
683 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600684 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600685 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600687 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600689 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600690 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
692 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600693 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600694
Karl Schultze4dc75c2016-02-02 15:37:51 -0700695 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
696 demo->uniform_data.mem_alloc.allocationSize, 0,
697 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600698 assert(!err);
699
Karl Schultze4dc75c2016-02-02 15:37:51 -0700700 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600702 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600703}
704
Karl Schultze4dc75c2016-02-02 15:37:51 -0700705static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600706 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600707 VkSemaphore presentCompleteSemaphore;
708 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
709 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
710 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600711 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800713 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600714
Karl Schultze4dc75c2016-02-02 15:37:51 -0700715 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
716 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 assert(!err);
718
719 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700720 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700722 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600723 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600724 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
725 // demo->swapchain is out of date (e.g. the window was resized) and
726 // must be recreated:
727 demo_resize(demo);
728 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800729 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600730 return;
731 } else if (err == VK_SUBOPTIMAL_KHR) {
732 // demo->swapchain is not as optimal as it could be, but the platform's
733 // presentation engine will still present the image correctly.
734 } else {
735 assert(!err);
736 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600737
Tony Barbour15a4ef62015-10-21 10:14:48 -0600738 demo_flush_init_cmd(demo);
739
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600740 // Wait for the present complete semaphore to be signaled to ensure
741 // that the image won't be rendered to until the presentation
742 // engine has fully released ownership to the application, and it is
743 // okay to render to the image.
744
Karl Schultze4dc75c2016-02-02 15:37:51 -0700745 VkPipelineStageFlags pipe_stage_flags =
746 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
747 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
748 .pNext = NULL,
749 .waitSemaphoreCount = 1,
750 .pWaitSemaphores = &presentCompleteSemaphore,
751 .pWaitDstStageMask = &pipe_stage_flags,
752 .commandBufferCount = 1,
753 .pCommandBuffers =
754 &demo->buffers[demo->current_buffer].cmd,
755 .signalSemaphoreCount = 0,
756 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600757
758 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600759 assert(!err);
760
Ian Elliotta0781972015-08-21 15:09:33 -0600761 VkPresentInfoKHR present = {
762 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600763 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600764 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700765 .pSwapchains = &demo->swapchain,
766 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600767 };
768
Karl Schultze4dc75c2016-02-02 15:37:51 -0700769 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600770 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600771 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
772 // demo->swapchain is out of date (e.g. the window was resized) and
773 // must be recreated:
774 demo_resize(demo);
775 } else if (err == VK_SUBOPTIMAL_KHR) {
776 // demo->swapchain is not as optimal as it could be, but the platform's
777 // presentation engine will still present the image correctly.
778 } else {
779 assert(!err);
780 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781
Chia-I Wucbb564e2015-04-16 22:02:10 +0800782 err = vkQueueWaitIdle(demo->queue);
783 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600784
Chia-I Wu63636062015-10-26 21:10:41 +0800785 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600786}
787
Karl Schultze4dc75c2016-02-02 15:37:51 -0700788static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600790 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600791
Ian Elliottb08e0d92015-11-20 11:55:46 -0700792 // Check the surface capabilities and formats
793 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700794 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
795 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600796 assert(!err);
797
Ian Elliott8528eff2015-08-07 15:56:59 -0600798 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700799 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
800 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600801 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600802 VkPresentModeKHR *presentModes =
803 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600804 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700805 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
806 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 assert(!err);
808
Ian Elliotta0781972015-08-21 15:09:33 -0600809 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600810 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700811 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600812 // If the surface size is undefined, the size is set to
813 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600814 swapchainExtent.width = demo->width;
815 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700816 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600817 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700818 swapchainExtent = surfCapabilities.currentExtent;
819 demo->width = surfCapabilities.currentExtent.width;
820 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600821 }
822
823 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600824 // tearing mode. If not, try IMMEDIATE which will usually be available,
825 // and is fastest (though it tears). If not, fall back to FIFO which is
826 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600827 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600828 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600829 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
830 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600831 break;
832 }
Ian Elliotta0781972015-08-21 15:09:33 -0600833 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
834 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
835 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600836 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600837 }
838
839 // Determine the number of VkImage's to use in the swap chain (we desire to
840 // own only 1 image at a time, besides the images being displayed and
841 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700842 uint32_t desiredNumberOfSwapchainImages =
843 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700844 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700845 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600846 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700847 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600848 }
849
Tony Barbour9fd6d352015-09-16 15:01:32 -0600850 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700851 if (surfCapabilities.supportedTransforms &
852 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700853 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600854 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700855 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600856 }
857
Ian Elliottcf074d32015-10-16 18:02:43 -0600858 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600859 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800860 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700861 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600862 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800863 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600864 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700865 .imageExtent =
866 {
867 .width = swapchainExtent.width, .height = swapchainExtent.height,
868 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700869 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600870 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700871 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700872 .imageArrayLayers = 1,
873 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
874 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600875 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600876 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600877 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600878 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600879 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600880 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881
Karl Schultze4dc75c2016-02-02 15:37:51 -0700882 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
883 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800884 assert(!err);
885
Ian Elliottcf074d32015-10-16 18:02:43 -0600886 // If we just re-created an existing swapchain, we should destroy the old
887 // swapchain at this point.
888 // Note: destroying the swapchain also cleans up all its associated
889 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800890 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700891 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600892 }
893
894 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600895 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600896 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800897
Karl Schultze4dc75c2016-02-02 15:37:51 -0700898 VkImage *swapchainImages =
899 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600900 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600901 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600902 &demo->swapchainImageCount,
903 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600904 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600905
Karl Schultze4dc75c2016-02-02 15:37:51 -0700906 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
907 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600908 assert(demo->buffers);
909
Ian Elliotta0781972015-08-21 15:09:33 -0600910 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600911 VkImageViewCreateInfo color_image_view = {
912 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600913 .pNext = NULL,
914 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700915 .components =
916 {
917 .r = VK_COMPONENT_SWIZZLE_R,
918 .g = VK_COMPONENT_SWIZZLE_G,
919 .b = VK_COMPONENT_SWIZZLE_B,
920 .a = VK_COMPONENT_SWIZZLE_A,
921 },
922 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
923 .baseMipLevel = 0,
924 .levelCount = 1,
925 .baseArrayLayer = 0,
926 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600927 .viewType = VK_IMAGE_VIEW_TYPE_2D,
928 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600929 };
930
Ian Elliotta0781972015-08-21 15:09:33 -0600931 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600932
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600933 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600934
Karl Schultze4dc75c2016-02-02 15:37:51 -0700935 err = vkCreateImageView(demo->device, &color_image_view, NULL,
936 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600937 assert(!err);
938 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700939
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500940
Mark Young04fd3d82016-01-25 14:43:50 -0700941 if (NULL != presentModes) {
942 free(presentModes);
943 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944}
945
Karl Schultze4dc75c2016-02-02 15:37:51 -0700946static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600947 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600948 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600949 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600951 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600952 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700953 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600955 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800956 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600957 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600958 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959 .flags = 0,
960 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200961
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600962 VkImageViewCreateInfo view = {
963 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600964 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800965 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600966 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700967 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
968 .baseMipLevel = 0,
969 .levelCount = 1,
970 .baseArrayLayer = 0,
971 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600973 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600974 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600975
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500976 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600977 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600978 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600979
980 demo->depth.format = depth_format;
981
982 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700983 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984 assert(!err);
985
Karl Schultze4dc75c2016-02-02 15:37:51 -0700986 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600987 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600988
Chia-I Wuf48700b2015-11-10 16:21:09 +0800989 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200990 demo->depth.mem_alloc.pNext = NULL;
991 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
992 demo->depth.mem_alloc.memoryTypeIndex = 0;
993
Karl Schultze4dc75c2016-02-02 15:37:51 -0700994 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
995 0, /* No requirements */
996 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600997 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500999 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001000 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1001 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001002 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001003
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001004 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001005 err =
1006 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001007 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001008
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1010 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001011 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1012 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001013
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014 /* create image view */
1015 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001016 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001017 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018}
1019
Tony Barbour1a86d032015-09-21 15:17:33 -06001020/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001022 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001023#ifdef __ANDROID__
1024#include <lunarg.ppm.h>
1025 char *cPtr;
1026 cPtr = (char*)___lunarg_ppm;
1027 if ((unsigned char*)cPtr >= (___lunarg_ppm + ___lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
1028 return false;
1029 }
1030 while(strncmp(cPtr++, "\n", 1));
1031 sscanf(cPtr, "%u %u", height, width);
1032 if (rgba_data == NULL) {
1033 return true;
1034 }
1035 while(strncmp(cPtr++, "\n", 1));
1036 if ((unsigned char*)cPtr >= (___lunarg_ppm + ___lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
1037 return false;
1038 }
1039 while(strncmp(cPtr++, "\n", 1));
1040
1041 for (int y = 0; y < *height; y++) {
1042 uint8_t *rowPtr = rgba_data;
1043 for (int x = 0; x < *width; x++) {
1044 memcpy(rowPtr, cPtr, 3);
1045 rowPtr[3] = 255; /* Alpha of 1 */
1046 rowPtr += 4;
1047 cPtr += 3;
1048 }
1049 rgba_data += layout->rowPitch;
1050 }
1051
1052 return true;
1053#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001054 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001055 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001056
Tony Barbour1a86d032015-09-21 15:17:33 -06001057 if (!fPtr)
1058 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001059
Tony Barbour1a86d032015-09-21 15:17:33 -06001060 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001061 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1062 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001063 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001064 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001065
Tony Barbour1a86d032015-09-21 15:17:33 -06001066 do {
1067 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001068 if (cPtr == NULL) {
1069 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001070 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001071 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001072 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001073
Tony Barbour1a86d032015-09-21 15:17:33 -06001074 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001075 if (rgba_data == NULL) {
1076 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001077 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001078 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001079 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001080 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001081 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1082 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001083 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001084 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001085
Karl Schultze4dc75c2016-02-02 15:37:51 -07001086 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001087 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001088 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001089 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001090 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001091 rowPtr[3] = 255; /* Alpha of 1 */
1092 rowPtr += 4;
1093 }
1094 rgba_data += layout->rowPitch;
1095 }
1096 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001097 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001098#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001099}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001100
Karl Schultze4dc75c2016-02-02 15:37:51 -07001101static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001102 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001103 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001104 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001105 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001106 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001107 int32_t tex_width;
1108 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001109 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001110 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111
Karl Schultze4dc75c2016-02-02 15:37:51 -07001112 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001113 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001114 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001116 tex_obj->tex_width = tex_width;
1117 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001119 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001120 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001122 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001124 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001126 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001127 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001128 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001129 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001130 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001131 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001134 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001135
Karl Schultze4dc75c2016-02-02 15:37:51 -07001136 err =
1137 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138 assert(!err);
1139
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001140 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001141
Chia-I Wuf48700b2015-11-10 16:21:09 +08001142 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001143 tex_obj->mem_alloc.pNext = NULL;
1144 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1145 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001146
Karl Schultze4dc75c2016-02-02 15:37:51 -07001147 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1148 required_props,
1149 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001150 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001151
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001152 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001153 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001154 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001155 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001156
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001157 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001158 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001159 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001161 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001162 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001163 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001165 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001167 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168 void *data;
1169
Karl Schultze4dc75c2016-02-02 15:37:51 -07001170 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1171 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172
Karl Schultze4dc75c2016-02-02 15:37:51 -07001173 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1174 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001175 assert(!err);
1176
1177 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1178 fprintf(stderr, "Error loading texture: %s\n", filename);
1179 }
1180
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001181 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001183
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001184 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001185 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001186 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1187 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001188 /* setting the image layout does not reference the actual memory so no need
1189 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190}
1191
Karl Schultze4dc75c2016-02-02 15:37:51 -07001192static void demo_destroy_texture_image(struct demo *demo,
1193 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001194 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001195 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1196 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001197}
1198
Karl Schultze4dc75c2016-02-02 15:37:51 -07001199static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001200 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001201 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001202 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001204 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001205
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001207 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208
Karl Schultze4dc75c2016-02-02 15:37:51 -07001209 if ((props.linearTilingFeatures &
1210 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1211 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001212 /* Device can texture using linear textures */
1213 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Karl Schultze4dc75c2016-02-02 15:37:51 -07001214 VK_IMAGE_TILING_LINEAR,
1215 VK_IMAGE_USAGE_SAMPLED_BIT,
1216 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
1217 } else if (props.optimalTilingFeatures &
1218 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001220 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001221
1222 memset(&staging_texture, 0, sizeof(staging_texture));
1223 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001224 VK_IMAGE_TILING_LINEAR,
1225 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1226 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001227
Karl Schultze4dc75c2016-02-02 15:37:51 -07001228 demo_prepare_texture_image(
1229 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1230 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1231 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001232
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001233 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001234 VK_IMAGE_ASPECT_COLOR_BIT,
1235 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001236 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1237 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001238
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001239 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001240 VK_IMAGE_ASPECT_COLOR_BIT,
1241 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001242 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1243 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001244
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001245 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001246 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1247 .srcOffset = {0, 0, 0},
1248 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1249 .dstOffset = {0, 0, 0},
1250 .extent = {staging_texture.tex_width,
1251 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001252 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001253 vkCmdCopyImage(
1254 demo->cmd, staging_texture.image,
1255 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1256 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001257
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001258 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001259 VK_IMAGE_ASPECT_COLOR_BIT,
1260 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001261 demo->textures[i].imageLayout,
1262 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001263
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001264 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001265
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001266 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001267 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001268 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1269 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001270 }
1271
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001272 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001273 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001275 .magFilter = VK_FILTER_NEAREST,
1276 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001277 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001278 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1279 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1280 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001281 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001282 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001283 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001284 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001285 .minLod = 0.0f,
1286 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001287 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001288 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001289 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001290
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001291 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001292 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001294 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001295 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001296 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001297 .components =
1298 {
1299 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1300 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1301 },
1302 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001303 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001304 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001305
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001307 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001308 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001309 assert(!err);
1310
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311 /* create image view */
1312 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001313 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001314 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315 assert(!err);
1316 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001318
Karl Schultze4dc75c2016-02-02 15:37:51 -07001319void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001320 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001321 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001322 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001323 int i;
1324 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001325 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001326 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001327 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001328
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001329 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001330 mat4x4_mul(MVP, VP, demo->model_matrix);
1331 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001332 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001333
Karl Schultze4dc75c2016-02-02 15:37:51 -07001334 for (i = 0; i < 12 * 3; i++) {
1335 data.position[i][0] = g_vertex_buffer_data[i * 3];
1336 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1337 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001338 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339 data.attr[i][0] = g_uv_buffer_data[2 * i];
1340 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001341 data.attr[i][2] = 0;
1342 data.attr[i][3] = 0;
1343 }
1344
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001345 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001346 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001347 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001348 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001349 err =
1350 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001351 assert(!err);
1352
Karl Schultze4dc75c2016-02-02 15:37:51 -07001353 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1354 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001355
Chia-I Wuf48700b2015-11-10 16:21:09 +08001356 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001357 demo->uniform_data.mem_alloc.pNext = NULL;
1358 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1359 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1360
Karl Schultze4dc75c2016-02-02 15:37:51 -07001361 pass = memory_type_from_properties(
1362 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1363 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001364 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001365
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1367 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001368 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001369
Karl Schultze4dc75c2016-02-02 15:37:51 -07001370 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1371 demo->uniform_data.mem_alloc.allocationSize, 0,
1372 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001373 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001374
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001375 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001376
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001377 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001378
Karl Schultze4dc75c2016-02-02 15:37:51 -07001379 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1380 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001381 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001382
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001383 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1384 demo->uniform_data.buffer_info.offset = 0;
1385 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001386}
1387
Karl Schultze4dc75c2016-02-02 15:37:51 -07001388static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001389 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001390 [0] =
1391 {
1392 .binding = 0,
1393 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1394 .descriptorCount = 1,
1395 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1396 .pImmutableSamplers = NULL,
1397 },
1398 [1] =
1399 {
1400 .binding = 1,
1401 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1402 .descriptorCount = DEMO_TEXTURE_COUNT,
1403 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1404 .pImmutableSamplers = NULL,
1405 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001406 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001407 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001408 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001409 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001410 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001411 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001412 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001413 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414
Karl Schultze4dc75c2016-02-02 15:37:51 -07001415 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1416 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001417 assert(!err);
1418
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001419 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001420 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1421 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001422 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001423 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001424 };
1425
Karl Schultze4dc75c2016-02-02 15:37:51 -07001426 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001427 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429}
1430
Karl Schultze4dc75c2016-02-02 15:37:51 -07001431static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001432 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001433 [0] =
1434 {
1435 .format = demo->format,
1436 .samples = VK_SAMPLE_COUNT_1_BIT,
1437 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1438 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1439 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1440 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1441 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1442 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1443 },
1444 [1] =
1445 {
1446 .format = demo->depth.format,
1447 .samples = VK_SAMPLE_COUNT_1_BIT,
1448 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1449 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1450 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1451 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1452 .initialLayout =
1453 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1454 .finalLayout =
1455 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1456 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001457 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001458 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001459 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001460 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001461 const VkAttachmentReference depth_reference = {
1462 .attachment = 1,
1463 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1464 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001465 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001466 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1467 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001468 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001469 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001470 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001471 .pColorAttachments = &color_reference,
1472 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001473 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001474 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001475 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001476 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001477 const VkRenderPassCreateInfo rp_info = {
1478 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1479 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001480 .attachmentCount = 2,
1481 .pAttachments = attachments,
1482 .subpassCount = 1,
1483 .pSubpasses = &subpass,
1484 .dependencyCount = 0,
1485 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001486 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001487 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001488
Chia-I Wu63636062015-10-26 21:10:41 +08001489 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001490 assert(!err);
1491}
1492
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001493//TODO: Merge shader reading
1494#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001495static VkShaderModule
1496demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001497 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001498 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001499 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001500
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001501 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1502 moduleCreateInfo.pNext = NULL;
1503
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001504 moduleCreateInfo.codeSize = size;
1505 moduleCreateInfo.pCode = code;
1506 moduleCreateInfo.flags = 0;
1507 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1508 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001509
1510 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511}
1512
Karl Schultze4dc75c2016-02-02 15:37:51 -07001513char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001514 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001515 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001516 void *shader_code;
1517
1518 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001519 if (!fp)
1520 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001521
1522 fseek(fp, 0L, SEEK_END);
1523 size = ftell(fp);
1524
1525 fseek(fp, 0L, SEEK_SET);
1526
1527 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001528 retval = fread(shader_code, size, 1, fp);
1529 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001530
1531 *psize = size;
1532
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001533 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001534 return shader_code;
1535}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001536#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001537
Karl Schultze4dc75c2016-02-02 15:37:51 -07001538static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001539#ifdef __ANDROID__
1540 VkShaderModuleCreateInfo sh_info = {};
1541 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1542
1543#include "cube.vert.h"
1544 sh_info.codeSize = sizeof(cube_vert);
1545 sh_info.pCode = cube_vert;
1546 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1547 assert(!err);
1548#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001549 void *vertShaderCode;
1550 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001551
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001552 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1553
Karl Schultze4dc75c2016-02-02 15:37:51 -07001554 demo->vert_shader_module =
1555 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001556
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001557 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001558#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001559
1560 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561}
1562
Karl Schultze4dc75c2016-02-02 15:37:51 -07001563static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001564#ifdef __ANDROID__
1565 VkShaderModuleCreateInfo sh_info = {};
1566 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1567
1568#include "cube.frag.h"
1569 sh_info.codeSize = sizeof(cube_frag);
1570 sh_info.pCode = cube_frag;
1571 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1572 assert(!err);
1573#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001574 void *fragShaderCode;
1575 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001576
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001577 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001578
Karl Schultze4dc75c2016-02-02 15:37:51 -07001579 demo->frag_shader_module =
1580 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001581
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001582 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001583#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001584
1585 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001586}
1587
Karl Schultze4dc75c2016-02-02 15:37:51 -07001588static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001589 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001590 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001591 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001592 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001593 VkPipelineRasterizationStateCreateInfo rs;
1594 VkPipelineColorBlendStateCreateInfo cb;
1595 VkPipelineDepthStencilStateCreateInfo ds;
1596 VkPipelineViewportStateCreateInfo vp;
1597 VkPipelineMultisampleStateCreateInfo ms;
1598 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1599 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001600 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601
Piers Daniellba839d12015-09-29 13:01:09 -06001602 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1603 memset(&dynamicState, 0, sizeof dynamicState);
1604 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1605 dynamicState.pDynamicStates = dynamicStateEnables;
1606
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001607 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001608 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001609 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001610
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001611 memset(&vi, 0, sizeof(vi));
1612 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1613
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001615 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001616 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617
1618 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001619 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1620 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001621 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001622 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001623 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001624 rs.rasterizerDiscardEnable = VK_FALSE;
1625 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001626 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627
1628 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001629 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1630 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001631 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001632 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001633 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001634 cb.attachmentCount = 1;
1635 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636
Tony Barbour29645d02015-01-16 14:27:35 -07001637 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001638 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001639 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001640 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1641 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001642 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001643 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1644 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001645
1646 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001647 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001648 ds.depthTestEnable = VK_TRUE;
1649 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001650 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001651 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001652 ds.back.failOp = VK_STENCIL_OP_KEEP;
1653 ds.back.passOp = VK_STENCIL_OP_KEEP;
1654 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001655 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001656 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001657
Tony Barbour29645d02015-01-16 14:27:35 -07001658 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001659 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001660 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001661 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001662
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001663 // Two stages: vs and fs
1664 pipeline.stageCount = 2;
1665 VkPipelineShaderStageCreateInfo shaderStages[2];
1666 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1667
Karl Schultze4dc75c2016-02-02 15:37:51 -07001668 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1669 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001670 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001671 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001672
Karl Schultze4dc75c2016-02-02 15:37:51 -07001673 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1674 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001675 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001676 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001677
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001678 memset(&pipelineCache, 0, sizeof(pipelineCache));
1679 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001680
Karl Schultze4dc75c2016-02-02 15:37:51 -07001681 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1682 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001683 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001684
Karl Schultze4dc75c2016-02-02 15:37:51 -07001685 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001686 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001687 pipeline.pRasterizationState = &rs;
1688 pipeline.pColorBlendState = &cb;
1689 pipeline.pMultisampleState = &ms;
1690 pipeline.pViewportState = &vp;
1691 pipeline.pDepthStencilState = &ds;
1692 pipeline.pStages = shaderStages;
1693 pipeline.renderPass = demo->render_pass;
1694 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001695
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001696 pipeline.renderPass = demo->render_pass;
1697
Karl Schultze4dc75c2016-02-02 15:37:51 -07001698 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1699 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700 assert(!err);
1701
Chia-I Wu63636062015-10-26 21:10:41 +08001702 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1703 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001704}
1705
Karl Schultze4dc75c2016-02-02 15:37:51 -07001706static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001707 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001708 [0] =
1709 {
1710 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1711 .descriptorCount = 1,
1712 },
1713 [1] =
1714 {
1715 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1716 .descriptorCount = DEMO_TEXTURE_COUNT,
1717 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001718 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001719 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001720 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001721 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001722 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001723 .poolSizeCount = 2,
1724 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001725 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001726 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001727
Karl Schultze4dc75c2016-02-02 15:37:51 -07001728 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1729 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001730 assert(!err);
1731}
1732
Karl Schultze4dc75c2016-02-02 15:37:51 -07001733static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001734 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001735 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001736 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001737 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001738
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001739 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001740 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001741 .pNext = NULL,
1742 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001743 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001744 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001745 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001746 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001747
Chia-I Wuae721ba2015-05-25 16:27:55 +08001748 memset(&tex_descs, 0, sizeof(tex_descs));
1749 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001750 tex_descs[i].sampler = demo->textures[i].sampler;
1751 tex_descs[i].imageView = demo->textures[i].view;
1752 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001753 }
1754
1755 memset(&writes, 0, sizeof(writes));
1756
1757 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001758 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001759 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001760 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001761 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001762
1763 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001764 writes[1].dstSet = demo->desc_set;
1765 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001766 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001767 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001768 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001769
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001770 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001771}
1772
Karl Schultze4dc75c2016-02-02 15:37:51 -07001773static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001774 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001775 attachments[1] = demo->depth.view;
1776
Chia-I Wuf973e322015-07-08 13:34:24 +08001777 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001778 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1779 .pNext = NULL,
1780 .renderPass = demo->render_pass,
1781 .attachmentCount = 2,
1782 .pAttachments = attachments,
1783 .width = demo->width,
1784 .height = demo->height,
1785 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001786 };
1787 VkResult U_ASSERT_ONLY err;
1788 uint32_t i;
1789
Karl Schultze4dc75c2016-02-02 15:37:51 -07001790 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1791 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001792 assert(demo->framebuffers);
1793
1794 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001795 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001796 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1797 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001798 assert(!err);
1799 }
1800}
1801
Karl Schultze4dc75c2016-02-02 15:37:51 -07001802static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001803 VkResult U_ASSERT_ONLY err;
1804
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001805 const VkCommandPoolCreateInfo cmd_pool_info = {
1806 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001807 .pNext = NULL,
1808 .queueFamilyIndex = demo->graphics_queue_node_index,
1809 .flags = 0,
1810 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001811 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1812 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001813 assert(!err);
1814
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001815 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001816 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001818 .commandPool = demo->cmd_pool,
1819 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001820 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001821 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822
1823 demo_prepare_buffers(demo);
1824 demo_prepare_depth(demo);
1825 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001826 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001828 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001829 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001831
Ian Elliotta0781972015-08-21 15:09:33 -06001832 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001833 err =
1834 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001835 assert(!err);
1836 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001837
Chia-I Wu63ea9262015-03-26 13:14:16 +08001838 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001839 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001840
Chia-I Wuf973e322015-07-08 13:34:24 +08001841 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001842
Ian Elliotta0781972015-08-21 15:09:33 -06001843 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001844 demo->current_buffer = i;
1845 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1846 }
1847
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001848 /*
1849 * Prepare functions above may generate pipeline commands
1850 * that need to be flushed before beginning the render loop.
1851 */
1852 demo_flush_init_cmd(demo);
1853
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001854 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001855 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856}
1857
Karl Schultze4dc75c2016-02-02 15:37:51 -07001858static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001859 uint32_t i;
1860
1861 demo->prepared = false;
1862
Tony Barbourd8e504f2015-10-08 14:26:24 -06001863 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001864 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001865 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001866 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001867 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001868
Chia-I Wu63636062015-10-26 21:10:41 +08001869 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1870 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1871 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1872 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1873 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001874
1875 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001876 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1877 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1878 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1879 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001880 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001881 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001882
Chia-I Wu63636062015-10-26 21:10:41 +08001883 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1884 vkDestroyImage(demo->device, demo->depth.image, NULL);
1885 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001886
Chia-I Wu63636062015-10-26 21:10:41 +08001887 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1888 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001889
Ian Elliotta0781972015-08-21 15:09:33 -06001890 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001891 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001892 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1893 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001894 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001895 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001896
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001897 free(demo->queue_props);
1898
Chia-I Wu63636062015-10-26 21:10:41 +08001899 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1900 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001901 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001902 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001903 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001904 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001905 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001906
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001907#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001908 if (demo->use_xlib) {
1909 XDestroyWindow(demo->display, demo->xlib_window);
1910 XCloseDisplay(demo->display);
1911 } else {
1912 xcb_destroy_window(demo->connection, demo->xcb_window);
1913 xcb_disconnect(demo->connection);
1914 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001915 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001916#elif defined(VK_USE_PLATFORM_XCB_KHR)
1917 xcb_destroy_window(demo->connection, demo->window);
1918 xcb_disconnect(demo->connection);
1919 free(demo->atom_wm_delete_window);
1920#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001921}
1922
Karl Schultze4dc75c2016-02-02 15:37:51 -07001923static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001924 uint32_t i;
1925
Mike Stroyanbb60b382015-10-28 09:46:57 -06001926 // Don't react to resize until after first initialization.
1927 if (!demo->prepared) {
1928 return;
1929 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001930 // In order to properly resize the window, we must re-create the swapchain
1931 // AND redo the command buffers, etc.
1932 //
1933 // First, perform part of the demo_cleanup() function:
1934 demo->prepared = false;
1935
1936 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001937 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001938 }
1939 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001940 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001941
Chia-I Wu63636062015-10-26 21:10:41 +08001942 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1943 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1944 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1945 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1946 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001947
1948 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001949 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1950 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1951 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1952 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001953 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001954
Chia-I Wu63636062015-10-26 21:10:41 +08001955 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1956 vkDestroyImage(demo->device, demo->depth.image, NULL);
1957 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001958
Chia-I Wu63636062015-10-26 21:10:41 +08001959 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1960 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001961
1962 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001963 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001964 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1965 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001966 }
Chia-I Wu63636062015-10-26 21:10:41 +08001967 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001968 free(demo->buffers);
1969
Ian Elliottcf074d32015-10-16 18:02:43 -06001970 // Second, re-perform the demo_prepare() function, which will re-create the
1971 // swapchain:
1972 demo_prepare(demo);
1973}
1974
David Pinedo2bb7c932015-06-18 17:03:14 -06001975// On MS-Windows, make this a global, so it's available to WndProc()
1976struct demo demo;
1977
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001978#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07001979static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001980 if (!demo->prepared)
1981 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001982 // Wait for work to finish before updating MVP.
1983 vkDeviceWaitIdle(demo->device);
1984 demo_update_data_buffer(demo);
1985
1986 demo_draw(demo);
1987
1988 // Wait for work to finish before updating MVP.
1989 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001990
David Pinedo2bb7c932015-06-18 17:03:14 -06001991 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001992 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06001993 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06001994 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001995}
Ian Elliott639ca472015-04-16 15:23:05 -06001996
1997// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001998LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1999 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002000 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002001 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002002 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002003 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002004 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002005 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002006 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002007 // Resize the application to the new window size, except when
2008 // it was minimized. Vulkan doesn't support images or swapchains
2009 // with width=0 and height=0.
2010 if (wParam != SIZE_MINIMIZED) {
2011 demo.width = lParam & 0xffff;
2012 demo.height = lParam & 0xffff0000 >> 16;
2013 demo_resize(&demo);
2014 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002015 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002016 default:
2017 break;
2018 }
2019 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2020}
2021
Karl Schultze4dc75c2016-02-02 15:37:51 -07002022static void demo_create_window(struct demo *demo) {
2023 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002024
2025 // Initialize the window class structure:
2026 win_class.cbSize = sizeof(WNDCLASSEX);
2027 win_class.style = CS_HREDRAW | CS_VREDRAW;
2028 win_class.lpfnWndProc = WndProc;
2029 win_class.cbClsExtra = 0;
2030 win_class.cbWndExtra = 0;
2031 win_class.hInstance = demo->connection; // hInstance
2032 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2033 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2034 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2035 win_class.lpszMenuName = NULL;
2036 win_class.lpszClassName = demo->name;
2037 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2038 // Register window class:
2039 if (!RegisterClassEx(&win_class)) {
2040 // It didn't work, so try to give a useful error:
2041 printf("Unexpected error trying to start the application!\n");
2042 fflush(stdout);
2043 exit(1);
2044 }
2045 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002046 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002047 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002048 demo->window = CreateWindowEx(0,
2049 demo->name, // class name
2050 demo->name, // app name
2051 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002052 WS_VISIBLE | WS_SYSMENU,
2053 100, 100, // x/y coords
2054 wr.right - wr.left, // width
2055 wr.bottom - wr.top, // height
2056 NULL, // handle to parent
2057 NULL, // handle to menu
2058 demo->connection, // hInstance
2059 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002060 if (!demo->window) {
2061 // It didn't work, so try to give a useful error:
2062 printf("Cannot create a window in which to draw!\n");
2063 fflush(stdout);
2064 exit(1);
2065 }
2066}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002067<<<<<<< 0d3f96c9766e309ab81b36fc908cdfde2ac6915a
Tony Barbour2593b182016-04-19 10:57:58 -06002068#else
2069static void demo_create_xlib_window(struct demo *demo) {
2070
2071 demo->display = XOpenDisplay(NULL);
2072 long visualMask = VisualScreenMask;
2073 int numberOfVisuals;
2074 XVisualInfo vInfoTemplate;
2075 vInfoTemplate.screen = DefaultScreen(demo->display);
2076 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2077 &vInfoTemplate, &numberOfVisuals);
2078
2079 Colormap colormap = XCreateColormap(
2080 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2081 visualInfo->visual, AllocNone);
2082
2083 XSetWindowAttributes windowAttributes;
2084 windowAttributes.colormap = colormap;
2085 windowAttributes.background_pixel = 0xFFFFFFFF;
2086 windowAttributes.border_pixel = 0;
2087 windowAttributes.event_mask =
2088 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2089
2090 demo->xlib_window = XCreateWindow(
2091 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2092 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2093 visualInfo->visual,
2094 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2095
2096 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2097 XMapWindow(demo->display, demo->xlib_window);
2098 XFlush(demo->display);
2099 demo->xlib_wm_delete_window =
2100 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2101}
2102static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2103 switch(event->type) {
2104 case ClientMessage:
2105 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2106 demo->quit = true;
2107 break;
2108 case KeyPress:
2109 switch (event->xkey.keycode) {
2110 case 0x9: // Escape
2111 demo->quit = true;
2112 break;
2113 case 0x71: // left arrow key
2114 demo->spin_angle += demo->spin_increment;
2115 break;
2116 case 0x72: // right arrow key
2117 demo->spin_angle -= demo->spin_increment;
2118 break;
2119 case 0x41:
2120 demo->pause = !demo->pause;
2121 break;
2122 }
2123 break;
2124 case ConfigureNotify:
2125 if ((demo->width != event->xconfigure.width) ||
2126 (demo->height != event->xconfigure.height)) {
2127 demo->width = event->xconfigure.width;
2128 demo->height = event->xconfigure.height;
2129 demo_resize(demo);
2130 }
2131 break;
2132 default:
2133 break;
2134 }
2135
2136}
2137
2138static void demo_run_xlib(struct demo *demo) {
2139
2140 while (!demo->quit) {
2141 XEvent event;
2142
2143 if (demo->pause) {
2144 XNextEvent(demo->display, &event);
2145 demo_handle_xlib_event(demo, &event);
2146 } else {
2147 while (XPending(demo->display) > 0) {
2148 XNextEvent(demo->display, &event);
2149 demo_handle_xlib_event(demo, &event);
2150 }
2151 }
2152
2153 // Wait for work to finish before updating MVP.
2154 vkDeviceWaitIdle(demo->device);
2155 demo_update_data_buffer(demo);
2156
2157 demo_draw(demo);
2158
2159 // Wait for work to finish before updating MVP.
2160 vkDeviceWaitIdle(demo->device);
2161 demo->curFrame++;
2162 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2163 demo->quit = true;
2164 }
2165}
2166
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002167#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002168static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002169 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002170 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002171 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002172 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002173 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002174 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002175 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002176 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2177 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002178 demo->quit = true;
2179 }
2180 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002181 case XCB_KEY_RELEASE: {
2182 const xcb_key_release_event_t *key =
2183 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002184
Karl Schultze4dc75c2016-02-02 15:37:51 -07002185 switch (key->detail) {
2186 case 0x9: // Escape
2187 demo->quit = true;
2188 break;
2189 case 0x71: // left arrow key
2190 demo->spin_angle += demo->spin_increment;
2191 break;
2192 case 0x72: // right arrow key
2193 demo->spin_angle -= demo->spin_increment;
2194 break;
2195 case 0x41:
2196 demo->pause = !demo->pause;
2197 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002198 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002199 } break;
2200 case XCB_CONFIGURE_NOTIFY: {
2201 const xcb_configure_notify_event_t *cfg =
2202 (const xcb_configure_notify_event_t *)event;
2203 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002204 demo->width = cfg->width;
2205 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002206 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002207 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002208 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002209 default:
2210 break;
2211 }
2212}
2213
Tony Barbour2593b182016-04-19 10:57:58 -06002214static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002215 xcb_flush(demo->connection);
2216
2217 while (!demo->quit) {
2218 xcb_generic_event_t *event;
2219
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002220 if (demo->pause) {
2221 event = xcb_wait_for_event(demo->connection);
2222 } else {
2223 event = xcb_poll_for_event(demo->connection);
2224 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002225 if (event) {
Tony Barbour2593b182016-04-19 10:57:58 -06002226 demo_handle_xcb_event(demo, event);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002227 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002228 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002229
2230 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002231 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002232 demo_update_data_buffer(demo);
2233
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002234 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002235
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002236 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002237 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002238 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002239 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002240 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002241 }
2242}
2243
Tony Barbour2593b182016-04-19 10:57:58 -06002244static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002245 uint32_t value_mask, value_list[32];
2246
Tony Barbour2593b182016-04-19 10:57:58 -06002247 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002248
2249 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2250 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002251 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002252 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002253
Tony Barbour2593b182016-04-19 10:57:58 -06002254 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002255 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2256 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2257 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002258
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002259 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002260 xcb_intern_atom_cookie_t cookie =
2261 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2262 xcb_intern_atom_reply_t *reply =
2263 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002264
Karl Schultze4dc75c2016-02-02 15:37:51 -07002265 xcb_intern_atom_cookie_t cookie2 =
2266 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2267 demo->atom_wm_delete_window =
2268 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002269
Tony Barbour2593b182016-04-19 10:57:58 -06002270 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002271 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002272 &(*demo->atom_wm_delete_window).atom);
2273 free(reply);
2274
Tony Barbour2593b182016-04-19 10:57:58 -06002275 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002276
Karl Schultze4dc75c2016-02-02 15:37:51 -07002277 // Force the x/y coordinates to 100,100 results are identical in consecutive
2278 // runs
2279 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002280 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002281 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002282}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002283#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2284static void demo_run(struct demo *demo) {
2285 if (!demo->prepared)
2286 return;
2287
2288 // Wait for work to finish before updating MVP.
2289 vkDeviceWaitIdle(demo->device);
2290 demo_update_data_buffer(demo);
2291
2292 demo_draw(demo);
2293
2294 // Wait for work to finish before updating MVP.
2295 vkDeviceWaitIdle(demo->device);
2296
2297 demo->curFrame++;
2298}
2299#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002300
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002301/*
2302 * Return 1 (true) if all layer names specified in check_names
2303 * can be found in given layer properties.
2304 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002305static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002306 uint32_t layer_count,
2307 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002308 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002309 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002310 for (uint32_t j = 0; j < layer_count; j++) {
2311 if (!strcmp(check_names[i], layers[j].layerName)) {
2312 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002313 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002314 }
2315 }
2316 if (!found) {
2317 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2318 return 0;
2319 }
2320 }
2321 return 1;
2322}
2323
Karl Schultze4dc75c2016-02-02 15:37:51 -07002324static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002325 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002326 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002327 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002328 uint32_t device_validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002329 char **instance_validation_layers = NULL;
2330 demo->enabled_extension_count = 0;
2331 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002332
Karl Schultz7c50ad62016-04-01 12:07:03 -06002333 char *instance_validation_layers_alt1[] = {
2334 "VK_LAYER_LUNARG_standard_validation"
2335 };
2336
2337 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskiaaab5c02016-03-17 15:08:18 -06002338 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Karl Schultz635272d2016-03-07 17:54:07 -07002339 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis1398c712016-03-09 16:12:48 -07002340 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
Karl Schultz7c50ad62016-04-01 12:07:03 -06002341 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002342 };
2343
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002344 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002345 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002346 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002347
Karl Schultz7c50ad62016-04-01 12:07:03 -06002348 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002349 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002350
Karl Schultz7c50ad62016-04-01 12:07:03 -06002351 instance_validation_layers = instance_validation_layers_alt1;
2352 if (instance_layer_count > 0) {
2353 VkLayerProperties *instance_layers =
2354 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2355 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2356 instance_layers);
2357 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002358
Karl Schultz7c50ad62016-04-01 12:07:03 -06002359
2360 validation_found = demo_check_layers(
2361 ARRAY_SIZE(instance_validation_layers_alt1),
2362 instance_validation_layers, instance_layer_count,
2363 instance_layers);
2364 if (validation_found) {
2365 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
2366 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2367 device_validation_layer_count = 1;
2368 } else {
2369 // use alternative set of validation layers
2370 instance_validation_layers = instance_validation_layers_alt2;
2371 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2372 validation_found = demo_check_layers(
2373 ARRAY_SIZE(instance_validation_layers_alt2),
2374 instance_validation_layers, instance_layer_count,
2375 instance_layers);
2376 device_validation_layer_count =
2377 ARRAY_SIZE(instance_validation_layers_alt2);
2378 for (uint32_t i = 0; i < device_validation_layer_count; i++) {
2379 demo->device_validation_layers[i] =
2380 instance_validation_layers[i];
2381 }
2382 }
2383 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002384 }
Dustin Graves7c287352016-01-27 13:48:06 -07002385
Karl Schultz7c50ad62016-04-01 12:07:03 -06002386 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002387 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002388 "required validation layer.\n\n"
2389 "Please look at the Getting Started guide for additional "
2390 "information.\n",
2391 "vkCreateInstance Failure");
2392 }
Dustin Graves7c287352016-01-27 13:48:06 -07002393 }
2394
2395 /* Look for instance extensions */
2396 VkBool32 surfaceExtFound = 0;
2397 VkBool32 platformSurfaceExtFound = 0;
Tony Barbour2593b182016-04-19 10:57:58 -06002398 VkBool32 xlibSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002399 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002400
Karl Schultze4dc75c2016-02-02 15:37:51 -07002401 err = vkEnumerateInstanceExtensionProperties(
2402 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002403 assert(!err);
2404
Dustin Graves7c287352016-01-27 13:48:06 -07002405 if (instance_extension_count > 0) {
2406 VkExtensionProperties *instance_extensions =
2407 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2408 err = vkEnumerateInstanceExtensionProperties(
2409 NULL, &instance_extension_count, instance_extensions);
2410 assert(!err);
2411 for (uint32_t i = 0; i < instance_extension_count; i++) {
2412 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2413 instance_extensions[i].extensionName)) {
2414 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002415 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002416 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002417 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002418#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002419 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2420 instance_extensions[i].extensionName)) {
2421 platformSurfaceExtFound = 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_WIN32_SURFACE_EXTENSION_NAME;
2424 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002425#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002426 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2427 instance_extensions[i].extensionName)) {
2428 platformSurfaceExtFound = 1;
2429 xlibSurfaceExtFound = 1;
2430 demo->extension_names[demo->enabled_extension_count++] =
2431 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2432 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002433#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002434 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2435 instance_extensions[i].extensionName)) {
2436 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002437 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002438 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2439 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002440#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2441 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2442 instance_extensions[i].extensionName)) {
2443 platformSurfaceExtFound = 1;
2444 demo->extension_names[demo->enabled_extension_count++] =
2445 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2446 }
2447#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002448 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2449 instance_extensions[i].extensionName)) {
2450 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002451 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002452 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2453 }
2454 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002455 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002456 }
Dustin Graves7c287352016-01-27 13:48:06 -07002457
2458 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002459 }
Dustin Graves7c287352016-01-27 13:48:06 -07002460
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002461 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002462 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2463 "the " VK_KHR_SURFACE_EXTENSION_NAME
2464 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002465 "Vulkan installable client driver (ICD) installed?\nPlease "
2466 "look at the Getting Started guide for additional "
2467 "information.\n",
2468 "vkCreateInstance Failure");
2469 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002470 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002471#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002472 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2473 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2474 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002475 "Vulkan installable client driver (ICD) installed?\nPlease "
2476 "look at the Getting Started guide for additional "
2477 "information.\n",
2478 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002479#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002480 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2481 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2482 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002483 "Vulkan installable client driver (ICD) installed?\nPlease "
2484 "look at the Getting Started guide for additional "
2485 "information.\n",
2486 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002487#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2488 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2489 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2490 " extension.\n\nDo you have a compatible "
2491 "Vulkan installable client driver (ICD) installed?\nPlease "
2492 "look at the Getting Started guide for additional "
2493 "information.\n",
2494 "vkCreateInstance Failure");
2495#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002496 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002497#if defined(VK_USE_PLATOFMR_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002498 if (demo->use_xlib && !xlibSurfaceExtFound) {
2499 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2500 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2501 " extension.\n\nDo you have a compatible "
2502 "Vulkan installable client driver (ICD) installed?\nPlease "
2503 "look at the Getting Started guide for additional "
2504 "information.\n",
2505 "vkCreateInstance Failure");
2506 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002507#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002508 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002509 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002510 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002511 .pApplicationName = APP_SHORT_NAME,
2512 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002513 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002514 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002515 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002516 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002517 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002518 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002519 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002520 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002521 .enabledLayerCount = demo->enabled_layer_count,
2522 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2523 .enabledExtensionCount = demo->enabled_extension_count,
2524 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002525 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002526
2527 /*
2528 * This is info for a temp callback to use during CreateInstance.
2529 * After the instance is created, we use the instance-based
2530 * function to register the final callback.
2531 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002532 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002533 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002534 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2535 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002536 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002537 dbgCreateInfo.pUserData = NULL;
2538 dbgCreateInfo.flags =
2539 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2540 inst_info.pNext = &dbgCreateInfo;
2541 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002542
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002543 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002544
Chia-I Wu63636062015-10-26 21:10:41 +08002545 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002546 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2547 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002548 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002549 "additional information.\n",
2550 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002551 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002552 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002553 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002554 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002555 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002556 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2557 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002558 "the Getting Started guide for additional information.\n",
2559 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002560 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002561
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002562 /* Make initial call to query gpu_count, then second call for gpu info*/
2563 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2564 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002565
2566 if (gpu_count > 0) {
2567 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2568 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2569 assert(!err);
2570 /* For cube demo we just grab the first physical device */
2571 demo->gpu = physical_devices[0];
2572 free(physical_devices);
2573 } else {
2574 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2575 "Do you have a compatible Vulkan installable client driver (ICD) "
2576 "installed?\nPlease look at the Getting Started guide for "
2577 "additional information.\n",
2578 "vkEnumeratePhysicalDevices Failure");
2579 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002580
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002581 /* Look for validation layers */
2582 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002583 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002584 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002585 err =
2586 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002587 assert(!err);
2588
Dustin Graves7c287352016-01-27 13:48:06 -07002589 if (device_layer_count > 0) {
2590 VkLayerProperties *device_layers =
2591 malloc(sizeof(VkLayerProperties) * device_layer_count);
2592 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2593 device_layers);
2594 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002595
Dustin Graves7c287352016-01-27 13:48:06 -07002596 if (demo->validate) {
2597 validation_found = demo_check_layers(device_validation_layer_count,
2598 demo->device_validation_layers,
2599 device_layer_count,
2600 device_layers);
2601 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002602 }
Dustin Graves7c287352016-01-27 13:48:06 -07002603
2604 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002605 }
2606
Dustin Graves7c287352016-01-27 13:48:06 -07002607 if (demo->validate && !validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002608 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find "
Dustin Graves7c287352016-01-27 13:48:06 -07002609 "a required validation layer.\n\n"
2610 "Please look at the Getting Started guide for additional "
2611 "information.\n",
2612 "vkCreateDevice Failure");
2613 }
2614
2615 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002616 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002617 VkBool32 swapchainExtFound = 0;
2618 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002619 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002620
Karl Schultze4dc75c2016-02-02 15:37:51 -07002621 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2622 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002623 assert(!err);
2624
Dustin Graves7c287352016-01-27 13:48:06 -07002625 if (device_extension_count > 0) {
2626 VkExtensionProperties *device_extensions =
2627 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2628 err = vkEnumerateDeviceExtensionProperties(
2629 demo->gpu, NULL, &device_extension_count, device_extensions);
2630 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002631
Dustin Graves7c287352016-01-27 13:48:06 -07002632 for (uint32_t i = 0; i < device_extension_count; i++) {
2633 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2634 device_extensions[i].extensionName)) {
2635 swapchainExtFound = 1;
2636 demo->extension_names[demo->enabled_extension_count++] =
2637 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2638 }
2639 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002640 }
Dustin Graves7c287352016-01-27 13:48:06 -07002641
2642 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002643 }
Dustin Graves7c287352016-01-27 13:48:06 -07002644
Tony Barbourcc69f452015-10-08 13:59:42 -06002645 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002646 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2647 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2648 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002649 "Vulkan installable client driver (ICD) installed?\nPlease "
2650 "look at the Getting Started guide for additional "
2651 "information.\n",
2652 "vkCreateInstance Failure");
2653 }
2654
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002655 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002656 demo->CreateDebugReportCallback =
2657 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2658 demo->inst, "vkCreateDebugReportCallbackEXT");
2659 demo->DestroyDebugReportCallback =
2660 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2661 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002662 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002663 ERR_EXIT(
2664 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2665 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002666 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002667 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002668 ERR_EXIT(
2669 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2670 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002671 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002672 demo->DebugReportMessage =
2673 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2674 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002675 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002676 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002677 "vkGetProcAddr Failure");
2678 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002679
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002680 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002681 PFN_vkDebugReportCallbackEXT callback;
2682 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002683 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002684 dbgCreateInfo.pNext = NULL;
2685 dbgCreateInfo.pfnCallback = callback;
2686 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002687 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002688 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002689 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2690 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002691 switch (err) {
2692 case VK_SUCCESS:
2693 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002694 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002695 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2696 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002697 break;
2698 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002699 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2700 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002701 break;
2702 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002703 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002704 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002705
2706 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002707 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2708 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002709 assert(demo->queue_count >= 1);
2710
Karl Schultze4dc75c2016-02-02 15:37:51 -07002711 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2712 demo->queue_count * sizeof(VkQueueFamilyProperties));
2713 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2714 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002715 // Find a queue that supports gfx
2716 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002717 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2718 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002719 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2720 break;
2721 }
2722 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002723 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002724 // If app has specific feature requirements it should check supported
2725 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002726 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002727 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002728
Tony Barbourda2bad52016-01-22 14:36:40 -07002729 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2730 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2731 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2732 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2733 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2734}
2735
Karl Schultze4dc75c2016-02-02 15:37:51 -07002736static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002737 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002738 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002739 const VkDeviceQueueCreateInfo queue = {
2740 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2741 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002742 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002743 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002744 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002745
2746 VkDeviceCreateInfo device = {
2747 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2748 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002749 .queueCreateInfoCount = 1,
2750 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002751 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002752 .ppEnabledLayerNames =
2753 (const char *const *)((demo->validate)
2754 ? demo->device_validation_layers
2755 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002756 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002757 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2758 .pEnabledFeatures =
2759 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002760 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002761
Chia-I Wu63636062015-10-26 21:10:41 +08002762 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002763 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002764}
2765
Karl Schultze4dc75c2016-02-02 15:37:51 -07002766static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002767 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002768 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002769
Karl Schultze4dc75c2016-02-02 15:37:51 -07002770// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002771#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002772 VkWin32SurfaceCreateInfoKHR createInfo;
2773 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2774 createInfo.pNext = NULL;
2775 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002776 createInfo.hinstance = demo->connection;
2777 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002778
Karl Schultze4dc75c2016-02-02 15:37:51 -07002779 err =
2780 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002781#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2782 VkAndroidSurfaceCreateInfoKHR createInfo;
2783 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2784 createInfo.pNext = NULL;
2785 createInfo.flags = 0;
2786 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002787
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002788 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2789#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002790 if (demo->use_xlib) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002791#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002792 VkXlibSurfaceCreateInfoKHR createInfo;
2793 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2794 createInfo.pNext = NULL;
2795 createInfo.flags = 0;
2796 createInfo.dpy = demo->display;
2797 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002798
Tony Barbour2593b182016-04-19 10:57:58 -06002799 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2800 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002801#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002802 }
2803 else {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002804#if defined(VK_USE_PLATOFORM_X11_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002805 VkXcbSurfaceCreateInfoKHR createInfo;
2806 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2807 createInfo.pNext = NULL;
2808 createInfo.flags = 0;
2809 createInfo.connection = demo->connection;
2810 createInfo.window = demo->xcb_window;
2811
2812 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002813#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002814 }
Tony Barbour2593b182016-04-19 10:57:58 -06002815 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002816
Tony Barbourcc69f452015-10-08 13:59:42 -06002817 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002818 VkBool32 *supportsPresent =
2819 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002820 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002821 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002822 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002823 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002824
2825 // Search for a graphics and a present queue in the array of queue
2826 // families, try to find one that supports both
2827 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002828 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002829 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002830 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2831 if (graphicsQueueNodeIndex == UINT32_MAX) {
2832 graphicsQueueNodeIndex = i;
2833 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002834
Ian Elliottaaae5352015-07-06 14:27:58 -06002835 if (supportsPresent[i] == VK_TRUE) {
2836 graphicsQueueNodeIndex = i;
2837 presentQueueNodeIndex = i;
2838 break;
2839 }
2840 }
2841 }
2842 if (presentQueueNodeIndex == UINT32_MAX) {
2843 // If didn't find a queue that supports both graphics and present, then
2844 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002845 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002846 if (supportsPresent[i] == VK_TRUE) {
2847 presentQueueNodeIndex = i;
2848 break;
2849 }
2850 }
2851 }
2852 free(supportsPresent);
2853
2854 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002855 if (graphicsQueueNodeIndex == UINT32_MAX ||
2856 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002857 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002858 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002859 }
2860
2861 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002862 // synchronization, and appropriate tracking for QueueSubmit.
2863 // NOTE: While it is possible for an application to use a separate graphics
2864 // and a present queues, this demo program assumes it is only using
2865 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002866 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2867 ERR_EXIT("Could not find a common 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 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002872
Tony Barbourda2bad52016-01-22 14:36:40 -07002873 demo_create_device(demo);
2874
2875 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2876 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2877 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2878 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2879 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2880
Karl Schultze4dc75c2016-02-02 15:37:51 -07002881 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2882 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002883
Ian Elliottaaae5352015-07-06 14:27:58 -06002884 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002885 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002886 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002887 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002888 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002889 VkSurfaceFormatKHR *surfFormats =
2890 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002891 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002892 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002893 assert(!err);
2894 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2895 // the surface has no preferred format. Otherwise, at least one
2896 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002897 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002898 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002899 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002900 assert(formatCount >= 1);
2901 demo->format = surfFormats[0].format;
2902 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002903 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002904
David Pinedo2bb7c932015-06-18 17:03:14 -06002905 demo->quit = false;
2906 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002907
2908 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002909 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002910}
2911
Karl Schultze4dc75c2016-02-02 15:37:51 -07002912static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002913#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002914 const xcb_setup_t *setup;
2915 xcb_screen_iterator_t iter;
2916 int scr;
2917
2918 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002919 if (demo->connection == NULL) {
2920 printf("Cannot find a compatible Vulkan installable client driver "
2921 "(ICD).\nExiting ...\n");
2922 fflush(stdout);
2923 exit(1);
2924 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002925
2926 setup = xcb_get_setup(demo->connection);
2927 iter = xcb_setup_roots_iterator(setup);
2928 while (scr-- > 0)
2929 xcb_screen_next(&iter);
2930
2931 demo->screen = iter.data;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002932#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002933}
2934
Karl Schultze4dc75c2016-02-02 15:37:51 -07002935static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002936 vec3 eye = {0.0f, 3.0f, 5.0f};
2937 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002938 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002939
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002940 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002941 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002942
Piers Daniell735ee532015-02-23 16:23:13 -07002943 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002944 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002945 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002946 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002947 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002948 if (strcmp(argv[i], "--break") == 0) {
2949 demo->use_break = true;
2950 continue;
2951 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002952 if (strcmp(argv[i], "--validate") == 0) {
2953 demo->validate = true;
2954 continue;
2955 }
Tony Barbour2593b182016-04-19 10:57:58 -06002956 if (strcmp(argv[i], "--xlib") == 0) {
2957 demo->use_xlib = true;
2958 continue;
2959 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002960 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2961 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2962 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002963 i++;
2964 continue;
2965 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002966
Karl Schultze4dc75c2016-02-02 15:37:51 -07002967 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2968 "[--c <framecount>]\n",
2969 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002970 fflush(stderr);
2971 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002972 }
2973
Tony Barbour2593b182016-04-19 10:57:58 -06002974 if (!demo->use_xlib)
2975 demo_init_connection(demo);
2976
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002977 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002978
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002979 demo->width = 500;
2980 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002981
2982 demo->spin_angle = 0.01f;
2983 demo->spin_increment = 0.01f;
2984 demo->pause = false;
2985
Karl Schultze4dc75c2016-02-02 15:37:51 -07002986 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2987 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002988 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2989 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002990}
2991
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002992#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07002993// Include header required for parsing the command line options.
2994#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002995
Karl Schultze4dc75c2016-02-02 15:37:51 -07002996int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
2997 int nCmdShow) {
2998 MSG msg; // message
2999 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003000 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003001 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003002
Karl Schultze4dc75c2016-02-02 15:37:51 -07003003 // Use the CommandLine functions to get the command line arguments.
3004 // Unfortunately, Microsoft outputs
3005 // this information as wide characters for Unicode, and we simply want the
3006 // Ascii version to be compatible
3007 // with the non-Windows side. So, we have to convert the information to
3008 // Ascii character strings.
3009 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3010 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003011 argc = 0;
3012 }
3013
Karl Schultze4dc75c2016-02-02 15:37:51 -07003014 if (argc > 0) {
3015 argv = (char **)malloc(sizeof(char *) * argc);
3016 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003017 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003018 } else {
3019 for (int iii = 0; iii < argc; iii++) {
3020 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003021 size_t numConverted = 0;
3022
Karl Schultze4dc75c2016-02-02 15:37:51 -07003023 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3024 if (argv[iii] != NULL) {
3025 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3026 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003027 }
3028 }
3029 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003030 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003031 argv = NULL;
3032 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003033
3034 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003035
3036 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003037 if (argc > 0 && argv != NULL) {
3038 for (int iii = 0; iii < argc; iii++) {
3039 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003040 free(argv[iii]);
3041 }
3042 }
3043 free(argv);
3044 }
3045
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003046 demo.connection = hInstance;
3047 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003048 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003049 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003050
3051 demo_prepare(&demo);
3052
Karl Schultze4dc75c2016-02-02 15:37:51 -07003053 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003054
3055 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003056 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003057 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003058 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003059 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003060 done = true; // if found, quit app
3061 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003062 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003063 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003064 DispatchMessage(&msg);
3065 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003066 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003067 }
3068
3069 demo_cleanup(&demo);
3070
Karl Schultze4dc75c2016-02-02 15:37:51 -07003071 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003072}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003073#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3074#include <android/log.h>
3075#include <android_native_app_glue.h>
3076static bool initialized = false;
3077static bool active = false;
3078struct demo demo;
3079
3080static int32_t processInput(struct android_app* app, AInputEvent* event) {
3081 return 0;
3082}
3083
3084static void processCommand(struct android_app* app, int32_t cmd) {
3085 switch(cmd) {
3086 case APP_CMD_INIT_WINDOW: {
3087 if (app->window) {
3088 demo_init(&demo, 0, NULL);
3089 demo.window = (void*)app->window;
3090 demo_init_vk_swapchain(&demo);
3091 demo_prepare(&demo);
3092 initialized = true;
3093 }
3094 break;
3095 }
3096 case APP_CMD_GAINED_FOCUS: {
3097 active = true;
3098 break;
3099 }
3100 case APP_CMD_LOST_FOCUS: {
3101 active = false;
3102 break;
3103 }
3104 }
3105}
3106
3107void android_main(struct android_app *app)
3108{
3109 app_dummy();
3110
Cody Northrop8707a6e2016-04-26 19:59:19 -06003111#ifdef ANDROID
3112 int vulkanSupport = InitVulkan();
3113 if (vulkanSupport == 0)
3114 return;
3115#endif
3116
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003117 app->onAppCmd = processCommand;
3118 app->onInputEvent = processInput;
3119
3120 while(1) {
3121 int events;
3122 struct android_poll_source* source;
3123 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3124 if (source) {
3125 source->process(app, source);
3126 }
3127
3128 if (app->destroyRequested != 0) {
3129 demo_cleanup(&demo);
3130 return;
3131 }
3132 }
3133 if (initialized && active) {
3134 demo_run(&demo);
3135 }
3136 }
3137
3138}
3139#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003140int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003141 struct demo demo;
3142
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003143 demo_init(&demo, argc, argv);
Tony Barbour2593b182016-04-19 10:57:58 -06003144 if (demo.use_xlib)
3145 demo_create_xlib_window(&demo);
3146 else
3147 demo_create_xcb_window(&demo);
3148
Tony Barbourcc69f452015-10-08 13:59:42 -06003149 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003150
3151 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003152
3153 if (demo.use_xlib)
3154 demo_run_xlib(&demo);
3155 else
3156 demo_run_xcb(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003157
3158 demo_cleanup(&demo);
3159
Karl Schultz0218c002016-03-22 17:06:13 -06003160 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003161}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003162#endif