blob: 082ed61350a582aeb21a0ea260fc7be52ec1fb56 [file] [log] [blame]
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001 /*
Karl Schultze4dc75c2016-02-02 15:37:51 -07002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
Ian Elliotta748eaf2015-04-28 15:50:36 -06005 *
Jon Ashburn7b44e362016-04-19 11:30:31 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Ian Elliotta748eaf2015-04-28 15:50:36 -06009 *
Jon Ashburn7b44e362016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Ian Elliotta748eaf2015-04-28 15:50:36 -060011 *
Jon Ashburn7b44e362016-04-19 11:30:31 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060017 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060022 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070023
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070030#include <signal.h>
Tony Barbour2593b182016-04-19 10:57:58 -060031#ifdef __linux__
32#include <X11/Xutil.h>
33#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060034
Ian Elliott639ca472015-04-16 15:23:05 -060035#ifdef _WIN32
36#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060037#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060038#endif // _WIN32
39
Cody Northrop8707a6e2016-04-26 19:59:19 -060040#ifdef ANDROID
41#include "vulkan_wrapper.h"
42#else
David Pinedoc5193c12015-11-06 12:54:48 -070043#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060044#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070045
David Pinedo541526f2015-11-24 09:00:24 -070046#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070062#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060067
Michael Lentinec6cde4b2016-04-18 13:20:45 -050068#elif defined __ANDROID__
69#include <android/log.h>
70#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
73 exit(1); \
74 } while (0)
75#else
Karl Schultze4dc75c2016-02-02 15:37:51 -070076#define ERR_EXIT(err_msg, err_class) \
77 do { \
78 printf(err_msg); \
79 fflush(stdout); \
80 exit(1); \
81 } while (0)
Michael Lentinec6cde4b2016-04-18 13:20:45 -050082#endif
Ian Elliott07264132015-04-28 11:35:02 -060083
Karl Schultze4dc75c2016-02-02 15:37:51 -070084#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
85 { \
86 demo->fp##entrypoint = \
87 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
88 if (demo->fp##entrypoint == NULL) { \
89 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
90 "vkGetInstanceProcAddr Failure"); \
91 } \
92 }
Ian Elliottaaae5352015-07-06 14:27:58 -060093
Jon Ashburn7d8342c2015-10-08 15:58:23 -060094static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
95
Karl Schultze4dc75c2016-02-02 15:37:51 -070096#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
97 { \
98 if (!g_gdpa) \
99 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
100 demo->inst, "vkGetDeviceProcAddr"); \
101 demo->fp##entrypoint = \
102 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
103 if (demo->fp##entrypoint == NULL) { \
104 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
105 "vkGetDeviceProcAddr Failure"); \
106 } \
107 }
Ian Elliott673898b2015-06-22 15:07:49 -0600108
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600109/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700110 * structure to track all objects related to a texture.
111 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600112struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600113 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700114
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600115 VkImage image;
116 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600117
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800118 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500119 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600120 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700121 int32_t tex_width, tex_height;
122};
123
Karl Schultze4dc75c2016-02-02 15:37:51 -0700124static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600125
Karl Schultz0218c002016-03-22 17:06:13 -0600126static int validation_error = 0;
127
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600128struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700130 float mvp[4][4];
131 float position[12 * 3][4];
132 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600133};
134
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600135struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600136 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700137 float mvp[4][4];
138 float position[12 * 3][4];
139 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600140};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600141
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600142//--------------------------------------------------------------------------------------
143// Mesh and VertexFormat Data
144//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700145// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146struct Vertex
147{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600148 float posX, posY, posZ, posW; // Position data
149 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150};
151
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600152struct VertexPosTex
153{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600154 float posX, posY, posZ, posW; // Position data
155 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600156};
157
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600158#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600159#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600160
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600161static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 -1.0f,-1.0f, 1.0f,
164 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 -1.0f, 1.0f,-1.0f,
167 -1.0f,-1.0f,-1.0f,
168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f, 1.0f,-1.0f,
171 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600178 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600181 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 -1.0f, 1.0f, 1.0f,
185 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600188 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 1.0f, 1.0f, 1.0f,
192 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600194 1.0f,-1.0f,-1.0f,
195 1.0f, 1.0f,-1.0f,
196
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600199 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800200 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600201 1.0f,-1.0f, 1.0f,
202 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600203};
204
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600205static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 0.0f, 0.0f, // -X side
207 1.0f, 0.0f,
208 1.0f, 1.0f,
209 1.0f, 1.0f,
210 0.0f, 1.0f,
211 0.0f, 0.0f,
212
213 1.0f, 0.0f, // -Z side
214 0.0f, 1.0f,
215 0.0f, 0.0f,
216 1.0f, 0.0f,
217 1.0f, 1.0f,
218 0.0f, 1.0f,
219
220 1.0f, 1.0f, // -Y side
221 1.0f, 0.0f,
222 0.0f, 0.0f,
223 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 0.0f, 0.0f,
225 0.0f, 1.0f,
226
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600228 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800229 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 1.0f, // +X side
235 0.0f, 1.0f,
236 0.0f, 0.0f,
237 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600238 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600239 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600240
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800241 0.0f, 1.0f, // +Z side
242 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600243 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600244 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800245 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600246 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249
Karl Schultze4dc75c2016-02-02 15:37:51 -0700250void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600251 int i;
252
253 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700254 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600255 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
256 }
257 printf("\n");
258 fflush(stdout);
259}
260
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600262 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700263 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600264 printf("\n");
265 fflush(stdout);
266}
267
Karl Schultze4dc75c2016-02-02 15:37:51 -0700268VKAPI_ATTR VkBool32 VKAPI_CALL
269dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
270 uint64_t srcObject, size_t location, int32_t msgCode,
271 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
272 char *message = (char *)malloc(strlen(pMsg) + 100);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600273
Karl Schultze4dc75c2016-02-02 15:37:51 -0700274 assert(message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600275
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700276 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700277 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
278 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600279 validation_error = 1;
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -0700280 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700281 // We know that we're submitting queues without fences, ignore this
282 // warning
283 if (strstr(pMsg,
284 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600285 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600286 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700287 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
288 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600289 validation_error = 1;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600290 } else {
Karl Schultz0218c002016-03-22 17:06:13 -0600291 validation_error = 1;
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600292 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600293 }
294
295#ifdef _WIN32
296 MessageBox(NULL, message, "Alert", MB_OK);
297#else
Karl Schultze4dc75c2016-02-02 15:37:51 -0700298 printf("%s\n", message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600299 fflush(stdout);
300#endif
301 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600302
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600303 /*
304 * false indicates that layer should not bail-out of an
305 * API call that had validation failures. This may mean that the
306 * app dies inside the driver due to invalid parameter(s).
307 * That's what would happen without validation layers, so we'll
308 * keep that behavior here.
309 */
310 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600311}
312
Karl Schultz0c3c1512016-03-25 15:35:18 -0600313VKAPI_ATTR VkBool32 VKAPI_CALL
314BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
315 uint64_t srcObject, size_t location, int32_t msgCode,
316 const char *pLayerPrefix, const char *pMsg,
317 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700318#ifndef WIN32
319 raise(SIGTRAP);
320#else
321 DebugBreak();
322#endif
323
324 return false;
325}
326
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600327typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600328 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800329 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600330 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600331} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600332
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500334#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600335#define APP_NAME_STR_LEN 80
336 HINSTANCE connection; // hInstance - Windows Instance
337 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700338 HWND window; // hWnd - window handle
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600339#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -0600340 Display* display;
341 Window xlib_window;
342 Atom xlib_wm_delete_window;
Tobin Ehlis43ed2982016-04-28 10:17:33 -0600343
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344 xcb_connection_t *connection;
345 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600346 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800347 xcb_intern_atom_reply_t *atom_wm_delete_window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500348#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
349 ANativeWindow* window;
350#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700351 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600352 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700353 bool use_staging_buffer;
Tony Barbour2593b182016-04-19 10:57:58 -0600354 bool use_xlib;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600357 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkDevice device;
359 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700360 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600361 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600362 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600363 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
Tony Barbourda2bad52016-01-22 14:36:40 -0700365 uint32_t enabled_extension_count;
366 uint32_t enabled_layer_count;
367 char *extension_names[64];
368 char *device_validation_layers[64];
369
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600371 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600372 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Karl Schultze4dc75c2016-02-02 15:37:51 -0700374 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
375 fpGetPhysicalDeviceSurfaceSupportKHR;
376 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
377 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
378 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
379 fpGetPhysicalDeviceSurfaceFormatsKHR;
380 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
381 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600382 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
383 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
384 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
385 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
386 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600387 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600388 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600389 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800391 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600392
393 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600396 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800397 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500398 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600399 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400 } depth;
401
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403
404 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800406 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500407 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600408 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600409 } uniform_data;
410
Karl Schultze4dc75c2016-02-02 15:37:51 -0700411 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500412 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600413 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600414 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800415 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600416 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600417
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600418 mat4x4 projection_matrix;
419 mat4x4 view_matrix;
420 mat4x4 model_matrix;
421
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600422 float spin_angle;
423 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600424 bool pause;
425
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600426 VkShaderModule vert_shader_module;
427 VkShaderModule frag_shader_module;
428
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429 VkDescriptorPool desc_pool;
430 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800431
Tony Barbourd8e504f2015-10-08 14:26:24 -0600432 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800433
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600434 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600435 int32_t curFrame;
436 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600437 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600438 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700439 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
440 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
441 VkDebugReportCallbackEXT msg_callback;
442 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600443
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600444 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600445 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600446};
447
Ian Elliottcf074d32015-10-16 18:02:43 -0600448// Forward declaration:
449static void demo_resize(struct demo *demo);
450
Karl Schultze4dc75c2016-02-02 15:37:51 -0700451static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
452 VkFlags requirements_mask,
453 uint32_t *typeIndex) {
454 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600455 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700456 if ((typeBits & 1) == 1) {
457 // Type is available, does it match user properties?
458 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
459 requirements_mask) == requirements_mask) {
460 *typeIndex = i;
461 return true;
462 }
463 }
464 typeBits >>= 1;
465 }
466 // No memory types matched, return failure
467 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600468}
469
Karl Schultze4dc75c2016-02-02 15:37:51 -0700470static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600471 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600473 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474 return;
475
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 assert(!err);
478
Karl Schultze4dc75c2016-02-02 15:37:51 -0700479 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800480 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700481 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
482 .pNext = NULL,
483 .waitSemaphoreCount = 0,
484 .pWaitSemaphores = NULL,
485 .pWaitDstStageMask = NULL,
486 .commandBufferCount = 1,
487 .pCommandBuffers = cmd_bufs,
488 .signalSemaphoreCount = 0,
489 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600490
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600491 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 assert(!err);
493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 assert(!err);
496
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600497 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600498 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499}
500
Karl Schultze4dc75c2016-02-02 15:37:51 -0700501static void demo_set_image_layout(struct demo *demo, VkImage image,
502 VkImageAspectFlags aspectMask,
503 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700504 VkImageLayout new_image_layout,
505 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600506 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600507
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600508 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800509 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800510 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800512 .commandPool = demo->cmd_pool,
513 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700514 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515 };
516
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800517 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518 assert(!err);
519
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700520 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
521 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600522 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800523 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600524 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800525 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800526 .occlusionQueryEnable = VK_FALSE,
527 .queryFlags = 0,
528 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700530 VkCommandBufferBeginInfo cmd_buf_info = {
531 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
532 .pNext = NULL,
533 .flags = 0,
534 .pInheritanceInfo = &cmd_buf_hinfo,
535 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600536 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600537 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600538 }
539
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600540 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600542 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700543 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800544 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600545 .oldLayout = old_image_layout,
546 .newLayout = new_image_layout,
547 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700548 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800550 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800552 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553 }
554
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700555 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700556 image_memory_barrier.dstAccessMask =
557 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700558 }
559
560 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700561 image_memory_barrier.dstAccessMask =
562 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700563 }
564
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600565 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600566 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700567 image_memory_barrier.dstAccessMask =
568 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600569 }
570
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600571 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600572
Tony Barbour50bbca42015-06-29 16:20:35 -0600573 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
574 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600575
Karl Schultze4dc75c2016-02-02 15:37:51 -0700576 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
577 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600578}
579
Karl Schultze4dc75c2016-02-02 15:37:51 -0700580static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700581 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
582 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600583 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800584 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600585 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800586 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800587 .occlusionQueryEnable = VK_FALSE,
588 .queryFlags = 0,
589 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700590 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700591 const VkCommandBufferBeginInfo cmd_buf_info = {
592 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
593 .pNext = NULL,
594 .flags = 0,
595 .pInheritanceInfo = &cmd_buf_hinfo,
596 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800597 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700598 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
599 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800600 };
601 const VkRenderPassBeginInfo rp_begin = {
602 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
603 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800604 .renderPass = demo->render_pass,
605 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800606 .renderArea.offset.x = 0,
607 .renderArea.offset.y = 0,
608 .renderArea.extent.width = demo->width,
609 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600610 .clearValueCount = 2,
611 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700612 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800613 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600615 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 assert(!err);
617
Tony Barbour5c5b1632016-04-26 11:13:48 -0600618 // We can use LAYOUT_UNDEFINED as a wildcard here because we don't care what
619 // happens to the previous contents of the image
620 VkImageMemoryBarrier image_memory_barrier = {
621 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
622 .pNext = NULL,
623 .srcAccessMask = 0,
624 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
625 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
626 .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
627 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
628 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
629 .image = demo->buffers[demo->current_buffer].image,
630 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
631
632 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
633 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
634 NULL, 1, &image_memory_barrier);
635
Chia-I Wuf051d262015-10-27 19:25:11 +0800636 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800637
Karl Schultze4dc75c2016-02-02 15:37:51 -0700638 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
639 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
640 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
641 NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600642 VkViewport viewport;
643 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700644 viewport.height = (float)demo->height;
645 viewport.width = (float)demo->width;
646 viewport.minDepth = (float)0.0f;
647 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700648 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600649
650 VkRect2D scissor;
651 memset(&scissor, 0, sizeof(scissor));
652 scissor.extent.width = demo->width;
653 scissor.extent.height = demo->height;
654 scissor.offset.x = 0;
655 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700656 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600657 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800658 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659
Tony Barbour15a4ef62015-10-21 10:14:48 -0600660 VkImageMemoryBarrier prePresentBarrier = {
661 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
662 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800663 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700664 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600665 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700666 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600667 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800668 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700669 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600670
671 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
672 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700673 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
674 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
675 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600676
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600677 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600678 assert(!err);
679}
680
Karl Schultze4dc75c2016-02-02 15:37:51 -0700681void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600682 mat4x4 MVP, Model, VP;
683 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600684 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600685 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600687 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600689 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600690 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
692 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600693 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600694
Karl Schultze4dc75c2016-02-02 15:37:51 -0700695 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
696 demo->uniform_data.mem_alloc.allocationSize, 0,
697 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600698 assert(!err);
699
Karl Schultze4dc75c2016-02-02 15:37:51 -0700700 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600702 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600703}
704
Karl Schultze4dc75c2016-02-02 15:37:51 -0700705static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600706 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600707 VkSemaphore presentCompleteSemaphore;
708 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
709 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
710 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600711 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800713 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600714
Karl Schultze4dc75c2016-02-02 15:37:51 -0700715 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
716 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 assert(!err);
718
719 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700720 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700722 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600723 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600724 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
725 // demo->swapchain is out of date (e.g. the window was resized) and
726 // must be recreated:
727 demo_resize(demo);
728 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800729 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600730 return;
731 } else if (err == VK_SUBOPTIMAL_KHR) {
732 // demo->swapchain is not as optimal as it could be, but the platform's
733 // presentation engine will still present the image correctly.
734 } else {
735 assert(!err);
736 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600737
Tony Barbour15a4ef62015-10-21 10:14:48 -0600738 demo_flush_init_cmd(demo);
739
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600740 // Wait for the present complete semaphore to be signaled to ensure
741 // that the image won't be rendered to until the presentation
742 // engine has fully released ownership to the application, and it is
743 // okay to render to the image.
744
Karl Schultze4dc75c2016-02-02 15:37:51 -0700745 VkPipelineStageFlags pipe_stage_flags =
746 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
747 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
748 .pNext = NULL,
749 .waitSemaphoreCount = 1,
750 .pWaitSemaphores = &presentCompleteSemaphore,
751 .pWaitDstStageMask = &pipe_stage_flags,
752 .commandBufferCount = 1,
753 .pCommandBuffers =
754 &demo->buffers[demo->current_buffer].cmd,
755 .signalSemaphoreCount = 0,
756 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600757
758 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600759 assert(!err);
760
Ian Elliotta0781972015-08-21 15:09:33 -0600761 VkPresentInfoKHR present = {
762 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600763 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600764 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700765 .pSwapchains = &demo->swapchain,
766 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600767 };
768
Karl Schultze4dc75c2016-02-02 15:37:51 -0700769 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600770 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600771 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
772 // demo->swapchain is out of date (e.g. the window was resized) and
773 // must be recreated:
774 demo_resize(demo);
775 } else if (err == VK_SUBOPTIMAL_KHR) {
776 // demo->swapchain is not as optimal as it could be, but the platform's
777 // presentation engine will still present the image correctly.
778 } else {
779 assert(!err);
780 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781
Chia-I Wucbb564e2015-04-16 22:02:10 +0800782 err = vkQueueWaitIdle(demo->queue);
783 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600784
Chia-I Wu63636062015-10-26 21:10:41 +0800785 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600786}
787
Karl Schultze4dc75c2016-02-02 15:37:51 -0700788static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600790 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600791
Ian Elliottb08e0d92015-11-20 11:55:46 -0700792 // Check the surface capabilities and formats
793 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700794 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
795 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600796 assert(!err);
797
Ian Elliott8528eff2015-08-07 15:56:59 -0600798 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700799 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
800 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600801 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600802 VkPresentModeKHR *presentModes =
803 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600804 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700805 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
806 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 assert(!err);
808
Ian Elliotta0781972015-08-21 15:09:33 -0600809 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600810 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700811 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600812 // If the surface size is undefined, the size is set to
813 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600814 swapchainExtent.width = demo->width;
815 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700816 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600817 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700818 swapchainExtent = surfCapabilities.currentExtent;
819 demo->width = surfCapabilities.currentExtent.width;
820 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600821 }
822
823 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600824 // tearing mode. If not, try IMMEDIATE which will usually be available,
825 // and is fastest (though it tears). If not, fall back to FIFO which is
826 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600827 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600828 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600829 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
830 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600831 break;
832 }
Ian Elliotta0781972015-08-21 15:09:33 -0600833 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
834 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
835 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600836 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600837 }
838
839 // Determine the number of VkImage's to use in the swap chain (we desire to
840 // own only 1 image at a time, besides the images being displayed and
841 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700842 uint32_t desiredNumberOfSwapchainImages =
843 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700844 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700845 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600846 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700847 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600848 }
849
Tony Barbour9fd6d352015-09-16 15:01:32 -0600850 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700851 if (surfCapabilities.supportedTransforms &
852 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700853 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600854 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700855 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600856 }
857
Ian Elliottcf074d32015-10-16 18:02:43 -0600858 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600859 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800860 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700861 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600862 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800863 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600864 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700865 .imageExtent =
866 {
867 .width = swapchainExtent.width, .height = swapchainExtent.height,
868 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700869 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600870 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700871 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700872 .imageArrayLayers = 1,
873 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
874 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600875 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600876 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600877 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600878 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600879 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600880 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881
Karl Schultze4dc75c2016-02-02 15:37:51 -0700882 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
883 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800884 assert(!err);
885
Ian Elliottcf074d32015-10-16 18:02:43 -0600886 // If we just re-created an existing swapchain, we should destroy the old
887 // swapchain at this point.
888 // Note: destroying the swapchain also cleans up all its associated
889 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800890 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700891 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600892 }
893
894 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600895 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600896 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800897
Karl Schultze4dc75c2016-02-02 15:37:51 -0700898 VkImage *swapchainImages =
899 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600900 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600901 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600902 &demo->swapchainImageCount,
903 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600904 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600905
Karl Schultze4dc75c2016-02-02 15:37:51 -0700906 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
907 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600908 assert(demo->buffers);
909
Ian Elliotta0781972015-08-21 15:09:33 -0600910 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600911 VkImageViewCreateInfo color_image_view = {
912 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600913 .pNext = NULL,
914 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700915 .components =
916 {
917 .r = VK_COMPONENT_SWIZZLE_R,
918 .g = VK_COMPONENT_SWIZZLE_G,
919 .b = VK_COMPONENT_SWIZZLE_B,
920 .a = VK_COMPONENT_SWIZZLE_A,
921 },
922 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
923 .baseMipLevel = 0,
924 .levelCount = 1,
925 .baseArrayLayer = 0,
926 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600927 .viewType = VK_IMAGE_VIEW_TYPE_2D,
928 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600929 };
930
Ian Elliotta0781972015-08-21 15:09:33 -0600931 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600932
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600933 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600934
Karl Schultze4dc75c2016-02-02 15:37:51 -0700935 err = vkCreateImageView(demo->device, &color_image_view, NULL,
936 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600937 assert(!err);
938 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700939
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500940
Mark Young04fd3d82016-01-25 14:43:50 -0700941 if (NULL != presentModes) {
942 free(presentModes);
943 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944}
945
Karl Schultze4dc75c2016-02-02 15:37:51 -0700946static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600947 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600948 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600949 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600951 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600952 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700953 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600955 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800956 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600957 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600958 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959 .flags = 0,
960 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200961
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600962 VkImageViewCreateInfo view = {
963 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600964 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800965 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600966 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700967 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
968 .baseMipLevel = 0,
969 .levelCount = 1,
970 .baseArrayLayer = 0,
971 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600973 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600974 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600975
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500976 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600977 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600978 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600979
980 demo->depth.format = depth_format;
981
982 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700983 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984 assert(!err);
985
Karl Schultze4dc75c2016-02-02 15:37:51 -0700986 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600987 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600988
Chia-I Wuf48700b2015-11-10 16:21:09 +0800989 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200990 demo->depth.mem_alloc.pNext = NULL;
991 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
992 demo->depth.mem_alloc.memoryTypeIndex = 0;
993
Karl Schultze4dc75c2016-02-02 15:37:51 -0700994 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
995 0, /* No requirements */
996 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600997 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500999 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001000 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1001 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001002 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001003
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001004 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001005 err =
1006 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001007 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001008
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
1010 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001011 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1012 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001013
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014 /* create image view */
1015 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001016 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001017 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018}
1019
Tony Barbour1a86d032015-09-21 15:17:33 -06001020/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001022 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001023#ifdef __ANDROID__
1024#include <lunarg.ppm.h>
1025 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001026 cPtr = (char*)lunarg_ppm;
1027 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001028 return false;
1029 }
1030 while(strncmp(cPtr++, "\n", 1));
1031 sscanf(cPtr, "%u %u", height, width);
1032 if (rgba_data == NULL) {
1033 return true;
1034 }
1035 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001036 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001037 return false;
1038 }
1039 while(strncmp(cPtr++, "\n", 1));
1040
1041 for (int y = 0; y < *height; y++) {
1042 uint8_t *rowPtr = rgba_data;
1043 for (int x = 0; x < *width; x++) {
1044 memcpy(rowPtr, cPtr, 3);
1045 rowPtr[3] = 255; /* Alpha of 1 */
1046 rowPtr += 4;
1047 cPtr += 3;
1048 }
1049 rgba_data += layout->rowPitch;
1050 }
1051
1052 return true;
1053#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001054 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001055 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001056
Tony Barbour1a86d032015-09-21 15:17:33 -06001057 if (!fPtr)
1058 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001059
Tony Barbour1a86d032015-09-21 15:17:33 -06001060 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001061 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1062 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001063 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001064 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001065
Tony Barbour1a86d032015-09-21 15:17:33 -06001066 do {
1067 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001068 if (cPtr == NULL) {
1069 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001070 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001071 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001072 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001073
Tony Barbour1a86d032015-09-21 15:17:33 -06001074 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001075 if (rgba_data == NULL) {
1076 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001077 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001078 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001079 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001080 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001081 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1082 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001083 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001084 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001085
Karl Schultze4dc75c2016-02-02 15:37:51 -07001086 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001087 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001088 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001089 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001090 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001091 rowPtr[3] = 255; /* Alpha of 1 */
1092 rowPtr += 4;
1093 }
1094 rgba_data += layout->rowPitch;
1095 }
1096 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001097 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001098#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001099}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001100
Karl Schultze4dc75c2016-02-02 15:37:51 -07001101static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001102 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001103 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001104 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001105 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001106 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001107 int32_t tex_width;
1108 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001109 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001110 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111
Karl Schultze4dc75c2016-02-02 15:37:51 -07001112 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001113 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001114 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001116 tex_obj->tex_width = tex_width;
1117 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001119 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001120 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001122 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001124 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001126 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001127 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001128 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001129 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001130 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001131 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001134 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001135
Karl Schultze4dc75c2016-02-02 15:37:51 -07001136 err =
1137 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138 assert(!err);
1139
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001140 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001141
Chia-I Wuf48700b2015-11-10 16:21:09 +08001142 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001143 tex_obj->mem_alloc.pNext = NULL;
1144 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1145 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001146
Karl Schultze4dc75c2016-02-02 15:37:51 -07001147 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1148 required_props,
1149 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001150 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001151
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001152 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001153 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001154 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001155 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001156
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001157 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001158 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001159 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001161 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001162 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001163 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001165 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001167 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168 void *data;
1169
Karl Schultze4dc75c2016-02-02 15:37:51 -07001170 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1171 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172
Karl Schultze4dc75c2016-02-02 15:37:51 -07001173 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1174 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001175 assert(!err);
1176
1177 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1178 fprintf(stderr, "Error loading texture: %s\n", filename);
1179 }
1180
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001181 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001183
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001184 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001185 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001186 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1187 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001188 /* setting the image layout does not reference the actual memory so no need
1189 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190}
1191
Karl Schultze4dc75c2016-02-02 15:37:51 -07001192static void demo_destroy_texture_image(struct demo *demo,
1193 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001194 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001195 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1196 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001197}
1198
Karl Schultze4dc75c2016-02-02 15:37:51 -07001199static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001200 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001201 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001202 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001204 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001205
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001207 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208
Karl Schultze4dc75c2016-02-02 15:37:51 -07001209 if ((props.linearTilingFeatures &
1210 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1211 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001212 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001213 demo_prepare_texture_image(
1214 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1215 VK_IMAGE_USAGE_SAMPLED_BIT,
1216 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1217 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001218 } else if (props.optimalTilingFeatures &
1219 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001220 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001221 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001222
1223 memset(&staging_texture, 0, sizeof(staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001224 demo_prepare_texture_image(
1225 demo, tex_files[i], &staging_texture, VK_IMAGE_TILING_LINEAR,
1226 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1227 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1228 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001229
Karl Schultze4dc75c2016-02-02 15:37:51 -07001230 demo_prepare_texture_image(
1231 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1232 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1233 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001235 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001236 VK_IMAGE_ASPECT_COLOR_BIT,
1237 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001238 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1239 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001241 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001242 VK_IMAGE_ASPECT_COLOR_BIT,
1243 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001244 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1245 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001246
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001247 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001248 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1249 .srcOffset = {0, 0, 0},
1250 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1251 .dstOffset = {0, 0, 0},
1252 .extent = {staging_texture.tex_width,
1253 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001254 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001255 vkCmdCopyImage(
1256 demo->cmd, staging_texture.image,
1257 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1258 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001259
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001260 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001261 VK_IMAGE_ASPECT_COLOR_BIT,
1262 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001263 demo->textures[i].imageLayout,
1264 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001265
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001266 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001267
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001268 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001269 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001270 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1271 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001272 }
1273
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001274 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001275 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001277 .magFilter = VK_FILTER_NEAREST,
1278 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001279 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001280 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1281 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1282 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001283 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001284 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001285 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001286 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001287 .minLod = 0.0f,
1288 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001289 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001290 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001291 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001292
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001293 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001294 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001295 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001296 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001297 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001298 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001299 .components =
1300 {
1301 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1302 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1303 },
1304 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001305 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001307
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001308 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001309 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001310 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311 assert(!err);
1312
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313 /* create image view */
1314 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001315 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001316 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317 assert(!err);
1318 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001319}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001320
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001322 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001323 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001324 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001325 int i;
1326 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001327 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001328 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001329 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001330
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001331 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001332 mat4x4_mul(MVP, VP, demo->model_matrix);
1333 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001334 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001335
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 for (i = 0; i < 12 * 3; i++) {
1337 data.position[i][0] = g_vertex_buffer_data[i * 3];
1338 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1339 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001340 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001341 data.attr[i][0] = g_uv_buffer_data[2 * i];
1342 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001343 data.attr[i][2] = 0;
1344 data.attr[i][3] = 0;
1345 }
1346
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001347 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001348 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001349 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001350 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001351 err =
1352 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001353 assert(!err);
1354
Karl Schultze4dc75c2016-02-02 15:37:51 -07001355 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1356 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001357
Chia-I Wuf48700b2015-11-10 16:21:09 +08001358 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001359 demo->uniform_data.mem_alloc.pNext = NULL;
1360 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1361 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1362
Karl Schultze4dc75c2016-02-02 15:37:51 -07001363 pass = memory_type_from_properties(
Tony Barbour5342ef52016-04-28 15:05:09 -06001364 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1365 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001367 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001368
Karl Schultze4dc75c2016-02-02 15:37:51 -07001369 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1370 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001371 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001372
Karl Schultze4dc75c2016-02-02 15:37:51 -07001373 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1374 demo->uniform_data.mem_alloc.allocationSize, 0,
1375 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001376 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001377
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001378 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001379
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001380 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001381
Karl Schultze4dc75c2016-02-02 15:37:51 -07001382 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1383 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001384 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001385
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001386 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1387 demo->uniform_data.buffer_info.offset = 0;
1388 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001389}
1390
Karl Schultze4dc75c2016-02-02 15:37:51 -07001391static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001392 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001393 [0] =
1394 {
1395 .binding = 0,
1396 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1397 .descriptorCount = 1,
1398 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1399 .pImmutableSamplers = NULL,
1400 },
1401 [1] =
1402 {
1403 .binding = 1,
1404 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1405 .descriptorCount = DEMO_TEXTURE_COUNT,
1406 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1407 .pImmutableSamplers = NULL,
1408 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001409 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001410 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001411 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001412 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001413 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001414 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001415 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001416 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417
Karl Schultze4dc75c2016-02-02 15:37:51 -07001418 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1419 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001420 assert(!err);
1421
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001422 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001423 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1424 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001425 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001426 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001427 };
1428
Karl Schultze4dc75c2016-02-02 15:37:51 -07001429 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001430 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001431 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432}
1433
Karl Schultze4dc75c2016-02-02 15:37:51 -07001434static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001435 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001436 [0] =
1437 {
1438 .format = demo->format,
1439 .samples = VK_SAMPLE_COUNT_1_BIT,
1440 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1441 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1442 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1443 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1444 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1445 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1446 },
1447 [1] =
1448 {
1449 .format = demo->depth.format,
1450 .samples = VK_SAMPLE_COUNT_1_BIT,
1451 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1452 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1453 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1454 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1455 .initialLayout =
1456 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1457 .finalLayout =
1458 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1459 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001460 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001461 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001462 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001463 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001464 const VkAttachmentReference depth_reference = {
1465 .attachment = 1,
1466 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1467 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001468 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001469 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1470 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001471 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001472 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001473 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001474 .pColorAttachments = &color_reference,
1475 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001476 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001477 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001478 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001479 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001480 const VkRenderPassCreateInfo rp_info = {
1481 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1482 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001483 .attachmentCount = 2,
1484 .pAttachments = attachments,
1485 .subpassCount = 1,
1486 .pSubpasses = &subpass,
1487 .dependencyCount = 0,
1488 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001489 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001490 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001491
Chia-I Wu63636062015-10-26 21:10:41 +08001492 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001493 assert(!err);
1494}
1495
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001496//TODO: Merge shader reading
1497#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001498static VkShaderModule
1499demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001500 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001501 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001502 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001503
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001504 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1505 moduleCreateInfo.pNext = NULL;
1506
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001507 moduleCreateInfo.codeSize = size;
1508 moduleCreateInfo.pCode = code;
1509 moduleCreateInfo.flags = 0;
1510 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1511 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001512
1513 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001514}
1515
Karl Schultze4dc75c2016-02-02 15:37:51 -07001516char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001517 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001518 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001519 void *shader_code;
1520
1521 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001522 if (!fp)
1523 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001524
1525 fseek(fp, 0L, SEEK_END);
1526 size = ftell(fp);
1527
1528 fseek(fp, 0L, SEEK_SET);
1529
1530 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001531 retval = fread(shader_code, size, 1, fp);
1532 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001533
1534 *psize = size;
1535
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001536 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001537 return shader_code;
1538}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001539#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001540
Karl Schultze4dc75c2016-02-02 15:37:51 -07001541static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001542#ifdef __ANDROID__
1543 VkShaderModuleCreateInfo sh_info = {};
1544 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1545
1546#include "cube.vert.h"
1547 sh_info.codeSize = sizeof(cube_vert);
1548 sh_info.pCode = cube_vert;
1549 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1550 assert(!err);
1551#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001552 void *vertShaderCode;
1553 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001554
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001555 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1556
Karl Schultze4dc75c2016-02-02 15:37:51 -07001557 demo->vert_shader_module =
1558 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001559
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001560 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001561#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001562
1563 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001564}
1565
Karl Schultze4dc75c2016-02-02 15:37:51 -07001566static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001567#ifdef __ANDROID__
1568 VkShaderModuleCreateInfo sh_info = {};
1569 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1570
1571#include "cube.frag.h"
1572 sh_info.codeSize = sizeof(cube_frag);
1573 sh_info.pCode = cube_frag;
1574 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1575 assert(!err);
1576#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001577 void *fragShaderCode;
1578 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001579
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001580 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001581
Karl Schultze4dc75c2016-02-02 15:37:51 -07001582 demo->frag_shader_module =
1583 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001584
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001585 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001586#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001587
1588 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001589}
1590
Karl Schultze4dc75c2016-02-02 15:37:51 -07001591static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001592 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001593 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001594 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001595 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001596 VkPipelineRasterizationStateCreateInfo rs;
1597 VkPipelineColorBlendStateCreateInfo cb;
1598 VkPipelineDepthStencilStateCreateInfo ds;
1599 VkPipelineViewportStateCreateInfo vp;
1600 VkPipelineMultisampleStateCreateInfo ms;
1601 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1602 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001603 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001604
Piers Daniellba839d12015-09-29 13:01:09 -06001605 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1606 memset(&dynamicState, 0, sizeof dynamicState);
1607 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1608 dynamicState.pDynamicStates = dynamicStateEnables;
1609
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001610 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001611 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001612 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001613
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001614 memset(&vi, 0, sizeof(vi));
1615 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1616
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001618 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001619 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001620
1621 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001622 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1623 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001624 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001625 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001626 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001627 rs.rasterizerDiscardEnable = VK_FALSE;
1628 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001629 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630
1631 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001632 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1633 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001634 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001635 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001636 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001637 cb.attachmentCount = 1;
1638 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639
Tony Barbour29645d02015-01-16 14:27:35 -07001640 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001641 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001642 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001643 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1644 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001645 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001646 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1647 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001648
1649 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001650 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001651 ds.depthTestEnable = VK_TRUE;
1652 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001653 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001654 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001655 ds.back.failOp = VK_STENCIL_OP_KEEP;
1656 ds.back.passOp = VK_STENCIL_OP_KEEP;
1657 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001658 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001659 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001660
Tony Barbour29645d02015-01-16 14:27:35 -07001661 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001662 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001663 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001664 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001665
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001666 // Two stages: vs and fs
1667 pipeline.stageCount = 2;
1668 VkPipelineShaderStageCreateInfo shaderStages[2];
1669 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1670
Karl Schultze4dc75c2016-02-02 15:37:51 -07001671 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1672 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001673 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001674 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001675
Karl Schultze4dc75c2016-02-02 15:37:51 -07001676 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1677 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001678 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001679 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001680
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001681 memset(&pipelineCache, 0, sizeof(pipelineCache));
1682 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001683
Karl Schultze4dc75c2016-02-02 15:37:51 -07001684 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1685 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001686 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001687
Karl Schultze4dc75c2016-02-02 15:37:51 -07001688 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001689 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001690 pipeline.pRasterizationState = &rs;
1691 pipeline.pColorBlendState = &cb;
1692 pipeline.pMultisampleState = &ms;
1693 pipeline.pViewportState = &vp;
1694 pipeline.pDepthStencilState = &ds;
1695 pipeline.pStages = shaderStages;
1696 pipeline.renderPass = demo->render_pass;
1697 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001698
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001699 pipeline.renderPass = demo->render_pass;
1700
Karl Schultze4dc75c2016-02-02 15:37:51 -07001701 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1702 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703 assert(!err);
1704
Chia-I Wu63636062015-10-26 21:10:41 +08001705 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1706 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707}
1708
Karl Schultze4dc75c2016-02-02 15:37:51 -07001709static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001710 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001711 [0] =
1712 {
1713 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1714 .descriptorCount = 1,
1715 },
1716 [1] =
1717 {
1718 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1719 .descriptorCount = DEMO_TEXTURE_COUNT,
1720 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001721 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001722 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001723 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001724 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001725 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001726 .poolSizeCount = 2,
1727 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001728 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001729 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001730
Karl Schultze4dc75c2016-02-02 15:37:51 -07001731 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1732 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001733 assert(!err);
1734}
1735
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001737 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001738 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001739 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001740 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001741
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001742 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001743 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001744 .pNext = NULL,
1745 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001746 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001747 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001748 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001749 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001750
Chia-I Wuae721ba2015-05-25 16:27:55 +08001751 memset(&tex_descs, 0, sizeof(tex_descs));
1752 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001753 tex_descs[i].sampler = demo->textures[i].sampler;
1754 tex_descs[i].imageView = demo->textures[i].view;
1755 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001756 }
1757
1758 memset(&writes, 0, sizeof(writes));
1759
1760 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001761 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001762 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001763 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001764 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001765
1766 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001767 writes[1].dstSet = demo->desc_set;
1768 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001769 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001770 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001771 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001772
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001773 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001774}
1775
Karl Schultze4dc75c2016-02-02 15:37:51 -07001776static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001777 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001778 attachments[1] = demo->depth.view;
1779
Chia-I Wuf973e322015-07-08 13:34:24 +08001780 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001781 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1782 .pNext = NULL,
1783 .renderPass = demo->render_pass,
1784 .attachmentCount = 2,
1785 .pAttachments = attachments,
1786 .width = demo->width,
1787 .height = demo->height,
1788 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001789 };
1790 VkResult U_ASSERT_ONLY err;
1791 uint32_t i;
1792
Karl Schultze4dc75c2016-02-02 15:37:51 -07001793 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1794 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001795 assert(demo->framebuffers);
1796
1797 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001798 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001799 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1800 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001801 assert(!err);
1802 }
1803}
1804
Karl Schultze4dc75c2016-02-02 15:37:51 -07001805static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001806 VkResult U_ASSERT_ONLY err;
1807
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001808 const VkCommandPoolCreateInfo cmd_pool_info = {
1809 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001810 .pNext = NULL,
1811 .queueFamilyIndex = demo->graphics_queue_node_index,
1812 .flags = 0,
1813 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001814 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1815 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001816 assert(!err);
1817
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001818 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001819 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001821 .commandPool = demo->cmd_pool,
1822 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001823 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001824 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001825
1826 demo_prepare_buffers(demo);
1827 demo_prepare_depth(demo);
1828 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001829 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001831 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001832 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001833 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001834
Ian Elliotta0781972015-08-21 15:09:33 -06001835 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001836 err =
1837 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001838 assert(!err);
1839 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001840
Chia-I Wu63ea9262015-03-26 13:14:16 +08001841 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001842 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001843
Chia-I Wuf973e322015-07-08 13:34:24 +08001844 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001845
Ian Elliotta0781972015-08-21 15:09:33 -06001846 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001847 demo->current_buffer = i;
1848 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1849 }
1850
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001851 /*
1852 * Prepare functions above may generate pipeline commands
1853 * that need to be flushed before beginning the render loop.
1854 */
1855 demo_flush_init_cmd(demo);
1856
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001857 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001858 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001859}
1860
Karl Schultze4dc75c2016-02-02 15:37:51 -07001861static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001862 uint32_t i;
1863
1864 demo->prepared = false;
1865
Tony Barbourd8e504f2015-10-08 14:26:24 -06001866 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001867 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001868 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001869 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001870 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001871
Chia-I Wu63636062015-10-26 21:10:41 +08001872 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1873 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1874 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1875 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1876 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001877
1878 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001879 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1880 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1881 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1882 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001883 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001884 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001885
Chia-I Wu63636062015-10-26 21:10:41 +08001886 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1887 vkDestroyImage(demo->device, demo->depth.image, NULL);
1888 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001889
Chia-I Wu63636062015-10-26 21:10:41 +08001890 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1891 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001892
Ian Elliotta0781972015-08-21 15:09:33 -06001893 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001894 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001895 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1896 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001897 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001898 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001899
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001900 free(demo->queue_props);
1901
Chia-I Wu63636062015-10-26 21:10:41 +08001902 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1903 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001904 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001905 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001906 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001907 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001908 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001909
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001910#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06001911 if (demo->use_xlib) {
1912 XDestroyWindow(demo->display, demo->xlib_window);
1913 XCloseDisplay(demo->display);
1914 } else {
1915 xcb_destroy_window(demo->connection, demo->xcb_window);
1916 xcb_disconnect(demo->connection);
1917 }
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001918 free(demo->atom_wm_delete_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001919#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06001920 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001921 xcb_disconnect(demo->connection);
1922 free(demo->atom_wm_delete_window);
1923#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06001924}
1925
Karl Schultze4dc75c2016-02-02 15:37:51 -07001926static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001927 uint32_t i;
1928
Mike Stroyanbb60b382015-10-28 09:46:57 -06001929 // Don't react to resize until after first initialization.
1930 if (!demo->prepared) {
1931 return;
1932 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001933 // In order to properly resize the window, we must re-create the swapchain
1934 // AND redo the command buffers, etc.
1935 //
1936 // First, perform part of the demo_cleanup() function:
1937 demo->prepared = false;
1938
1939 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001940 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001941 }
1942 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001943 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001944
Chia-I Wu63636062015-10-26 21:10:41 +08001945 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1946 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1947 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1948 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1949 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001950
1951 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001952 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1953 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1954 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1955 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001956 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001957
Chia-I Wu63636062015-10-26 21:10:41 +08001958 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1959 vkDestroyImage(demo->device, demo->depth.image, NULL);
1960 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001961
Chia-I Wu63636062015-10-26 21:10:41 +08001962 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1963 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001964
1965 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001966 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001967 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1968 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001969 }
Chia-I Wu63636062015-10-26 21:10:41 +08001970 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001971 free(demo->buffers);
1972
Ian Elliottcf074d32015-10-16 18:02:43 -06001973 // Second, re-perform the demo_prepare() function, which will re-create the
1974 // swapchain:
1975 demo_prepare(demo);
1976}
1977
David Pinedo2bb7c932015-06-18 17:03:14 -06001978// On MS-Windows, make this a global, so it's available to WndProc()
1979struct demo demo;
1980
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001981#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07001982static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001983 if (!demo->prepared)
1984 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001985 // Wait for work to finish before updating MVP.
1986 vkDeviceWaitIdle(demo->device);
1987 demo_update_data_buffer(demo);
1988
1989 demo_draw(demo);
1990
1991 // Wait for work to finish before updating MVP.
1992 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001993
David Pinedo2bb7c932015-06-18 17:03:14 -06001994 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001995 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06001996 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06001997 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001998}
Ian Elliott639ca472015-04-16 15:23:05 -06001999
2000// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002001LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2002 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002003 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002004 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002005 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002006 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002007 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06002008 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002009 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002010 // Resize the application to the new window size, except when
2011 // it was minimized. Vulkan doesn't support images or swapchains
2012 // with width=0 and height=0.
2013 if (wParam != SIZE_MINIMIZED) {
2014 demo.width = lParam & 0xffff;
2015 demo.height = lParam & 0xffff0000 >> 16;
2016 demo_resize(&demo);
2017 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002018 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002019 default:
2020 break;
2021 }
2022 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2023}
2024
Karl Schultze4dc75c2016-02-02 15:37:51 -07002025static void demo_create_window(struct demo *demo) {
2026 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002027
2028 // Initialize the window class structure:
2029 win_class.cbSize = sizeof(WNDCLASSEX);
2030 win_class.style = CS_HREDRAW | CS_VREDRAW;
2031 win_class.lpfnWndProc = WndProc;
2032 win_class.cbClsExtra = 0;
2033 win_class.cbWndExtra = 0;
2034 win_class.hInstance = demo->connection; // hInstance
2035 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2036 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2037 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2038 win_class.lpszMenuName = NULL;
2039 win_class.lpszClassName = demo->name;
2040 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2041 // Register window class:
2042 if (!RegisterClassEx(&win_class)) {
2043 // It didn't work, so try to give a useful error:
2044 printf("Unexpected error trying to start the application!\n");
2045 fflush(stdout);
2046 exit(1);
2047 }
2048 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002049 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002050 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002051 demo->window = CreateWindowEx(0,
2052 demo->name, // class name
2053 demo->name, // app name
2054 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002055 WS_VISIBLE | WS_SYSMENU,
2056 100, 100, // x/y coords
2057 wr.right - wr.left, // width
2058 wr.bottom - wr.top, // height
2059 NULL, // handle to parent
2060 NULL, // handle to menu
2061 demo->connection, // hInstance
2062 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002063 if (!demo->window) {
2064 // It didn't work, so try to give a useful error:
2065 printf("Cannot create a window in which to draw!\n");
2066 fflush(stdout);
2067 exit(1);
2068 }
2069}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002070#elif defined(VK_USE_PLATFORM_XLIB_KHR) | defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002071static void demo_create_xlib_window(struct demo *demo) {
2072
2073 demo->display = XOpenDisplay(NULL);
2074 long visualMask = VisualScreenMask;
2075 int numberOfVisuals;
2076 XVisualInfo vInfoTemplate;
2077 vInfoTemplate.screen = DefaultScreen(demo->display);
2078 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2079 &vInfoTemplate, &numberOfVisuals);
2080
2081 Colormap colormap = XCreateColormap(
2082 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2083 visualInfo->visual, AllocNone);
2084
2085 XSetWindowAttributes windowAttributes;
2086 windowAttributes.colormap = colormap;
2087 windowAttributes.background_pixel = 0xFFFFFFFF;
2088 windowAttributes.border_pixel = 0;
2089 windowAttributes.event_mask =
2090 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2091
2092 demo->xlib_window = XCreateWindow(
2093 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2094 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2095 visualInfo->visual,
2096 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2097
2098 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2099 XMapWindow(demo->display, demo->xlib_window);
2100 XFlush(demo->display);
2101 demo->xlib_wm_delete_window =
2102 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2103}
2104static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2105 switch(event->type) {
2106 case ClientMessage:
2107 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2108 demo->quit = true;
2109 break;
2110 case KeyPress:
2111 switch (event->xkey.keycode) {
2112 case 0x9: // Escape
2113 demo->quit = true;
2114 break;
2115 case 0x71: // left arrow key
2116 demo->spin_angle += demo->spin_increment;
2117 break;
2118 case 0x72: // right arrow key
2119 demo->spin_angle -= demo->spin_increment;
2120 break;
2121 case 0x41:
2122 demo->pause = !demo->pause;
2123 break;
2124 }
2125 break;
2126 case ConfigureNotify:
2127 if ((demo->width != event->xconfigure.width) ||
2128 (demo->height != event->xconfigure.height)) {
2129 demo->width = event->xconfigure.width;
2130 demo->height = event->xconfigure.height;
2131 demo_resize(demo);
2132 }
2133 break;
2134 default:
2135 break;
2136 }
2137
2138}
2139
2140static void demo_run_xlib(struct demo *demo) {
2141
2142 while (!demo->quit) {
2143 XEvent event;
2144
2145 if (demo->pause) {
2146 XNextEvent(demo->display, &event);
2147 demo_handle_xlib_event(demo, &event);
2148 } else {
2149 while (XPending(demo->display) > 0) {
2150 XNextEvent(demo->display, &event);
2151 demo_handle_xlib_event(demo, &event);
2152 }
2153 }
2154
2155 // Wait for work to finish before updating MVP.
2156 vkDeviceWaitIdle(demo->device);
2157 demo_update_data_buffer(demo);
2158
2159 demo_draw(demo);
2160
2161 // Wait for work to finish before updating MVP.
2162 vkDeviceWaitIdle(demo->device);
2163 demo->curFrame++;
2164 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2165 demo->quit = true;
2166 }
2167}
2168
2169static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002170 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002171 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002172 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002173 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002174 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002175 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002176 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002177 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2178 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002179 demo->quit = true;
2180 }
2181 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002182 case XCB_KEY_RELEASE: {
2183 const xcb_key_release_event_t *key =
2184 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002185
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 switch (key->detail) {
2187 case 0x9: // Escape
2188 demo->quit = true;
2189 break;
2190 case 0x71: // left arrow key
2191 demo->spin_angle += demo->spin_increment;
2192 break;
2193 case 0x72: // right arrow key
2194 demo->spin_angle -= demo->spin_increment;
2195 break;
2196 case 0x41:
2197 demo->pause = !demo->pause;
2198 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002199 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002200 } break;
2201 case XCB_CONFIGURE_NOTIFY: {
2202 const xcb_configure_notify_event_t *cfg =
2203 (const xcb_configure_notify_event_t *)event;
2204 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002205 demo->width = cfg->width;
2206 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002207 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002208 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002209 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002210 default:
2211 break;
2212 }
2213}
2214
Tony Barbour2593b182016-04-19 10:57:58 -06002215static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002216 xcb_flush(demo->connection);
2217
2218 while (!demo->quit) {
2219 xcb_generic_event_t *event;
2220
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002221 if (demo->pause) {
2222 event = xcb_wait_for_event(demo->connection);
2223 } else {
2224 event = xcb_poll_for_event(demo->connection);
2225 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002226 if (event) {
Tony Barbour2593b182016-04-19 10:57:58 -06002227 demo_handle_xcb_event(demo, event);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002228 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002229 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002230
2231 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002232 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002233 demo_update_data_buffer(demo);
2234
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002235 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002236
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002237 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002238 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002239 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002240 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002241 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002242 }
2243}
2244
Tony Barbour2593b182016-04-19 10:57:58 -06002245static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002246 uint32_t value_mask, value_list[32];
2247
Tony Barbour2593b182016-04-19 10:57:58 -06002248 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002249
2250 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2251 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002252 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002253 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002254
Tony Barbour2593b182016-04-19 10:57:58 -06002255 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002256 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2257 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2258 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002259
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002260 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002261 xcb_intern_atom_cookie_t cookie =
2262 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2263 xcb_intern_atom_reply_t *reply =
2264 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002265
Karl Schultze4dc75c2016-02-02 15:37:51 -07002266 xcb_intern_atom_cookie_t cookie2 =
2267 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2268 demo->atom_wm_delete_window =
2269 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002270
Tony Barbour2593b182016-04-19 10:57:58 -06002271 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002272 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002273 &(*demo->atom_wm_delete_window).atom);
2274 free(reply);
2275
Tony Barbour2593b182016-04-19 10:57:58 -06002276 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002277
Karl Schultze4dc75c2016-02-02 15:37:51 -07002278 // Force the x/y coordinates to 100,100 results are identical in consecutive
2279 // runs
2280 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002281 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002282 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002283}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002284#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2285static void demo_run(struct demo *demo) {
2286 if (!demo->prepared)
2287 return;
2288
2289 // Wait for work to finish before updating MVP.
2290 vkDeviceWaitIdle(demo->device);
2291 demo_update_data_buffer(demo);
2292
2293 demo_draw(demo);
2294
2295 // Wait for work to finish before updating MVP.
2296 vkDeviceWaitIdle(demo->device);
2297
2298 demo->curFrame++;
2299}
2300#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002301
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002302/*
2303 * Return 1 (true) if all layer names specified in check_names
2304 * can be found in given layer properties.
2305 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002306static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002307 uint32_t layer_count,
2308 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002309 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002310 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002311 for (uint32_t j = 0; j < layer_count; j++) {
2312 if (!strcmp(check_names[i], layers[j].layerName)) {
2313 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002314 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002315 }
2316 }
2317 if (!found) {
2318 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2319 return 0;
2320 }
2321 }
2322 return 1;
2323}
2324
Karl Schultze4dc75c2016-02-02 15:37:51 -07002325static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002326 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002327 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002328 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002329 uint32_t device_validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002330 char **instance_validation_layers = NULL;
2331 demo->enabled_extension_count = 0;
2332 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002333
Karl Schultz7c50ad62016-04-01 12:07:03 -06002334 char *instance_validation_layers_alt1[] = {
2335 "VK_LAYER_LUNARG_standard_validation"
2336 };
2337
2338 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskiaaab5c02016-03-17 15:08:18 -06002339 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Karl Schultz635272d2016-03-07 17:54:07 -07002340 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis1398c712016-03-09 16:12:48 -07002341 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
Karl Schultz7c50ad62016-04-01 12:07:03 -06002342 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002343 };
2344
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002345 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002346 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002347 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002348
Karl Schultz7c50ad62016-04-01 12:07:03 -06002349 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002350 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002351
Karl Schultz7c50ad62016-04-01 12:07:03 -06002352 instance_validation_layers = instance_validation_layers_alt1;
2353 if (instance_layer_count > 0) {
2354 VkLayerProperties *instance_layers =
2355 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2356 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2357 instance_layers);
2358 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002359
Karl Schultz7c50ad62016-04-01 12:07:03 -06002360
2361 validation_found = demo_check_layers(
2362 ARRAY_SIZE(instance_validation_layers_alt1),
2363 instance_validation_layers, instance_layer_count,
2364 instance_layers);
2365 if (validation_found) {
2366 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
2367 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2368 device_validation_layer_count = 1;
2369 } else {
2370 // use alternative set of validation layers
2371 instance_validation_layers = instance_validation_layers_alt2;
2372 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2373 validation_found = demo_check_layers(
2374 ARRAY_SIZE(instance_validation_layers_alt2),
2375 instance_validation_layers, instance_layer_count,
2376 instance_layers);
2377 device_validation_layer_count =
2378 ARRAY_SIZE(instance_validation_layers_alt2);
2379 for (uint32_t i = 0; i < device_validation_layer_count; i++) {
2380 demo->device_validation_layers[i] =
2381 instance_validation_layers[i];
2382 }
2383 }
2384 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002385 }
Dustin Graves7c287352016-01-27 13:48:06 -07002386
Karl Schultz7c50ad62016-04-01 12:07:03 -06002387 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002388 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002389 "required validation layer.\n\n"
2390 "Please look at the Getting Started guide for additional "
2391 "information.\n",
2392 "vkCreateInstance Failure");
2393 }
Dustin Graves7c287352016-01-27 13:48:06 -07002394 }
2395
2396 /* Look for instance extensions */
2397 VkBool32 surfaceExtFound = 0;
2398 VkBool32 platformSurfaceExtFound = 0;
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002399#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002400 VkBool32 xlibSurfaceExtFound = 0;
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002401#endif
Karl Schultz7c50ad62016-04-01 12:07:03 -06002402 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002403
Karl Schultze4dc75c2016-02-02 15:37:51 -07002404 err = vkEnumerateInstanceExtensionProperties(
2405 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002406 assert(!err);
2407
Dustin Graves7c287352016-01-27 13:48:06 -07002408 if (instance_extension_count > 0) {
2409 VkExtensionProperties *instance_extensions =
2410 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2411 err = vkEnumerateInstanceExtensionProperties(
2412 NULL, &instance_extension_count, instance_extensions);
2413 assert(!err);
2414 for (uint32_t i = 0; i < instance_extension_count; i++) {
2415 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2416 instance_extensions[i].extensionName)) {
2417 surfaceExtFound = 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_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002420 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002421#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002422 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2423 instance_extensions[i].extensionName)) {
2424 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002425 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002426 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2427 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002428#endif
2429#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002430 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
2431 instance_extensions[i].extensionName)) {
2432 platformSurfaceExtFound = 1;
2433 xlibSurfaceExtFound = 1;
2434 demo->extension_names[demo->enabled_extension_count++] =
2435 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
2436 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002437#endif
2438#if defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07002439 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2440 instance_extensions[i].extensionName)) {
2441 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002442 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002443 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2444 }
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002445#endif
2446#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002447 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
2448 instance_extensions[i].extensionName)) {
2449 platformSurfaceExtFound = 1;
2450 demo->extension_names[demo->enabled_extension_count++] =
2451 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
2452 }
2453#endif
Dustin Graves7c287352016-01-27 13:48:06 -07002454 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2455 instance_extensions[i].extensionName)) {
2456 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002457 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002458 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2459 }
2460 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002461 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002462 }
Dustin Graves7c287352016-01-27 13:48:06 -07002463
2464 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002465 }
Dustin Graves7c287352016-01-27 13:48:06 -07002466
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002467 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002468 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2469 "the " VK_KHR_SURFACE_EXTENSION_NAME
2470 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002471 "Vulkan installable client driver (ICD) installed?\nPlease "
2472 "look at the Getting Started guide for additional "
2473 "information.\n",
2474 "vkCreateInstance Failure");
2475 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002476 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002477#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002478 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2479 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2480 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002481 "Vulkan installable client driver (ICD) installed?\nPlease "
2482 "look at the Getting Started guide for additional "
2483 "information.\n",
2484 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002485#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002486 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2487 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2488 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002489 "Vulkan installable client driver (ICD) installed?\nPlease "
2490 "look at the Getting Started guide for additional "
2491 "information.\n",
2492 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002493#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2494 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2495 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2496 " extension.\n\nDo you have a compatible "
2497 "Vulkan installable client driver (ICD) installed?\nPlease "
2498 "look at the Getting Started guide for additional "
2499 "information.\n",
2500 "vkCreateInstance Failure");
2501#endif
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002502 }
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002503#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002504 if (demo->use_xlib && !xlibSurfaceExtFound) {
2505 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2506 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2507 " extension.\n\nDo you have a compatible "
2508 "Vulkan installable client driver (ICD) installed?\nPlease "
2509 "look at the Getting Started guide for additional "
2510 "information.\n",
2511 "vkCreateInstance Failure");
2512 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002513#endif
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002514 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002515 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002516 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002517 .pApplicationName = APP_SHORT_NAME,
2518 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002519 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002520 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002521 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002522 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002523 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002524 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002525 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002526 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002527 .enabledLayerCount = demo->enabled_layer_count,
2528 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2529 .enabledExtensionCount = demo->enabled_extension_count,
2530 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002531 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002532
2533 /*
2534 * This is info for a temp callback to use during CreateInstance.
2535 * After the instance is created, we use the instance-based
2536 * function to register the final callback.
2537 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002538 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002539 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002540 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2541 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002542 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002543 dbgCreateInfo.pUserData = NULL;
2544 dbgCreateInfo.flags =
2545 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2546 inst_info.pNext = &dbgCreateInfo;
2547 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002548
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002549 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002550
Chia-I Wu63636062015-10-26 21:10:41 +08002551 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002552 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2553 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002554 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002555 "additional information.\n",
2556 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002557 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002558 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002559 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002560 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002561 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002562 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2563 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002564 "the Getting Started guide for additional information.\n",
2565 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002566 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002567
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002568 /* Make initial call to query gpu_count, then second call for gpu info*/
2569 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2570 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002571
2572 if (gpu_count > 0) {
2573 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2574 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2575 assert(!err);
2576 /* For cube demo we just grab the first physical device */
2577 demo->gpu = physical_devices[0];
2578 free(physical_devices);
2579 } else {
2580 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2581 "Do you have a compatible Vulkan installable client driver (ICD) "
2582 "installed?\nPlease look at the Getting Started guide for "
2583 "additional information.\n",
2584 "vkEnumeratePhysicalDevices Failure");
2585 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002586
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002587 /* Look for validation layers */
2588 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002589 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002590 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002591 err =
2592 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002593 assert(!err);
2594
Dustin Graves7c287352016-01-27 13:48:06 -07002595 if (device_layer_count > 0) {
2596 VkLayerProperties *device_layers =
2597 malloc(sizeof(VkLayerProperties) * device_layer_count);
2598 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2599 device_layers);
2600 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002601
Dustin Graves7c287352016-01-27 13:48:06 -07002602 if (demo->validate) {
2603 validation_found = demo_check_layers(device_validation_layer_count,
2604 demo->device_validation_layers,
2605 device_layer_count,
2606 device_layers);
2607 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002608 }
Dustin Graves7c287352016-01-27 13:48:06 -07002609
2610 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002611 }
2612
Dustin Graves7c287352016-01-27 13:48:06 -07002613 if (demo->validate && !validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002614 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find "
Dustin Graves7c287352016-01-27 13:48:06 -07002615 "a required validation layer.\n\n"
2616 "Please look at the Getting Started guide for additional "
2617 "information.\n",
2618 "vkCreateDevice Failure");
2619 }
2620
2621 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002622 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002623 VkBool32 swapchainExtFound = 0;
2624 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002625 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002626
Karl Schultze4dc75c2016-02-02 15:37:51 -07002627 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2628 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002629 assert(!err);
2630
Dustin Graves7c287352016-01-27 13:48:06 -07002631 if (device_extension_count > 0) {
2632 VkExtensionProperties *device_extensions =
2633 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2634 err = vkEnumerateDeviceExtensionProperties(
2635 demo->gpu, NULL, &device_extension_count, device_extensions);
2636 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002637
Dustin Graves7c287352016-01-27 13:48:06 -07002638 for (uint32_t i = 0; i < device_extension_count; i++) {
2639 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2640 device_extensions[i].extensionName)) {
2641 swapchainExtFound = 1;
2642 demo->extension_names[demo->enabled_extension_count++] =
2643 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2644 }
2645 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002646 }
Dustin Graves7c287352016-01-27 13:48:06 -07002647
2648 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002649 }
Dustin Graves7c287352016-01-27 13:48:06 -07002650
Tony Barbourcc69f452015-10-08 13:59:42 -06002651 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002652 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2653 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2654 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002655 "Vulkan installable client driver (ICD) installed?\nPlease "
2656 "look at the Getting Started guide for additional "
2657 "information.\n",
2658 "vkCreateInstance Failure");
2659 }
2660
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002661 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002662 demo->CreateDebugReportCallback =
2663 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2664 demo->inst, "vkCreateDebugReportCallbackEXT");
2665 demo->DestroyDebugReportCallback =
2666 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2667 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002668 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002669 ERR_EXIT(
2670 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2671 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002672 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002673 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002674 ERR_EXIT(
2675 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2676 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002677 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002678 demo->DebugReportMessage =
2679 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2680 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002681 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002682 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002683 "vkGetProcAddr Failure");
2684 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002685
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002686 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002687 PFN_vkDebugReportCallbackEXT callback;
2688 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002689 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002690 dbgCreateInfo.pNext = NULL;
2691 dbgCreateInfo.pfnCallback = callback;
2692 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002693 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002694 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002695 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2696 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002697 switch (err) {
2698 case VK_SUCCESS:
2699 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002700 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002701 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2702 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002703 break;
2704 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002705 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2706 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002707 break;
2708 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002709 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002710 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002711
2712 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002713 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2714 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002715 assert(demo->queue_count >= 1);
2716
Karl Schultze4dc75c2016-02-02 15:37:51 -07002717 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2718 demo->queue_count * sizeof(VkQueueFamilyProperties));
2719 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2720 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002721 // Find a queue that supports gfx
2722 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002723 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2724 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002725 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2726 break;
2727 }
2728 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002729 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002730 // If app has specific feature requirements it should check supported
2731 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002732 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002733 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002734
Tony Barbourda2bad52016-01-22 14:36:40 -07002735 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2736 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2737 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2738 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2739 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2740}
2741
Karl Schultze4dc75c2016-02-02 15:37:51 -07002742static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002743 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002744 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002745 const VkDeviceQueueCreateInfo queue = {
2746 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2747 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002748 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002749 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002750 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002751
2752 VkDeviceCreateInfo device = {
2753 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2754 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002755 .queueCreateInfoCount = 1,
2756 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002757 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002758 .ppEnabledLayerNames =
2759 (const char *const *)((demo->validate)
2760 ? demo->device_validation_layers
2761 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002762 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002763 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2764 .pEnabledFeatures =
2765 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002766 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002767
Chia-I Wu63636062015-10-26 21:10:41 +08002768 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002769 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002770}
2771
Karl Schultze4dc75c2016-02-02 15:37:51 -07002772static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002773 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002774 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002775
Karl Schultze4dc75c2016-02-02 15:37:51 -07002776// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002777#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07002778 VkWin32SurfaceCreateInfoKHR createInfo;
2779 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2780 createInfo.pNext = NULL;
2781 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002782 createInfo.hinstance = demo->connection;
2783 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002784
Karl Schultze4dc75c2016-02-02 15:37:51 -07002785 err =
2786 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002787#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2788 VkAndroidSurfaceCreateInfoKHR createInfo;
2789 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
2790 createInfo.pNext = NULL;
2791 createInfo.flags = 0;
2792 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002793
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002794 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
2795#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002796 if (demo->use_xlib) {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002797#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002798 VkXlibSurfaceCreateInfoKHR createInfo;
2799 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
2800 createInfo.pNext = NULL;
2801 createInfo.flags = 0;
2802 createInfo.dpy = demo->display;
2803 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002804
Tony Barbour2593b182016-04-19 10:57:58 -06002805 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
2806 &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002807#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002808 }
2809 else {
Tobin Ehlis43ed2982016-04-28 10:17:33 -06002810#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002811 VkXcbSurfaceCreateInfoKHR createInfo;
2812 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2813 createInfo.pNext = NULL;
2814 createInfo.flags = 0;
2815 createInfo.connection = demo->connection;
2816 createInfo.window = demo->xcb_window;
2817
2818 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002819#endif
Tony Barbour2593b182016-04-19 10:57:58 -06002820 }
Tony Barbour2593b182016-04-19 10:57:58 -06002821 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002822
Tony Barbourcc69f452015-10-08 13:59:42 -06002823 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002824 VkBool32 *supportsPresent =
2825 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002826 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002827 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002828 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002829 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002830
2831 // Search for a graphics and a present queue in the array of queue
2832 // families, try to find one that supports both
2833 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002834 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002835 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002836 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2837 if (graphicsQueueNodeIndex == UINT32_MAX) {
2838 graphicsQueueNodeIndex = i;
2839 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002840
Ian Elliottaaae5352015-07-06 14:27:58 -06002841 if (supportsPresent[i] == VK_TRUE) {
2842 graphicsQueueNodeIndex = i;
2843 presentQueueNodeIndex = i;
2844 break;
2845 }
2846 }
2847 }
2848 if (presentQueueNodeIndex == UINT32_MAX) {
2849 // If didn't find a queue that supports both graphics and present, then
2850 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002851 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002852 if (supportsPresent[i] == VK_TRUE) {
2853 presentQueueNodeIndex = i;
2854 break;
2855 }
2856 }
2857 }
2858 free(supportsPresent);
2859
2860 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002861 if (graphicsQueueNodeIndex == UINT32_MAX ||
2862 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002863 ERR_EXIT("Could not find a 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 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002868 // synchronization, and appropriate tracking for QueueSubmit.
2869 // NOTE: While it is possible for an application to use a separate graphics
2870 // and a present queues, this demo program assumes it is only using
2871 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002872 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2873 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002874 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002875 }
2876
2877 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002878
Tony Barbourda2bad52016-01-22 14:36:40 -07002879 demo_create_device(demo);
2880
2881 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2882 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2883 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2884 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2885 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2886
Karl Schultze4dc75c2016-02-02 15:37:51 -07002887 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2888 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002889
Ian Elliottaaae5352015-07-06 14:27:58 -06002890 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002891 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002892 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002893 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002894 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002895 VkSurfaceFormatKHR *surfFormats =
2896 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002897 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002898 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002899 assert(!err);
2900 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2901 // the surface has no preferred format. Otherwise, at least one
2902 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002903 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002904 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002905 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002906 assert(formatCount >= 1);
2907 demo->format = surfFormats[0].format;
2908 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002909 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002910
David Pinedo2bb7c932015-06-18 17:03:14 -06002911 demo->quit = false;
2912 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002913
2914 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002915 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002916}
2917
Karl Schultze4dc75c2016-02-02 15:37:51 -07002918static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002919#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002920 const xcb_setup_t *setup;
2921 xcb_screen_iterator_t iter;
2922 int scr;
2923
2924 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002925 if (demo->connection == NULL) {
2926 printf("Cannot find a compatible Vulkan installable client driver "
2927 "(ICD).\nExiting ...\n");
2928 fflush(stdout);
2929 exit(1);
2930 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002931
2932 setup = xcb_get_setup(demo->connection);
2933 iter = xcb_setup_roots_iterator(setup);
2934 while (scr-- > 0)
2935 xcb_screen_next(&iter);
2936
2937 demo->screen = iter.data;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002938#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002939}
2940
Karl Schultze4dc75c2016-02-02 15:37:51 -07002941static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002942 vec3 eye = {0.0f, 3.0f, 5.0f};
2943 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002944 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002945
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002946 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002947 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002948
Piers Daniell735ee532015-02-23 16:23:13 -07002949 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002950 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002951 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002952 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002953 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002954 if (strcmp(argv[i], "--break") == 0) {
2955 demo->use_break = true;
2956 continue;
2957 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002958 if (strcmp(argv[i], "--validate") == 0) {
2959 demo->validate = true;
2960 continue;
2961 }
Tony Barbour2593b182016-04-19 10:57:58 -06002962 if (strcmp(argv[i], "--xlib") == 0) {
2963 demo->use_xlib = true;
2964 continue;
2965 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002966 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2967 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2968 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002969 i++;
2970 continue;
2971 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002972
Karl Schultze4dc75c2016-02-02 15:37:51 -07002973 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2974 "[--c <framecount>]\n",
2975 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002976 fflush(stderr);
2977 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002978 }
2979
Tony Barbour2593b182016-04-19 10:57:58 -06002980 if (!demo->use_xlib)
2981 demo_init_connection(demo);
2982
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002983 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002984
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002985 demo->width = 500;
2986 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002987
2988 demo->spin_angle = 0.01f;
2989 demo->spin_increment = 0.01f;
2990 demo->pause = false;
2991
Karl Schultze4dc75c2016-02-02 15:37:51 -07002992 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2993 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002994 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2995 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002996}
2997
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002998#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07002999// Include header required for parsing the command line options.
3000#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003001
Karl Schultze4dc75c2016-02-02 15:37:51 -07003002int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3003 int nCmdShow) {
3004 MSG msg; // message
3005 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003006 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003007 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003008
Karl Schultze4dc75c2016-02-02 15:37:51 -07003009 // Use the CommandLine functions to get the command line arguments.
3010 // Unfortunately, Microsoft outputs
3011 // this information as wide characters for Unicode, and we simply want the
3012 // Ascii version to be compatible
3013 // with the non-Windows side. So, we have to convert the information to
3014 // Ascii character strings.
3015 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3016 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003017 argc = 0;
3018 }
3019
Karl Schultze4dc75c2016-02-02 15:37:51 -07003020 if (argc > 0) {
3021 argv = (char **)malloc(sizeof(char *) * argc);
3022 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003023 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003024 } else {
3025 for (int iii = 0; iii < argc; iii++) {
3026 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003027 size_t numConverted = 0;
3028
Karl Schultze4dc75c2016-02-02 15:37:51 -07003029 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3030 if (argv[iii] != NULL) {
3031 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3032 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003033 }
3034 }
3035 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003036 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003037 argv = NULL;
3038 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003039
3040 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003041
3042 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003043 if (argc > 0 && argv != NULL) {
3044 for (int iii = 0; iii < argc; iii++) {
3045 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003046 free(argv[iii]);
3047 }
3048 }
3049 free(argv);
3050 }
3051
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003052 demo.connection = hInstance;
3053 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003054 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003055 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003056
3057 demo_prepare(&demo);
3058
Karl Schultze4dc75c2016-02-02 15:37:51 -07003059 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003060
3061 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003062 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003063 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003064 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003065 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003066 done = true; // if found, quit app
3067 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003068 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003069 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003070 DispatchMessage(&msg);
3071 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003072 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003073 }
3074
3075 demo_cleanup(&demo);
3076
Karl Schultze4dc75c2016-02-02 15:37:51 -07003077 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003078}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003079#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3080#include <android/log.h>
3081#include <android_native_app_glue.h>
3082static bool initialized = false;
3083static bool active = false;
3084struct demo demo;
3085
3086static int32_t processInput(struct android_app* app, AInputEvent* event) {
3087 return 0;
3088}
3089
3090static void processCommand(struct android_app* app, int32_t cmd) {
3091 switch(cmd) {
3092 case APP_CMD_INIT_WINDOW: {
3093 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003094 // We're getting a new window. If the app is starting up, we
3095 // need to initialize. If the app has already been
3096 // initialized, that means that we lost our previous window,
3097 // which means that we have a lot of work to do. At a minimum,
3098 // we need to destroy the swapchain and surface associated with
3099 // the old window, and create a new surface and swapchain.
3100 // However, since there are a lot of other objects/state that
3101 // is tied to the swapchain, it's easiest to simply cleanup and
3102 // start over (i.e. use a brute-force approach of re-starting
3103 // the app)
3104 if (demo.prepared) {
3105 demo_cleanup(&demo);
3106 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003107 demo_init(&demo, 0, NULL);
3108 demo.window = (void*)app->window;
3109 demo_init_vk_swapchain(&demo);
3110 demo_prepare(&demo);
3111 initialized = true;
3112 }
3113 break;
3114 }
3115 case APP_CMD_GAINED_FOCUS: {
3116 active = true;
3117 break;
3118 }
3119 case APP_CMD_LOST_FOCUS: {
3120 active = false;
3121 break;
3122 }
3123 }
3124}
3125
3126void android_main(struct android_app *app)
3127{
3128 app_dummy();
3129
Cody Northrop8707a6e2016-04-26 19:59:19 -06003130#ifdef ANDROID
3131 int vulkanSupport = InitVulkan();
3132 if (vulkanSupport == 0)
3133 return;
3134#endif
3135
Ian Elliottf05d5d12016-05-17 12:03:35 -06003136 demo.prepared = false;
3137
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003138 app->onAppCmd = processCommand;
3139 app->onInputEvent = processInput;
3140
3141 while(1) {
3142 int events;
3143 struct android_poll_source* source;
3144 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
3145 if (source) {
3146 source->process(app, source);
3147 }
3148
3149 if (app->destroyRequested != 0) {
3150 demo_cleanup(&demo);
3151 return;
3152 }
3153 }
3154 if (initialized && active) {
3155 demo_run(&demo);
3156 }
3157 }
3158
3159}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003160#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003161int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003162 struct demo demo;
3163
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003164 demo_init(&demo, argc, argv);
Tony Barbour2593b182016-04-19 10:57:58 -06003165 if (demo.use_xlib)
3166 demo_create_xlib_window(&demo);
3167 else
3168 demo_create_xcb_window(&demo);
3169
Tony Barbourcc69f452015-10-08 13:59:42 -06003170 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003171
3172 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003173
3174 if (demo.use_xlib)
3175 demo_run_xlib(&demo);
3176 else
3177 demo_run_xcb(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003178
3179 demo_cleanup(&demo);
3180
Karl Schultz0218c002016-03-22 17:06:13 -06003181 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003182}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003183#endif