blob: 52080173f75c13d89cc73b9d64d48741161997ec [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
Ian Elliotta748eaf2015-04-28 15:50:36 -06002 *
Courtney Goeltzenleuchterbb387b72015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Courtney Goeltzenleuchter24df6832015-12-02 14:53:22 -07004 * Copyright (C) 2015 Google Inc.
Ian Elliotta748eaf2015-04-28 15:50:36 -06005 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060023 *
24 * Author: Chia-I Wu <olv@lunarg.com>
25 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
26 * Author: Ian Elliott <ian@LunarG.com>
27 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060028 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060029#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070035#include <signal.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036
Ian Elliott639ca472015-04-16 15:23:05 -060037#ifdef _WIN32
38#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060039#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060040#endif // _WIN32
41
David Pinedoc5193c12015-11-06 12:54:48 -070042#include <vulkan/vulkan.h>
Ian Elliottf4a4e442015-11-17 17:29:40 -070043
Ian Elliottf4a4e442015-11-17 17:29:40 -070044#include "vulkan/vk_lunarg_debug_report.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045
Ian Elliottb08e0d92015-11-20 11:55:46 -070046
David Pinedo541526f2015-11-24 09:00:24 -070047#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048#include "linmath.h"
49
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliott07264132015-04-28 11:35:02 -060062#ifdef _WIN32
63#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
68
Ian Elliott07264132015-04-28 11:35:02 -060069#else // _WIN32
70
71#define ERR_EXIT(err_msg, err_class) \
72 do { \
73 printf(err_msg); \
74 fflush(stdout); \
75 exit(1); \
76 } while (0)
77#endif // _WIN32
78
Ian Elliottaaae5352015-07-06 14:27:58 -060079#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
80{ \
81 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
82 if (demo->fp##entrypoint == NULL) { \
83 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
84 "vkGetInstanceProcAddr Failure"); \
85 } \
86}
87
Jon Ashburn7d8342c2015-10-08 15:58:23 -060088static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
89
Ian Elliott673898b2015-06-22 15:07:49 -060090#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
91{ \
Jon Ashburn7d8342c2015-10-08 15:58:23 -060092 if(!g_gdpa) \
93 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
94 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060095 if (demo->fp##entrypoint == NULL) { \
96 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
97 "vkGetDeviceProcAddr Failure"); \
98 } \
99}
100
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600101/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700102 * structure to track all objects related to a texture.
103 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600104struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600105 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700106
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600107 VkImage image;
108 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600109
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800110 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500111 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600112 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700113 int32_t tex_width, tex_height;
114};
115
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600117 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600118};
119
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600120struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600121 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600122 float mvp[4][4];
123 float position[12*3][4];
124 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600125};
126
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600127struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600128 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600129 float mvp[4][4];
130 float position[12*3][4];
131 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600132};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600133
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600134//--------------------------------------------------------------------------------------
135// Mesh and VertexFormat Data
136//--------------------------------------------------------------------------------------
137struct Vertex
138{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600139 float posX, posY, posZ, posW; // Position data
140 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600141};
142
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600143struct VertexPosTex
144{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600145 float posX, posY, posZ, posW; // Position data
146 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600147};
148
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600149#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600150#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600151
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600152static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800153 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 -1.0f,-1.0f, 1.0f,
155 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 -1.0f, 1.0f,-1.0f,
158 -1.0f,-1.0f,-1.0f,
159
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 1.0f, 1.0f,-1.0f,
162 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600165 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600169 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 -1.0f, 1.0f, 1.0f,
176 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600179 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 1.0f, 1.0f, 1.0f,
183 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f,-1.0f,-1.0f,
186 1.0f, 1.0f,-1.0f,
187
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600190 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 1.0f,-1.0f, 1.0f,
193 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600194};
195
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600196static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 0.0f, 0.0f, // -X side
198 1.0f, 0.0f,
199 1.0f, 1.0f,
200 1.0f, 1.0f,
201 0.0f, 1.0f,
202 0.0f, 0.0f,
203
204 1.0f, 0.0f, // -Z side
205 0.0f, 1.0f,
206 0.0f, 0.0f,
207 1.0f, 0.0f,
208 1.0f, 1.0f,
209 0.0f, 1.0f,
210
211 1.0f, 1.0f, // -Y side
212 1.0f, 0.0f,
213 0.0f, 0.0f,
214 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600215 0.0f, 0.0f,
216 0.0f, 1.0f,
217
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600219 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600221 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600223 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800225 1.0f, 1.0f, // +X side
226 0.0f, 1.0f,
227 0.0f, 0.0f,
228 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 0.0f, 1.0f, // +Z side
233 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600235 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800236 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600237 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600238};
239
240void dumpMatrix(const char *note, mat4x4 MVP)
241{
242 int i;
243
244 printf("%s: \n", note);
245 for (i=0; i<4; i++) {
246 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
247 }
248 printf("\n");
249 fflush(stdout);
250}
251
252void dumpVec4(const char *note, vec4 vector)
253{
254 printf("%s: \n", note);
255 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
256 printf("\n");
257 fflush(stdout);
258}
259
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600260VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600261 VkFlags msgFlags,
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700262 VkDebugReportObjectTypeLUNARG objType,
Tony Barbour155cce82015-07-03 10:33:54 -0600263 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600264 size_t location,
265 int32_t msgCode,
266 const char* pLayerPrefix,
267 const char* pMsg,
Courtney Goeltzenleuchter04ae89b2015-11-30 11:52:06 -0700268 const void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600269{
270 char *message = (char *) malloc(strlen(pMsg)+100);
271
272 assert (message);
273
Courtney Goeltzenleuchter15735822015-11-25 10:30:56 -0700274 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600275 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Courtney Goeltzenleuchter15735822015-11-25 10:30:56 -0700276 } else if (msgFlags & VK_DEBUG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600277 // We know that we're submitting queues without fences, ignore this warning
278 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600279 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600280 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600281 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600282 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600283 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600284 }
285
286#ifdef _WIN32
287 MessageBox(NULL, message, "Alert", MB_OK);
288#else
289 printf("%s\n",message);
290 fflush(stdout);
291#endif
292 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600293
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600294 /*
295 * false indicates that layer should not bail-out of an
296 * API call that had validation failures. This may mean that the
297 * app dies inside the driver due to invalid parameter(s).
298 * That's what would happen without validation layers, so we'll
299 * keep that behavior here.
300 */
301 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600302}
303
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700304VkBool32 BreakCallback(
305 VkFlags msgFlags,
306 VkDebugReportObjectTypeLUNARG objType,
307 uint64_t srcObject,
308 size_t location,
309 int32_t msgCode,
310 const char* pLayerPrefix,
311 const char* pMsg,
312 const void* pUserData)
313{
314#ifndef WIN32
315 raise(SIGTRAP);
316#else
317 DebugBreak();
318#endif
319
320 return false;
321}
322
Ian Elliotta0781972015-08-21 15:09:33 -0600323typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600324 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800325 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600326 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600327} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600328
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600330#ifdef _WIN32
331#define APP_NAME_STR_LEN 80
332 HINSTANCE connection; // hInstance - Windows Instance
333 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
334 HWND window; // hWnd - window handle
335#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336 xcb_connection_t *connection;
337 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800338 xcb_window_t window;
339 xcb_intern_atom_reply_t *atom_wm_delete_window;
Tony Barbour6839bec2015-07-13 16:37:21 -0600340#endif // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -0700341 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600342 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700343 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600345 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600346 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600347 VkDevice device;
348 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700349 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600350 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600351 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600352 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
354 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600355 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600356 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357
Ian Elliotta0781972015-08-21 15:09:33 -0600358 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
Ian Elliottf4a4e442015-11-17 17:29:40 -0700359 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
360 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
361 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600362 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
363 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
364 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
365 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
366 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600367 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600368 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600369 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800371 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372
373 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800377 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500378 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600379 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380 } depth;
381
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600382 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383
384 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800386 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500387 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600388 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600389 } uniform_data;
390
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800391 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500392 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600394 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800395 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600396 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600397
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600398 mat4x4 projection_matrix;
399 mat4x4 view_matrix;
400 mat4x4 model_matrix;
401
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600402 float spin_angle;
403 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600404 bool pause;
405
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600406 VkShaderModule vert_shader_module;
407 VkShaderModule frag_shader_module;
408
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600409 VkDescriptorPool desc_pool;
410 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800411
Tony Barbourd8e504f2015-10-08 14:26:24 -0600412 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800413
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600414 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600415 int32_t curFrame;
416 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600417 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600418 bool use_break;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -0700419 PFN_vkCreateDebugReportCallbackLUNARG CreateDebugReportCallback;
420 PFN_vkDestroyDebugReportCallbackLUNARG DestroyDebugReportCallback;
Courtney Goeltzenleuchterdd0e6172015-11-25 14:07:05 -0700421 VkDebugReportCallbackLUNARG msg_callback;
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700422 PFN_vkDebugReportMessageLUNARG DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600423
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600424 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600425 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600426};
427
Ian Elliottcf074d32015-10-16 18:02:43 -0600428// Forward declaration:
429static void demo_resize(struct demo *demo);
430
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600431static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600432{
433 // Search memtypes to find first index with those properties
434 for (uint32_t i = 0; i < 32; i++) {
435 if ((typeBits & 1) == 1) {
436 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600437 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600438 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600439 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600440 }
441 }
442 typeBits >>= 1;
443 }
444 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600445 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600446}
447
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600448static void demo_flush_init_cmd(struct demo *demo)
449{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600450 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600452 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600453 return;
454
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600456 assert(!err);
457
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800458 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800459 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600460 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800461 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
462 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800463 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600464 .pWaitSemaphores = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800465 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600466 .pCommandBuffers = cmd_bufs,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800467 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600468 .pSignalSemaphores = NULL
469 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600471 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472 assert(!err);
473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600475 assert(!err);
476
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600477 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479}
480
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481static void demo_set_image_layout(
482 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600483 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600484 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600485 VkImageLayout old_image_layout,
486 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600488 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600489
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800491 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800492 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600493 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800494 .commandPool = demo->cmd_pool,
495 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800496 .bufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600497 };
498
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800499 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600500 assert(!err);
501
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800502 VkCommandBufferBeginInfo cmd_buf_info = {
503 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600504 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600505 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800506 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600507 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800508 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800509 .occlusionQueryEnable = VK_FALSE,
510 .queryFlags = 0,
511 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600512 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600513 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600514 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515 }
516
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600517 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600518 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600519 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800520 .srcAccessMask = 0,
521 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600522 .oldLayout = old_image_layout,
523 .newLayout = new_image_layout,
524 .image = image,
Jeremy Hayes105ca502015-11-17 18:19:55 -0700525 .subresourceRange = { aspectMask, 0, 1, 0, 1 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526 };
527
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800528 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800530 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600531 }
532
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700533 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700534 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700535 }
536
537 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700538 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700539 }
540
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600542 /* Make sure any Copy or CPU writes to image are flushed */
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700543 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600544 }
545
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600546 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547
Tony Barbour50bbca42015-06-29 16:20:35 -0600548 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
549 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550
Chia-I Wu86365072015-10-26 17:08:33 +0800551 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552}
553
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800554static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555{
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800556 const VkCommandBufferBeginInfo cmd_buf_info = {
557 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600558 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600559 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800560 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600561 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800562 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800563 .occlusionQueryEnable = VK_FALSE,
564 .queryFlags = 0,
565 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700566 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800567 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600568 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
569 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800570 };
571 const VkRenderPassBeginInfo rp_begin = {
572 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
573 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800574 .renderPass = demo->render_pass,
575 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800576 .renderArea.offset.x = 0,
577 .renderArea.offset.y = 0,
578 .renderArea.extent.width = demo->width,
579 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600580 .clearValueCount = 2,
581 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700582 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800583 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600584
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600585 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586 assert(!err);
587
Chia-I Wuf051d262015-10-27 19:25:11 +0800588 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800589
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600590 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600591 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600592 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600593 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600594
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600595 VkViewport viewport;
596 memset(&viewport, 0, sizeof(viewport));
597 viewport.height = (float) demo->height;
598 viewport.width = (float) demo->width;
599 viewport.minDepth = (float) 0.0f;
600 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600601 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600602
603 VkRect2D scissor;
604 memset(&scissor, 0, sizeof(scissor));
605 scissor.extent.width = demo->width;
606 scissor.extent.height = demo->height;
607 scissor.offset.x = 0;
608 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600609 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600610
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600611 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800612 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613
Tony Barbour15a4ef62015-10-21 10:14:48 -0600614 VkImageMemoryBarrier prePresentBarrier = {
615 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
616 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800617 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
618 .dstAccessMask = 0,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600619 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700620 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600621 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800622 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600623 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
624 };
625
626 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
627 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wu082a6202015-10-31 00:31:16 +0800628 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu86365072015-10-26 17:08:33 +0800629 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600630
631
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600632 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600633 assert(!err);
634}
635
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600636
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600637void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600639 mat4x4 MVP, Model, VP;
640 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600641 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600642 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600644 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600646 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600647 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700648 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600649 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600650
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200651 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652 assert(!err);
653
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600654 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600656 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657}
658
659static void demo_draw(struct demo *demo)
660{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600661 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600662 VkSemaphore presentCompleteSemaphore;
663 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
664 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
665 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600666 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600667 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800668 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669
Ian Elliottaaae5352015-07-06 14:27:58 -0600670 err = vkCreateSemaphore(demo->device,
671 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +0800672 NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600673 &presentCompleteSemaphore);
674 assert(!err);
675
676 // Get the index of the next available swapchain image:
Ian Elliottcf074d32015-10-16 18:02:43 -0600677 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600678 UINT64_MAX,
679 presentCompleteSemaphore,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700680 NULL,// TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600681 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600682 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
683 // demo->swapchain is out of date (e.g. the window was resized) and
684 // must be recreated:
685 demo_resize(demo);
686 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800687 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600688 return;
689 } else if (err == VK_SUBOPTIMAL_KHR) {
690 // demo->swapchain is not as optimal as it could be, but the platform's
691 // presentation engine will still present the image correctly.
692 } else {
693 assert(!err);
694 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600695
Tony Barbour15a4ef62015-10-21 10:14:48 -0600696 // Assume the command buffer has been run on current_buffer before so
697 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
698 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
699 VK_IMAGE_ASPECT_COLOR_BIT,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700700 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600701 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
702 demo_flush_init_cmd(demo);
703
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600704 // Wait for the present complete semaphore to be signaled to ensure
705 // that the image won't be rendered to until the presentation
706 // engine has fully released ownership to the application, and it is
707 // okay to render to the image.
708
Ian Elliottf4a4e442015-11-17 17:29:40 -0700709// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600710 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800711 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
712 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800713 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600714 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800715 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600716 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800717 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600718 .pSignalSemaphores = NULL
719 };
720
721 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722 assert(!err);
723
Ian Elliotta0781972015-08-21 15:09:33 -0600724 VkPresentInfoKHR present = {
725 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600726 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600727 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700728 .pSwapchains = &demo->swapchain,
729 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600730 };
731
732// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600733 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600734 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
735 // demo->swapchain is out of date (e.g. the window was resized) and
736 // must be recreated:
737 demo_resize(demo);
738 } else if (err == VK_SUBOPTIMAL_KHR) {
739 // demo->swapchain is not as optimal as it could be, but the platform's
740 // presentation engine will still present the image correctly.
741 } else {
742 assert(!err);
743 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600744
Chia-I Wucbb564e2015-04-16 22:02:10 +0800745 err = vkQueueWaitIdle(demo->queue);
746 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600747
Chia-I Wu63636062015-10-26 21:10:41 +0800748 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600749}
750
751static void demo_prepare_buffers(struct demo *demo)
752{
Ian Elliottaaae5352015-07-06 14:27:58 -0600753 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600754 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600755
Ian Elliottb08e0d92015-11-20 11:55:46 -0700756 // Check the surface capabilities and formats
757 VkSurfaceCapabilitiesKHR surfCapabilities;
758 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu,
759 demo->surface,
760 &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600761 assert(!err);
762
Ian Elliott8528eff2015-08-07 15:56:59 -0600763 uint32_t presentModeCount;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700764 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
765 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600766 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600767 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600768 VkPresentModeKHR *presentModes =
769 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600770 assert(presentModes);
Ian Elliottb08e0d92015-11-20 11:55:46 -0700771 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
772 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600773 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600774 assert(!err);
775
Ian Elliotta0781972015-08-21 15:09:33 -0600776 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600777 // width and height are either both -1, or both not -1.
Ian Elliottb08e0d92015-11-20 11:55:46 -0700778 if (surfCapabilities.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600779 {
780 // If the surface size is undefined, the size is set to
781 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600782 swapchainExtent.width = demo->width;
783 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600784 }
785 else
786 {
787 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700788 swapchainExtent = surfCapabilities.currentExtent;
789 demo->width = surfCapabilities.currentExtent.width;
790 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600791 }
792
793 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600794 // tearing mode. If not, try IMMEDIATE which will usually be available,
795 // and is fastest (though it tears). If not, fall back to FIFO which is
796 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600797 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600798 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600799 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
800 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600801 break;
802 }
Ian Elliotta0781972015-08-21 15:09:33 -0600803 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
804 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
805 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600806 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 }
808
809 // Determine the number of VkImage's to use in the swap chain (we desire to
810 // own only 1 image at a time, besides the images being displayed and
811 // queued for display):
Ian Elliottb08e0d92015-11-20 11:55:46 -0700812 uint32_t desiredNumberOfSwapchainImages = surfCapabilities.minImageCount + 1;
813 if ((surfCapabilities.maxImageCount > 0) &&
814 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600815 {
816 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700817 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600818 }
819
Tony Barbour9fd6d352015-09-16 15:01:32 -0600820 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700821 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
822 preTransform = VK_SURFACE_TRANSFORM_NONE_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600823 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700824 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600825 }
826
Ian Elliottcf074d32015-10-16 18:02:43 -0600827 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600828 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800829 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700830 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600831 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800832 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600833 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800834 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600835 .width = swapchainExtent.width,
836 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600837 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700838 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600839 .preTransform = preTransform,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700840 .imageArrayLayers = 1,
841 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
842 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600843 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600844 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600845 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600846 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600848 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600849
Ian Elliottb08e0d92015-11-20 11:55:46 -0700850 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800851 assert(!err);
852
Ian Elliottcf074d32015-10-16 18:02:43 -0600853 // If we just re-created an existing swapchain, we should destroy the old
854 // swapchain at this point.
855 // Note: destroying the swapchain also cleans up all its associated
856 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800857 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700858 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600859 }
860
861 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600862 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600863 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800864
Ian Elliotta0781972015-08-21 15:09:33 -0600865 VkImage* swapchainImages =
866 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
867 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600868 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600869 &demo->swapchainImageCount,
870 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600871 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600872
Ian Elliotta0781972015-08-21 15:09:33 -0600873 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600874 assert(demo->buffers);
875
Ian Elliotta0781972015-08-21 15:09:33 -0600876 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600877 VkImageViewCreateInfo color_image_view = {
878 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600879 .pNext = NULL,
880 .format = demo->format,
Chia-I Wub6d30c62015-10-27 19:00:15 +0800881 .components = {
Chia-I Wuf051d262015-10-27 19:25:11 +0800882 .r = VK_COMPONENT_SWIZZLE_R,
883 .g = VK_COMPONENT_SWIZZLE_G,
884 .b = VK_COMPONENT_SWIZZLE_B,
885 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600886 },
887 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600888 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600889 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800890 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600891 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800892 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600893 },
894 .viewType = VK_IMAGE_VIEW_TYPE_2D,
895 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600896 };
897
Ian Elliotta0781972015-08-21 15:09:33 -0600898 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600899
Ian Elliottf4a4e442015-11-17 17:29:40 -0700900 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Tony Barbour15a4ef62015-10-21 10:14:48 -0600901 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600902 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600903 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600904 VK_IMAGE_LAYOUT_UNDEFINED,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700905 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600906
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600907 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600908
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600909 err = vkCreateImageView(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +0800910 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600911 assert(!err);
912 }
913}
914
915static void demo_prepare_depth(struct demo *demo)
916{
Tony Barbour72304ef2015-04-16 15:59:00 -0600917 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600918 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600919 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600920 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600921 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600922 .format = depth_format,
923 .extent = { demo->width, demo->height, 1 },
924 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600925 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800926 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600927 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600928 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600929 .flags = 0,
930 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200931
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600932 VkImageViewCreateInfo view = {
933 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600934 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800935 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600936 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600937 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600938 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600939 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800940 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600941 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800942 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600943 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600945 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600946 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600947
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500948 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600949 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600950 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600951
952 demo->depth.format = depth_format;
953
954 /* create image */
Chia-I Wu63636062015-10-26 21:10:41 +0800955 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956 &demo->depth.image);
957 assert(!err);
958
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600959 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600960 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600961 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600962
Chia-I Wuf48700b2015-11-10 16:21:09 +0800963 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200964 demo->depth.mem_alloc.pNext = NULL;
965 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
966 demo->depth.mem_alloc.memoryTypeIndex = 0;
967
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600968 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600969 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600970 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200971 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600972 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600973
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500974 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800975 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +0800976 NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500977 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500979 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600980 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500981 demo->depth.mem, 0);
982 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600984 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600985 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600986 VK_IMAGE_LAYOUT_UNDEFINED,
987 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600988
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600989 /* create image view */
990 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800991 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600992 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600993}
994
Tony Barbour1a86d032015-09-21 15:17:33 -0600995/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700996bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600997 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600998 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600999{
Tony Barbour1a86d032015-09-21 15:17:33 -06001000 FILE *fPtr = fopen(filename,"rb");
1001 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001002
Tony Barbour1a86d032015-09-21 15:17:33 -06001003 if (!fPtr)
1004 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001005
Tony Barbour1a86d032015-09-21 15:17:33 -06001006 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001007 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1008 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001009 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001010 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001011
Tony Barbour1a86d032015-09-21 15:17:33 -06001012 do {
1013 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001014 if (cPtr == NULL) {
1015 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001016 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001017 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001018 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001019
Tony Barbour1a86d032015-09-21 15:17:33 -06001020 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001021 if (rgba_data == NULL) {
1022 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001023 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001024 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001025 fgets(header, 256, fPtr); // Format
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001026 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1027 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001028 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001029 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001030
Tony Barbour1a86d032015-09-21 15:17:33 -06001031 for(int y = 0; y < *height; y++)
1032 {
1033 uint8_t *rowPtr = rgba_data;
1034 for(int x = 0; x < *width; x++)
1035 {
1036 fread(rowPtr, 3, 1, fPtr);
1037 rowPtr[3] = 255; /* Alpha of 1 */
1038 rowPtr += 4;
1039 }
1040 rgba_data += layout->rowPitch;
1041 }
1042 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001043 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001044}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001045
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046static void demo_prepare_texture_image(struct demo *demo,
1047 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001048 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001049 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001050 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001051 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001053 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001054 int32_t tex_width;
1055 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001056 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001057 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001058
David Pinedo30bd71d2015-04-23 08:16:57 -06001059 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1060 {
1061 printf("Failed to load textures\n");
1062 fflush(stdout);
1063 exit(1);
1064 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001066 tex_obj->tex_width = tex_width;
1067 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001068
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001069 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001072 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 .format = tex_format,
1074 .extent = { tex_width, tex_height, 1 },
1075 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001076 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001077 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001079 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080 .flags = 0,
1081 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001083 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001084
Chia-I Wu63636062015-10-26 21:10:41 +08001085 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001086 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001087 assert(!err);
1088
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001089 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001090
Chia-I Wuf48700b2015-11-10 16:21:09 +08001091 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001092 tex_obj->mem_alloc.pNext = NULL;
1093 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1094 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001095
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001096 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1097 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001098
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001099 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001100 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001101 &(tex_obj->mem));
1102 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001104 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001105 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001106 tex_obj->mem, 0);
1107 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001108
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001109 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001110 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001111 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001112 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001113 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001114 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001115 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001116 void *data;
1117
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001118 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001120 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121 assert(!err);
1122
1123 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1124 fprintf(stderr, "Error loading texture: %s\n", filename);
1125 }
1126
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001127 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001128 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001129
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001130 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001131 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001132 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001133 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001134 tex_obj->imageLayout);
1135 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001136}
1137
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001138static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139{
1140 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001141 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1142 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143}
1144
1145static void demo_prepare_textures(struct demo *demo)
1146{
Tony Barbour72304ef2015-04-16 15:59:00 -06001147 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001148 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001149 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001150
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001151 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001152
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001153 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001154 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155
FslNopper6a7e50e2015-05-06 21:42:01 +02001156 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001157 /* Device can texture using linear textures */
1158 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001159 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001160 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001161 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001162 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163
1164 memset(&staging_texture, 0, sizeof(staging_texture));
1165 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001166 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167
1168 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001169 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001170 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wuc42afd72015-10-27 18:53:22 +08001171 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001173 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001174 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001175 staging_texture.imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001176 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001177
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001178 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001179 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001180 demo->textures[i].imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001181 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001183 VkImageCopy copy_region = {
Tony Barbour466f0012015-11-02 11:47:51 -07001184 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001185 .srcOffset = { 0, 0, 0 },
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001186 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1187 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001188 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1189 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001190 vkCmdCopyImage(demo->cmd,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001191 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1192 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001193 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001194
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001195 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001196 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001197 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001198 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001200 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001202 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001203 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001204 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1205 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001206 }
1207
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001208 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001209 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001211 .magFilter = VK_FILTER_NEAREST,
1212 .minFilter = VK_FILTER_NEAREST,
1213 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001214 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1215 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1216 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001217 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001218 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001220 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001221 .minLod = 0.0f,
1222 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001223 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001224 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001226
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001227 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001228 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001230 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001231 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001232 .format = tex_format,
Chia-I Wub6d30c62015-10-27 19:00:15 +08001233 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuf051d262015-10-27 19:25:11 +08001234 VK_COMPONENT_SWIZZLE_G,
1235 VK_COMPONENT_SWIZZLE_B,
1236 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001237 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001238 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001240
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001242 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001243 &demo->textures[i].sampler);
1244 assert(!err);
1245
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001246 /* create image view */
1247 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001248 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001249 &demo->textures[i].view);
1250 assert(!err);
1251 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001253
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001254void demo_prepare_cube_data_buffer(struct demo *demo)
1255{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001256 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001257 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001258 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001259 int i;
1260 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001261 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001262 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001263 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001264
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001265 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001266 mat4x4_mul(MVP, VP, demo->model_matrix);
1267 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001268// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001269
1270 for (i=0; i<12*3; i++) {
1271 data.position[i][0] = g_vertex_buffer_data[i*3];
1272 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1273 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1274 data.position[i][3] = 1.0f;
1275 data.attr[i][0] = g_uv_buffer_data[2*i];
1276 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1277 data.attr[i][2] = 0;
1278 data.attr[i][3] = 0;
1279 }
1280
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001281 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001282 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001283 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001284 buf_info.size = sizeof(data);
Chia-I Wu63636062015-10-26 21:10:41 +08001285 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1286 &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001287 assert(!err);
1288
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001289 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001290
Chia-I Wuf48700b2015-11-10 16:21:09 +08001291 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001292 demo->uniform_data.mem_alloc.pNext = NULL;
1293 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1294 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1295
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001296 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001297 mem_reqs.memoryTypeBits,
1298 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001299 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001300 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001301
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001302 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +08001303 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001304 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001305
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001306 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001307 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001308
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001309 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001310
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001311 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001312
Tony Barbour155cce82015-07-03 10:33:54 -06001313 err = vkBindBufferMemory(demo->device,
1314 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001315 demo->uniform_data.mem, 0);
1316 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001317
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001318 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1319 demo->uniform_data.buffer_info.offset = 0;
1320 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001321}
1322
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001323static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001324{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001325 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001326 [0] = {
Chia-I Wued577562015-10-31 00:31:16 +08001327 .binding = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001328 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001329 .descriptorCount = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001330 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001331 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001332 },
1333 [1] = {
Chia-I Wued577562015-10-31 00:31:16 +08001334 .binding = 1,
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001335 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001336 .descriptorCount = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001337 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001338 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001339 },
1340 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001341 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001342 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001343 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001344 .bindingCount = 2,
Chia-I Wu0e76e3f2015-10-31 00:31:16 +08001345 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001346 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001347 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001348
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001349 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001350 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001351 assert(!err);
1352
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001353 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1354 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1355 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001356 .setLayoutCount = 1,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001357 .pSetLayouts = &demo->desc_layout,
1358 };
1359
1360 err = vkCreatePipelineLayout(demo->device,
1361 &pPipelineLayoutCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +08001362 NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001363 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365}
1366
Chia-I Wuf973e322015-07-08 13:34:24 +08001367static void demo_prepare_render_pass(struct demo *demo)
1368{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001369 const VkAttachmentDescription attachments[2] = {
1370 [0] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001371 .format = demo->format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001372 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001373 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1374 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1375 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1376 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1377 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1378 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1379 },
1380 [1] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001381 .format = demo->depth.format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001382 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001383 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1384 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1385 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1386 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1387 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1388 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1389 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001390 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001391 const VkAttachmentReference color_reference = {
1392 .attachment = 0,
1393 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1394 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001395 const VkAttachmentReference depth_reference = {
1396 .attachment = 1,
1397 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1398 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001399 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001400 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1401 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001402 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001403 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001404 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001405 .pColorAttachments = &color_reference,
1406 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001407 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001408 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001409 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001410 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001411 const VkRenderPassCreateInfo rp_info = {
1412 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1413 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001414 .attachmentCount = 2,
1415 .pAttachments = attachments,
1416 .subpassCount = 1,
1417 .pSubpasses = &subpass,
1418 .dependencyCount = 0,
1419 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001420 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001421 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001422
Chia-I Wu63636062015-10-26 21:10:41 +08001423 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001424 assert(!err);
1425}
1426
Chia-I Wu46176f12015-10-31 00:31:16 +08001427static VkShaderModule demo_prepare_shader_module(struct demo* demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001428 const void* code,
1429 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430{
Chia-I Wu46176f12015-10-31 00:31:16 +08001431 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001432 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001433 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001435 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1436 moduleCreateInfo.pNext = NULL;
1437
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001438 moduleCreateInfo.codeSize = size;
1439 moduleCreateInfo.pCode = code;
1440 moduleCreateInfo.flags = 0;
1441 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1442 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001443
1444 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001445}
1446
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001447char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001448{
1449 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001450 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001451 void *shader_code;
1452
1453 FILE *fp = fopen(filename, "rb");
1454 if (!fp) return NULL;
1455
1456 fseek(fp, 0L, SEEK_END);
1457 size = ftell(fp);
1458
1459 fseek(fp, 0L, SEEK_SET);
1460
1461 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001462 retval = fread(shader_code, size, 1, fp);
1463 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001464
1465 *psize = size;
1466
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001467 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001468 return shader_code;
1469}
1470
Chia-I Wu46176f12015-10-31 00:31:16 +08001471static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001473 void *vertShaderCode;
1474 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001475
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001476 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1477
Tony Barbourf331eb32015-11-06 10:10:37 -07001478 demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001479
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001480 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001481
1482 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001483}
1484
Chia-I Wu46176f12015-10-31 00:31:16 +08001485static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001486{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001487 void *fragShaderCode;
1488 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001489
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001490 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001491
Tony Barbourf331eb32015-11-06 10:10:37 -07001492 demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001493
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001494 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001495
1496 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001497}
1498
1499static void demo_prepare_pipeline(struct demo *demo)
1500{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001501 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001502 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001503 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001504 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001505 VkPipelineRasterizationStateCreateInfo rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001506 VkPipelineColorBlendStateCreateInfo cb;
1507 VkPipelineDepthStencilStateCreateInfo ds;
1508 VkPipelineViewportStateCreateInfo vp;
1509 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001510 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniellba839d12015-09-29 13:01:09 -06001511 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001512 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001513
Piers Daniellba839d12015-09-29 13:01:09 -06001514 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1515 memset(&dynamicState, 0, sizeof dynamicState);
1516 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1517 dynamicState.pDynamicStates = dynamicStateEnables;
1518
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001520 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001521 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001523 memset(&vi, 0, sizeof(vi));
1524 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1525
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001526 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001527 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001528 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001529
1530 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001531 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1532 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001533 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001534 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001535 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001536 rs.rasterizerDiscardEnable = VK_FALSE;
1537 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001538
1539 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001540 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1541 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001542 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001543 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001544 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001545 cb.attachmentCount = 1;
1546 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001547
Tony Barbour29645d02015-01-16 14:27:35 -07001548 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001549 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001550 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001551 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1552 vp.scissorCount = 1;
1553 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001554
1555 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001556 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001557 ds.depthTestEnable = VK_TRUE;
1558 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001559 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001560 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001561 ds.back.failOp = VK_STENCIL_OP_KEEP;
1562 ds.back.passOp = VK_STENCIL_OP_KEEP;
1563 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001564 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001565 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566
Tony Barbour29645d02015-01-16 14:27:35 -07001567 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001568 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001569 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001570 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001572 // Two stages: vs and fs
1573 pipeline.stageCount = 2;
1574 VkPipelineShaderStageCreateInfo shaderStages[2];
1575 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1576
1577 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001578 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1579 shaderStages[0].module = demo_prepare_vs(demo);
1580 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001581
1582 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001583 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1584 shaderStages[1].module = demo_prepare_fs(demo);
1585 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001586
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001587 memset(&pipelineCache, 0, sizeof(pipelineCache));
1588 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001589
Chia-I Wu63636062015-10-26 21:10:41 +08001590 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001591 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001592
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001593 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001594 pipeline.pInputAssemblyState = &ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001595 pipeline.pRasterizationState = &rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001596 pipeline.pColorBlendState = &cb;
1597 pipeline.pMultisampleState = &ms;
1598 pipeline.pViewportState = &vp;
1599 pipeline.pDepthStencilState = &ds;
1600 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001601 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001602 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001603
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001604 pipeline.renderPass = demo->render_pass;
1605
Chia-I Wu63636062015-10-26 21:10:41 +08001606 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1607 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001608 assert(!err);
1609
Chia-I Wu63636062015-10-26 21:10:41 +08001610 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1611 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001612}
1613
Chia-I Wu63ea9262015-03-26 13:14:16 +08001614static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001615{
Chia-I Wuf051d262015-10-27 19:25:11 +08001616 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001617 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001618 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001619 .descriptorCount = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001620 },
1621 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001622 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001623 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001624 },
1625 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001626 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001627 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001628 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001629 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001630 .poolSizeCount = 2,
1631 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001632 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001633 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001634
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001635 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001636 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001637 assert(!err);
1638}
1639
1640static void demo_prepare_descriptor_set(struct demo *demo)
1641{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001642 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001643 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001644 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001645 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001646
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001647 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001648 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001649 .pNext = NULL,
1650 .descriptorPool = demo->desc_pool,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001651 .setLayoutCount = 1,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001652 .pSetLayouts = &demo->desc_layout
1653 };
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001654 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001655 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001656
Chia-I Wuae721ba2015-05-25 16:27:55 +08001657 memset(&tex_descs, 0, sizeof(tex_descs));
1658 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001659 tex_descs[i].sampler = demo->textures[i].sampler;
1660 tex_descs[i].imageView = demo->textures[i].view;
1661 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001662 }
1663
1664 memset(&writes, 0, sizeof(writes));
1665
1666 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001667 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001668 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001669 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001670 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001671
1672 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001673 writes[1].dstSet = demo->desc_set;
1674 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001675 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001676 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001677 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001678
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001679 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001680}
1681
Chia-I Wuf973e322015-07-08 13:34:24 +08001682static void demo_prepare_framebuffers(struct demo *demo)
1683{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001684 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001685 attachments[1] = demo->depth.view;
1686
Chia-I Wuf973e322015-07-08 13:34:24 +08001687 const VkFramebufferCreateInfo fb_info = {
1688 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1689 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001690 .renderPass = demo->render_pass,
1691 .attachmentCount = 2,
1692 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001693 .width = demo->width,
1694 .height = demo->height,
1695 .layers = 1,
1696 };
1697 VkResult U_ASSERT_ONLY err;
1698 uint32_t i;
1699
Tony Barbourd8e504f2015-10-08 14:26:24 -06001700 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1701 assert(demo->framebuffers);
1702
1703 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001704 attachments[0] = demo->buffers[i].view;
Chia-I Wu63636062015-10-26 21:10:41 +08001705 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001706 assert(!err);
1707 }
1708}
1709
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710static void demo_prepare(struct demo *demo)
1711{
Cody Northrop91e221b2015-07-09 18:08:32 -06001712 VkResult U_ASSERT_ONLY err;
1713
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001714 const VkCommandPoolCreateInfo cmd_pool_info = {
1715 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001716 .pNext = NULL,
1717 .queueFamilyIndex = demo->graphics_queue_node_index,
1718 .flags = 0,
1719 };
Chia-I Wu63636062015-10-26 21:10:41 +08001720 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001721 assert(!err);
1722
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001723 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001724 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001725 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001726 .commandPool = demo->cmd_pool,
1727 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001728 .bufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730
1731 demo_prepare_buffers(demo);
1732 demo_prepare_depth(demo);
1733 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001734 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001735
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001736 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001737 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739
Ian Elliotta0781972015-08-21 15:09:33 -06001740 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001741 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001742 assert(!err);
1743 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001744
Chia-I Wu63ea9262015-03-26 13:14:16 +08001745 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001746 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001747
Chia-I Wuf973e322015-07-08 13:34:24 +08001748 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001749
Ian Elliotta0781972015-08-21 15:09:33 -06001750 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001751 demo->current_buffer = i;
1752 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1753 }
1754
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001755 /*
1756 * Prepare functions above may generate pipeline commands
1757 * that need to be flushed before beginning the render loop.
1758 */
1759 demo_flush_init_cmd(demo);
1760
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001761 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001762 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001763}
1764
David Pinedo2bb7c932015-06-18 17:03:14 -06001765static void demo_cleanup(struct demo *demo)
1766{
1767 uint32_t i;
1768
1769 demo->prepared = false;
1770
Tony Barbourd8e504f2015-10-08 14:26:24 -06001771 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001772 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001773 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001774 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001775 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001776
Chia-I Wu63636062015-10-26 21:10:41 +08001777 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1778 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1779 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1780 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1781 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001782
1783 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001784 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1785 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1786 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1787 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001788 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001789 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001790
Chia-I Wu63636062015-10-26 21:10:41 +08001791 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1792 vkDestroyImage(demo->device, demo->depth.image, NULL);
1793 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001794
Chia-I Wu63636062015-10-26 21:10:41 +08001795 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1796 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001797
Ian Elliotta0781972015-08-21 15:09:33 -06001798 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001799 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001800 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001801 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001802 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001803
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001804 free(demo->queue_props);
1805
Chia-I Wu63636062015-10-26 21:10:41 +08001806 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1807 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001808 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001809 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001810 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001811 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001812 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001813
1814#ifndef _WIN32
1815 xcb_destroy_window(demo->connection, demo->window);
1816 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001817 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001818#endif // _WIN32
1819}
1820
Ian Elliottcf074d32015-10-16 18:02:43 -06001821static void demo_resize(struct demo *demo)
1822{
1823 uint32_t i;
1824
Mike Stroyanbb60b382015-10-28 09:46:57 -06001825 // Don't react to resize until after first initialization.
1826 if (!demo->prepared) {
1827 return;
1828 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001829 // In order to properly resize the window, we must re-create the swapchain
1830 // AND redo the command buffers, etc.
1831 //
1832 // First, perform part of the demo_cleanup() function:
1833 demo->prepared = false;
1834
1835 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001836 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001837 }
1838 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001839 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001840
Chia-I Wu63636062015-10-26 21:10:41 +08001841 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1842 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1843 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1844 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1845 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001846
1847 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001848 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1849 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1850 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1851 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001852 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001853
Chia-I Wu63636062015-10-26 21:10:41 +08001854 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1855 vkDestroyImage(demo->device, demo->depth.image, NULL);
1856 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001857
Chia-I Wu63636062015-10-26 21:10:41 +08001858 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1859 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001860
1861 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001862 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001863 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001864 }
Chia-I Wu63636062015-10-26 21:10:41 +08001865 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001866 free(demo->buffers);
1867
Ian Elliottcf074d32015-10-16 18:02:43 -06001868 // Second, re-perform the demo_prepare() function, which will re-create the
1869 // swapchain:
1870 demo_prepare(demo);
1871}
1872
David Pinedo2bb7c932015-06-18 17:03:14 -06001873// On MS-Windows, make this a global, so it's available to WndProc()
1874struct demo demo;
1875
Ian Elliott639ca472015-04-16 15:23:05 -06001876#ifdef _WIN32
1877static void demo_run(struct demo *demo)
1878{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001879 if (!demo->prepared)
1880 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001881 // Wait for work to finish before updating MVP.
1882 vkDeviceWaitIdle(demo->device);
1883 demo_update_data_buffer(demo);
1884
1885 demo_draw(demo);
1886
1887 // Wait for work to finish before updating MVP.
1888 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001889
David Pinedo2bb7c932015-06-18 17:03:14 -06001890 demo->curFrame++;
1891
1892 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1893 {
1894 demo->quit=true;
1895 demo_cleanup(demo);
1896 ExitProcess(0);
1897 }
1898
1899}
Ian Elliott639ca472015-04-16 15:23:05 -06001900
1901// MS-Windows event handling function:
1902LRESULT CALLBACK WndProc(HWND hWnd,
1903 UINT uMsg,
1904 WPARAM wParam,
1905 LPARAM lParam)
1906{
Ian Elliott639ca472015-04-16 15:23:05 -06001907 switch(uMsg)
1908 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001909 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001910 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001911 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001912 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001913 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001914 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001915 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001916 demo.width = lParam & 0xffff;
1917 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001918 demo_resize(&demo);
1919 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001920 default:
1921 break;
1922 }
1923 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1924}
1925
1926static void demo_create_window(struct demo *demo)
1927{
1928 WNDCLASSEX win_class;
1929
1930 // Initialize the window class structure:
1931 win_class.cbSize = sizeof(WNDCLASSEX);
1932 win_class.style = CS_HREDRAW | CS_VREDRAW;
1933 win_class.lpfnWndProc = WndProc;
1934 win_class.cbClsExtra = 0;
1935 win_class.cbWndExtra = 0;
1936 win_class.hInstance = demo->connection; // hInstance
1937 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1938 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1939 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1940 win_class.lpszMenuName = NULL;
1941 win_class.lpszClassName = demo->name;
1942 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1943 // Register window class:
1944 if (!RegisterClassEx(&win_class)) {
1945 // It didn't work, so try to give a useful error:
1946 printf("Unexpected error trying to start the application!\n");
1947 fflush(stdout);
1948 exit(1);
1949 }
1950 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001951 RECT wr = { 0, 0, demo->width, demo->height };
1952 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001953 demo->window = CreateWindowEx(0,
1954 demo->name, // class name
1955 demo->name, // app name
1956 WS_OVERLAPPEDWINDOW | // window style
1957 WS_VISIBLE |
1958 WS_SYSMENU,
1959 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001960 wr.right-wr.left, // width
1961 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001962 NULL, // handle to parent
1963 NULL, // handle to menu
1964 demo->connection, // hInstance
1965 NULL); // no extra parameters
1966 if (!demo->window) {
1967 // It didn't work, so try to give a useful error:
1968 printf("Cannot create a window in which to draw!\n");
1969 fflush(stdout);
1970 exit(1);
1971 }
1972}
1973#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001974static void demo_handle_event(struct demo *demo,
1975 const xcb_generic_event_t *event)
1976{
Piers Daniell735ee532015-02-23 16:23:13 -07001977 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001978 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001979 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001980 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001981 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001982 case XCB_CLIENT_MESSAGE:
1983 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1984 (*demo->atom_wm_delete_window).atom) {
1985 demo->quit = true;
1986 }
1987 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001988 case XCB_KEY_RELEASE:
1989 {
1990 const xcb_key_release_event_t *key =
1991 (const xcb_key_release_event_t *) event;
1992
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001993 switch (key->detail) {
1994 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001995 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001996 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001997 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001998 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001999 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002000 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002001 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002002 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002003 case 0x41:
2004 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002005 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002006 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002007 }
2008 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06002009 case XCB_CONFIGURE_NOTIFY:
2010 {
2011 const xcb_configure_notify_event_t *cfg =
2012 (const xcb_configure_notify_event_t *) event;
2013 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2014 demo_resize(demo);
2015 }
2016 }
2017 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002018 default:
2019 break;
2020 }
2021}
2022
2023static void demo_run(struct demo *demo)
2024{
2025 xcb_flush(demo->connection);
2026
2027 while (!demo->quit) {
2028 xcb_generic_event_t *event;
2029
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002030 if (demo->pause) {
2031 event = xcb_wait_for_event(demo->connection);
2032 } else {
2033 event = xcb_poll_for_event(demo->connection);
2034 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002035 if (event) {
2036 demo_handle_event(demo, event);
2037 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002038 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002039
2040 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002041 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002042 demo_update_data_buffer(demo);
2043
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002044 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002045
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002046 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002047 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002048 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002049 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002050 demo->quit = true;
2051
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002052 }
2053}
2054
2055static void demo_create_window(struct demo *demo)
2056{
2057 uint32_t value_mask, value_list[32];
2058
2059 demo->window = xcb_generate_id(demo->connection);
2060
2061 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2062 value_list[0] = demo->screen->black_pixel;
2063 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002064 XCB_EVENT_MASK_EXPOSURE |
2065 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002066
2067 xcb_create_window(demo->connection,
2068 XCB_COPY_FROM_PARENT,
2069 demo->window, demo->screen->root,
2070 0, 0, demo->width, demo->height, 0,
2071 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2072 demo->screen->root_visual,
2073 value_mask, value_list);
2074
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002075 /* Magic code that will send notification when window is destroyed */
2076 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2077 "WM_PROTOCOLS");
2078 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2079
2080 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2081 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2082
2083 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2084 demo->window, (*reply).atom, 4, 32, 1,
2085 &(*demo->atom_wm_delete_window).atom);
2086 free(reply);
2087
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002088 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002089
2090 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2091 const uint32_t coords[] = {100, 100};
2092 xcb_configure_window(demo->connection, demo->window,
2093 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002094}
Ian Elliott639ca472015-04-16 15:23:05 -06002095#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002096
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002097/*
2098 * Return 1 (true) if all layer names specified in check_names
2099 * can be found in given layer properties.
2100 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002101static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002102 uint32_t layer_count, VkLayerProperties *layers)
2103{
2104 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002105 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002106 for (uint32_t j = 0; j < layer_count; j++) {
2107 if (!strcmp(check_names[i], layers[j].layerName)) {
2108 found = 1;
2109 }
2110 }
2111 if (!found) {
2112 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2113 return 0;
2114 }
2115 }
2116 return 1;
2117}
2118
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002119static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002120{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002121 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002122 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002123 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002124 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002125 VkLayerProperties *instance_layers;
2126 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002127 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002128 uint32_t instance_layer_count = 0;
2129 uint32_t enabled_extension_count = 0;
2130 uint32_t enabled_layer_count = 0;
2131
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002132 char *instance_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002133 "VK_LAYER_LUNARG_Threading",
2134 "VK_LAYER_LUNARG_MemTracker",
2135 "VK_LAYER_LUNARG_ObjectTracker",
2136 "VK_LAYER_LUNARG_DrawState",
2137 "VK_LAYER_LUNARG_ParamChecker",
Jon Ashburnf453b372015-11-25 08:25:11 -07002138 "VK_LAYER_LUNARG_Swapchain",
2139 "VK_LAYER_LUNARG_DeviceLimits",
2140 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002141 };
2142
2143 char *device_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002144 "VK_LAYER_LUNARG_Threading",
2145 "VK_LAYER_LUNARG_MemTracker",
2146 "VK_LAYER_LUNARG_ObjectTracker",
2147 "VK_LAYER_LUNARG_DrawState",
2148 "VK_LAYER_LUNARG_ParamChecker",
Jon Ashburnf453b372015-11-25 08:25:11 -07002149 "VK_LAYER_LUNARG_Swapchain",
2150 "VK_LAYER_LUNARG_DeviceLimits",
2151 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002152 };
2153
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002154 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002155 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002156 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002157 assert(!err);
2158
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002159 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002160 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002161 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002162
2163 if (demo->validate) {
2164 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2165 instance_layer_count, instance_layers);
2166 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002167 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002168 "required validation layer.\n\n"
2169 "Please look at the Getting Started guide for additional "
2170 "information.\n",
2171 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002172 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002173 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002174 }
2175
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002176 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 assert(!err);
2178
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002179 VkBool32 surfaceExtFound = 0;
2180 VkBool32 platformSurfaceExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002181 memset(extension_names, 0, sizeof(extension_names));
2182 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002183 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002184 assert(!err);
2185 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002186 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002187 surfaceExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002188 extension_names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002189 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002190#ifdef _WIN32
2191 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2192 platformSurfaceExtFound = 1;
2193 extension_names[enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2194 }
2195#else // _WIN32
2196 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2197 platformSurfaceExtFound = 1;
2198 extension_names[enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2199 }
2200#endif // _WIN32
Courtney Goeltzenleuchter96403642015-11-25 13:40:37 -07002201 if (!strcmp(VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002202 if (demo->validate) {
Courtney Goeltzenleuchter96403642015-11-25 13:40:37 -07002203 extension_names[enabled_extension_count++] = VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002204 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002205 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002206 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002207 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002208 if (!surfaceExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002209 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002210 VK_KHR_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002211 "Vulkan installable client driver (ICD) installed?\nPlease "
2212 "look at the Getting Started guide for additional "
2213 "information.\n",
2214 "vkCreateInstance Failure");
2215 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002216 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002217#ifdef _WIN32
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002218 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002219 VK_KHR_WIN32_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002220 "Vulkan installable client driver (ICD) installed?\nPlease "
2221 "look at the Getting Started guide for additional "
2222 "information.\n",
2223 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002224#else // _WIN32
2225 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2226 VK_KHR_XCB_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2227 "Vulkan installable client driver (ICD) installed?\nPlease "
2228 "look at the Getting Started guide for additional "
2229 "information.\n",
2230 "vkCreateInstance Failure");
2231#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002232 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002233 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002234 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002235 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002236 .pApplicationName = APP_SHORT_NAME,
2237 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002238 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002240 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002241 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002242 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002243 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002244 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002245 .pApplicationInfo = &app,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002246 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002247 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002248 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002249 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002250 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002251
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002252 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002253
Chia-I Wu63636062015-10-26 21:10:41 +08002254 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002255 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2256 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002257 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002258 "additional information.\n",
2259 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002260 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002261 ERR_EXIT("Cannot find a specified extension library"
2262 ".\nMake sure your layers path is set appropriately\n",
2263 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002264 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002265 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2266 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002267 "the Getting Started guide for additional information.\n",
2268 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002269 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002270
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002271 free(instance_layers);
2272 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002273
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002274 /* Make initial call to query gpu_count, then second call for gpu info*/
2275 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2276 assert(!err && gpu_count > 0);
2277 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2278 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2279 assert(!err);
2280 /* For cube demo we just grab the first physical device */
2281 demo->gpu = physical_devices[0];
2282 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002283
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002284 /* Look for validation layers */
2285 validation_found = 0;
2286 enabled_layer_count = 0;
2287 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002288 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002289 assert(!err);
2290
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002291 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002292 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002293 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002294
2295 if (demo->validate) {
2296 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2297 device_layer_count, device_layers);
2298 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002299 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002300 "a required validation layer.\n\n"
2301 "Please look at the Getting Started guide for additional "
2302 "information.\n",
2303 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002304 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002305 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002306 }
2307
2308 uint32_t device_extension_count = 0;
2309 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002310 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002311 demo->gpu, NULL, &device_extension_count, NULL);
2312 assert(!err);
2313
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002314 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002315 enabled_extension_count = 0;
2316 memset(extension_names, 0, sizeof(extension_names));
2317 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002318 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002319 demo->gpu, NULL, &device_extension_count, device_extensions);
2320 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002321
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002322 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002323 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002324 swapchainExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002325 extension_names[enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002326 }
2327 assert(enabled_extension_count < 64);
2328 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002329 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002330 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002331 VK_KHR_SWAPCHAIN_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002332 "Vulkan installable client driver (ICD) installed?\nPlease "
2333 "look at the Getting Started guide for additional "
2334 "information.\n",
2335 "vkCreateInstance Failure");
2336 }
2337
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002338 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002339 demo->CreateDebugReportCallback = (PFN_vkCreateDebugReportCallbackLUNARG) vkGetInstanceProcAddr(demo->inst, "vkCreateDebugReportCallbackLUNARG");
2340 demo->DestroyDebugReportCallback = (PFN_vkDestroyDebugReportCallbackLUNARG) vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugReportCallbackLUNARG");
2341 if (!demo->CreateDebugReportCallback) {
2342 ERR_EXIT("GetProcAddr: Unable to find vkCreateDebugReportCallbackLUNARG\n",
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002343 "vkGetProcAddr Failure");
2344 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002345 if (!demo->DestroyDebugReportCallback) {
2346 ERR_EXIT("GetProcAddr: Unable to find vkDestroyDebugReportCallbackLUNARG\n",
Tony Barbourd70b42c2015-06-30 14:14:19 -06002347 "vkGetProcAddr Failure");
2348 }
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002349 demo->DebugReportMessage = (PFN_vkDebugReportMessageLUNARG) vkGetInstanceProcAddr(demo->inst, "vkDebugReportMessageLUNARG");
2350 if (!demo->DebugReportMessage) {
2351 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageLUNARG\n",
2352 "vkGetProcAddr Failure");
2353 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002354
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002355 PFN_vkDebugReportCallbackLUNARG callback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002356
2357 if (!demo->use_break) {
2358 callback = dbgFunc;
2359 } else {
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002360 callback = dbgFunc;
2361 // TODO add a break callback defined locally since there is no longer
2362 // one included in the loader
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002363 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002364 VkDebugReportCallbackCreateInfoLUNARG dbgCreateInfo;
2365 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_LUNARG;
2366 dbgCreateInfo.pNext = NULL;
2367 dbgCreateInfo.pfnCallback = callback;
2368 dbgCreateInfo.pUserData = NULL;
2369 dbgCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT | VK_DEBUG_REPORT_WARN_BIT;
2370 err = demo->CreateDebugReportCallback(
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002371 demo->inst,
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002372 &dbgCreateInfo,
2373 NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002374 &demo->msg_callback);
2375 switch (err) {
2376 case VK_SUCCESS:
2377 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002378 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002379 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2380 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002381 break;
2382 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002383 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2384 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002385 break;
2386 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002387 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002388 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002389
2390 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002391 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002392 assert(demo->queue_count >= 1);
2393
2394 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002395 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002396 // Find a queue that supports gfx
2397 uint32_t gfx_queue_idx = 0;
2398 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2399 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2400 break;
2401 }
2402 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002403 // Query fine-grained feature support for this device.
2404 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002405 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002406 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002407
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002408 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002409 const VkDeviceQueueCreateInfo queue = {
2410 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2411 .pNext = NULL,
2412 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu8b497442015-11-06 06:42:02 +08002413 .queueCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002414 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002415 };
2416
2417 VkDeviceCreateInfo device = {
2418 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2419 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002420 .queueCreateInfoCount = 1,
2421 .pQueueCreateInfos = &queue,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002422 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002423 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002424 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002425 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002426 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002427 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002428
Chia-I Wu63636062015-10-26 21:10:41 +08002429 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002430 assert(!err);
2431
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002432 free(device_layers);
2433
Ian Elliotta0781972015-08-21 15:09:33 -06002434 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
Ian Elliottf2ff8022015-11-23 12:48:15 -07002435 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2436 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2437 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002438 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002439 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2440 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2441 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2442 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002443}
2444
Tony Barbourcc69f452015-10-08 13:59:42 -06002445static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002446{
2447 VkResult err;
2448 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002449
Ian Elliottb08e0d92015-11-20 11:55:46 -07002450 // Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002451#ifdef _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -07002452 err = vkCreateWin32SurfaceKHR(demo->inst, demo->connection, demo->window,
2453 NULL, &demo->surface);
2454
Ian Elliottaaae5352015-07-06 14:27:58 -06002455#else // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -07002456 err = vkCreateXcbSurfaceKHR(demo->inst, demo->connection, demo->window,
2457 NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002458#endif // _WIN32
2459
Tony Barbourcc69f452015-10-08 13:59:42 -06002460 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002461 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2462 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002463 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
Ian Elliottb08e0d92015-11-20 11:55:46 -07002464 demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002465 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002466 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002467
2468 // Search for a graphics and a present queue in the array of queue
2469 // families, try to find one that supports both
2470 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2471 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002472 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002473 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2474 if (graphicsQueueNodeIndex == UINT32_MAX) {
2475 graphicsQueueNodeIndex = i;
2476 }
2477
2478 if (supportsPresent[i] == VK_TRUE) {
2479 graphicsQueueNodeIndex = i;
2480 presentQueueNodeIndex = i;
2481 break;
2482 }
2483 }
2484 }
2485 if (presentQueueNodeIndex == UINT32_MAX) {
2486 // If didn't find a queue that supports both graphics and present, then
2487 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002488 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002489 if (supportsPresent[i] == VK_TRUE) {
2490 presentQueueNodeIndex = i;
2491 break;
2492 }
2493 }
2494 }
2495 free(supportsPresent);
2496
2497 // Generate error if could not find both a graphics and a present queue
2498 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2499 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002500 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002501 }
2502
2503 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002504 // synchronization, and appropriate tracking for QueueSubmit.
2505 // NOTE: While it is possible for an application to use a separate graphics
2506 // and a present queues, this demo program assumes it is only using
2507 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002508 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2509 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002510 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002511 }
2512
2513 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002514
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002515 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002516 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002517
Ian Elliottaaae5352015-07-06 14:27:58 -06002518 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002519 uint32_t formatCount;
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002520 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002521 demo->surface,
2522 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002523 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002524 VkSurfaceFormatKHR *surfFormats =
2525 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002526 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002527 demo->surface,
2528 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002529 assert(!err);
2530 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2531 // the surface has no preferred format. Otherwise, at least one
2532 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002533 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2534 {
2535 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2536 }
2537 else
2538 {
2539 assert(formatCount >= 1);
2540 demo->format = surfFormats[0].format;
2541 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002542 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002543
David Pinedo2bb7c932015-06-18 17:03:14 -06002544 demo->quit = false;
2545 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002546
2547 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002548 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002549}
2550
2551static void demo_init_connection(struct demo *demo)
2552{
Ian Elliott639ca472015-04-16 15:23:05 -06002553#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002554 const xcb_setup_t *setup;
2555 xcb_screen_iterator_t iter;
2556 int scr;
2557
2558 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002559 if (demo->connection == NULL) {
2560 printf("Cannot find a compatible Vulkan installable client driver "
2561 "(ICD).\nExiting ...\n");
2562 fflush(stdout);
2563 exit(1);
2564 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002565
2566 setup = xcb_get_setup(demo->connection);
2567 iter = xcb_setup_roots_iterator(setup);
2568 while (scr-- > 0)
2569 xcb_screen_next(&iter);
2570
2571 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002572#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002573}
2574
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002575static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002576{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002577 vec3 eye = {0.0f, 3.0f, 5.0f};
2578 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002579 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002580
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002581 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002582 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002583
Piers Daniell735ee532015-02-23 16:23:13 -07002584 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002585 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002586 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002587 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002588 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002589 if (strcmp(argv[i], "--break") == 0) {
2590 demo->use_break = true;
2591 continue;
2592 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002593 if (strcmp(argv[i], "--validate") == 0) {
2594 demo->validate = true;
2595 continue;
2596 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002597 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002598 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002599 i < argc-1 &&
2600 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2601 demo->frameCount >= 0)
2602 {
2603 i++;
2604 continue;
2605 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002606
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002607 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002608 fflush(stderr);
2609 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002610 }
2611
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002612 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002613 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002614
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002615 demo->width = 500;
2616 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002617
2618 demo->spin_angle = 0.01f;
2619 demo->spin_increment = 0.01f;
2620 demo->pause = false;
2621
Piers Daniell735ee532015-02-23 16:23:13 -07002622 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002623 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2624 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002625}
2626
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002627
Ian Elliott639ca472015-04-16 15:23:05 -06002628#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002629extern int __getmainargs(
2630 int * _Argc,
2631 char *** _Argv,
2632 char *** _Env,
2633 int _DoWildCard,
2634 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002635
Ian Elliotta748eaf2015-04-28 15:50:36 -06002636int WINAPI WinMain(HINSTANCE hInstance,
2637 HINSTANCE hPrevInstance,
2638 LPSTR pCmdLine,
2639 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002640{
2641 MSG msg; // message
2642 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002643 int argc;
2644 char** argv;
2645 char** env;
2646 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002647
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002648 __getmainargs(&argc,&argv,&env,0,&new_mode);
2649
2650 demo_init(&demo, argc, argv);
2651 demo.connection = hInstance;
2652 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002653 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002654 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002655
2656 demo_prepare(&demo);
2657
2658 done = false; //initialize loop condition variable
2659 /* main message loop*/
2660 while(!done)
2661 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002662 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002663 if (msg.message == WM_QUIT) //check for a quit message
2664 {
2665 done = true; //if found, quit app
2666 }
2667 else
2668 {
2669 /* Translate and dispatch to event queue*/
2670 TranslateMessage(&msg);
2671 DispatchMessage(&msg);
2672 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002673 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002674 }
2675
2676 demo_cleanup(&demo);
2677
Tony Barbourf9921732015-04-22 11:36:22 -06002678 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002679}
2680#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002681int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002682{
2683 struct demo demo;
2684
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002685 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002686 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002687 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002688
2689 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002690 demo_run(&demo);
2691
2692 demo_cleanup(&demo);
2693
2694 return 0;
2695}
Ian Elliott639ca472015-04-16 15:23:05 -06002696#endif // _WIN32