blob: 8fbcf9b641108cc47141eda55a40ff5b584574b7 [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
David Pinedoc5193c12015-11-06 12:54:48 -070040#include <vulkan/vulkan.h>
Ian Elliottb08e0d92015-11-20 11:55:46 -070041
David Pinedo541526f2015-11-24 09:00:24 -070042#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060043#include "linmath.h"
44
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060046#define APP_SHORT_NAME "cube"
47#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060049#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
50
Tony Barbourfdc2d352015-04-22 09:02:32 -060051#if defined(NDEBUG) && defined(__GNUC__)
52#define U_ASSERT_ONLY __attribute__((unused))
53#else
54#define U_ASSERT_ONLY
55#endif
56
Ian Elliott07264132015-04-28 11:35:02 -060057#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070058#define ERR_EXIT(err_msg, err_class) \
59 do { \
60 MessageBox(NULL, err_msg, err_class, MB_OK); \
61 exit(1); \
62 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060063
Michael Lentinec6cde4b2016-04-18 13:20:45 -050064#elif defined __ANDROID__
65#include <android/log.h>
66#define ERR_EXIT(err_msg, err_class) \
67 do { \
68 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
69 exit(1); \
70 } while (0)
71#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070072#define ERR_EXIT(err_msg, err_class) \
73 do { \
74 printf(err_msg); \
75 fflush(stdout); \
76 exit(1); \
77 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050078#endif
Ian Elliott07264132015-04-28 11:35:02 -060079
Karl Schultze4dc75c2016-02-02 15:37:51 -070080#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
81 { \
82 demo->fp##entrypoint = \
83 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
84 if (demo->fp##entrypoint == NULL) { \
85 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
86 "vkGetInstanceProcAddr Failure"); \
87 } \
88 }
Ian Elliottaaae5352015-07-06 14:27:58 -060089
Jon Ashburn7d8342c2015-10-08 15:58:23 -060090static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
91
Karl Schultze4dc75c2016-02-02 15:37:51 -070092#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
93 { \
94 if (!g_gdpa) \
95 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
96 demo->inst, "vkGetDeviceProcAddr"); \
97 demo->fp##entrypoint = \
98 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
99 if (demo->fp##entrypoint == NULL) { \
100 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
101 "vkGetDeviceProcAddr Failure"); \
102 } \
103 }
Ian Elliott673898b2015-06-22 15:07:49 -0600104
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600105/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700106 * structure to track all objects related to a texture.
107 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600108struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600109 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700110
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600111 VkImage image;
112 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600113
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800114 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500115 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600116 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700117 int32_t tex_width, tex_height;
118};
119
Karl Schultze4dc75c2016-02-02 15:37:51 -0700120static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600121
Karl Schultz0218c002016-03-22 17:06:13 -0600122static int validation_error = 0;
123
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600124struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600125 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700126 float mvp[4][4];
127 float position[12 * 3][4];
128 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129};
130
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600131struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600132 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700133 float mvp[4][4];
134 float position[12 * 3][4];
135 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600136};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600137
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600138//--------------------------------------------------------------------------------------
139// Mesh and VertexFormat Data
140//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700141// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600142struct Vertex
143{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600144 float posX, posY, posZ, posW; // Position data
145 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146};
147
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600148struct VertexPosTex
149{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600150 float posX, posY, posZ, posW; // Position data
151 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600152};
153
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600154#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600155#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600156
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600157static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 -1.0f,-1.0f, 1.0f,
160 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800161 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 -1.0f, 1.0f,-1.0f,
163 -1.0f,-1.0f,-1.0f,
164
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f, 1.0f,-1.0f,
167 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800168 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600170 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f,-1.0f,-1.0f, // -Y side
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,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800175 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600177 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 -1.0f, 1.0f, 1.0f,
181 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800182 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600184 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f, 1.0f, 1.0f,
188 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800189 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190 1.0f,-1.0f,-1.0f,
191 1.0f, 1.0f,-1.0f,
192
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600194 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600195 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800196 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600197 1.0f,-1.0f, 1.0f,
198 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600199};
200
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600201static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 0.0f, 0.0f, // -X side
203 1.0f, 0.0f,
204 1.0f, 1.0f,
205 1.0f, 1.0f,
206 0.0f, 1.0f,
207 0.0f, 0.0f,
208
209 1.0f, 0.0f, // -Z side
210 0.0f, 1.0f,
211 0.0f, 0.0f,
212 1.0f, 0.0f,
213 1.0f, 1.0f,
214 0.0f, 1.0f,
215
216 1.0f, 1.0f, // -Y side
217 1.0f, 0.0f,
218 0.0f, 0.0f,
219 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220 0.0f, 0.0f,
221 0.0f, 1.0f,
222
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600224 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800225 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600226 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600228 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800230 1.0f, 1.0f, // +X side
231 0.0f, 1.0f,
232 0.0f, 0.0f,
233 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600235 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 0.0f, 1.0f, // +Z side
238 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600239 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600240 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800241 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600242 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600243};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700244// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600245
Karl Schultze4dc75c2016-02-02 15:37:51 -0700246void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247 int i;
248
249 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700250 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600251 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
252 }
253 printf("\n");
254 fflush(stdout);
255}
256
Karl Schultze4dc75c2016-02-02 15:37:51 -0700257void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600258 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700259 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600260 printf("\n");
261 fflush(stdout);
262}
263
Karl Schultze4dc75c2016-02-02 15:37:51 -0700264VKAPI_ATTR VkBool32 VKAPI_CALL
265dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
266 uint64_t srcObject, size_t location, int32_t msgCode,
267 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
268 char *message = (char *)malloc(strlen(pMsg) + 100);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600269
Karl Schultze4dc75c2016-02-02 15:37:51 -0700270 assert(message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600271
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700272 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700273 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
274 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600275 validation_error = 1;
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -0700276 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700277 // We know that we're submitting queues without fences, ignore this
278 // warning
279 if (strstr(pMsg,
280 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600281 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600282 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700283 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
284 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600285 validation_error = 1;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600286 } else {
Karl Schultz0218c002016-03-22 17:06:13 -0600287 validation_error = 1;
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600288 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600289 }
290
291#ifdef _WIN32
292 MessageBox(NULL, message, "Alert", MB_OK);
293#else
Karl Schultze4dc75c2016-02-02 15:37:51 -0700294 printf("%s\n", message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600295 fflush(stdout);
296#endif
297 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600298
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600299 /*
300 * false indicates that layer should not bail-out of an
301 * API call that had validation failures. This may mean that the
302 * app dies inside the driver due to invalid parameter(s).
303 * That's what would happen without validation layers, so we'll
304 * keep that behavior here.
305 */
306 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600307}
308
Karl Schultz0c3c1512016-03-25 15:35:18 -0600309VKAPI_ATTR VkBool32 VKAPI_CALL
310BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
311 uint64_t srcObject, size_t location, int32_t msgCode,
312 const char *pLayerPrefix, const char *pMsg,
313 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700314#ifndef WIN32
315 raise(SIGTRAP);
316#else
317 DebugBreak();
318#endif
319
320 return false;
321}
322
Ian Elliotta0781972015-08-21 15:09:33 -0600323typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600324 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800325 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600326 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600327} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600328
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500330#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600331#define APP_NAME_STR_LEN 80
332 HINSTANCE connection; // hInstance - Windows Instance
333 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700334 HWND window; // hWnd - window handle
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500335#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600336 Display* display;
337 Window xlib_window;
338 Atom xlib_wm_delete_window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500339#elif defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600340 xcb_connection_t *connection;
341 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600342 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800343 xcb_intern_atom_reply_t *atom_wm_delete_window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500344#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
345 ANativeWindow* window;
346#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700347 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600348 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700349 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600350 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600352 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600353 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600354 VkDevice device;
355 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700356 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600357 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600358 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600359 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Tony Barbourda2bad52016-01-22 14:36:40 -0700361 uint32_t enabled_extension_count;
362 uint32_t enabled_layer_count;
363 char *extension_names[64];
364 char *device_validation_layers[64];
365
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600368 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369
Karl Schultze4dc75c2016-02-02 15:37:51 -0700370 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
371 fpGetPhysicalDeviceSurfaceSupportKHR;
372 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
373 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
374 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
375 fpGetPhysicalDeviceSurfaceFormatsKHR;
376 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
377 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600378 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
379 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
380 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
381 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
382 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600383 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600384 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600385 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600386
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800387 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388
389 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600390 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600391
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800393 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500394 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600395 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600396 } depth;
397
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600398 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600399
400 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800402 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500403 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600404 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600405 } uniform_data;
406
Karl Schultze4dc75c2016-02-02 15:37:51 -0700407 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500408 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600409 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600410 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800411 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600412 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600413
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600414 mat4x4 projection_matrix;
415 mat4x4 view_matrix;
416 mat4x4 model_matrix;
417
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600418 float spin_angle;
419 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600420 bool pause;
421
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600422 VkShaderModule vert_shader_module;
423 VkShaderModule frag_shader_module;
424
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600425 VkDescriptorPool desc_pool;
426 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800427
Tony Barbourd8e504f2015-10-08 14:26:24 -0600428 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800429
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600430 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600431 int32_t curFrame;
432 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600433 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600434 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700435 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
436 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
437 VkDebugReportCallbackEXT msg_callback;
438 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600439
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600440 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600441 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600442};
443
Ian Elliottcf074d32015-10-16 18:02:43 -0600444// Forward declaration:
445static void demo_resize(struct demo *demo);
446
Karl Schultze4dc75c2016-02-02 15:37:51 -0700447static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
448 VkFlags requirements_mask,
449 uint32_t *typeIndex) {
450 // Search memtypes to find first index with those properties
451 for (uint32_t i = 0; i < 32; i++) {
452 if ((typeBits & 1) == 1) {
453 // Type is available, does it match user properties?
454 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
455 requirements_mask) == requirements_mask) {
456 *typeIndex = i;
457 return true;
458 }
459 }
460 typeBits >>= 1;
461 }
462 // No memory types matched, return failure
463 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600464}
465
Karl Schultze4dc75c2016-02-02 15:37:51 -0700466static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600467 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470 return;
471
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473 assert(!err);
474
Karl Schultze4dc75c2016-02-02 15:37:51 -0700475 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800476 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700477 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
478 .pNext = NULL,
479 .waitSemaphoreCount = 0,
480 .pWaitSemaphores = NULL,
481 .pWaitDstStageMask = NULL,
482 .commandBufferCount = 1,
483 .pCommandBuffers = cmd_bufs,
484 .signalSemaphoreCount = 0,
485 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600486
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600487 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488 assert(!err);
489
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491 assert(!err);
492
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600493 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495}
496
Karl Schultze4dc75c2016-02-02 15:37:51 -0700497static void demo_set_image_layout(struct demo *demo, VkImage image,
498 VkImageAspectFlags aspectMask,
499 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700500 VkImageLayout new_image_layout,
501 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600502 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800505 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800506 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600507 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800508 .commandPool = demo->cmd_pool,
509 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700510 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511 };
512
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800513 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600514 assert(!err);
515
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700516 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
517 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800519 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600520 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800521 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800522 .occlusionQueryEnable = VK_FALSE,
523 .queryFlags = 0,
524 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700526 VkCommandBufferBeginInfo cmd_buf_info = {
527 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
528 .pNext = NULL,
529 .flags = 0,
530 .pInheritanceInfo = &cmd_buf_hinfo,
531 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600532 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600533 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600534 }
535
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600536 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600538 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700539 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800540 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600541 .oldLayout = old_image_layout,
542 .newLayout = new_image_layout,
543 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700544 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600545
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800546 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800548 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549 }
550
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700551 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700552 image_memory_barrier.dstAccessMask =
553 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700554 }
555
556 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700557 image_memory_barrier.dstAccessMask =
558 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700559 }
560
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600561 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600562 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700563 image_memory_barrier.dstAccessMask =
564 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600565 }
566
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600567 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600568
Tony Barbour50bbca42015-06-29 16:20:35 -0600569 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
570 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600571
Karl Schultze4dc75c2016-02-02 15:37:51 -0700572 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
573 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600574}
575
Karl Schultze4dc75c2016-02-02 15:37:51 -0700576static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700577 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
578 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600579 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800580 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600581 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800582 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800583 .occlusionQueryEnable = VK_FALSE,
584 .queryFlags = 0,
585 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700586 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700587 const VkCommandBufferBeginInfo cmd_buf_info = {
588 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
589 .pNext = NULL,
590 .flags = 0,
591 .pInheritanceInfo = &cmd_buf_hinfo,
592 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800593 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700594 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
595 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800596 };
597 const VkRenderPassBeginInfo rp_begin = {
598 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
599 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800600 .renderPass = demo->render_pass,
601 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800602 .renderArea.offset.x = 0,
603 .renderArea.offset.y = 0,
604 .renderArea.extent.width = demo->width,
605 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600606 .clearValueCount = 2,
607 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700608 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800609 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600611 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612 assert(!err);
613
Tony Barbour5c5b1632016-04-26 11:13:48 -0600614 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
615 // happens to the previous contents of the image
616 VkImageMemoryBarrier image_memory_barrier = {
617 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
618 .pNext = NULL,
619 .srcAccessMask = 0,
620 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
621 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
622 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
623 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
624 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
625 .image = demo->buffers[demo->current_buffer].image,
626 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
627
628 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
629 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
630 NULL, 1, &image_memory_barrier);
631
Chia-I Wuf051d262015-10-27 19:25:11 +0800632 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800633
Karl Schultze4dc75c2016-02-02 15:37:51 -0700634 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
635 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
636 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
637 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600638 VkViewport viewport;
639 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700640 viewport.height = (float)demo->height;
641 viewport.width = (float)demo->width;
642 viewport.minDepth = (float)0.0f;
643 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700644 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600645
646 VkRect2D scissor;
647 memset(&scissor, 0, sizeof(scissor));
648 scissor.extent.width = demo->width;
649 scissor.extent.height = demo->height;
650 scissor.offset.x = 0;
651 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700652 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600653 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800654 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655
Tony Barbour15a4ef62015-10-21 10:14:48 -0600656 VkImageMemoryBarrier prePresentBarrier = {
657 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
658 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800659 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700660 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600661 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700662 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600663 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800664 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700665 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600666
667 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
668 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700669 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
670 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
671 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600672
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600673 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674 assert(!err);
675}
676
Karl Schultze4dc75c2016-02-02 15:37:51 -0700677void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600678 mat4x4 MVP, Model, VP;
679 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600680 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600681 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600682
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600683 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600684
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600685 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600686 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700687 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
688 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600689 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600690
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
692 demo->uniform_data.mem_alloc.allocationSize, 0,
693 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600694 assert(!err);
695
Karl Schultze4dc75c2016-02-02 15:37:51 -0700696 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600698 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600699}
700
Karl Schultze4dc75c2016-02-02 15:37:51 -0700701static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600702 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600703 VkSemaphore presentCompleteSemaphore;
704 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
705 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
706 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600707 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600708 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800709 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600710
Karl Schultze4dc75c2016-02-02 15:37:51 -0700711 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
712 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600713 assert(!err);
714
715 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700716 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700718 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600719 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600720 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
721 // demo->swapchain is out of date (e.g. the window was resized) and
722 // must be recreated:
723 demo_resize(demo);
724 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800725 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600726 return;
727 } else if (err == VK_SUBOPTIMAL_KHR) {
728 // demo->swapchain is not as optimal as it could be, but the platform's
729 // presentation engine will still present the image correctly.
730 } else {
731 assert(!err);
732 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600733
Tony Barbour15a4ef62015-10-21 10:14:48 -0600734 demo_flush_init_cmd(demo);
735
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600736 // Wait for the present complete semaphore to be signaled to ensure
737 // that the image won't be rendered to until the presentation
738 // engine has fully released ownership to the application, and it is
739 // okay to render to the image.
740
Karl Schultze4dc75c2016-02-02 15:37:51 -0700741 VkPipelineStageFlags pipe_stage_flags =
742 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
743 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
744 .pNext = NULL,
745 .waitSemaphoreCount = 1,
746 .pWaitSemaphores = &presentCompleteSemaphore,
747 .pWaitDstStageMask = &pipe_stage_flags,
748 .commandBufferCount = 1,
749 .pCommandBuffers =
750 &demo->buffers[demo->current_buffer].cmd,
751 .signalSemaphoreCount = 0,
752 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600753
754 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600755 assert(!err);
756
Ian Elliotta0781972015-08-21 15:09:33 -0600757 VkPresentInfoKHR present = {
758 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600759 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600760 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700761 .pSwapchains = &demo->swapchain,
762 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600763 };
764
Karl Schultze4dc75c2016-02-02 15:37:51 -0700765 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600766 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600767 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
768 // demo->swapchain is out of date (e.g. the window was resized) and
769 // must be recreated:
770 demo_resize(demo);
771 } else if (err == VK_SUBOPTIMAL_KHR) {
772 // demo->swapchain is not as optimal as it could be, but the platform's
773 // presentation engine will still present the image correctly.
774 } else {
775 assert(!err);
776 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600777
Chia-I Wucbb564e2015-04-16 22:02:10 +0800778 err = vkQueueWaitIdle(demo->queue);
779 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600780
Chia-I Wu63636062015-10-26 21:10:41 +0800781 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600782}
783
Karl Schultze4dc75c2016-02-02 15:37:51 -0700784static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600785 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600786 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600787
Ian Elliottb08e0d92015-11-20 11:55:46 -0700788 // Check the surface capabilities and formats
789 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700790 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
791 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600792 assert(!err);
793
Ian Elliott8528eff2015-08-07 15:56:59 -0600794 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700795 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
796 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600797 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600798 VkPresentModeKHR *presentModes =
799 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600800 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700801 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
802 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600803 assert(!err);
804
Ian Elliotta0781972015-08-21 15:09:33 -0600805 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600806 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700807 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600808 // If the surface size is undefined, the size is set to
809 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600810 swapchainExtent.width = demo->width;
811 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700812 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600813 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700814 swapchainExtent = surfCapabilities.currentExtent;
815 demo->width = surfCapabilities.currentExtent.width;
816 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600817 }
818
819 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600820 // tearing mode. If not, try IMMEDIATE which will usually be available,
821 // and is fastest (though it tears). If not, fall back to FIFO which is
822 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600823 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600824 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600825 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
826 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600827 break;
828 }
Ian Elliotta0781972015-08-21 15:09:33 -0600829 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
830 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
831 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600832 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600833 }
834
835 // Determine the number of VkImage's to use in the swap chain (we desire to
836 // own only 1 image at a time, besides the images being displayed and
837 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700838 uint32_t desiredNumberOfSwapchainImages =
839 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700840 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700841 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600842 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700843 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600844 }
845
Tony Barbour9fd6d352015-09-16 15:01:32 -0600846 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700847 if (surfCapabilities.supportedTransforms &
848 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700849 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600850 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700851 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600852 }
853
Ian Elliottcf074d32015-10-16 18:02:43 -0600854 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600855 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800856 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700857 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600858 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800859 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600860 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700861 .imageExtent =
862 {
863 .width = swapchainExtent.width, .height = swapchainExtent.height,
864 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700865 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600866 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700867 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700868 .imageArrayLayers = 1,
869 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
870 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600871 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600872 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600873 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600874 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600875 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600876 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600877
Karl Schultze4dc75c2016-02-02 15:37:51 -0700878 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
879 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800880 assert(!err);
881
Ian Elliottcf074d32015-10-16 18:02:43 -0600882 // If we just re-created an existing swapchain, we should destroy the old
883 // swapchain at this point.
884 // Note: destroying the swapchain also cleans up all its associated
885 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800886 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700887 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600888 }
889
890 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600891 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600892 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800893
Karl Schultze4dc75c2016-02-02 15:37:51 -0700894 VkImage *swapchainImages =
895 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600896 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600897 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600898 &demo->swapchainImageCount,
899 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600900 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600901
Karl Schultze4dc75c2016-02-02 15:37:51 -0700902 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
903 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600904 assert(demo->buffers);
905
Ian Elliotta0781972015-08-21 15:09:33 -0600906 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600907 VkImageViewCreateInfo color_image_view = {
908 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600909 .pNext = NULL,
910 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700911 .components =
912 {
913 .r = VK_COMPONENT_SWIZZLE_R,
914 .g = VK_COMPONENT_SWIZZLE_G,
915 .b = VK_COMPONENT_SWIZZLE_B,
916 .a = VK_COMPONENT_SWIZZLE_A,
917 },
918 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
919 .baseMipLevel = 0,
920 .levelCount = 1,
921 .baseArrayLayer = 0,
922 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600923 .viewType = VK_IMAGE_VIEW_TYPE_2D,
924 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600925 };
926
Ian Elliotta0781972015-08-21 15:09:33 -0600927 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600928
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600929 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600930
Karl Schultze4dc75c2016-02-02 15:37:51 -0700931 err = vkCreateImageView(demo->device, &color_image_view, NULL,
932 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600933 assert(!err);
934 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700935
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500936
Mark Young04fd3d82016-01-25 14:43:50 -0700937 if (NULL != presentModes) {
938 free(presentModes);
939 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600940}
941
Karl Schultze4dc75c2016-02-02 15:37:51 -0700942static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600943 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600944 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600945 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600946 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600947 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600948 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700949 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600951 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800952 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600953 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600954 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955 .flags = 0,
956 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200957
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600958 VkImageViewCreateInfo view = {
959 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600960 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800961 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600962 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700963 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
964 .baseMipLevel = 0,
965 .levelCount = 1,
966 .baseArrayLayer = 0,
967 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600968 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600969 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600970 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600971
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500972 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600973 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600974 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600975
976 demo->depth.format = depth_format;
977
978 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700979 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600980 assert(!err);
981
Karl Schultze4dc75c2016-02-02 15:37:51 -0700982 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600983 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600984
Chia-I Wuf48700b2015-11-10 16:21:09 +0800985 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200986 demo->depth.mem_alloc.pNext = NULL;
987 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
988 demo->depth.mem_alloc.memoryTypeIndex = 0;
989
Karl Schultze4dc75c2016-02-02 15:37:51 -0700990 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
991 0, /* No requirements */
992 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600993 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600994
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500995 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700996 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
997 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500998 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600999
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001000 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001001 err =
1002 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001003 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004
Karl Schultze4dc75c2016-02-02 15:37:51 -07001005 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1006 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001007 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1008 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001009
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001010 /* create image view */
1011 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001012 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014}
1015
Tony Barbour1a86d032015-09-21 15:17:33 -06001016/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001018 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001019#ifdef __ANDROID__
1020#include <lunarg.ppm.h>
1021 char *cPtr;
1022 cPtr = (char*)___lunarg_ppm;
1023 if ((unsigned char*)cPtr >= (___lunarg_ppm + ___lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
1024 return false;
1025 }
1026 while(strncmp(cPtr++, "\n", 1));
1027 sscanf(cPtr, "%u %u", height, width);
1028 if (rgba_data == NULL) {
1029 return true;
1030 }
1031 while(strncmp(cPtr++, "\n", 1));
1032 if ((unsigned char*)cPtr >= (___lunarg_ppm + ___lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
1033 return false;
1034 }
1035 while(strncmp(cPtr++, "\n", 1));
1036
1037 for (int y = 0; y < *height; y++) {
1038 uint8_t *rowPtr = rgba_data;
1039 for (int x = 0; x < *width; x++) {
1040 memcpy(rowPtr, cPtr, 3);
1041 rowPtr[3] = 255; /* Alpha of 1 */
1042 rowPtr += 4;
1043 cPtr += 3;
1044 }
1045 rgba_data += layout->rowPitch;
1046 }
1047
1048 return true;
1049#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001050 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001051 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001052
Tony Barbour1a86d032015-09-21 15:17:33 -06001053 if (!fPtr)
1054 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001055
Tony Barbour1a86d032015-09-21 15:17:33 -06001056 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001057 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1058 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001059 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001060 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001061
Tony Barbour1a86d032015-09-21 15:17:33 -06001062 do {
1063 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001064 if (cPtr == NULL) {
1065 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001066 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001067 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001068 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001069
Tony Barbour1a86d032015-09-21 15:17:33 -06001070 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001071 if (rgba_data == NULL) {
1072 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001073 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001074 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001075 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001076 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001077 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1078 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001079 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001080 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001081
Karl Schultze4dc75c2016-02-02 15:37:51 -07001082 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001083 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001084 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001085 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001086 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001087 rowPtr[3] = 255; /* Alpha of 1 */
1088 rowPtr += 4;
1089 }
1090 rgba_data += layout->rowPitch;
1091 }
1092 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001093 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001094#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001095}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001096
Karl Schultze4dc75c2016-02-02 15:37:51 -07001097static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001098 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001099 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001100 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001101 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001102 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001103 int32_t tex_width;
1104 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001105 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001106 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107
Karl Schultze4dc75c2016-02-02 15:37:51 -07001108 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001109 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001110 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001112 tex_obj->tex_width = tex_width;
1113 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001114
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001115 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001116 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001118 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001120 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001122 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001123 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001124 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001125 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001126 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001127 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001128 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001130 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001131
Karl Schultze4dc75c2016-02-02 15:37:51 -07001132 err =
1133 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134 assert(!err);
1135
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001136 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001137
Chia-I Wuf48700b2015-11-10 16:21:09 +08001138 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001139 tex_obj->mem_alloc.pNext = NULL;
1140 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1141 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142
Karl Schultze4dc75c2016-02-02 15:37:51 -07001143 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1144 required_props,
1145 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001146 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001147
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001148 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001149 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001150 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001151 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001152
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001153 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001154 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001155 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001156
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001157 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001158 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001159 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001161 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001163 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164 void *data;
1165
Karl Schultze4dc75c2016-02-02 15:37:51 -07001166 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1167 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168
Karl Schultze4dc75c2016-02-02 15:37:51 -07001169 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1170 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001171 assert(!err);
1172
1173 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1174 fprintf(stderr, "Error loading texture: %s\n", filename);
1175 }
1176
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001177 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001179
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001180 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001181 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001182 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1183 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001184 /* setting the image layout does not reference the actual memory so no need
1185 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001186}
1187
Karl Schultze4dc75c2016-02-02 15:37:51 -07001188static void demo_destroy_texture_image(struct demo *demo,
1189 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001191 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1192 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001193}
1194
Karl Schultze4dc75c2016-02-02 15:37:51 -07001195static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001196 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001197 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001198 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001200 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001202 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001203 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001204
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205 if ((props.linearTilingFeatures &
1206 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1207 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208 /* Device can texture using linear textures */
1209 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Karl Schultze4dc75c2016-02-02 15:37:51 -07001210 VK_IMAGE_TILING_LINEAR,
1211 VK_IMAGE_USAGE_SAMPLED_BIT,
1212 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
1213 } else if (props.optimalTilingFeatures &
1214 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001215 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001216 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217
1218 memset(&staging_texture, 0, sizeof(staging_texture));
1219 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001220 VK_IMAGE_TILING_LINEAR,
1221 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1222 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001223
Karl Schultze4dc75c2016-02-02 15:37:51 -07001224 demo_prepare_texture_image(
1225 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1226 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1227 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001228
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001229 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001230 VK_IMAGE_ASPECT_COLOR_BIT,
1231 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001232 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1233 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001235 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001236 VK_IMAGE_ASPECT_COLOR_BIT,
1237 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001238 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1239 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001241 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001242 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1243 .srcOffset = {0, 0, 0},
1244 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1245 .dstOffset = {0, 0, 0},
1246 .extent = {staging_texture.tex_width,
1247 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001248 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001249 vkCmdCopyImage(
1250 demo->cmd, staging_texture.image,
1251 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1252 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001253
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001254 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001255 VK_IMAGE_ASPECT_COLOR_BIT,
1256 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001257 demo->textures[i].imageLayout,
1258 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001259
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001260 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001261
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001262 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001263 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001264 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1265 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001266 }
1267
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001268 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001269 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001270 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001271 .magFilter = VK_FILTER_NEAREST,
1272 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001273 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001274 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1275 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1276 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001277 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001278 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001279 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001280 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001281 .minLod = 0.0f,
1282 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001283 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001284 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001285 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001286
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001287 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001288 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001289 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001290 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001291 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001292 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001293 .components =
1294 {
1295 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1296 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1297 },
1298 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001299 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001300 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001301
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001303 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001304 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001305 assert(!err);
1306
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307 /* create image view */
1308 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001309 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001310 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311 assert(!err);
1312 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001314
Karl Schultze4dc75c2016-02-02 15:37:51 -07001315void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001316 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001317 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001318 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001319 int i;
1320 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001321 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001322 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001323 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001324
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001325 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001326 mat4x4_mul(MVP, VP, demo->model_matrix);
1327 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001328 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001329
Karl Schultze4dc75c2016-02-02 15:37:51 -07001330 for (i = 0; i < 12 * 3; i++) {
1331 data.position[i][0] = g_vertex_buffer_data[i * 3];
1332 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1333 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001334 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001335 data.attr[i][0] = g_uv_buffer_data[2 * i];
1336 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001337 data.attr[i][2] = 0;
1338 data.attr[i][3] = 0;
1339 }
1340
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001341 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001342 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001343 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001344 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001345 err =
1346 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001347 assert(!err);
1348
Karl Schultze4dc75c2016-02-02 15:37:51 -07001349 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1350 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001351
Chia-I Wuf48700b2015-11-10 16:21:09 +08001352 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001353 demo->uniform_data.mem_alloc.pNext = NULL;
1354 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1355 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1356
Karl Schultze4dc75c2016-02-02 15:37:51 -07001357 pass = memory_type_from_properties(
1358 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1359 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001360 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001361
Karl Schultze4dc75c2016-02-02 15:37:51 -07001362 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1363 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001364 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001365
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1367 demo->uniform_data.mem_alloc.allocationSize, 0,
1368 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001369 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001370
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001371 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001372
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001373 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001374
Karl Schultze4dc75c2016-02-02 15:37:51 -07001375 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1376 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001377 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001378
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001379 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1380 demo->uniform_data.buffer_info.offset = 0;
1381 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001382}
1383
Karl Schultze4dc75c2016-02-02 15:37:51 -07001384static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001385 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001386 [0] =
1387 {
1388 .binding = 0,
1389 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1390 .descriptorCount = 1,
1391 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1392 .pImmutableSamplers = NULL,
1393 },
1394 [1] =
1395 {
1396 .binding = 1,
1397 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1398 .descriptorCount = DEMO_TEXTURE_COUNT,
1399 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1400 .pImmutableSamplers = NULL,
1401 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001402 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001403 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001404 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001405 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001406 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001407 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001408 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001409 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410
Karl Schultze4dc75c2016-02-02 15:37:51 -07001411 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1412 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001413 assert(!err);
1414
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001415 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1417 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001418 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001419 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001420 };
1421
Karl Schultze4dc75c2016-02-02 15:37:51 -07001422 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001423 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425}
1426
Karl Schultze4dc75c2016-02-02 15:37:51 -07001427static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001428 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001429 [0] =
1430 {
1431 .format = demo->format,
1432 .samples = VK_SAMPLE_COUNT_1_BIT,
1433 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1434 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1435 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1436 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1437 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1438 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1439 },
1440 [1] =
1441 {
1442 .format = demo->depth.format,
1443 .samples = VK_SAMPLE_COUNT_1_BIT,
1444 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1445 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1446 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1447 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1448 .initialLayout =
1449 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1450 .finalLayout =
1451 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1452 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001453 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001454 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001455 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001456 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001457 const VkAttachmentReference depth_reference = {
1458 .attachment = 1,
1459 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1460 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001461 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001462 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1463 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001464 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001465 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001466 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001467 .pColorAttachments = &color_reference,
1468 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001469 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001470 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001471 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001472 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001473 const VkRenderPassCreateInfo rp_info = {
1474 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1475 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001476 .attachmentCount = 2,
1477 .pAttachments = attachments,
1478 .subpassCount = 1,
1479 .pSubpasses = &subpass,
1480 .dependencyCount = 0,
1481 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001482 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001483 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001484
Chia-I Wu63636062015-10-26 21:10:41 +08001485 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001486 assert(!err);
1487}
1488
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001489//TODO: Merge shader reading
1490#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001491static VkShaderModule
1492demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001493 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001494 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001495 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001496
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001497 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1498 moduleCreateInfo.pNext = NULL;
1499
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001500 moduleCreateInfo.codeSize = size;
1501 moduleCreateInfo.pCode = code;
1502 moduleCreateInfo.flags = 0;
1503 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1504 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001505
1506 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001507}
1508
Karl Schultze4dc75c2016-02-02 15:37:51 -07001509char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001510 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001511 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001512 void *shader_code;
1513
1514 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001515 if (!fp)
1516 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001517
1518 fseek(fp, 0L, SEEK_END);
1519 size = ftell(fp);
1520
1521 fseek(fp, 0L, SEEK_SET);
1522
1523 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001524 retval = fread(shader_code, size, 1, fp);
1525 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001526
1527 *psize = size;
1528
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001529 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001530 return shader_code;
1531}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001532#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001533
Karl Schultze4dc75c2016-02-02 15:37:51 -07001534static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001535#ifdef __ANDROID__
1536 VkShaderModuleCreateInfo sh_info = {};
1537 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1538
1539#include "cube.vert.h"
1540 sh_info.codeSize = sizeof(cube_vert);
1541 sh_info.pCode = cube_vert;
1542 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1543 assert(!err);
1544#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001545 void *vertShaderCode;
1546 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001547
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001548 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1549
Karl Schultze4dc75c2016-02-02 15:37:51 -07001550 demo->vert_shader_module =
1551 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001552
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001553 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001554#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001555
1556 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001557}
1558
Karl Schultze4dc75c2016-02-02 15:37:51 -07001559static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001560#ifdef __ANDROID__
1561 VkShaderModuleCreateInfo sh_info = {};
1562 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1563
1564#include "cube.frag.h"
1565 sh_info.codeSize = sizeof(cube_frag);
1566 sh_info.pCode = cube_frag;
1567 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1568 assert(!err);
1569#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001570 void *fragShaderCode;
1571 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001572
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001573 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001574
Karl Schultze4dc75c2016-02-02 15:37:51 -07001575 demo->frag_shader_module =
1576 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001577
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001578 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001579#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001580
1581 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582}
1583
Karl Schultze4dc75c2016-02-02 15:37:51 -07001584static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001585 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001586 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001587 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001588 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001589 VkPipelineRasterizationStateCreateInfo rs;
1590 VkPipelineColorBlendStateCreateInfo cb;
1591 VkPipelineDepthStencilStateCreateInfo ds;
1592 VkPipelineViewportStateCreateInfo vp;
1593 VkPipelineMultisampleStateCreateInfo ms;
1594 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1595 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001596 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597
Piers Daniellba839d12015-09-29 13:01:09 -06001598 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1599 memset(&dynamicState, 0, sizeof dynamicState);
1600 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1601 dynamicState.pDynamicStates = dynamicStateEnables;
1602
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001603 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001604 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001605 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001607 memset(&vi, 0, sizeof(vi));
1608 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1609
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001610 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001611 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001612 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001613
1614 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001615 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1616 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001617 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001618 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001619 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001620 rs.rasterizerDiscardEnable = VK_FALSE;
1621 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001622 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001623
1624 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001625 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1626 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001627 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001628 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001629 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001630 cb.attachmentCount = 1;
1631 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001632
Tony Barbour29645d02015-01-16 14:27:35 -07001633 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001634 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001635 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001636 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1637 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001638 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001639 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1640 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001641
1642 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001643 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001644 ds.depthTestEnable = VK_TRUE;
1645 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001646 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001647 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001648 ds.back.failOp = VK_STENCIL_OP_KEEP;
1649 ds.back.passOp = VK_STENCIL_OP_KEEP;
1650 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001651 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001652 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001653
Tony Barbour29645d02015-01-16 14:27:35 -07001654 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001655 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001656 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001657 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001658
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001659 // Two stages: vs and fs
1660 pipeline.stageCount = 2;
1661 VkPipelineShaderStageCreateInfo shaderStages[2];
1662 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1663
Karl Schultze4dc75c2016-02-02 15:37:51 -07001664 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1665 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001666 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001667 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001668
Karl Schultze4dc75c2016-02-02 15:37:51 -07001669 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1670 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001671 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001672 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001673
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001674 memset(&pipelineCache, 0, sizeof(pipelineCache));
1675 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676
Karl Schultze4dc75c2016-02-02 15:37:51 -07001677 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1678 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001679 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001680
Karl Schultze4dc75c2016-02-02 15:37:51 -07001681 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001682 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001683 pipeline.pRasterizationState = &rs;
1684 pipeline.pColorBlendState = &cb;
1685 pipeline.pMultisampleState = &ms;
1686 pipeline.pViewportState = &vp;
1687 pipeline.pDepthStencilState = &ds;
1688 pipeline.pStages = shaderStages;
1689 pipeline.renderPass = demo->render_pass;
1690 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001691
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001692 pipeline.renderPass = demo->render_pass;
1693
Karl Schultze4dc75c2016-02-02 15:37:51 -07001694 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1695 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001696 assert(!err);
1697
Chia-I Wu63636062015-10-26 21:10:41 +08001698 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1699 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700}
1701
Karl Schultze4dc75c2016-02-02 15:37:51 -07001702static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001703 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704 [0] =
1705 {
1706 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1707 .descriptorCount = 1,
1708 },
1709 [1] =
1710 {
1711 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1712 .descriptorCount = DEMO_TEXTURE_COUNT,
1713 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001714 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001715 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001716 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001717 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001718 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001719 .poolSizeCount = 2,
1720 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001721 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001722 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001723
Karl Schultze4dc75c2016-02-02 15:37:51 -07001724 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1725 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001726 assert(!err);
1727}
1728
Karl Schultze4dc75c2016-02-02 15:37:51 -07001729static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001730 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001731 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001732 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001733 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001734
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001735 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001736 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001737 .pNext = NULL,
1738 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001739 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001740 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001741 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001742 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001743
Chia-I Wuae721ba2015-05-25 16:27:55 +08001744 memset(&tex_descs, 0, sizeof(tex_descs));
1745 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001746 tex_descs[i].sampler = demo->textures[i].sampler;
1747 tex_descs[i].imageView = demo->textures[i].view;
1748 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001749 }
1750
1751 memset(&writes, 0, sizeof(writes));
1752
1753 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001754 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001755 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001756 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001757 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001758
1759 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001760 writes[1].dstSet = demo->desc_set;
1761 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001762 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001763 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001764 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001765
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001766 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001767}
1768
Karl Schultze4dc75c2016-02-02 15:37:51 -07001769static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001770 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001771 attachments[1] = demo->depth.view;
1772
Chia-I Wuf973e322015-07-08 13:34:24 +08001773 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001774 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1775 .pNext = NULL,
1776 .renderPass = demo->render_pass,
1777 .attachmentCount = 2,
1778 .pAttachments = attachments,
1779 .width = demo->width,
1780 .height = demo->height,
1781 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001782 };
1783 VkResult U_ASSERT_ONLY err;
1784 uint32_t i;
1785
Karl Schultze4dc75c2016-02-02 15:37:51 -07001786 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1787 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001788 assert(demo->framebuffers);
1789
1790 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001791 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001792 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1793 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001794 assert(!err);
1795 }
1796}
1797
Karl Schultze4dc75c2016-02-02 15:37:51 -07001798static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001799 VkResult U_ASSERT_ONLY err;
1800
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001801 const VkCommandPoolCreateInfo cmd_pool_info = {
1802 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001803 .pNext = NULL,
1804 .queueFamilyIndex = demo->graphics_queue_node_index,
1805 .flags = 0,
1806 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001807 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1808 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001809 assert(!err);
1810
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001811 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001812 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001813 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001814 .commandPool = demo->cmd_pool,
1815 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001816 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001818
1819 demo_prepare_buffers(demo);
1820 demo_prepare_depth(demo);
1821 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001822 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001823
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001824 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001825 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001826 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827
Ian Elliotta0781972015-08-21 15:09:33 -06001828 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001829 err =
1830 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001831 assert(!err);
1832 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001833
Chia-I Wu63ea9262015-03-26 13:14:16 +08001834 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001835 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001836
Chia-I Wuf973e322015-07-08 13:34:24 +08001837 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001838
Ian Elliotta0781972015-08-21 15:09:33 -06001839 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001840 demo->current_buffer = i;
1841 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1842 }
1843
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001844 /*
1845 * Prepare functions above may generate pipeline commands
1846 * that need to be flushed before beginning the render loop.
1847 */
1848 demo_flush_init_cmd(demo);
1849
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001850 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001851 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852}
1853
Karl Schultze4dc75c2016-02-02 15:37:51 -07001854static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001855 uint32_t i;
1856
1857 demo->prepared = false;
1858
Tony Barbourd8e504f2015-10-08 14:26:24 -06001859 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001860 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001861 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001862 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001863 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001864
Chia-I Wu63636062015-10-26 21:10:41 +08001865 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1866 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1867 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1868 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1869 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001870
1871 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001872 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1873 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1874 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1875 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001876 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001877 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001878
Chia-I Wu63636062015-10-26 21:10:41 +08001879 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1880 vkDestroyImage(demo->device, demo->depth.image, NULL);
1881 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001882
Chia-I Wu63636062015-10-26 21:10:41 +08001883 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1884 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001885
Ian Elliotta0781972015-08-21 15:09:33 -06001886 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001887 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001888 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1889 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001890 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001891 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001892
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001893 free(demo->queue_props);
1894
Chia-I Wu63636062015-10-26 21:10:41 +08001895 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1896 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001897 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001898 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001899 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001900 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001901 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001902
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001903#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001904 if (demo->use_xlib) {
1905 XDestroyWindow(demo->display, demo->xlib_window);
1906 XCloseDisplay(demo->display);
1907 } else {
1908 xcb_destroy_window(demo->connection, demo->xcb_window);
1909 xcb_disconnect(demo->connection);
1910 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001911 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001912#elif defined(VK_USE_PLATFORM_XCB_KHR)
1913 xcb_destroy_window(demo->connection, demo->window);
1914 xcb_disconnect(demo->connection);
1915 free(demo->atom_wm_delete_window);
1916#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001917}
1918
Karl Schultze4dc75c2016-02-02 15:37:51 -07001919static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001920 uint32_t i;
1921
Mike Stroyanbb60b382015-10-28 09:46:57 -06001922 // Don't react to resize until after first initialization.
1923 if (!demo->prepared) {
1924 return;
1925 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001926 // In order to properly resize the window, we must re-create the swapchain
1927 // AND redo the command buffers, etc.
1928 //
1929 // First, perform part of the demo_cleanup() function:
1930 demo->prepared = false;
1931
1932 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001933 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001934 }
1935 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001936 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001937
Chia-I Wu63636062015-10-26 21:10:41 +08001938 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1939 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1940 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1941 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1942 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001943
1944 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001945 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1946 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1947 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1948 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001949 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001950
Chia-I Wu63636062015-10-26 21:10:41 +08001951 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1952 vkDestroyImage(demo->device, demo->depth.image, NULL);
1953 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001954
Chia-I Wu63636062015-10-26 21:10:41 +08001955 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1956 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001957
1958 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001959 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001960 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1961 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001962 }
Chia-I Wu63636062015-10-26 21:10:41 +08001963 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001964 free(demo->buffers);
1965
Ian Elliottcf074d32015-10-16 18:02:43 -06001966 // Second, re-perform the demo_prepare() function, which will re-create the
1967 // swapchain:
1968 demo_prepare(demo);
1969}
1970
David Pinedo2bb7c932015-06-18 17:03:14 -06001971// On MS-Windows, make this a global, so it's available to WndProc()
1972struct demo demo;
1973
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001974#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07001975static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001976 if (!demo->prepared)
1977 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001978 // Wait for work to finish before updating MVP.
1979 vkDeviceWaitIdle(demo->device);
1980 demo_update_data_buffer(demo);
1981
1982 demo_draw(demo);
1983
1984 // Wait for work to finish before updating MVP.
1985 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001986
David Pinedo2bb7c932015-06-18 17:03:14 -06001987 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001988 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06001989 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06001990 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001991}
Ian Elliott639ca472015-04-16 15:23:05 -06001992
1993// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001994LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1995 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001996 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06001997 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001998 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001999 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002000 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002001 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002002 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002003 // Resize the application to the new window size, except when
2004 // it was minimized. Vulkan doesn't support images or swapchains
2005 // with width=0 and height=0.
2006 if (wParam != SIZE_MINIMIZED) {
2007 demo.width = lParam & 0xffff;
2008 demo.height = lParam & 0xffff0000 >> 16;
2009 demo_resize(&demo);
2010 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002011 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002012 default:
2013 break;
2014 }
2015 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2016}
2017
Karl Schultze4dc75c2016-02-02 15:37:51 -07002018static void demo_create_window(struct demo *demo) {
2019 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002020
2021 // Initialize the window class structure:
2022 win_class.cbSize = sizeof(WNDCLASSEX);
2023 win_class.style = CS_HREDRAW | CS_VREDRAW;
2024 win_class.lpfnWndProc = WndProc;
2025 win_class.cbClsExtra = 0;
2026 win_class.cbWndExtra = 0;
2027 win_class.hInstance = demo->connection; // hInstance
2028 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2029 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2030 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2031 win_class.lpszMenuName = NULL;
2032 win_class.lpszClassName = demo->name;
2033 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2034 // Register window class:
2035 if (!RegisterClassEx(&win_class)) {
2036 // It didn't work, so try to give a useful error:
2037 printf("Unexpected error trying to start the application!\n");
2038 fflush(stdout);
2039 exit(1);
2040 }
2041 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002042 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002043 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002044 demo->window = CreateWindowEx(0,
2045 demo->name, // class name
2046 demo->name, // app name
2047 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002048 WS_VISIBLE | WS_SYSMENU,
2049 100, 100, // x/y coords
2050 wr.right - wr.left, // width
2051 wr.bottom - wr.top, // height
2052 NULL, // handle to parent
2053 NULL, // handle to menu
2054 demo->connection, // hInstance
2055 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002056 if (!demo->window) {
2057 // It didn't work, so try to give a useful error:
2058 printf("Cannot create a window in which to draw!\n");
2059 fflush(stdout);
2060 exit(1);
2061 }
2062}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002063<<<<<<< 0d3f96c9766e309ab81b36fc908cdfde2ac6915a
Tony Barbour2593b182016-04-19 10:57:58 -06002064#else
2065static void demo_create_xlib_window(struct demo *demo) {
2066
2067 demo->display = XOpenDisplay(NULL);
2068 long visualMask = VisualScreenMask;
2069 int numberOfVisuals;
2070 XVisualInfo vInfoTemplate;
2071 vInfoTemplate.screen = DefaultScreen(demo->display);
2072 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2073 &vInfoTemplate, &numberOfVisuals);
2074
2075 Colormap colormap = XCreateColormap(
2076 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2077 visualInfo->visual, AllocNone);
2078
2079 XSetWindowAttributes windowAttributes;
2080 windowAttributes.colormap = colormap;
2081 windowAttributes.background_pixel = 0xFFFFFFFF;
2082 windowAttributes.border_pixel = 0;
2083 windowAttributes.event_mask =
2084 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2085
2086 demo->xlib_window = XCreateWindow(
2087 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2088 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2089 visualInfo->visual,
2090 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2091
2092 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2093 XMapWindow(demo->display, demo->xlib_window);
2094 XFlush(demo->display);
2095 demo->xlib_wm_delete_window =
2096 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2097}
2098static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2099 switch(event->type) {
2100 case ClientMessage:
2101 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2102 demo->quit = true;
2103 break;
2104 case KeyPress:
2105 switch (event->xkey.keycode) {
2106 case 0x9: // Escape
2107 demo->quit = true;
2108 break;
2109 case 0x71: // left arrow key
2110 demo->spin_angle += demo->spin_increment;
2111 break;
2112 case 0x72: // right arrow key
2113 demo->spin_angle -= demo->spin_increment;
2114 break;
2115 case 0x41:
2116 demo->pause = !demo->pause;
2117 break;
2118 }
2119 break;
2120 case ConfigureNotify:
2121 if ((demo->width != event->xconfigure.width) ||
2122 (demo->height != event->xconfigure.height)) {
2123 demo->width = event->xconfigure.width;
2124 demo->height = event->xconfigure.height;
2125 demo_resize(demo);
2126 }
2127 break;
2128 default:
2129 break;
2130 }
2131
2132}
2133
2134static void demo_run_xlib(struct demo *demo) {
2135
2136 while (!demo->quit) {
2137 XEvent event;
2138
2139 if (demo->pause) {
2140 XNextEvent(demo->display, &event);
2141 demo_handle_xlib_event(demo, &event);
2142 } else {
2143 while (XPending(demo->display) > 0) {
2144 XNextEvent(demo->display, &event);
2145 demo_handle_xlib_event(demo, &event);
2146 }
2147 }
2148
2149 // Wait for work to finish before updating MVP.
2150 vkDeviceWaitIdle(demo->device);
2151 demo_update_data_buffer(demo);
2152
2153 demo_draw(demo);
2154
2155 // Wait for work to finish before updating MVP.
2156 vkDeviceWaitIdle(demo->device);
2157 demo->curFrame++;
2158 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2159 demo->quit = true;
2160 }
2161}
2162
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002163#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002164static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002165 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002166 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002167 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002168 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002169 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002170 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002171 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002172 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2173 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002174 demo->quit = true;
2175 }
2176 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002177 case XCB_KEY_RELEASE: {
2178 const xcb_key_release_event_t *key =
2179 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002180
Karl Schultze4dc75c2016-02-02 15:37:51 -07002181 switch (key->detail) {
2182 case 0x9: // Escape
2183 demo->quit = true;
2184 break;
2185 case 0x71: // left arrow key
2186 demo->spin_angle += demo->spin_increment;
2187 break;
2188 case 0x72: // right arrow key
2189 demo->spin_angle -= demo->spin_increment;
2190 break;
2191 case 0x41:
2192 demo->pause = !demo->pause;
2193 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002194 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002195 } break;
2196 case XCB_CONFIGURE_NOTIFY: {
2197 const xcb_configure_notify_event_t *cfg =
2198 (const xcb_configure_notify_event_t *)event;
2199 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002200 demo->width = cfg->width;
2201 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002202 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002203 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002204 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002205 default:
2206 break;
2207 }
2208}
2209
Tony Barbour2593b182016-04-19 10:57:58 -06002210static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002211 xcb_flush(demo->connection);
2212
2213 while (!demo->quit) {
2214 xcb_generic_event_t *event;
2215
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002216 if (demo->pause) {
2217 event = xcb_wait_for_event(demo->connection);
2218 } else {
2219 event = xcb_poll_for_event(demo->connection);
2220 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002221 if (event) {
Tony Barbour2593b182016-04-19 10:57:58 -06002222 demo_handle_xcb_event(demo, event);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002223 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002224 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002225
2226 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002227 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002228 demo_update_data_buffer(demo);
2229
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002230 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002231
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002232 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002233 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002234 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002235 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002236 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002237 }
2238}
2239
Tony Barbour2593b182016-04-19 10:57:58 -06002240static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002241 uint32_t value_mask, value_list[32];
2242
Tony Barbour2593b182016-04-19 10:57:58 -06002243 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002244
2245 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2246 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002247 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002248 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002249
Tony Barbour2593b182016-04-19 10:57:58 -06002250 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002251 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2252 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2253 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002254
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002255 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002256 xcb_intern_atom_cookie_t cookie =
2257 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2258 xcb_intern_atom_reply_t *reply =
2259 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002260
Karl Schultze4dc75c2016-02-02 15:37:51 -07002261 xcb_intern_atom_cookie_t cookie2 =
2262 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2263 demo->atom_wm_delete_window =
2264 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002265
Tony Barbour2593b182016-04-19 10:57:58 -06002266 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002267 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002268 &(*demo->atom_wm_delete_window).atom);
2269 free(reply);
2270
Tony Barbour2593b182016-04-19 10:57:58 -06002271 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002272
Karl Schultze4dc75c2016-02-02 15:37:51 -07002273 // Force the x/y coordinates to 100,100 results are identical in consecutive
2274 // runs
2275 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002276 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002277 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002278}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002279#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2280static void demo_run(struct demo *demo) {
2281 if (!demo->prepared)
2282 return;
2283
2284 // Wait for work to finish before updating MVP.
2285 vkDeviceWaitIdle(demo->device);
2286 demo_update_data_buffer(demo);
2287
2288 demo_draw(demo);
2289
2290 // Wait for work to finish before updating MVP.
2291 vkDeviceWaitIdle(demo->device);
2292
2293 demo->curFrame++;
2294}
2295#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002296
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002297/*
2298 * Return 1 (true) if all layer names specified in check_names
2299 * can be found in given layer properties.
2300 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002301static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002302 uint32_t layer_count,
2303 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002304 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002305 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002306 for (uint32_t j = 0; j < layer_count; j++) {
2307 if (!strcmp(check_names[i], layers[j].layerName)) {
2308 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002309 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002310 }
2311 }
2312 if (!found) {
2313 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2314 return 0;
2315 }
2316 }
2317 return 1;
2318}
2319
Karl Schultze4dc75c2016-02-02 15:37:51 -07002320static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002321 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002322 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002323 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002324 uint32_t device_validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002325 char **instance_validation_layers = NULL;
2326 demo->enabled_extension_count = 0;
2327 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002328
Karl Schultz7c50ad62016-04-01 12:07:03 -06002329 char *instance_validation_layers_alt1[] = {
2330 "VK_LAYER_LUNARG_standard_validation"
2331 };
2332
2333 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskiaaab5c02016-03-17 15:08:18 -06002334 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Karl Schultz635272d2016-03-07 17:54:07 -07002335 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis1398c712016-03-09 16:12:48 -07002336 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
Karl Schultz7c50ad62016-04-01 12:07:03 -06002337 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002338 };
2339
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002340 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002341 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002342 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002343
Karl Schultz7c50ad62016-04-01 12:07:03 -06002344 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002345 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002346
Karl Schultz7c50ad62016-04-01 12:07:03 -06002347 instance_validation_layers = instance_validation_layers_alt1;
2348 if (instance_layer_count > 0) {
2349 VkLayerProperties *instance_layers =
2350 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2351 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2352 instance_layers);
2353 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002354
Karl Schultz7c50ad62016-04-01 12:07:03 -06002355
2356 validation_found = demo_check_layers(
2357 ARRAY_SIZE(instance_validation_layers_alt1),
2358 instance_validation_layers, instance_layer_count,
2359 instance_layers);
2360 if (validation_found) {
2361 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
2362 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2363 device_validation_layer_count = 1;
2364 } else {
2365 // use alternative set of validation layers
2366 instance_validation_layers = instance_validation_layers_alt2;
2367 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2368 validation_found = demo_check_layers(
2369 ARRAY_SIZE(instance_validation_layers_alt2),
2370 instance_validation_layers, instance_layer_count,
2371 instance_layers);
2372 device_validation_layer_count =
2373 ARRAY_SIZE(instance_validation_layers_alt2);
2374 for (uint32_t i = 0; i < device_validation_layer_count; i++) {
2375 demo->device_validation_layers[i] =
2376 instance_validation_layers[i];
2377 }
2378 }
2379 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002380 }
Dustin Graves7c287352016-01-27 13:48:06 -07002381
Karl Schultz7c50ad62016-04-01 12:07:03 -06002382 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002383 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002384 "required validation layer.\n\n"
2385 "Please look at the Getting Started guide for additional "
2386 "information.\n",
2387 "vkCreateInstance Failure");
2388 }
Dustin Graves7c287352016-01-27 13:48:06 -07002389 }
2390
2391 /* Look for instance extensions */
2392 VkBool32 surfaceExtFound = 0;
2393 VkBool32 platformSurfaceExtFound = 0;
Tony Barbour2593b182016-04-19 10:57:58 -06002394 VkBool32 xlibSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002395 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002396
Karl Schultze4dc75c2016-02-02 15:37:51 -07002397 err = vkEnumerateInstanceExtensionProperties(
2398 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002399 assert(!err);
2400
Dustin Graves7c287352016-01-27 13:48:06 -07002401 if (instance_extension_count > 0) {
2402 VkExtensionProperties *instance_extensions =
2403 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2404 err = vkEnumerateInstanceExtensionProperties(
2405 NULL, &instance_extension_count, instance_extensions);
2406 assert(!err);
2407 for (uint32_t i = 0; i < instance_extension_count; i++) {
2408 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2409 instance_extensions[i].extensionName)) {
2410 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002411 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002412 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002413 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002414#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002415 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2416 instance_extensions[i].extensionName)) {
2417 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002418 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002419 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2420 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002421#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002422 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2423 instance_extensions[i].extensionName)) {
2424 platformSurfaceExtFound = 1;
2425 xlibSurfaceExtFound = 1;
2426 demo->extension_names[demo->enabled_extension_count++] =
2427 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2428 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002429#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002430 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2431 instance_extensions[i].extensionName)) {
2432 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002433 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002434 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2435 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002436#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2437 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2438 instance_extensions[i].extensionName)) {
2439 platformSurfaceExtFound = 1;
2440 demo->extension_names[demo->enabled_extension_count++] =
2441 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2442 }
2443#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002444 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2445 instance_extensions[i].extensionName)) {
2446 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002447 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002448 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2449 }
2450 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002451 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002452 }
Dustin Graves7c287352016-01-27 13:48:06 -07002453
2454 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002455 }
Dustin Graves7c287352016-01-27 13:48:06 -07002456
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002457 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002458 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2459 "the " VK_KHR_SURFACE_EXTENSION_NAME
2460 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002461 "Vulkan installable client driver (ICD) installed?\nPlease "
2462 "look at the Getting Started guide for additional "
2463 "information.\n",
2464 "vkCreateInstance Failure");
2465 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002466 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002467#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002468 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2469 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2470 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002471 "Vulkan installable client driver (ICD) installed?\nPlease "
2472 "look at the Getting Started guide for additional "
2473 "information.\n",
2474 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002475#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002476 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2477 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2478 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002479 "Vulkan installable client driver (ICD) installed?\nPlease "
2480 "look at the Getting Started guide for additional "
2481 "information.\n",
2482 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002483#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2484 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2485 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2486 " extension.\n\nDo you have a compatible "
2487 "Vulkan installable client driver (ICD) installed?\nPlease "
2488 "look at the Getting Started guide for additional "
2489 "information.\n",
2490 "vkCreateInstance Failure");
2491#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002492 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002493#if defined(VK_USE_PLATOFMR_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002494 if (demo->use_xlib && !xlibSurfaceExtFound) {
2495 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2496 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2497 " extension.\n\nDo you have a compatible "
2498 "Vulkan installable client driver (ICD) installed?\nPlease "
2499 "look at the Getting Started guide for additional "
2500 "information.\n",
2501 "vkCreateInstance Failure");
2502 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002503#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002504 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002505 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002506 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002507 .pApplicationName = APP_SHORT_NAME,
2508 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002509 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002510 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002511 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002512 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002513 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002514 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002515 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002516 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002517 .enabledLayerCount = demo->enabled_layer_count,
2518 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2519 .enabledExtensionCount = demo->enabled_extension_count,
2520 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002521 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002522
2523 /*
2524 * This is info for a temp callback to use during CreateInstance.
2525 * After the instance is created, we use the instance-based
2526 * function to register the final callback.
2527 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002528 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002529 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002530 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2531 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002532 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002533 dbgCreateInfo.pUserData = NULL;
2534 dbgCreateInfo.flags =
2535 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2536 inst_info.pNext = &dbgCreateInfo;
2537 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002538
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002539 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002540
Chia-I Wu63636062015-10-26 21:10:41 +08002541 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002542 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2543 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002544 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002545 "additional information.\n",
2546 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002547 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002548 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002549 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002550 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002551 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002552 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2553 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002554 "the Getting Started guide for additional information.\n",
2555 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002556 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002557
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002558 /* Make initial call to query gpu_count, then second call for gpu info*/
2559 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2560 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002561
2562 if (gpu_count > 0) {
2563 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2564 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2565 assert(!err);
2566 /* For cube demo we just grab the first physical device */
2567 demo->gpu = physical_devices[0];
2568 free(physical_devices);
2569 } else {
2570 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2571 "Do you have a compatible Vulkan installable client driver (ICD) "
2572 "installed?\nPlease look at the Getting Started guide for "
2573 "additional information.\n",
2574 "vkEnumeratePhysicalDevices Failure");
2575 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002576
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002577 /* Look for validation layers */
2578 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002579 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002580 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002581 err =
2582 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002583 assert(!err);
2584
Dustin Graves7c287352016-01-27 13:48:06 -07002585 if (device_layer_count > 0) {
2586 VkLayerProperties *device_layers =
2587 malloc(sizeof(VkLayerProperties) * device_layer_count);
2588 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2589 device_layers);
2590 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002591
Dustin Graves7c287352016-01-27 13:48:06 -07002592 if (demo->validate) {
2593 validation_found = demo_check_layers(device_validation_layer_count,
2594 demo->device_validation_layers,
2595 device_layer_count,
2596 device_layers);
2597 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002598 }
Dustin Graves7c287352016-01-27 13:48:06 -07002599
2600 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002601 }
2602
Dustin Graves7c287352016-01-27 13:48:06 -07002603 if (demo->validate && !validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002604 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find "
Dustin Graves7c287352016-01-27 13:48:06 -07002605 "a required validation layer.\n\n"
2606 "Please look at the Getting Started guide for additional "
2607 "information.\n",
2608 "vkCreateDevice Failure");
2609 }
2610
2611 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002612 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002613 VkBool32 swapchainExtFound = 0;
2614 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002615 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002616
Karl Schultze4dc75c2016-02-02 15:37:51 -07002617 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2618 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002619 assert(!err);
2620
Dustin Graves7c287352016-01-27 13:48:06 -07002621 if (device_extension_count > 0) {
2622 VkExtensionProperties *device_extensions =
2623 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2624 err = vkEnumerateDeviceExtensionProperties(
2625 demo->gpu, NULL, &device_extension_count, device_extensions);
2626 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002627
Dustin Graves7c287352016-01-27 13:48:06 -07002628 for (uint32_t i = 0; i < device_extension_count; i++) {
2629 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2630 device_extensions[i].extensionName)) {
2631 swapchainExtFound = 1;
2632 demo->extension_names[demo->enabled_extension_count++] =
2633 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2634 }
2635 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002636 }
Dustin Graves7c287352016-01-27 13:48:06 -07002637
2638 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002639 }
Dustin Graves7c287352016-01-27 13:48:06 -07002640
Tony Barbourcc69f452015-10-08 13:59:42 -06002641 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002642 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2643 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2644 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002645 "Vulkan installable client driver (ICD) installed?\nPlease "
2646 "look at the Getting Started guide for additional "
2647 "information.\n",
2648 "vkCreateInstance Failure");
2649 }
2650
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002651 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002652 demo->CreateDebugReportCallback =
2653 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2654 demo->inst, "vkCreateDebugReportCallbackEXT");
2655 demo->DestroyDebugReportCallback =
2656 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2657 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002658 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002659 ERR_EXIT(
2660 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2661 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002662 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002663 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002664 ERR_EXIT(
2665 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2666 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002667 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002668 demo->DebugReportMessage =
2669 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2670 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002671 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002672 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002673 "vkGetProcAddr Failure");
2674 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002675
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002676 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002677 PFN_vkDebugReportCallbackEXT callback;
2678 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002679 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002680 dbgCreateInfo.pNext = NULL;
2681 dbgCreateInfo.pfnCallback = callback;
2682 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002683 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002684 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002685 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2686 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002687 switch (err) {
2688 case VK_SUCCESS:
2689 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002690 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002691 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2692 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002693 break;
2694 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002695 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2696 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002697 break;
2698 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002699 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002700 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002701
2702 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002703 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2704 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002705 assert(demo->queue_count >= 1);
2706
Karl Schultze4dc75c2016-02-02 15:37:51 -07002707 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2708 demo->queue_count * sizeof(VkQueueFamilyProperties));
2709 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2710 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002711 // Find a queue that supports gfx
2712 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002713 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2714 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002715 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2716 break;
2717 }
2718 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002719 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002720 // If app has specific feature requirements it should check supported
2721 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002722 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002723 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002724
Tony Barbourda2bad52016-01-22 14:36:40 -07002725 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2726 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2727 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2728 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2729 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2730}
2731
Karl Schultze4dc75c2016-02-02 15:37:51 -07002732static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002733 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002734 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002735 const VkDeviceQueueCreateInfo queue = {
2736 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2737 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002738 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002739 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002740 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002741
2742 VkDeviceCreateInfo device = {
2743 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2744 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002745 .queueCreateInfoCount = 1,
2746 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002747 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002748 .ppEnabledLayerNames =
2749 (const char *const *)((demo->validate)
2750 ? demo->device_validation_layers
2751 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002752 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002753 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2754 .pEnabledFeatures =
2755 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002756 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002757
Chia-I Wu63636062015-10-26 21:10:41 +08002758 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002759 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002760}
2761
Karl Schultze4dc75c2016-02-02 15:37:51 -07002762static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002763 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002764 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002765
Karl Schultze4dc75c2016-02-02 15:37:51 -07002766// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002767#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002768 VkWin32SurfaceCreateInfoKHR createInfo;
2769 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2770 createInfo.pNext = NULL;
2771 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002772 createInfo.hinstance = demo->connection;
2773 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002774
Karl Schultze4dc75c2016-02-02 15:37:51 -07002775 err =
2776 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002777#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2778 VkAndroidSurfaceCreateInfoKHR createInfo;
2779 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2780 createInfo.pNext = NULL;
2781 createInfo.flags = 0;
2782 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002783
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002784 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2785#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002786 if (demo->use_xlib) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002787#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002788 VkXlibSurfaceCreateInfoKHR createInfo;
2789 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2790 createInfo.pNext = NULL;
2791 createInfo.flags = 0;
2792 createInfo.dpy = demo->display;
2793 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002794
Tony Barbour2593b182016-04-19 10:57:58 -06002795 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2796 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002797#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002798 }
2799 else {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002800#if defined(VK_USE_PLATOFORM_X11_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002801 VkXcbSurfaceCreateInfoKHR createInfo;
2802 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2803 createInfo.pNext = NULL;
2804 createInfo.flags = 0;
2805 createInfo.connection = demo->connection;
2806 createInfo.window = demo->xcb_window;
2807
2808 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002809#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002810 }
Tony Barbour2593b182016-04-19 10:57:58 -06002811 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002812
Tony Barbourcc69f452015-10-08 13:59:42 -06002813 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002814 VkBool32 *supportsPresent =
2815 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002816 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002817 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002818 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002819 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002820
2821 // Search for a graphics and a present queue in the array of queue
2822 // families, try to find one that supports both
2823 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002824 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002825 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002826 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2827 if (graphicsQueueNodeIndex == UINT32_MAX) {
2828 graphicsQueueNodeIndex = i;
2829 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002830
Ian Elliottaaae5352015-07-06 14:27:58 -06002831 if (supportsPresent[i] == VK_TRUE) {
2832 graphicsQueueNodeIndex = i;
2833 presentQueueNodeIndex = i;
2834 break;
2835 }
2836 }
2837 }
2838 if (presentQueueNodeIndex == UINT32_MAX) {
2839 // If didn't find a queue that supports both graphics and present, then
2840 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002841 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002842 if (supportsPresent[i] == VK_TRUE) {
2843 presentQueueNodeIndex = i;
2844 break;
2845 }
2846 }
2847 }
2848 free(supportsPresent);
2849
2850 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002851 if (graphicsQueueNodeIndex == UINT32_MAX ||
2852 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002853 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002854 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002855 }
2856
2857 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002858 // synchronization, and appropriate tracking for QueueSubmit.
2859 // NOTE: While it is possible for an application to use a separate graphics
2860 // and a present queues, this demo program assumes it is only using
2861 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002862 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2863 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002864 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002865 }
2866
2867 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002868
Tony Barbourda2bad52016-01-22 14:36:40 -07002869 demo_create_device(demo);
2870
2871 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2872 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2873 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2874 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2875 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2876
Karl Schultze4dc75c2016-02-02 15:37:51 -07002877 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2878 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002879
Ian Elliottaaae5352015-07-06 14:27:58 -06002880 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002881 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002882 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002883 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002884 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002885 VkSurfaceFormatKHR *surfFormats =
2886 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002887 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002888 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002889 assert(!err);
2890 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2891 // the surface has no preferred format. Otherwise, at least one
2892 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002893 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002894 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002895 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002896 assert(formatCount >= 1);
2897 demo->format = surfFormats[0].format;
2898 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002899 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002900
David Pinedo2bb7c932015-06-18 17:03:14 -06002901 demo->quit = false;
2902 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002903
2904 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002905 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002906}
2907
Karl Schultze4dc75c2016-02-02 15:37:51 -07002908static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002909#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002910 const xcb_setup_t *setup;
2911 xcb_screen_iterator_t iter;
2912 int scr;
2913
2914 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002915 if (demo->connection == NULL) {
2916 printf("Cannot find a compatible Vulkan installable client driver "
2917 "(ICD).\nExiting ...\n");
2918 fflush(stdout);
2919 exit(1);
2920 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002921
2922 setup = xcb_get_setup(demo->connection);
2923 iter = xcb_setup_roots_iterator(setup);
2924 while (scr-- > 0)
2925 xcb_screen_next(&iter);
2926
2927 demo->screen = iter.data;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002928#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002929}
2930
Karl Schultze4dc75c2016-02-02 15:37:51 -07002931static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002932 vec3 eye = {0.0f, 3.0f, 5.0f};
2933 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002934 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002935
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002936 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002937 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002938
Piers Daniell735ee532015-02-23 16:23:13 -07002939 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002940 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002941 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002942 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002943 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002944 if (strcmp(argv[i], "--break") == 0) {
2945 demo->use_break = true;
2946 continue;
2947 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002948 if (strcmp(argv[i], "--validate") == 0) {
2949 demo->validate = true;
2950 continue;
2951 }
Tony Barbour2593b182016-04-19 10:57:58 -06002952 if (strcmp(argv[i], "--xlib") == 0) {
2953 demo->use_xlib = true;
2954 continue;
2955 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002956 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2957 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2958 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002959 i++;
2960 continue;
2961 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002962
Karl Schultze4dc75c2016-02-02 15:37:51 -07002963 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2964 "[--c <framecount>]\n",
2965 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002966 fflush(stderr);
2967 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002968 }
2969
Tony Barbour2593b182016-04-19 10:57:58 -06002970 if (!demo->use_xlib)
2971 demo_init_connection(demo);
2972
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002973 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002974
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002975 demo->width = 500;
2976 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002977
2978 demo->spin_angle = 0.01f;
2979 demo->spin_increment = 0.01f;
2980 demo->pause = false;
2981
Karl Schultze4dc75c2016-02-02 15:37:51 -07002982 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2983 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002984 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2985 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002986}
2987
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002988#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07002989// Include header required for parsing the command line options.
2990#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002991
Karl Schultze4dc75c2016-02-02 15:37:51 -07002992int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
2993 int nCmdShow) {
2994 MSG msg; // message
2995 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002996 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002997 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06002998
Karl Schultze4dc75c2016-02-02 15:37:51 -07002999 // Use the CommandLine functions to get the command line arguments.
3000 // Unfortunately, Microsoft outputs
3001 // this information as wide characters for Unicode, and we simply want the
3002 // Ascii version to be compatible
3003 // with the non-Windows side. So, we have to convert the information to
3004 // Ascii character strings.
3005 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3006 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003007 argc = 0;
3008 }
3009
Karl Schultze4dc75c2016-02-02 15:37:51 -07003010 if (argc > 0) {
3011 argv = (char **)malloc(sizeof(char *) * argc);
3012 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003013 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003014 } else {
3015 for (int iii = 0; iii < argc; iii++) {
3016 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003017 size_t numConverted = 0;
3018
Karl Schultze4dc75c2016-02-02 15:37:51 -07003019 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3020 if (argv[iii] != NULL) {
3021 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3022 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003023 }
3024 }
3025 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003026 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003027 argv = NULL;
3028 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003029
3030 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003031
3032 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003033 if (argc > 0 && argv != NULL) {
3034 for (int iii = 0; iii < argc; iii++) {
3035 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003036 free(argv[iii]);
3037 }
3038 }
3039 free(argv);
3040 }
3041
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003042 demo.connection = hInstance;
3043 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003044 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003045 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003046
3047 demo_prepare(&demo);
3048
Karl Schultze4dc75c2016-02-02 15:37:51 -07003049 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003050
3051 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003052 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003053 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003054 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003055 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003056 done = true; // if found, quit app
3057 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003058 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003059 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003060 DispatchMessage(&msg);
3061 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003062 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003063 }
3064
3065 demo_cleanup(&demo);
3066
Karl Schultze4dc75c2016-02-02 15:37:51 -07003067 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003068}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003069#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3070#include <android/log.h>
3071#include <android_native_app_glue.h>
3072static bool initialized = false;
3073static bool active = false;
3074struct demo demo;
3075
3076static int32_t processInput(struct android_app* app, AInputEvent* event) {
3077 return 0;
3078}
3079
3080static void processCommand(struct android_app* app, int32_t cmd) {
3081 switch(cmd) {
3082 case APP_CMD_INIT_WINDOW: {
3083 if (app->window) {
3084 demo_init(&demo, 0, NULL);
3085 demo.window = (void*)app->window;
3086 demo_init_vk_swapchain(&demo);
3087 demo_prepare(&demo);
3088 initialized = true;
3089 }
3090 break;
3091 }
3092 case APP_CMD_GAINED_FOCUS: {
3093 active = true;
3094 break;
3095 }
3096 case APP_CMD_LOST_FOCUS: {
3097 active = false;
3098 break;
3099 }
3100 }
3101}
3102
3103void android_main(struct android_app *app)
3104{
3105 app_dummy();
3106
3107 app->onAppCmd = processCommand;
3108 app->onInputEvent = processInput;
3109
3110 while(1) {
3111 int events;
3112 struct android_poll_source* source;
3113 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3114 if (source) {
3115 source->process(app, source);
3116 }
3117
3118 if (app->destroyRequested != 0) {
3119 demo_cleanup(&demo);
3120 return;
3121 }
3122 }
3123 if (initialized && active) {
3124 demo_run(&demo);
3125 }
3126 }
3127
3128}
3129#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003130int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003131 struct demo demo;
3132
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003133 demo_init(&demo, argc, argv);
Tony Barbour2593b182016-04-19 10:57:58 -06003134 if (demo.use_xlib)
3135 demo_create_xlib_window(&demo);
3136 else
3137 demo_create_xcb_window(&demo);
3138
Tony Barbourcc69f452015-10-08 13:59:42 -06003139 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003140
3141 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003142
3143 if (demo.use_xlib)
3144 demo_run_xlib(&demo);
3145 else
3146 demo_run_xcb(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003147
3148 demo_cleanup(&demo);
3149
Karl Schultz0218c002016-03-22 17:06:13 -06003150 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003151}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003152#endif