blob: f72afe2ffe9f56945e6c1f3752b89bf891e1c67b [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
Ian Elliotta748eaf2015-04-28 15:50:36 -06004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060022 *
23 * Author: Chia-I Wu <olv@lunarg.com>
24 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
25 * Author: Ian Elliott <ian@LunarG.com>
26 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060027 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdbool.h>
33#include <assert.h>
34
Ian Elliott639ca472015-04-16 15:23:05 -060035#ifdef _WIN32
36#pragma comment(linker, "/subsystem:windows")
37#include <windows.h>
38#define APP_NAME_STR_LEN 80
39#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060040#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060041#endif // _WIN32
42
David Pinedoc5193c12015-11-06 12:54:48 -070043#include <vulkan/vulkan.h>
44#include <vulkan/vk_ext_khr_swapchain.h>
45#include <vulkan/vk_ext_khr_device_swapchain.h>
David Pinedo541526f2015-11-24 09:00:24 -070046#include <vulkan/vk_lunarg_debug_report.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060047
David Pinedo541526f2015-11-24 09:00:24 -070048#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060049#include "linmath.h"
50
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060051#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060055#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
56
Tony Barbourfdc2d352015-04-22 09:02:32 -060057#if defined(NDEBUG) && defined(__GNUC__)
58#define U_ASSERT_ONLY __attribute__((unused))
59#else
60#define U_ASSERT_ONLY
61#endif
62
Ian Elliott07264132015-04-28 11:35:02 -060063#ifdef _WIN32
64#define ERR_EXIT(err_msg, err_class) \
65 do { \
66 MessageBox(NULL, err_msg, err_class, MB_OK); \
67 exit(1); \
68 } while (0)
69
Ian Elliott07264132015-04-28 11:35:02 -060070#else // _WIN32
71
72#define ERR_EXIT(err_msg, err_class) \
73 do { \
74 printf(err_msg); \
75 fflush(stdout); \
76 exit(1); \
77 } while (0)
78#endif // _WIN32
79
Ian Elliottaaae5352015-07-06 14:27:58 -060080#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
81{ \
82 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
83 if (demo->fp##entrypoint == NULL) { \
84 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
85 "vkGetInstanceProcAddr Failure"); \
86 } \
87}
88
Jon Ashburn7d8342c2015-10-08 15:58:23 -060089static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
90
Ian Elliott673898b2015-06-22 15:07:49 -060091#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
92{ \
Jon Ashburn7d8342c2015-10-08 15:58:23 -060093 if(!g_gdpa) \
94 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
95 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060096 if (demo->fp##entrypoint == NULL) { \
97 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
98 "vkGetDeviceProcAddr Failure"); \
99 } \
100}
101
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600102/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700103 * structure to track all objects related to a texture.
104 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600105struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600106 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700107
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600108 VkImage image;
109 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600110
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800111 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500112 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600113 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700114 int32_t tex_width, tex_height;
115};
116
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600117static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600118 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600119};
120
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600121struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600122 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600123 float mvp[4][4];
124 float position[12*3][4];
125 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600126};
127
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600128struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600130 float mvp[4][4];
131 float position[12*3][4];
132 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600133};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600134
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600135//--------------------------------------------------------------------------------------
136// Mesh and VertexFormat Data
137//--------------------------------------------------------------------------------------
138struct Vertex
139{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600140 float posX, posY, posZ, posW; // Position data
141 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600142};
143
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600144struct VertexPosTex
145{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600146 float posX, posY, posZ, posW; // Position data
147 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600148};
149
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600151#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600152
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600153static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800154 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155 -1.0f,-1.0f, 1.0f,
156 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800157 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 -1.0f, 1.0f,-1.0f,
159 -1.0f,-1.0f,-1.0f,
160
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800161 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 1.0f, 1.0f,-1.0f,
163 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800164 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600166 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800168 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600170 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800171 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600173 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800175 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176 -1.0f, 1.0f, 1.0f,
177 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800178 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600180 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800182 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183 1.0f, 1.0f, 1.0f,
184 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600186 1.0f,-1.0f,-1.0f,
187 1.0f, 1.0f,-1.0f,
188
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800189 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600191 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600193 1.0f,-1.0f, 1.0f,
194 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600195};
196
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600197static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 0.0f, 0.0f, // -X side
199 1.0f, 0.0f,
200 1.0f, 1.0f,
201 1.0f, 1.0f,
202 0.0f, 1.0f,
203 0.0f, 0.0f,
204
205 1.0f, 0.0f, // -Z side
206 0.0f, 1.0f,
207 0.0f, 0.0f,
208 1.0f, 0.0f,
209 1.0f, 1.0f,
210 0.0f, 1.0f,
211
212 1.0f, 1.0f, // -Y side
213 1.0f, 0.0f,
214 0.0f, 0.0f,
215 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600216 0.0f, 0.0f,
217 0.0f, 1.0f,
218
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600220 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800221 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600222 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600224 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800226 1.0f, 1.0f, // +X side
227 0.0f, 1.0f,
228 0.0f, 0.0f,
229 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600230 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600231 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 0.0f, 1.0f, // +Z side
234 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600235 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600236 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600238 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600239};
240
241void dumpMatrix(const char *note, mat4x4 MVP)
242{
243 int i;
244
245 printf("%s: \n", note);
246 for (i=0; i<4; i++) {
247 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
248 }
249 printf("\n");
250 fflush(stdout);
251}
252
253void dumpVec4(const char *note, vec4 vector)
254{
255 printf("%s: \n", note);
256 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
257 printf("\n");
258 fflush(stdout);
259}
260
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600261VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600262 VkFlags msgFlags,
263 VkDbgObjectType objType,
264 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600265 size_t location,
266 int32_t msgCode,
267 const char* pLayerPrefix,
268 const char* pMsg,
269 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600270{
271 char *message = (char *) malloc(strlen(pMsg)+100);
272
273 assert (message);
274
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600275 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
276 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
277 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600278 // We know that we're submitting queues without fences, ignore this warning
279 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600280 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600281 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600282 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600283 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600284 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600285 }
286
287#ifdef _WIN32
288 MessageBox(NULL, message, "Alert", MB_OK);
289#else
290 printf("%s\n",message);
291 fflush(stdout);
292#endif
293 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600294
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600295 /*
296 * false indicates that layer should not bail-out of an
297 * API call that had validation failures. This may mean that the
298 * app dies inside the driver due to invalid parameter(s).
299 * That's what would happen without validation layers, so we'll
300 * keep that behavior here.
301 */
302 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600303}
304
Ian Elliotta0781972015-08-21 15:09:33 -0600305typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600306 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800307 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600308 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600309} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600310
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600312#ifdef _WIN32
313#define APP_NAME_STR_LEN 80
314 HINSTANCE connection; // hInstance - Windows Instance
315 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
316 HWND window; // hWnd - window handle
317#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318 xcb_connection_t *connection;
319 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800320 xcb_window_t window;
321 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotta0781972015-08-21 15:09:33 -0600322 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600323#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600324 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700325 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600328 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600329 VkDevice device;
330 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700331 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600332 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600333 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600334 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
336 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600337 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600338 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600339
Ian Elliotta0781972015-08-21 15:09:33 -0600340 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
341 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
342 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
343 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
344 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
345 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
346 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
347 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
348 PFN_vkQueuePresentKHR fpQueuePresentKHR;
349 VkSurfaceDescriptionWindowKHR surface_description;
350 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600351 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600352 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800354 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
356 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600357 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600358
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800360 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500361 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600362 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363 } depth;
364
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600365 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366
367 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600368 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800369 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500370 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600371 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600372 } uniform_data;
373
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800374 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500375 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600377 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800378 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600379 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600381 mat4x4 projection_matrix;
382 mat4x4 view_matrix;
383 mat4x4 model_matrix;
384
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600385 float spin_angle;
386 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600387 bool pause;
388
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600389 VkShaderModule vert_shader_module;
390 VkShaderModule frag_shader_module;
391
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 VkDescriptorPool desc_pool;
393 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800394
Tony Barbourd8e504f2015-10-08 14:26:24 -0600395 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800396
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600397 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600398 int32_t curFrame;
399 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600400 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600401 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600402 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600403 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600404 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600405 VkDbgMsgCallback msg_callback;
406
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600407 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600408 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600409};
410
Ian Elliottcf074d32015-10-16 18:02:43 -0600411// Forward declaration:
412static void demo_resize(struct demo *demo);
413
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600414static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600415{
416 // Search memtypes to find first index with those properties
417 for (uint32_t i = 0; i < 32; i++) {
418 if ((typeBits & 1) == 1) {
419 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600420 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600421 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600422 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600423 }
424 }
425 typeBits >>= 1;
426 }
427 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600428 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600429}
430
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431static void demo_flush_init_cmd(struct demo *demo)
432{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600433 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600434
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600435 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436 return;
437
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600438 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439 assert(!err);
440
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800441 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800442 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600443 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800444 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
445 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800446 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600447 .pWaitSemaphores = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800448 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600449 .pCommandBuffers = cmd_bufs,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800450 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600451 .pSignalSemaphores = NULL
452 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600453
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600454 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600455 assert(!err);
456
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 assert(!err);
459
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600460 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600462}
463
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600464static void demo_set_image_layout(
465 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600466 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600467 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600468 VkImageLayout old_image_layout,
469 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600471 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600473 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800474 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800475 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800477 .commandPool = demo->cmd_pool,
478 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800479 .bufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600480 };
481
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800482 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600483 assert(!err);
484
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800485 VkCommandBufferBeginInfo cmd_buf_info = {
486 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600488 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800489 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600490 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800491 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800492 .occlusionQueryEnable = VK_FALSE,
493 .queryFlags = 0,
494 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600496 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600497 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 }
499
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600500 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800503 .srcAccessMask = 0,
504 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505 .oldLayout = old_image_layout,
506 .newLayout = new_image_layout,
507 .image = image,
Jeremy Hayes105ca502015-11-17 18:19:55 -0700508 .subresourceRange = { aspectMask, 0, 1, 0, 1 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509 };
510
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800511 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600512 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800513 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600514 }
515
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700516 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
517 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
518 }
519
520 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
521 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
522 }
523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525 /* Make sure any Copy or CPU writes to image are flushed */
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700526 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600527 }
528
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600529 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530
Tony Barbour50bbca42015-06-29 16:20:35 -0600531 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
532 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533
Chia-I Wu86365072015-10-26 17:08:33 +0800534 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600535}
536
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800537static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538{
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800539 const VkCommandBufferBeginInfo cmd_buf_info = {
540 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600541 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600542 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800543 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600544 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800545 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800546 .occlusionQueryEnable = VK_FALSE,
547 .queryFlags = 0,
548 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700549 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800550 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600551 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
552 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800553 };
554 const VkRenderPassBeginInfo rp_begin = {
555 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
556 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800557 .renderPass = demo->render_pass,
558 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800559 .renderArea.offset.x = 0,
560 .renderArea.offset.y = 0,
561 .renderArea.extent.width = demo->width,
562 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600563 .clearValueCount = 2,
564 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700565 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800566 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600568 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600569 assert(!err);
570
Chia-I Wuf051d262015-10-27 19:25:11 +0800571 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800572
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600573 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600574 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600575 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600576 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600577
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600578 VkViewport viewport;
579 memset(&viewport, 0, sizeof(viewport));
580 viewport.height = (float) demo->height;
581 viewport.width = (float) demo->width;
582 viewport.minDepth = (float) 0.0f;
583 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600584 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600585
586 VkRect2D scissor;
587 memset(&scissor, 0, sizeof(scissor));
588 scissor.extent.width = demo->width;
589 scissor.extent.height = demo->height;
590 scissor.offset.x = 0;
591 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600592 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600593
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600594 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800595 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596
Tony Barbour15a4ef62015-10-21 10:14:48 -0600597 VkImageMemoryBarrier prePresentBarrier = {
598 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
599 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800600 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
601 .dstAccessMask = 0,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600602 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
603 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
604 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800605 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600606 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
607 };
608
609 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
610 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wu082a6202015-10-31 00:31:16 +0800611 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu86365072015-10-26 17:08:33 +0800612 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600613
614
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600615 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 assert(!err);
617}
618
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600619
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600620void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600621{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600622 mat4x4 MVP, Model, VP;
623 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600624 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600625 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600627 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600628
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600629 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600630 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700631 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600632 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600633
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200634 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 -0600635 assert(!err);
636
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600637 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600639 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600640}
641
642static void demo_draw(struct demo *demo)
643{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600644 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600645 VkSemaphore presentCompleteSemaphore;
646 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
647 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
648 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600649 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600650 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800651 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Ian Elliottaaae5352015-07-06 14:27:58 -0600653 err = vkCreateSemaphore(demo->device,
654 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +0800655 NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600656 &presentCompleteSemaphore);
657 assert(!err);
658
659 // Get the index of the next available swapchain image:
Ian Elliottcf074d32015-10-16 18:02:43 -0600660 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600661 UINT64_MAX,
662 presentCompleteSemaphore,
663 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600664 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
665 // demo->swapchain is out of date (e.g. the window was resized) and
666 // must be recreated:
667 demo_resize(demo);
668 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800669 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600670 return;
671 } else if (err == VK_SUBOPTIMAL_KHR) {
672 // demo->swapchain is not as optimal as it could be, but the platform's
673 // presentation engine will still present the image correctly.
674 } else {
675 assert(!err);
676 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600677
Tony Barbour15a4ef62015-10-21 10:14:48 -0600678 // Assume the command buffer has been run on current_buffer before so
679 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
680 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
681 VK_IMAGE_ASPECT_COLOR_BIT,
682 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
683 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
684 demo_flush_init_cmd(demo);
685
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600686 // Wait for the present complete semaphore to be signaled to ensure
687 // that the image won't be rendered to until the presentation
688 // engine has fully released ownership to the application, and it is
689 // okay to render to the image.
690
Ian Elliotta0781972015-08-21 15:09:33 -0600691// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600692 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800693 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
694 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800695 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600696 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800697 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600698 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800699 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600700 .pSignalSemaphores = NULL
701 };
702
703 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600704 assert(!err);
705
Ian Elliotta0781972015-08-21 15:09:33 -0600706 VkPresentInfoKHR present = {
707 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600708 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600709 .swapchainCount = 1,
Ian Elliottcf074d32015-10-16 18:02:43 -0600710 .swapchains = &demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600711 .imageIndices = &demo->current_buffer,
712 };
713
714// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600715 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600716 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
717 // demo->swapchain is out of date (e.g. the window was resized) and
718 // must be recreated:
719 demo_resize(demo);
720 } else if (err == VK_SUBOPTIMAL_KHR) {
721 // demo->swapchain is not as optimal as it could be, but the platform's
722 // presentation engine will still present the image correctly.
723 } else {
724 assert(!err);
725 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600726
Chia-I Wucbb564e2015-04-16 22:02:10 +0800727 err = vkQueueWaitIdle(demo->queue);
728 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600729
Chia-I Wu63636062015-10-26 21:10:41 +0800730 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600731}
732
733static void demo_prepare_buffers(struct demo *demo)
734{
Ian Elliottaaae5352015-07-06 14:27:58 -0600735 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600736 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600737
738 // Check the surface properties and formats
Ian Elliotta0781972015-08-21 15:09:33 -0600739 VkSurfacePropertiesKHR surfProperties;
740 err = demo->fpGetSurfacePropertiesKHR(demo->device,
741 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600742 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600743 assert(!err);
744
Ian Elliott8528eff2015-08-07 15:56:59 -0600745 uint32_t presentModeCount;
Ian Elliotta0781972015-08-21 15:09:33 -0600746 err = demo->fpGetSurfacePresentModesKHR(demo->device,
747 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600748 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600749 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600750 VkPresentModeKHR *presentModes =
751 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600752 assert(presentModes);
Ian Elliotta0781972015-08-21 15:09:33 -0600753 err = demo->fpGetSurfacePresentModesKHR(demo->device,
754 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600755 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600756 assert(!err);
757
Ian Elliotta0781972015-08-21 15:09:33 -0600758 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600759 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600760 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600761 {
762 // If the surface size is undefined, the size is set to
763 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600764 swapchainExtent.width = demo->width;
765 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600766 }
767 else
768 {
769 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600770 swapchainExtent = surfProperties.currentExtent;
Ian Elliottcf074d32015-10-16 18:02:43 -0600771 demo->width = surfProperties.currentExtent.width;
772 demo->height = surfProperties.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600773 }
774
775 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600776 // tearing mode. If not, try IMMEDIATE which will usually be available,
777 // and is fastest (though it tears). If not, fall back to FIFO which is
778 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600779 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600780 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600781 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
782 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600783 break;
784 }
Ian Elliotta0781972015-08-21 15:09:33 -0600785 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
786 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
787 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600788 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 }
790
791 // Determine the number of VkImage's to use in the swap chain (we desire to
792 // own only 1 image at a time, besides the images being displayed and
793 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600794 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600795 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600796 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600797 {
798 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600799 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600800 }
801
Tony Barbour9fd6d352015-09-16 15:01:32 -0600802 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600803 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
804 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600805 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600806 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 }
808
Ian Elliottcf074d32015-10-16 18:02:43 -0600809 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600810 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800811 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600812 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
813 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800814 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600815 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800816 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600817 .width = swapchainExtent.width,
818 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600820 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600821 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800822 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600823 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
824 .queueFamilyCount = 0,
825 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600826 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600827 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600828 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600829 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600830 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600831
Ian Elliottcf074d32015-10-16 18:02:43 -0600832 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800833 assert(!err);
834
Ian Elliottcf074d32015-10-16 18:02:43 -0600835 // If we just re-created an existing swapchain, we should destroy the old
836 // swapchain at this point.
837 // Note: destroying the swapchain also cleans up all its associated
838 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800839 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottcf074d32015-10-16 18:02:43 -0600840 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
841 }
842
843 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600844 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600845 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800846
Ian Elliotta0781972015-08-21 15:09:33 -0600847 VkImage* swapchainImages =
848 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
849 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600850 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600851 &demo->swapchainImageCount,
852 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600853 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600854
Ian Elliotta0781972015-08-21 15:09:33 -0600855 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600856 assert(demo->buffers);
857
Ian Elliotta0781972015-08-21 15:09:33 -0600858 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600859 VkImageViewCreateInfo color_image_view = {
860 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600861 .pNext = NULL,
862 .format = demo->format,
Chia-I Wub6d30c62015-10-27 19:00:15 +0800863 .components = {
Chia-I Wuf051d262015-10-27 19:25:11 +0800864 .r = VK_COMPONENT_SWIZZLE_R,
865 .g = VK_COMPONENT_SWIZZLE_G,
866 .b = VK_COMPONENT_SWIZZLE_B,
867 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600868 },
869 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600870 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600871 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800872 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600873 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800874 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600875 },
876 .viewType = VK_IMAGE_VIEW_TYPE_2D,
877 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600878 };
879
Ian Elliotta0781972015-08-21 15:09:33 -0600880 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881
Tony Barbour15a4ef62015-10-21 10:14:48 -0600882 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
883 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600884 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600885 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600886 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600887 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600888
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600889 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600890
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600891 err = vkCreateImageView(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +0800892 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600893 assert(!err);
894 }
895}
896
897static void demo_prepare_depth(struct demo *demo)
898{
Tony Barbour72304ef2015-04-16 15:59:00 -0600899 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600900 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600901 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600902 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600903 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600904 .format = depth_format,
905 .extent = { demo->width, demo->height, 1 },
906 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600907 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800908 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600909 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600910 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600911 .flags = 0,
912 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200913
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600914 VkImageViewCreateInfo view = {
915 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600916 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800917 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600918 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600919 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600920 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600921 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800922 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600923 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800924 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600925 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600926 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600927 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600928 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600929
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500930 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600931 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600932 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600933
934 demo->depth.format = depth_format;
935
936 /* create image */
Chia-I Wu63636062015-10-26 21:10:41 +0800937 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600938 &demo->depth.image);
939 assert(!err);
940
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600941 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600942 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600943 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600944
Chia-I Wuf48700b2015-11-10 16:21:09 +0800945 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200946 demo->depth.mem_alloc.pNext = NULL;
947 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
948 demo->depth.mem_alloc.memoryTypeIndex = 0;
949
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600950 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600951 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600952 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200953 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600954 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500956 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800957 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +0800958 NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500959 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600960
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500961 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600962 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500963 demo->depth.mem, 0);
964 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600965
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600966 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600967 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600968 VK_IMAGE_LAYOUT_UNDEFINED,
969 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600970
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971 /* create image view */
972 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800973 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600974 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600975}
976
Tony Barbour1a86d032015-09-21 15:17:33 -0600977/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600979 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600980 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600981{
Tony Barbour1a86d032015-09-21 15:17:33 -0600982 FILE *fPtr = fopen(filename,"rb");
983 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600984
Tony Barbour1a86d032015-09-21 15:17:33 -0600985 if (!fPtr)
986 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600987
Tony Barbour1a86d032015-09-21 15:17:33 -0600988 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600989 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
990 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600991 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600992 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600993
Tony Barbour1a86d032015-09-21 15:17:33 -0600994 do {
995 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600996 if (cPtr == NULL) {
997 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600998 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600999 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001000 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001001
Tony Barbour1a86d032015-09-21 15:17:33 -06001002 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001003 if (rgba_data == NULL) {
1004 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001005 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001006 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001007 fgets(header, 256, fPtr); // Format
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001008 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1009 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001010 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001011 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001012
Tony Barbour1a86d032015-09-21 15:17:33 -06001013 for(int y = 0; y < *height; y++)
1014 {
1015 uint8_t *rowPtr = rgba_data;
1016 for(int x = 0; x < *width; x++)
1017 {
1018 fread(rowPtr, 3, 1, fPtr);
1019 rowPtr[3] = 255; /* Alpha of 1 */
1020 rowPtr += 4;
1021 }
1022 rgba_data += layout->rowPitch;
1023 }
1024 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001025 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001026}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001027
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028static void demo_prepare_texture_image(struct demo *demo,
1029 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001030 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001031 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001032 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001033 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001035 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001036 int32_t tex_width;
1037 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001038 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001039 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040
David Pinedo30bd71d2015-04-23 08:16:57 -06001041 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1042 {
1043 printf("Failed to load textures\n");
1044 fflush(stdout);
1045 exit(1);
1046 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001048 tex_obj->tex_width = tex_width;
1049 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001050
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001051 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001052 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001053 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001054 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055 .format = tex_format,
1056 .extent = { tex_width, tex_height, 1 },
1057 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001058 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001059 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001060 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001061 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001062 .flags = 0,
1063 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001065 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Chia-I Wu63636062015-10-26 21:10:41 +08001067 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001068 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069 assert(!err);
1070
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001071 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001072
Chia-I Wuf48700b2015-11-10 16:21:09 +08001073 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001074 tex_obj->mem_alloc.pNext = NULL;
1075 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1076 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001077
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001078 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1079 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001080
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001081 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001082 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001083 &(tex_obj->mem));
1084 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001086 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001087 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001088 tex_obj->mem, 0);
1089 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001090
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001091 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001092 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001093 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001094 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001095 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001096 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001097 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001098 void *data;
1099
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001100 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001101
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001102 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103 assert(!err);
1104
1105 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1106 fprintf(stderr, "Error loading texture: %s\n", filename);
1107 }
1108
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001109 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001110 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001111
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001112 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001113 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001114 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001115 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001116 tex_obj->imageLayout);
1117 /* 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 -07001118}
1119
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001120static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121{
1122 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001123 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1124 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125}
1126
1127static void demo_prepare_textures(struct demo *demo)
1128{
Tony Barbour72304ef2015-04-16 15:59:00 -06001129 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001130 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001131 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001133 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001135 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001136 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137
FslNopper6a7e50e2015-05-06 21:42:01 +02001138 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139 /* Device can texture using linear textures */
1140 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001141 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001142 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001144 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001145
1146 memset(&staging_texture, 0, sizeof(staging_texture));
1147 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001148 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001149
1150 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001151 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001152 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wuc42afd72015-10-27 18:53:22 +08001153 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001154
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001155 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001156 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001157 staging_texture.imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001158 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001160 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001161 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001162 demo->textures[i].imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001163 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001165 VkImageCopy copy_region = {
Tony Barbour466f0012015-11-02 11:47:51 -07001166 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167 .srcOffset = { 0, 0, 0 },
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001168 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1169 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1171 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001172 vkCmdCopyImage(demo->cmd,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001173 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1174 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001175 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001176
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001177 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001178 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001179 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001180 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001181
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001182 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001183
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001184 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001185 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001186 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1187 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001188 }
1189
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001190 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001192 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001193 .magFilter = VK_FILTER_NEAREST,
1194 .minFilter = VK_FILTER_NEAREST,
1195 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001196 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1197 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1198 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001200 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001201 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001202 .minLod = 0.0f,
1203 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001204 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001205 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001207
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001208 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001209 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001211 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001212 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001213 .format = tex_format,
Chia-I Wub6d30c62015-10-27 19:00:15 +08001214 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuf051d262015-10-27 19:25:11 +08001215 VK_COMPONENT_SWIZZLE_G,
1216 VK_COMPONENT_SWIZZLE_B,
1217 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001218 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001219 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001221
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001222 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001223 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 &demo->textures[i].sampler);
1225 assert(!err);
1226
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001227 /* create image view */
1228 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001229 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001230 &demo->textures[i].view);
1231 assert(!err);
1232 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001233}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001234
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001235void demo_prepare_cube_data_buffer(struct demo *demo)
1236{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001237 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001238 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001239 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001240 int i;
1241 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001242 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001243 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001244 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001245
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001246 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001247 mat4x4_mul(MVP, VP, demo->model_matrix);
1248 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001249// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001250
1251 for (i=0; i<12*3; i++) {
1252 data.position[i][0] = g_vertex_buffer_data[i*3];
1253 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1254 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1255 data.position[i][3] = 1.0f;
1256 data.attr[i][0] = g_uv_buffer_data[2*i];
1257 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1258 data.attr[i][2] = 0;
1259 data.attr[i][3] = 0;
1260 }
1261
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001262 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001263 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001264 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001265 buf_info.size = sizeof(data);
Chia-I Wu63636062015-10-26 21:10:41 +08001266 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1267 &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001268 assert(!err);
1269
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001270 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001271
Chia-I Wuf48700b2015-11-10 16:21:09 +08001272 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001273 demo->uniform_data.mem_alloc.pNext = NULL;
1274 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1275 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1276
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001277 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001278 mem_reqs.memoryTypeBits,
1279 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001280 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001281 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001282
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001283 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +08001284 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001285 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001286
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001287 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 -05001288 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001289
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001290 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001291
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001292 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001293
Tony Barbour155cce82015-07-03 10:33:54 -06001294 err = vkBindBufferMemory(demo->device,
1295 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001296 demo->uniform_data.mem, 0);
1297 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001298
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001299 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1300 demo->uniform_data.buffer_info.offset = 0;
1301 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001302}
1303
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001304static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001305{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001306 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001307 [0] = {
Chia-I Wued577562015-10-31 00:31:16 +08001308 .binding = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001309 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001310 .descriptorCount = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001311 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001312 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001313 },
1314 [1] = {
Chia-I Wued577562015-10-31 00:31:16 +08001315 .binding = 1,
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001316 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001317 .descriptorCount = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001318 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001319 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001320 },
1321 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001322 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001323 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001324 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001325 .bindingCount = 2,
Chia-I Wu0e76e3f2015-10-31 00:31:16 +08001326 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001327 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001328 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001329
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001330 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001331 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001332 assert(!err);
1333
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001334 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1335 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1336 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001337 .setLayoutCount = 1,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001338 .pSetLayouts = &demo->desc_layout,
1339 };
1340
1341 err = vkCreatePipelineLayout(demo->device,
1342 &pPipelineLayoutCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +08001343 NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001344 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346}
1347
Chia-I Wuf973e322015-07-08 13:34:24 +08001348static void demo_prepare_render_pass(struct demo *demo)
1349{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001350 const VkAttachmentDescription attachments[2] = {
1351 [0] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001352 .format = demo->format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001353 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001354 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1355 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1356 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1357 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1358 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1359 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1360 },
1361 [1] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001362 .format = demo->depth.format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001363 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001364 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1365 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1366 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1367 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1368 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1369 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1370 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001371 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001372 const VkAttachmentReference color_reference = {
1373 .attachment = 0,
1374 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1375 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001376 const VkAttachmentReference depth_reference = {
1377 .attachment = 1,
1378 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1379 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001380 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001381 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1382 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001383 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001384 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001385 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001386 .pColorAttachments = &color_reference,
1387 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001388 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001389 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001390 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001391 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001392 const VkRenderPassCreateInfo rp_info = {
1393 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1394 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001395 .attachmentCount = 2,
1396 .pAttachments = attachments,
1397 .subpassCount = 1,
1398 .pSubpasses = &subpass,
1399 .dependencyCount = 0,
1400 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001401 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001402 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001403
Chia-I Wu63636062015-10-26 21:10:41 +08001404 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001405 assert(!err);
1406}
1407
Chia-I Wu46176f12015-10-31 00:31:16 +08001408static VkShaderModule demo_prepare_shader_module(struct demo* demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001409 const void* code,
1410 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411{
Chia-I Wu46176f12015-10-31 00:31:16 +08001412 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001413 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001414 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001416 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1417 moduleCreateInfo.pNext = NULL;
1418
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001419 moduleCreateInfo.codeSize = size;
1420 moduleCreateInfo.pCode = code;
1421 moduleCreateInfo.flags = 0;
1422 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1423 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001424
1425 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001426}
1427
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001428char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001429{
1430 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001431 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001432 void *shader_code;
1433
1434 FILE *fp = fopen(filename, "rb");
1435 if (!fp) return NULL;
1436
1437 fseek(fp, 0L, SEEK_END);
1438 size = ftell(fp);
1439
1440 fseek(fp, 0L, SEEK_SET);
1441
1442 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001443 retval = fread(shader_code, size, 1, fp);
1444 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001445
1446 *psize = size;
1447
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001448 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001449 return shader_code;
1450}
1451
Chia-I Wu46176f12015-10-31 00:31:16 +08001452static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001453{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001454 void *vertShaderCode;
1455 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001456
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001457 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1458
Tony Barbourf331eb32015-11-06 10:10:37 -07001459 demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001460
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001461 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001462
1463 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001464}
1465
Chia-I Wu46176f12015-10-31 00:31:16 +08001466static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001467{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001468 void *fragShaderCode;
1469 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001470
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001471 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001472
Tony Barbourf331eb32015-11-06 10:10:37 -07001473 demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001474
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001475 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001476
1477 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478}
1479
1480static void demo_prepare_pipeline(struct demo *demo)
1481{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001482 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001483 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001484 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001485 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001486 VkPipelineRasterizationStateCreateInfo rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001487 VkPipelineColorBlendStateCreateInfo cb;
1488 VkPipelineDepthStencilStateCreateInfo ds;
1489 VkPipelineViewportStateCreateInfo vp;
1490 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001491 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniellba839d12015-09-29 13:01:09 -06001492 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001493 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001494
Piers Daniellba839d12015-09-29 13:01:09 -06001495 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1496 memset(&dynamicState, 0, sizeof dynamicState);
1497 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1498 dynamicState.pDynamicStates = dynamicStateEnables;
1499
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001500 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001501 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001502 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001503
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001504 memset(&vi, 0, sizeof(vi));
1505 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1506
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001507 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001508 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001509 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001510
1511 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001512 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1513 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001514 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001515 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001516 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001517 rs.rasterizerDiscardEnable = VK_FALSE;
1518 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519
1520 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001521 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1522 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001523 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001524 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001525 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001526 cb.attachmentCount = 1;
1527 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528
Tony Barbour29645d02015-01-16 14:27:35 -07001529 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001530 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001531 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001532 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1533 vp.scissorCount = 1;
1534 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001535
1536 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001537 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001538 ds.depthTestEnable = VK_TRUE;
1539 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001540 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001541 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001542 ds.back.failOp = VK_STENCIL_OP_KEEP;
1543 ds.back.passOp = VK_STENCIL_OP_KEEP;
1544 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001545 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001546 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001547
Tony Barbour29645d02015-01-16 14:27:35 -07001548 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001549 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001550 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001551 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001553 // Two stages: vs and fs
1554 pipeline.stageCount = 2;
1555 VkPipelineShaderStageCreateInfo shaderStages[2];
1556 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1557
1558 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001559 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1560 shaderStages[0].module = demo_prepare_vs(demo);
1561 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001562
1563 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001564 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1565 shaderStages[1].module = demo_prepare_fs(demo);
1566 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001567
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001568 memset(&pipelineCache, 0, sizeof(pipelineCache));
1569 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570
Chia-I Wu63636062015-10-26 21:10:41 +08001571 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001572 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001573
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001574 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001575 pipeline.pInputAssemblyState = &ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001576 pipeline.pRasterizationState = &rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001577 pipeline.pColorBlendState = &cb;
1578 pipeline.pMultisampleState = &ms;
1579 pipeline.pViewportState = &vp;
1580 pipeline.pDepthStencilState = &ds;
1581 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001582 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001583 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001584
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001585 pipeline.renderPass = demo->render_pass;
1586
Chia-I Wu63636062015-10-26 21:10:41 +08001587 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1588 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001589 assert(!err);
1590
Chia-I Wu63636062015-10-26 21:10:41 +08001591 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1592 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593}
1594
Chia-I Wu63ea9262015-03-26 13:14:16 +08001595static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001596{
Chia-I Wuf051d262015-10-27 19:25:11 +08001597 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001598 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001599 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001600 .descriptorCount = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001601 },
1602 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001603 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001604 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001605 },
1606 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001607 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001608 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001609 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001610 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001611 .poolSizeCount = 2,
1612 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001613 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001614 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001615
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001616 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001617 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001618 assert(!err);
1619}
1620
1621static void demo_prepare_descriptor_set(struct demo *demo)
1622{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001623 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001624 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001625 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001626 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001627
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001628 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001629 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001630 .pNext = NULL,
1631 .descriptorPool = demo->desc_pool,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001632 .setLayoutCount = 1,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001633 .pSetLayouts = &demo->desc_layout
1634 };
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001635 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001636 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001637
Chia-I Wuae721ba2015-05-25 16:27:55 +08001638 memset(&tex_descs, 0, sizeof(tex_descs));
1639 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001640 tex_descs[i].sampler = demo->textures[i].sampler;
1641 tex_descs[i].imageView = demo->textures[i].view;
1642 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001643 }
1644
1645 memset(&writes, 0, sizeof(writes));
1646
1647 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001648 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001649 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001650 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001651 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001652
1653 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001654 writes[1].dstSet = demo->desc_set;
1655 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001656 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001657 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001658 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001659
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001660 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001661}
1662
Chia-I Wuf973e322015-07-08 13:34:24 +08001663static void demo_prepare_framebuffers(struct demo *demo)
1664{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001665 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001666 attachments[1] = demo->depth.view;
1667
Chia-I Wuf973e322015-07-08 13:34:24 +08001668 const VkFramebufferCreateInfo fb_info = {
1669 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1670 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001671 .renderPass = demo->render_pass,
1672 .attachmentCount = 2,
1673 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001674 .width = demo->width,
1675 .height = demo->height,
1676 .layers = 1,
1677 };
1678 VkResult U_ASSERT_ONLY err;
1679 uint32_t i;
1680
Tony Barbourd8e504f2015-10-08 14:26:24 -06001681 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1682 assert(demo->framebuffers);
1683
1684 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001685 attachments[0] = demo->buffers[i].view;
Chia-I Wu63636062015-10-26 21:10:41 +08001686 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001687 assert(!err);
1688 }
1689}
1690
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691static void demo_prepare(struct demo *demo)
1692{
Cody Northrop91e221b2015-07-09 18:08:32 -06001693 VkResult U_ASSERT_ONLY err;
1694
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001695 const VkCommandPoolCreateInfo cmd_pool_info = {
1696 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001697 .pNext = NULL,
1698 .queueFamilyIndex = demo->graphics_queue_node_index,
1699 .flags = 0,
1700 };
Chia-I Wu63636062015-10-26 21:10:41 +08001701 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001702 assert(!err);
1703
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001704 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001705 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001706 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001707 .commandPool = demo->cmd_pool,
1708 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001709 .bufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711
1712 demo_prepare_buffers(demo);
1713 demo_prepare_depth(demo);
1714 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001715 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001716
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001717 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001718 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720
Ian Elliotta0781972015-08-21 15:09:33 -06001721 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001722 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001723 assert(!err);
1724 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001725
Chia-I Wu63ea9262015-03-26 13:14:16 +08001726 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001727 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001728
Chia-I Wuf973e322015-07-08 13:34:24 +08001729 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001730
Ian Elliotta0781972015-08-21 15:09:33 -06001731 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001732 demo->current_buffer = i;
1733 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1734 }
1735
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001736 /*
1737 * Prepare functions above may generate pipeline commands
1738 * that need to be flushed before beginning the render loop.
1739 */
1740 demo_flush_init_cmd(demo);
1741
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001742 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001743 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744}
1745
David Pinedo2bb7c932015-06-18 17:03:14 -06001746static void demo_cleanup(struct demo *demo)
1747{
1748 uint32_t i;
1749
1750 demo->prepared = false;
1751
Tony Barbourd8e504f2015-10-08 14:26:24 -06001752 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001753 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001754 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001755 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001756 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001757
Chia-I Wu63636062015-10-26 21:10:41 +08001758 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1759 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1760 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1761 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1762 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001763
1764 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001765 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1766 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1767 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1768 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001769 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001770 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001771
Chia-I Wu63636062015-10-26 21:10:41 +08001772 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1773 vkDestroyImage(demo->device, demo->depth.image, NULL);
1774 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001775
Chia-I Wu63636062015-10-26 21:10:41 +08001776 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1777 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001778
Ian Elliotta0781972015-08-21 15:09:33 -06001779 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001780 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001781 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001782 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001783 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001784
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001785 free(demo->queue_props);
1786
Chia-I Wu63636062015-10-26 21:10:41 +08001787 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1788 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001789 if (demo->validate) {
1790 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1791 }
Chia-I Wu63636062015-10-26 21:10:41 +08001792 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001793
1794#ifndef _WIN32
1795 xcb_destroy_window(demo->connection, demo->window);
1796 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001797 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001798#endif // _WIN32
1799}
1800
Ian Elliottcf074d32015-10-16 18:02:43 -06001801static void demo_resize(struct demo *demo)
1802{
1803 uint32_t i;
1804
Mike Stroyanbb60b382015-10-28 09:46:57 -06001805 // Don't react to resize until after first initialization.
1806 if (!demo->prepared) {
1807 return;
1808 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001809 // In order to properly resize the window, we must re-create the swapchain
1810 // AND redo the command buffers, etc.
1811 //
1812 // First, perform part of the demo_cleanup() function:
1813 demo->prepared = false;
1814
1815 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001816 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001817 }
1818 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001819 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001820
Chia-I Wu63636062015-10-26 21:10:41 +08001821 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1822 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1823 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1824 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1825 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001826
1827 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001828 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1829 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1830 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1831 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001832 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001833
Chia-I Wu63636062015-10-26 21:10:41 +08001834 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1835 vkDestroyImage(demo->device, demo->depth.image, NULL);
1836 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001837
Chia-I Wu63636062015-10-26 21:10:41 +08001838 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1839 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001840
1841 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001842 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001843 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001844 }
Chia-I Wu63636062015-10-26 21:10:41 +08001845 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001846 free(demo->buffers);
1847
Chia-I Wu63636062015-10-26 21:10:41 +08001848 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001849
Ian Elliottcf074d32015-10-16 18:02:43 -06001850
1851 // Second, re-perform the demo_prepare() function, which will re-create the
1852 // swapchain:
1853 demo_prepare(demo);
1854}
1855
David Pinedo2bb7c932015-06-18 17:03:14 -06001856// On MS-Windows, make this a global, so it's available to WndProc()
1857struct demo demo;
1858
Ian Elliott639ca472015-04-16 15:23:05 -06001859#ifdef _WIN32
1860static void demo_run(struct demo *demo)
1861{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001862 if (!demo->prepared)
1863 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001864 // Wait for work to finish before updating MVP.
1865 vkDeviceWaitIdle(demo->device);
1866 demo_update_data_buffer(demo);
1867
1868 demo_draw(demo);
1869
1870 // Wait for work to finish before updating MVP.
1871 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001872
David Pinedo2bb7c932015-06-18 17:03:14 -06001873 demo->curFrame++;
1874
1875 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1876 {
1877 demo->quit=true;
1878 demo_cleanup(demo);
1879 ExitProcess(0);
1880 }
1881
1882}
Ian Elliott639ca472015-04-16 15:23:05 -06001883
1884// MS-Windows event handling function:
1885LRESULT CALLBACK WndProc(HWND hWnd,
1886 UINT uMsg,
1887 WPARAM wParam,
1888 LPARAM lParam)
1889{
Ian Elliott639ca472015-04-16 15:23:05 -06001890 switch(uMsg)
1891 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001892 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001893 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001894 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001895 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001896 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001897 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001898 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001899 demo.width = lParam & 0xffff;
1900 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001901 demo_resize(&demo);
1902 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001903 default:
1904 break;
1905 }
1906 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1907}
1908
1909static void demo_create_window(struct demo *demo)
1910{
1911 WNDCLASSEX win_class;
1912
1913 // Initialize the window class structure:
1914 win_class.cbSize = sizeof(WNDCLASSEX);
1915 win_class.style = CS_HREDRAW | CS_VREDRAW;
1916 win_class.lpfnWndProc = WndProc;
1917 win_class.cbClsExtra = 0;
1918 win_class.cbWndExtra = 0;
1919 win_class.hInstance = demo->connection; // hInstance
1920 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1921 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1922 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1923 win_class.lpszMenuName = NULL;
1924 win_class.lpszClassName = demo->name;
1925 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1926 // Register window class:
1927 if (!RegisterClassEx(&win_class)) {
1928 // It didn't work, so try to give a useful error:
1929 printf("Unexpected error trying to start the application!\n");
1930 fflush(stdout);
1931 exit(1);
1932 }
1933 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001934 RECT wr = { 0, 0, demo->width, demo->height };
1935 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001936 demo->window = CreateWindowEx(0,
1937 demo->name, // class name
1938 demo->name, // app name
1939 WS_OVERLAPPEDWINDOW | // window style
1940 WS_VISIBLE |
1941 WS_SYSMENU,
1942 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001943 wr.right-wr.left, // width
1944 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001945 NULL, // handle to parent
1946 NULL, // handle to menu
1947 demo->connection, // hInstance
1948 NULL); // no extra parameters
1949 if (!demo->window) {
1950 // It didn't work, so try to give a useful error:
1951 printf("Cannot create a window in which to draw!\n");
1952 fflush(stdout);
1953 exit(1);
1954 }
1955}
1956#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957static void demo_handle_event(struct demo *demo,
1958 const xcb_generic_event_t *event)
1959{
Piers Daniell735ee532015-02-23 16:23:13 -07001960 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001961 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001962 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001963 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001965 case XCB_CLIENT_MESSAGE:
1966 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1967 (*demo->atom_wm_delete_window).atom) {
1968 demo->quit = true;
1969 }
1970 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971 case XCB_KEY_RELEASE:
1972 {
1973 const xcb_key_release_event_t *key =
1974 (const xcb_key_release_event_t *) event;
1975
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001976 switch (key->detail) {
1977 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001978 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001979 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001980 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001981 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001982 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001983 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001984 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001985 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001986 case 0x41:
1987 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001988 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001989 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001990 }
1991 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06001992 case XCB_CONFIGURE_NOTIFY:
1993 {
1994 const xcb_configure_notify_event_t *cfg =
1995 (const xcb_configure_notify_event_t *) event;
1996 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
1997 demo_resize(demo);
1998 }
1999 }
2000 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002001 default:
2002 break;
2003 }
2004}
2005
2006static void demo_run(struct demo *demo)
2007{
2008 xcb_flush(demo->connection);
2009
2010 while (!demo->quit) {
2011 xcb_generic_event_t *event;
2012
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002013 if (demo->pause) {
2014 event = xcb_wait_for_event(demo->connection);
2015 } else {
2016 event = xcb_poll_for_event(demo->connection);
2017 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002018 if (event) {
2019 demo_handle_event(demo, event);
2020 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002021 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002022
2023 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002024 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002025 demo_update_data_buffer(demo);
2026
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002027 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002028
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002029 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002030 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002031 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002032 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002033 demo->quit = true;
2034
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002035 }
2036}
2037
2038static void demo_create_window(struct demo *demo)
2039{
2040 uint32_t value_mask, value_list[32];
2041
2042 demo->window = xcb_generate_id(demo->connection);
2043
2044 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2045 value_list[0] = demo->screen->black_pixel;
2046 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002047 XCB_EVENT_MASK_EXPOSURE |
2048 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002049
2050 xcb_create_window(demo->connection,
2051 XCB_COPY_FROM_PARENT,
2052 demo->window, demo->screen->root,
2053 0, 0, demo->width, demo->height, 0,
2054 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2055 demo->screen->root_visual,
2056 value_mask, value_list);
2057
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002058 /* Magic code that will send notification when window is destroyed */
2059 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2060 "WM_PROTOCOLS");
2061 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2062
2063 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2064 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2065
2066 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2067 demo->window, (*reply).atom, 4, 32, 1,
2068 &(*demo->atom_wm_delete_window).atom);
2069 free(reply);
2070
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002071 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002072
2073 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2074 const uint32_t coords[] = {100, 100};
2075 xcb_configure_window(demo->connection, demo->window,
2076 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002077}
Ian Elliott639ca472015-04-16 15:23:05 -06002078#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002079
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002080/*
2081 * Return 1 (true) if all layer names specified in check_names
2082 * can be found in given layer properties.
2083 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002084static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002085 uint32_t layer_count, VkLayerProperties *layers)
2086{
2087 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002088 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002089 for (uint32_t j = 0; j < layer_count; j++) {
2090 if (!strcmp(check_names[i], layers[j].layerName)) {
2091 found = 1;
2092 }
2093 }
2094 if (!found) {
2095 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2096 return 0;
2097 }
2098 }
2099 return 1;
2100}
2101
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002102static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002103{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002104 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002105 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002106 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002107 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002108 VkLayerProperties *instance_layers;
2109 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002110 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002111 uint32_t instance_layer_count = 0;
2112 uint32_t enabled_extension_count = 0;
2113 uint32_t enabled_layer_count = 0;
2114
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002115 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002116 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002117 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002118 "ObjectTracker",
2119 "DrawState",
2120 "ParamChecker",
2121 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002122 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002123 "DeviceLimits",
2124 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002125 };
2126
2127 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002128 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002129 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002130 "ObjectTracker",
2131 "DrawState",
2132 "ParamChecker",
2133 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002134 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002135 "DeviceLimits",
2136 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002137 };
2138
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002139 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002140 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002141 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002142 assert(!err);
2143
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002144 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002145 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002146 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002147
2148 if (demo->validate) {
2149 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2150 instance_layer_count, instance_layers);
2151 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002152 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002153 "required validation layer.\n\n"
2154 "Please look at the Getting Started guide for additional "
2155 "information.\n",
2156 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002157 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002158 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002159 }
2160
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002161 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002162 assert(!err);
2163
Tony Barbourcc69f452015-10-08 13:59:42 -06002164 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002165 memset(extension_names, 0, sizeof(extension_names));
2166 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002167 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002168 assert(!err);
2169 for (uint32_t i = 0; i < instance_extension_count; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002170 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002171 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002172 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002173 }
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002174 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002175 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002176 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002178 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002179 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002180 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002181 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002182 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002183 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002184 "Vulkan installable client driver (ICD) installed?\nPlease "
2185 "look at the Getting Started guide for additional "
2186 "information.\n",
2187 "vkCreateInstance Failure");
2188 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002189 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002190 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002191 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002192 .pApplicationName = APP_SHORT_NAME,
2193 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002194 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002195 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002196 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002197 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002198 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002199 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002200 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002201 .pApplicationInfo = &app,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002202 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002203 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002204 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002205 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002206 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002207
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002208 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002209
Chia-I Wu63636062015-10-26 21:10:41 +08002210 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002211 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2212 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002213 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002214 "additional information.\n",
2215 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002216 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002217 ERR_EXIT("Cannot find a specified extension library"
2218 ".\nMake sure your layers path is set appropriately\n",
2219 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002220 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002221 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2222 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002223 "the Getting Started guide for additional information.\n",
2224 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002225 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002226
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002227 free(instance_layers);
2228 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002229
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002230 /* Make initial call to query gpu_count, then second call for gpu info*/
2231 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2232 assert(!err && gpu_count > 0);
2233 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2234 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2235 assert(!err);
2236 /* For cube demo we just grab the first physical device */
2237 demo->gpu = physical_devices[0];
2238 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002240 /* Look for validation layers */
2241 validation_found = 0;
2242 enabled_layer_count = 0;
2243 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002244 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002245 assert(!err);
2246
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002247 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002248 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002249 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002250
2251 if (demo->validate) {
2252 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2253 device_layer_count, device_layers);
2254 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002255 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002256 "a required validation layer.\n\n"
2257 "Please look at the Getting Started guide for additional "
2258 "information.\n",
2259 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002260 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002261 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002262 }
2263
2264 uint32_t device_extension_count = 0;
2265 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002266 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002267 demo->gpu, NULL, &device_extension_count, NULL);
2268 assert(!err);
2269
Tony Barbourcc69f452015-10-08 13:59:42 -06002270 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002271 enabled_extension_count = 0;
2272 memset(extension_names, 0, sizeof(extension_names));
2273 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002274 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002275 demo->gpu, NULL, &device_extension_count, device_extensions);
2276 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002277
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002278 for (uint32_t i = 0; i < device_extension_count; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002279 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002280 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002281 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002282 }
2283 assert(enabled_extension_count < 64);
2284 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002285 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002286 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002287 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002288 "Vulkan installable client driver (ICD) installed?\nPlease "
2289 "look at the Getting Started guide for additional "
2290 "information.\n",
2291 "vkCreateInstance Failure");
2292 }
2293
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002294 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002295 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2296 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002297 if (!demo->dbgCreateMsgCallback) {
2298 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2299 "vkGetProcAddr Failure");
2300 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002301 if (!demo->dbgDestroyMsgCallback) {
2302 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2303 "vkGetProcAddr Failure");
2304 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002305 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2306 if (!demo->dbgBreakCallback) {
2307 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2308 "vkGetProcAddr Failure");
2309 }
2310
2311 PFN_vkDbgMsgCallback callback;
2312
2313 if (!demo->use_break) {
2314 callback = dbgFunc;
2315 } else {
2316 callback = demo->dbgBreakCallback;
2317 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002318 err = demo->dbgCreateMsgCallback(
2319 demo->inst,
2320 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002321 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002322 &demo->msg_callback);
2323 switch (err) {
2324 case VK_SUCCESS:
2325 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002326 case VK_ERROR_OUT_OF_HOST_MEMORY:
2327 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2328 "dbgCreateMsgCallback Failure");
2329 break;
2330 default:
2331 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2332 "dbgCreateMsgCallback Failure");
2333 break;
2334 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002335 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002336 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002337
2338 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002339 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002340 assert(demo->queue_count >= 1);
2341
2342 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002343 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002344 // Find a queue that supports gfx
2345 uint32_t gfx_queue_idx = 0;
2346 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2347 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2348 break;
2349 }
2350 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002351 // Query fine-grained feature support for this device.
2352 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002353 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002354 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002355
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002356 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002357 const VkDeviceQueueCreateInfo queue = {
2358 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2359 .pNext = NULL,
2360 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu8b497442015-11-06 06:42:02 +08002361 .queueCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002362 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002363 };
2364
2365 VkDeviceCreateInfo device = {
2366 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2367 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002368 .queueCreateInfoCount = 1,
2369 .pQueueCreateInfos = &queue,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002370 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002371 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002372 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002373 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002374 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002375 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002376
Chia-I Wu63636062015-10-26 21:10:41 +08002377 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002378 assert(!err);
2379
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002380 free(device_layers);
2381
Ian Elliotta0781972015-08-21 15:09:33 -06002382 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2383 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2384 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2385 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2386 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002387 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2388 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2389 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2390 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002391}
2392
Tony Barbourcc69f452015-10-08 13:59:42 -06002393static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002394{
2395 VkResult err;
2396 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002397
Tony Barbourcc69f452015-10-08 13:59:42 -06002398 // Construct the surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002399 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002400 demo->surface_description.pNext = NULL;
2401#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002402 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002403 demo->surface_description.pPlatformHandle = demo->connection;
2404 demo->surface_description.pPlatformWindow = demo->window;
2405#else // _WIN32
2406 demo->platform_handle_xcb.connection = demo->connection;
2407 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002408 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002409 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2410 demo->surface_description.pPlatformWindow = &demo->window;
2411#endif // _WIN32
2412
Tony Barbourcc69f452015-10-08 13:59:42 -06002413 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002414 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2415 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002416 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2417 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002418 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002419 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002420
2421 // Search for a graphics and a present queue in the array of queue
2422 // families, try to find one that supports both
2423 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2424 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002425 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002426 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2427 if (graphicsQueueNodeIndex == UINT32_MAX) {
2428 graphicsQueueNodeIndex = i;
2429 }
2430
2431 if (supportsPresent[i] == VK_TRUE) {
2432 graphicsQueueNodeIndex = i;
2433 presentQueueNodeIndex = i;
2434 break;
2435 }
2436 }
2437 }
2438 if (presentQueueNodeIndex == UINT32_MAX) {
2439 // If didn't find a queue that supports both graphics and present, then
2440 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002441 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002442 if (supportsPresent[i] == VK_TRUE) {
2443 presentQueueNodeIndex = i;
2444 break;
2445 }
2446 }
2447 }
2448 free(supportsPresent);
2449
2450 // Generate error if could not find both a graphics and a present queue
2451 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2452 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002453 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002454 }
2455
2456 // TODO: Add support for separate queues, including presentation,
2457 // synchronization, and appropriate tracking for QueueSubmit
2458 // While it is possible for an application to use a separate graphics and a
2459 // present queues, this demo program assumes it is only using one:
2460 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2461 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002462 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002463 }
2464
2465 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002466
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002467 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002468 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002469
Ian Elliottaaae5352015-07-06 14:27:58 -06002470 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002471 uint32_t formatCount;
Ian Elliotta0781972015-08-21 15:09:33 -06002472 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2473 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002474 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002475 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002476 VkSurfaceFormatKHR *surfFormats =
2477 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2478 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2479 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002480 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002481 assert(!err);
2482 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2483 // the surface has no preferred format. Otherwise, at least one
2484 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002485 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2486 {
2487 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2488 }
2489 else
2490 {
2491 assert(formatCount >= 1);
2492 demo->format = surfFormats[0].format;
2493 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002494 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002495
David Pinedo2bb7c932015-06-18 17:03:14 -06002496 demo->quit = false;
2497 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002498
2499 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002500 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002501}
2502
2503static void demo_init_connection(struct demo *demo)
2504{
Ian Elliott639ca472015-04-16 15:23:05 -06002505#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002506 const xcb_setup_t *setup;
2507 xcb_screen_iterator_t iter;
2508 int scr;
2509
2510 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002511 if (demo->connection == NULL) {
2512 printf("Cannot find a compatible Vulkan installable client driver "
2513 "(ICD).\nExiting ...\n");
2514 fflush(stdout);
2515 exit(1);
2516 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002517
2518 setup = xcb_get_setup(demo->connection);
2519 iter = xcb_setup_roots_iterator(setup);
2520 while (scr-- > 0)
2521 xcb_screen_next(&iter);
2522
2523 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002524#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002525}
2526
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002527static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002528{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002529 vec3 eye = {0.0f, 3.0f, 5.0f};
2530 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002531 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002532
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002533 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002534 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002535
Piers Daniell735ee532015-02-23 16:23:13 -07002536 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002537 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002538 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002539 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002540 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002541 if (strcmp(argv[i], "--break") == 0) {
2542 demo->use_break = true;
2543 continue;
2544 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002545 if (strcmp(argv[i], "--validate") == 0) {
2546 demo->validate = true;
2547 continue;
2548 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002549 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002550 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002551 i < argc-1 &&
2552 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2553 demo->frameCount >= 0)
2554 {
2555 i++;
2556 continue;
2557 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002558
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002559 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002560 fflush(stderr);
2561 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002562 }
2563
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002564 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002565 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002566
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002567 demo->width = 500;
2568 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002569
2570 demo->spin_angle = 0.01f;
2571 demo->spin_increment = 0.01f;
2572 demo->pause = false;
2573
Piers Daniell735ee532015-02-23 16:23:13 -07002574 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002575 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2576 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002577}
2578
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002579
Ian Elliott639ca472015-04-16 15:23:05 -06002580#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002581extern int __getmainargs(
2582 int * _Argc,
2583 char *** _Argv,
2584 char *** _Env,
2585 int _DoWildCard,
2586 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002587
Ian Elliotta748eaf2015-04-28 15:50:36 -06002588int WINAPI WinMain(HINSTANCE hInstance,
2589 HINSTANCE hPrevInstance,
2590 LPSTR pCmdLine,
2591 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002592{
2593 MSG msg; // message
2594 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002595 int argc;
2596 char** argv;
2597 char** env;
2598 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002599
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002600 __getmainargs(&argc,&argv,&env,0,&new_mode);
2601
2602 demo_init(&demo, argc, argv);
2603 demo.connection = hInstance;
2604 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002605 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002606 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002607
2608 demo_prepare(&demo);
2609
2610 done = false; //initialize loop condition variable
2611 /* main message loop*/
2612 while(!done)
2613 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002614 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002615 if (msg.message == WM_QUIT) //check for a quit message
2616 {
2617 done = true; //if found, quit app
2618 }
2619 else
2620 {
2621 /* Translate and dispatch to event queue*/
2622 TranslateMessage(&msg);
2623 DispatchMessage(&msg);
2624 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002625 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002626 }
2627
2628 demo_cleanup(&demo);
2629
Tony Barbourf9921732015-04-22 11:36:22 -06002630 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002631}
2632#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002633int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002634{
2635 struct demo demo;
2636
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002637 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002638 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002639 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002640
2641 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002642 demo_run(&demo);
2643
2644 demo_cleanup(&demo);
2645
2646 return 0;
2647}
Ian Elliott639ca472015-04-16 15:23:05 -06002648#endif // _WIN32