blob: 37c9ba04c2af5ea813bb91b0e0018108262efd54 [file] [log] [blame]
Ian Elliott421107f2015-04-28 15:50:36 -06001/*
Ian Elliott421107f2015-04-28 15:50:36 -06002 *
Courtney Goeltzenleuchter8a17da52015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Ian Elliott421107f2015-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 Goeltzenleuchter96cd7952015-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 Elliott421107f2015-04-28 15:50:36 -060027 */
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-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 Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060040#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060041#endif // _WIN32
42
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060043#include <vulkan.h>
Ian Elliott338dedb2015-08-21 15:09:33 -060044#include <vk_ext_khr_swapchain.h>
45#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060046#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060047
Mark Lobodzinskieccbb372015-09-01 09:00:16 -060048#include "vk_sdk_platform.h"
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060049#include "linmath.h"
50
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060051#define DEMO_TEXTURE_COUNT 1
Ian Elliott4e19ed02015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060054
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -060055#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
56
Tony Barbour22a30862015-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 Elliottcaa9f272015-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 Elliottcaa9f272015-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 Elliotte36b2082015-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 Ashburnccfa8412015-10-08 15:58:23 -060089static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
90
Ian Elliott1b6de092015-06-22 15:07:49 -060091#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
92{ \
Jon Ashburnccfa8412015-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 Elliott1b6de092015-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 Goeltzenleuchter7e334982014-10-30 15:14:16 -0600102/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700103 * structure to track all objects related to a texture.
104 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600105struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600106 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700107
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600108 VkImage image;
109 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600110
Chia-I Wu1f851912015-10-27 18:04:07 +0800111 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500112 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600113 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700114 int32_t tex_width, tex_height;
115};
116
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600117static char *tex_files[] = {
Tony Barboura26fb7d2015-09-21 15:17:33 -0600118 "lunarg.ppm"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600119};
120
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600121struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600122 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600123 float mvp[4][4];
124 float position[12*3][4];
125 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600126};
127
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600128struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600129 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600130 float mvp[4][4];
131 float position[12*3][4];
132 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600133};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600134
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600135//--------------------------------------------------------------------------------------
136// Mesh and VertexFormat Data
137//--------------------------------------------------------------------------------------
138struct Vertex
139{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600140 float posX, posY, posZ, posW; // Position data
141 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600142};
143
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600144struct VertexPosTex
145{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600146 float posX, posY, posZ, posW; // Position data
147 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600148};
149
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600150#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600151#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600152
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600153static const float g_vertex_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800154 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600155 -1.0f,-1.0f, 1.0f,
156 -1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800157 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600158 -1.0f, 1.0f,-1.0f,
159 -1.0f,-1.0f,-1.0f,
160
Chia-I Wuc3487c22015-04-22 14:56:17 +0800161 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600162 1.0f, 1.0f,-1.0f,
163 1.0f,-1.0f,-1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800164 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600165 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600166 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600167
Chia-I Wuc3487c22015-04-22 14:56:17 +0800168 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600169 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600170 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800171 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600172 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600173 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600174
Chia-I Wuc3487c22015-04-22 14:56:17 +0800175 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600176 -1.0f, 1.0f, 1.0f,
177 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800178 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600179 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600180 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600181
Chia-I Wuc3487c22015-04-22 14:56:17 +0800182 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600183 1.0f, 1.0f, 1.0f,
184 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800185 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600186 1.0f,-1.0f,-1.0f,
187 1.0f, 1.0f,-1.0f,
188
Chia-I Wuc3487c22015-04-22 14:56:17 +0800189 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600190 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600191 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800192 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600193 1.0f,-1.0f, 1.0f,
194 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600195};
196
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600197static const float g_uv_buffer_data[] = {
Chia-I Wuc3487c22015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600216 0.0f, 0.0f,
217 0.0f, 1.0f,
218
Chia-I Wuc3487c22015-04-22 14:56:17 +0800219 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600220 0.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800221 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600222 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600223 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600224 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600225
Chia-I Wuc3487c22015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600230 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600231 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600232
Chia-I Wuc3487c22015-04-22 14:56:17 +0800233 0.0f, 1.0f, // +Z side
234 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600235 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600236 0.0f, 0.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800237 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600238 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-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 Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600261VkBool32 dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600262 VkFlags msgFlags,
263 VkDbgObjectType objType,
264 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-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 Barbour5685ad72015-04-29 16:19:20 -0600270{
271 char *message = (char *) malloc(strlen(pMsg)+100);
272
273 assert (message);
274
Courtney Goeltzenleuchter1c7c65d2015-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 Barboura65ecc22015-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 Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600280 return false;
Tony Barboura65ecc22015-06-30 14:14:19 -0600281 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600282 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour5685ad72015-04-29 16:19:20 -0600283 } else {
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600284 return false;
Tony Barbour5685ad72015-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 Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600294
Courtney Goeltzenleuchtere4c43132015-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 Barbour5685ad72015-04-29 16:19:20 -0600303}
304
Ian Elliott338dedb2015-08-21 15:09:33 -0600305typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600306 VkImage image;
Chia-I Wu1f851912015-10-27 18:04:07 +0800307 VkCommandBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600308 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600309} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600310
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600311struct demo {
Ian Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600318 xcb_connection_t *connection;
319 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800320 xcb_window_t window;
321 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott338dedb2015-08-21 15:09:33 -0600322 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour7910de72015-07-13 16:37:21 -0600323#endif // _WIN32
Cody Northrop75db0322015-05-28 11:27:16 -0600324 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700325 bool use_staging_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600326
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600327 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600328 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600329 VkDevice device;
330 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700331 uint32_t graphics_queue_node_index;
Tony Barbour426b9052015-06-24 16:06:58 -0600332 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600333 VkQueueFamilyProperties *queue_props;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600334 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600335
336 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600337 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600338 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600339
Ian Elliott338dedb2015-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 Elliotte2688a52015-10-16 18:02:43 -0600351 VkSwapchainKHR swapchain;
Ian Elliott338dedb2015-08-21 15:09:33 -0600352 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600353
Chia-I Wu1f851912015-10-27 18:04:07 +0800354 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600355
356 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600357 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600358
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600359 VkImage image;
Chia-I Wu1f851912015-10-27 18:04:07 +0800360 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500361 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600362 VkImageView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600363 } depth;
364
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600365 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600366
367 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600368 VkBuffer buf;
Chia-I Wu1f851912015-10-27 18:04:07 +0800369 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500370 VkDeviceMemory mem;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600371 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600372 } uniform_data;
373
Chia-I Wu1f851912015-10-27 18:04:07 +0800374 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500375 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600376 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600377 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800378 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600379 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600380
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600381 mat4x4 projection_matrix;
382 mat4x4 view_matrix;
383 mat4x4 model_matrix;
384
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600385 float spin_angle;
386 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600387 bool pause;
388
Tony Barbour9400f092015-07-21 09:04:41 -0600389 VkShaderModule vert_shader_module;
390 VkShaderModule frag_shader_module;
391
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600392 VkDescriptorPool desc_pool;
393 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800394
Tony Barbour5aabff52015-10-08 14:26:24 -0600395 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800396
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600397 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600398 int32_t curFrame;
399 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600400 bool validate;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600401 bool use_break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600402 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600403 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600404 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600405 VkDbgMsgCallback msg_callback;
406
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600407 uint32_t current_buffer;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -0600408 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600409};
410
Ian Elliotte2688a52015-10-16 18:02:43 -0600411// Forward declaration:
412static void demo_resize(struct demo *demo);
413
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600414static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinski72346292015-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 Barbourf1eceb92015-10-15 12:42:56 -0600420 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinski72346292015-07-02 16:49:40 -0600421 *typeIndex = i;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600422 return true;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600423 }
424 }
425 typeBits >>= 1;
426 }
427 // No memory types matched, return failure
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600428 return false;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600429}
430
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600431static void demo_flush_init_cmd(struct demo *demo)
432{
Tony Barbour22a30862015-04-22 09:02:32 -0600433 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600434
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600435 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600436 return;
437
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600438 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600439 assert(!err);
440
Chia-I Wu1f851912015-10-27 18:04:07 +0800441 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wue420a332015-10-26 20:04:44 +0800442 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600443 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800444 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
445 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800446 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600447 .pWaitSemaphores = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800448 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600449 .pCommandBuffers = cmd_bufs,
Chia-I Wu763a7492015-10-26 20:48:51 +0800450 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600451 .pSignalSemaphores = NULL
452 };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600453
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600454 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600455 assert(!err);
456
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600457 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600458 assert(!err);
459
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600460 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600461 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600462}
463
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600464static void demo_set_image_layout(
465 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600466 VkImage image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600467 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600468 VkImageLayout old_image_layout,
469 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600470{
Tony Barbour22a30862015-04-22 09:02:32 -0600471 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600472
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600473 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800474 const VkCommandBufferAllocateInfo cmd = {
475 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600476 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +0800477 .commandPool = demo->cmd_pool,
478 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +0800479 .bufferCount = 1,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600480 };
481
Chia-I Wu1f851912015-10-27 18:04:07 +0800482 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600483 assert(!err);
484
Chia-I Wu1f851912015-10-27 18:04:07 +0800485 VkCommandBufferBeginInfo cmd_buf_info = {
486 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600487 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600488 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800489 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600490 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800491 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600492 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600493 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourc1098272015-10-23 10:53:30 -0600494 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600495 }
496
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600497 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600498 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600499 .pNext = NULL,
Chia-I Wu989de842015-10-27 19:54:37 +0800500 .srcAccessMask = 0,
501 .dstAccessMask = 0,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600502 .oldLayout = old_image_layout,
503 .newLayout = new_image_layout,
504 .image = image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600505 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600506 };
507
Chia-I Wu1f851912015-10-27 18:04:07 +0800508 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600509 /* Make sure anything that was copying from this image has completed */
Chia-I Wu989de842015-10-27 19:54:37 +0800510 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600511 }
512
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600513 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600514 /* Make sure any Copy or CPU writes to image are flushed */
Chia-I Wu989de842015-10-27 19:54:37 +0800515 image_memory_barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600516 }
517
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600518 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600519
Tony Barbourc2e987e2015-06-29 16:20:35 -0600520 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
521 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600522
Chia-I Wu3ffcd732015-10-26 17:08:33 +0800523 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600524}
525
Chia-I Wu1f851912015-10-27 18:04:07 +0800526static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600527{
Chia-I Wu1f851912015-10-27 18:04:07 +0800528 const VkCommandBufferBeginInfo cmd_buf_info = {
529 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600530 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600531 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800532 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600533 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800534 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700535 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800536 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600537 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
538 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800539 };
540 const VkRenderPassBeginInfo rp_begin = {
541 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
542 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800543 .renderPass = demo->render_pass,
544 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800545 .renderArea.offset.x = 0,
546 .renderArea.offset.y = 0,
547 .renderArea.extent.width = demo->width,
548 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600549 .clearValueCount = 2,
550 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700551 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800552 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600553
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600554 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600555 assert(!err);
556
Chia-I Wuc51b1212015-10-27 19:25:11 +0800557 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wuc278df82015-07-07 11:50:03 +0800558
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600559 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600560 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600561 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600562 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600563
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600564 VkViewport viewport;
565 memset(&viewport, 0, sizeof(viewport));
566 viewport.height = (float) demo->height;
567 viewport.width = (float) demo->width;
568 viewport.minDepth = (float) 0.0f;
569 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600570 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600571
572 VkRect2D scissor;
573 memset(&scissor, 0, sizeof(scissor));
574 scissor.extent.width = demo->width;
575 scissor.extent.height = demo->height;
576 scissor.offset.x = 0;
577 scissor.offset.y = 0;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600578 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600579
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600580 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800581 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600582
Tony Barbour40292ae2015-10-21 10:14:48 -0600583 VkImageMemoryBarrier prePresentBarrier = {
584 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
585 .pNext = NULL,
Chia-I Wu989de842015-10-27 19:54:37 +0800586 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
587 .dstAccessMask = 0,
Tony Barbour40292ae2015-10-21 10:14:48 -0600588 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
589 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
590 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wu1f851912015-10-27 18:04:07 +0800591 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600592 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
593 };
594
595 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
596 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wucba6cea2015-10-31 00:31:16 +0800597 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu3ffcd732015-10-26 17:08:33 +0800598 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour40292ae2015-10-21 10:14:48 -0600599
600
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600601 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600602 assert(!err);
603}
604
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600605
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600606void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600607{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600608 mat4x4 MVP, Model, VP;
609 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600610 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600611 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600612
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600613 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600614
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600615 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600616 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700617 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600618 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600619
Dominik Witczake82d5b62015-09-22 18:25:33 +0200620 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600621 assert(!err);
622
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600623 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600624
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600625 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600626}
627
628static void demo_draw(struct demo *demo)
629{
Tony Barbour22a30862015-04-22 09:02:32 -0600630 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600631 VkSemaphore presentCompleteSemaphore;
632 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
633 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
634 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600635 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600636 };
Chia-I Wue420a332015-10-26 20:04:44 +0800637 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600638
Ian Elliotte36b2082015-07-06 14:27:58 -0600639 err = vkCreateSemaphore(demo->device,
640 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +0800641 NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600642 &presentCompleteSemaphore);
643 assert(!err);
644
645 // Get the index of the next available swapchain image:
Ian Elliotte2688a52015-10-16 18:02:43 -0600646 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600647 UINT64_MAX,
648 presentCompleteSemaphore,
649 &demo->current_buffer);
Ian Elliotte2688a52015-10-16 18:02:43 -0600650 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
651 // demo->swapchain is out of date (e.g. the window was resized) and
652 // must be recreated:
653 demo_resize(demo);
654 demo_draw(demo);
Chia-I Wu69f40122015-10-26 21:10:41 +0800655 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -0600656 return;
657 } else if (err == VK_SUBOPTIMAL_KHR) {
658 // demo->swapchain is not as optimal as it could be, but the platform's
659 // presentation engine will still present the image correctly.
660 } else {
661 assert(!err);
662 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600663
Tony Barbour40292ae2015-10-21 10:14:48 -0600664 // Assume the command buffer has been run on current_buffer before so
665 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
666 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
667 VK_IMAGE_ASPECT_COLOR_BIT,
668 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
669 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
670 demo_flush_init_cmd(demo);
671
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600672 // Wait for the present complete semaphore to be signaled to ensure
673 // that the image won't be rendered to until the presentation
674 // engine has fully released ownership to the application, and it is
675 // okay to render to the image.
676
Ian Elliott338dedb2015-08-21 15:09:33 -0600677// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600678 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800679 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
680 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800681 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600682 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu763a7492015-10-26 20:48:51 +0800683 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600684 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu763a7492015-10-26 20:48:51 +0800685 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600686 .pSignalSemaphores = NULL
687 };
688
689 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600690 assert(!err);
691
Ian Elliott338dedb2015-08-21 15:09:33 -0600692 VkPresentInfoKHR present = {
693 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600694 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600695 .swapchainCount = 1,
Ian Elliotte2688a52015-10-16 18:02:43 -0600696 .swapchains = &demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600697 .imageIndices = &demo->current_buffer,
698 };
699
700// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600701 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliotte2688a52015-10-16 18:02:43 -0600702 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
703 // demo->swapchain is out of date (e.g. the window was resized) and
704 // must be recreated:
705 demo_resize(demo);
706 } else if (err == VK_SUBOPTIMAL_KHR) {
707 // demo->swapchain is not as optimal as it could be, but the platform's
708 // presentation engine will still present the image correctly.
709 } else {
710 assert(!err);
711 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600712
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800713 err = vkQueueWaitIdle(demo->queue);
714 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600715
Chia-I Wu69f40122015-10-26 21:10:41 +0800716 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600717}
718
719static void demo_prepare_buffers(struct demo *demo)
720{
Ian Elliotte36b2082015-07-06 14:27:58 -0600721 VkResult U_ASSERT_ONLY err;
Ian Elliotte2688a52015-10-16 18:02:43 -0600722 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600723
724 // Check the surface properties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600725 VkSurfacePropertiesKHR surfProperties;
726 err = demo->fpGetSurfacePropertiesKHR(demo->device,
727 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600728 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600729 assert(!err);
730
Ian Elliott7fe115d2015-08-07 15:56:59 -0600731 uint32_t presentModeCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600732 err = demo->fpGetSurfacePresentModesKHR(demo->device,
733 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600734 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600735 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600736 VkPresentModeKHR *presentModes =
737 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600738 assert(presentModes);
Ian Elliott338dedb2015-08-21 15:09:33 -0600739 err = demo->fpGetSurfacePresentModesKHR(demo->device,
740 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600741 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600742 assert(!err);
743
Ian Elliott338dedb2015-08-21 15:09:33 -0600744 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600745 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600746 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600747 {
748 // If the surface size is undefined, the size is set to
749 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600750 swapchainExtent.width = demo->width;
751 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600752 }
753 else
754 {
755 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -0600756 swapchainExtent = surfProperties.currentExtent;
Ian Elliotte2688a52015-10-16 18:02:43 -0600757 demo->width = surfProperties.currentExtent.width;
758 demo->height = surfProperties.currentExtent.height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600759 }
760
761 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3fc49c22015-07-27 13:53:11 -0600762 // tearing mode. If not, try IMMEDIATE which will usually be available,
763 // and is fastest (though it tears). If not, fall back to FIFO which is
764 // always available.
Ian Elliott338dedb2015-08-21 15:09:33 -0600765 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600766 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -0600767 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
768 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600769 break;
770 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600771 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
772 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
773 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3fc49c22015-07-27 13:53:11 -0600774 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600775 }
776
777 // Determine the number of VkImage's to use in the swap chain (we desire to
778 // own only 1 image at a time, besides the images being displayed and
779 // queued for display):
Ian Elliott338dedb2015-08-21 15:09:33 -0600780 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600781 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600782 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600783 {
784 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600785 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600786 }
787
Tony Barbourd161c542015-09-16 15:01:32 -0600788 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliott338dedb2015-08-21 15:09:33 -0600789 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
790 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600791 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600792 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600793 }
794
Ian Elliotte2688a52015-10-16 18:02:43 -0600795 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600796 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800797 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600798 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
799 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800800 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600801 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800802 .imageExtent = {
Ian Elliott338dedb2015-08-21 15:09:33 -0600803 .width = swapchainExtent.width,
804 .height = swapchainExtent.height,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600805 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600806 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600807 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800808 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600809 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
810 .queueFamilyCount = 0,
811 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600812 .presentMode = swapchainPresentMode,
Ian Elliotte2688a52015-10-16 18:02:43 -0600813 .oldSwapchain = oldSwapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600814 .clipped = true,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600815 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600816 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600817
Ian Elliotte2688a52015-10-16 18:02:43 -0600818 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800819 assert(!err);
820
Ian Elliotte2688a52015-10-16 18:02:43 -0600821 // If we just re-created an existing swapchain, we should destroy the old
822 // swapchain at this point.
823 // Note: destroying the swapchain also cleans up all its associated
824 // presentable images once the platform is done with them.
Chia-I Wue420a332015-10-26 20:04:44 +0800825 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliotte2688a52015-10-16 18:02:43 -0600826 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
827 }
828
829 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600830 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600831 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800832
Ian Elliott338dedb2015-08-21 15:09:33 -0600833 VkImage* swapchainImages =
834 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
835 assert(swapchainImages);
Ian Elliotte2688a52015-10-16 18:02:43 -0600836 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600837 &demo->swapchainImageCount,
838 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600839 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -0600840
Ian Elliott338dedb2015-08-21 15:09:33 -0600841 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600842 assert(demo->buffers);
843
Ian Elliott338dedb2015-08-21 15:09:33 -0600844 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600845 VkImageViewCreateInfo color_image_view = {
846 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600847 .pNext = NULL,
848 .format = demo->format,
Chia-I Wu4291d882015-10-27 19:00:15 +0800849 .components = {
Chia-I Wuc51b1212015-10-27 19:25:11 +0800850 .r = VK_COMPONENT_SWIZZLE_R,
851 .g = VK_COMPONENT_SWIZZLE_G,
852 .b = VK_COMPONENT_SWIZZLE_B,
853 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600854 },
855 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600856 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600857 .baseMipLevel = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800858 .levelCount = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600859 .baseArrayLayer = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800860 .layerCount = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600861 },
862 .viewType = VK_IMAGE_VIEW_TYPE_2D,
863 .flags = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600864 };
865
Ian Elliott338dedb2015-08-21 15:09:33 -0600866 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600867
Tony Barbour40292ae2015-10-21 10:14:48 -0600868 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
869 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600870 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -0600871 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600872 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600873 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600874
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600875 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600876
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600877 err = vkCreateImageView(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +0800878 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600879 assert(!err);
880 }
881}
882
883static void demo_prepare_depth(struct demo *demo)
884{
Tony Barbour8205d902015-04-16 15:59:00 -0600885 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600886 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600887 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600888 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600889 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600890 .format = depth_format,
891 .extent = { demo->width, demo->height, 1 },
892 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600893 .arrayLayers = 1,
Chia-I Wu3138d6a2015-10-31 00:31:16 +0800894 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour8205d902015-04-16 15:59:00 -0600895 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600896 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600897 .flags = 0,
898 };
Dominik Witczake82d5b62015-09-22 18:25:33 +0200899
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600900 VkImageViewCreateInfo view = {
901 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600902 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +0800903 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4ab40c42015-07-15 17:41:38 -0600904 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600905 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600906 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600907 .baseMipLevel = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800908 .levelCount = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600909 .baseArrayLayer = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800910 .layerCount = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600911 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600912 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600913 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600914 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600915
Mark Lobodzinski23182612015-05-29 09:32:35 -0500916 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600917 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600918 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600919
920 demo->depth.format = depth_format;
921
922 /* create image */
Chia-I Wu69f40122015-10-26 21:10:41 +0800923 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600924 &demo->depth.image);
925 assert(!err);
926
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600927 vkGetImageMemoryRequirements(demo->device,
Tony Barbourde4124d2015-07-03 10:33:54 -0600928 demo->depth.image, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -0600929 assert(!err);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600930
Dominik Witczake82d5b62015-09-22 18:25:33 +0200931 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
932 demo->depth.mem_alloc.pNext = NULL;
933 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
934 demo->depth.mem_alloc.memoryTypeIndex = 0;
935
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600936 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600937 mem_reqs.memoryTypeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600938 0, /* No requirements */
Dominik Witczake82d5b62015-09-22 18:25:33 +0200939 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600940 assert(pass);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600941
Mark Lobodzinski23182612015-05-29 09:32:35 -0500942 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +0800943 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu69f40122015-10-26 21:10:41 +0800944 NULL, &demo->depth.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500945 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600946
Mark Lobodzinski23182612015-05-29 09:32:35 -0500947 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600948 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500949 demo->depth.mem, 0);
950 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600951
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600952 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600953 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600954 VK_IMAGE_LAYOUT_UNDEFINED,
955 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600956
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600957 /* create image view */
958 view.image = demo->depth.image;
Chia-I Wu69f40122015-10-26 21:10:41 +0800959 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600960 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600961}
962
Tony Barboura26fb7d2015-09-21 15:17:33 -0600963/* Load a ppm file into memory */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700964bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600965 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600966 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600967{
Tony Barboura26fb7d2015-09-21 15:17:33 -0600968 FILE *fPtr = fopen(filename,"rb");
969 char header[256], *cPtr;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600970
Tony Barboura26fb7d2015-09-21 15:17:33 -0600971 if (!fPtr)
972 return false;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600973
Tony Barboura26fb7d2015-09-21 15:17:33 -0600974 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan85180252015-10-28 11:15:46 -0600975 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
976 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600977 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600978 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600979
Tony Barboura26fb7d2015-09-21 15:17:33 -0600980 do {
981 cPtr = fgets(header, 256, fPtr);
Mike Stroyan85180252015-10-28 11:15:46 -0600982 if (cPtr == NULL) {
983 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600984 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600985 }
Tony Barboura26fb7d2015-09-21 15:17:33 -0600986 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600987
Tony Barboura26fb7d2015-09-21 15:17:33 -0600988 sscanf(header, "%u %u", height, width);
Mike Stroyan85180252015-10-28 11:15:46 -0600989 if (rgba_data == NULL) {
990 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600991 return true;
Mike Stroyan85180252015-10-28 11:15:46 -0600992 }
Tony Barboura26fb7d2015-09-21 15:17:33 -0600993 fgets(header, 256, fPtr); // Format
Mike Stroyan85180252015-10-28 11:15:46 -0600994 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
995 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600996 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600997 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600998
Tony Barboura26fb7d2015-09-21 15:17:33 -0600999 for(int y = 0; y < *height; y++)
1000 {
1001 uint8_t *rowPtr = rgba_data;
1002 for(int x = 0; x < *width; x++)
1003 {
1004 fread(rowPtr, 3, 1, fPtr);
1005 rowPtr[3] = 255; /* Alpha of 1 */
1006 rowPtr += 4;
1007 }
1008 rgba_data += layout->rowPitch;
1009 }
1010 fclose(fPtr);
Mike Stroyan85180252015-10-28 11:15:46 -06001011 return true;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001012}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001013
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001014static void demo_prepare_texture_image(struct demo *demo,
1015 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001016 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001017 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001018 VkImageUsageFlags usage,
Tony Barbourf1eceb92015-10-15 12:42:56 -06001019 VkFlags required_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001020{
Mike Stroyande24a6f2015-06-15 14:21:03 -06001021 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001022 int32_t tex_width;
1023 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -06001024 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001025 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001026
David Pinedo61e77382015-04-23 08:16:57 -06001027 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1028 {
1029 printf("Failed to load textures\n");
1030 fflush(stdout);
1031 exit(1);
1032 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001033
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001034 tex_obj->tex_width = tex_width;
1035 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001036
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001037 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001038 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001039 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -06001040 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001041 .format = tex_format,
1042 .extent = { tex_width, tex_height, 1 },
1043 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -06001044 .arrayLayers = 1,
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001045 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001046 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001047 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001048 .flags = 0,
1049 };
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001050
Mark Lobodzinski23182612015-05-29 09:32:35 -05001051 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001052
Chia-I Wu69f40122015-10-26 21:10:41 +08001053 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001054 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001055 assert(!err);
1056
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001057 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell886be472015-02-23 16:23:13 -07001058
Dominik Witczake82d5b62015-09-22 18:25:33 +02001059 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1060 tex_obj->mem_alloc.pNext = NULL;
1061 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1062 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001063
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001064 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1065 assert(pass);
Mark Lobodzinski72346292015-07-02 16:49:40 -06001066
Mark Lobodzinski23182612015-05-29 09:32:35 -05001067 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +08001068 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001069 &(tex_obj->mem));
1070 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001071
Mark Lobodzinski23182612015-05-29 09:32:35 -05001072 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -06001073 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001074 tex_obj->mem, 0);
1075 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001076
Tony Barbourf1eceb92015-10-15 12:42:56 -06001077 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001078 const VkImageSubresource subres = {
Chia-I Wu195c9e12015-10-27 19:55:05 +08001079 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001080 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -06001081 .arrayLayer = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001082 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001083 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001084 void *data;
1085
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001086 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001087
Dominik Witczake82d5b62015-09-22 18:25:33 +02001088 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001089 assert(!err);
1090
1091 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1092 fprintf(stderr, "Error loading texture: %s\n", filename);
1093 }
1094
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001095 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001096 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001097
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001098 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001099 demo_set_image_layout(demo, tex_obj->image,
Tony Barbour983984f2015-10-12 11:07:58 -06001100 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001101 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001102 tex_obj->imageLayout);
1103 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001104}
1105
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001106static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001107{
1108 /* clean up staging resources */
Chia-I Wu69f40122015-10-26 21:10:41 +08001109 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1110 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001111}
1112
1113static void demo_prepare_textures(struct demo *demo)
1114{
Tony Barbour8205d902015-04-16 15:59:00 -06001115 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001116 VkFormatProperties props;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001117 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001118
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001119 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001120
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001121 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001122 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001123
FslNopper434db6a2015-05-06 21:42:01 +02001124 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001125 /* Device can texture using linear textures */
1126 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001127 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001128 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001129 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001130 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001131
1132 memset(&staging_texture, 0, sizeof(staging_texture));
1133 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wu1f851912015-10-27 18:04:07 +08001134 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001135
1136 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001137 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001138 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wu27d8ed92015-10-27 18:53:22 +08001139 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001140
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001141 demo_set_image_layout(demo, staging_texture.image,
Tony Barbour983984f2015-10-12 11:07:58 -06001142 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001143 staging_texture.imageLayout,
Chia-I Wu1f851912015-10-27 18:04:07 +08001144 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001145
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001146 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -06001147 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001148 demo->textures[i].imageLayout,
Chia-I Wu1f851912015-10-27 18:04:07 +08001149 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001150
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001151 VkImageCopy copy_region = {
Tony Barbourca5c4eb2015-11-02 11:47:51 -07001152 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001153 .srcOffset = { 0, 0, 0 },
Chia-I Wu1f851912015-10-27 18:04:07 +08001154 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1155 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001156 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1157 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001158 vkCmdCopyImage(demo->cmd,
Chia-I Wu1f851912015-10-27 18:04:07 +08001159 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1160 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001161 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001162
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001163 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -06001164 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wu1f851912015-10-27 18:04:07 +08001165 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001166 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001167
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001168 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001169
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001170 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001171 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001172 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1173 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001174 }
1175
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001176 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001177 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001178 .pNext = NULL,
Chia-I Wu3603b082015-10-26 16:49:32 +08001179 .magFilter = VK_FILTER_NEAREST,
1180 .minFilter = VK_FILTER_NEAREST,
1181 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wuce532f72015-10-26 17:32:47 +08001182 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1183 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1184 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001185 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001186 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001187 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001188 .minLod = 0.0f,
1189 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001190 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -06001191 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001192 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001193
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001194 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001195 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001196 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +08001197 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001198 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001199 .format = tex_format,
Chia-I Wu4291d882015-10-27 19:00:15 +08001200 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuc51b1212015-10-27 19:25:11 +08001201 VK_COMPONENT_SWIZZLE_G,
1202 VK_COMPONENT_SWIZZLE_B,
1203 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -06001204 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001205 .flags = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001206 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001207
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001208 /* create sampler */
Chia-I Wu69f40122015-10-26 21:10:41 +08001209 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001210 &demo->textures[i].sampler);
1211 assert(!err);
1212
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001213 /* create image view */
1214 view.image = demo->textures[i].image;
Chia-I Wu69f40122015-10-26 21:10:41 +08001215 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001216 &demo->textures[i].view);
1217 assert(!err);
1218 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001219}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001220
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001221void demo_prepare_cube_data_buffer(struct demo *demo)
1222{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001223 VkBufferCreateInfo buf_info;
Mark Lobodzinski23182612015-05-29 09:32:35 -05001224 VkMemoryRequirements mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001225 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001226 int i;
1227 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001228 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001229 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001230 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001231
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001232 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001233 mat4x4_mul(MVP, VP, demo->model_matrix);
1234 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001235// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001236
1237 for (i=0; i<12*3; i++) {
1238 data.position[i][0] = g_vertex_buffer_data[i*3];
1239 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1240 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1241 data.position[i][3] = 1.0f;
1242 data.attr[i][0] = g_uv_buffer_data[2*i];
1243 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1244 data.attr[i][2] = 0;
1245 data.attr[i][3] = 0;
1246 }
1247
Chia-I Wu714df452015-01-01 07:55:04 +08001248 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001249 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001250 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop86049392015-07-16 10:29:32 -06001251 buf_info.size = sizeof(data);
Chia-I Wu69f40122015-10-26 21:10:41 +08001252 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1253 &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001254 assert(!err);
1255
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001256 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +08001257
Dominik Witczake82d5b62015-09-22 18:25:33 +02001258 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1259 demo->uniform_data.mem_alloc.pNext = NULL;
1260 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1261 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1262
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001263 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001264 mem_reqs.memoryTypeBits,
1265 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczake82d5b62015-09-22 18:25:33 +02001266 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001267 assert(pass);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001268
Chia-I Wu1f851912015-10-27 18:04:07 +08001269 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu69f40122015-10-26 21:10:41 +08001270 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski23182612015-05-29 09:32:35 -05001271 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001272
Dominik Witczake82d5b62015-09-22 18:25:33 +02001273 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001274 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001275
Mark Lobodzinski23182612015-05-29 09:32:35 -05001276 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001277
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001278 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001279
Tony Barbourde4124d2015-07-03 10:33:54 -06001280 err = vkBindBufferMemory(demo->device,
1281 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001282 demo->uniform_data.mem, 0);
1283 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001284
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001285 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1286 demo->uniform_data.buffer_info.offset = 0;
1287 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001288}
1289
Chia-I Wuf8385062015-01-04 16:27:24 +08001290static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001291{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001292 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001293 [0] = {
Chia-I Wub5689ee2015-10-31 00:31:16 +08001294 .binding = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001295 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001296 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001297 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001298 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001299 },
1300 [1] = {
Chia-I Wub5689ee2015-10-31 00:31:16 +08001301 .binding = 1,
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001302 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001303 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001304 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001305 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001306 },
1307 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001308 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001309 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001310 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001311 .bindingCount = 2,
Chia-I Wuf03cbf72015-10-31 00:31:16 +08001312 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001313 };
Tony Barbour22a30862015-04-22 09:02:32 -06001314 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001315
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001316 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001317 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001318 assert(!err);
1319
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001320 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1321 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1322 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001323 .setLayoutCount = 1,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001324 .pSetLayouts = &demo->desc_layout,
1325 };
1326
1327 err = vkCreatePipelineLayout(demo->device,
1328 &pPipelineLayoutCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +08001329 NULL,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001330 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001331 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001332}
1333
Chia-I Wu76cd4222015-07-08 13:34:24 +08001334static void demo_prepare_render_pass(struct demo *demo)
1335{
Chia-I Wuc278df82015-07-07 11:50:03 +08001336 const VkAttachmentDescription attachments[2] = {
1337 [0] = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001338 .format = demo->format,
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001339 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wuc278df82015-07-07 11:50:03 +08001340 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1341 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1342 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1343 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1344 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1345 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1346 },
1347 [1] = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001348 .format = demo->depth.format,
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001349 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wuc278df82015-07-07 11:50:03 +08001350 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1351 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1352 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1353 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1354 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1355 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1356 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001357 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001358 const VkAttachmentReference color_reference = {
1359 .attachment = 0,
1360 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1361 };
Chia-I Wuce532f72015-10-26 17:32:47 +08001362 const VkAttachmentReference depth_reference = {
1363 .attachment = 1,
1364 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1365 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001366 const VkSubpassDescription subpass = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001367 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1368 .flags = 0,
Chia-I Wu763a7492015-10-26 20:48:51 +08001369 .inputAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001370 .pInputAttachments = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001371 .colorAttachmentCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001372 .pColorAttachments = &color_reference,
1373 .pResolveAttachments = NULL,
Chia-I Wuce532f72015-10-26 17:32:47 +08001374 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu763a7492015-10-26 20:48:51 +08001375 .preserveAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001376 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001377 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001378 const VkRenderPassCreateInfo rp_info = {
1379 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1380 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001381 .attachmentCount = 2,
1382 .pAttachments = attachments,
1383 .subpassCount = 1,
1384 .pSubpasses = &subpass,
1385 .dependencyCount = 0,
1386 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001387 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001388 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001389
Chia-I Wu69f40122015-10-26 21:10:41 +08001390 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001391 assert(!err);
1392}
1393
Chia-I Wu062ad152015-10-31 00:31:16 +08001394static VkShaderModule demo_prepare_shader_module(struct demo* demo,
1395 VkShaderStageFlagBits stage,
1396 const void* code,
1397 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001398{
Chia-I Wu062ad152015-10-31 00:31:16 +08001399 VkShaderModule module;
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001400 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001401 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001402
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001403 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1404 moduleCreateInfo.pNext = NULL;
1405
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001406 moduleCreateInfo.codeSize = size;
1407 moduleCreateInfo.pCode = code;
1408 moduleCreateInfo.flags = 0;
1409 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1410 assert(!err);
Chia-I Wu062ad152015-10-31 00:31:16 +08001411
1412 return module;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001413}
1414
Cody Northropacfb0492015-03-17 15:55:58 -06001415char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001416{
1417 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001418 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001419 void *shader_code;
1420
1421 FILE *fp = fopen(filename, "rb");
1422 if (!fp) return NULL;
1423
1424 fseek(fp, 0L, SEEK_END);
1425 size = ftell(fp);
1426
1427 fseek(fp, 0L, SEEK_SET);
1428
1429 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001430 retval = fread(shader_code, size, 1, fp);
1431 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001432
1433 *psize = size;
1434
Mike Stroyan85180252015-10-28 11:15:46 -06001435 fclose(fp);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001436 return shader_code;
1437}
1438
Chia-I Wu062ad152015-10-31 00:31:16 +08001439static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001440{
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001441 void *vertShaderCode;
1442 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001443
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001444 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1445
1446 demo->vert_shader_module = demo_prepare_shader_module(demo,
Chia-I Wu062ad152015-10-31 00:31:16 +08001447 VK_SHADER_STAGE_VERTEX_BIT, vertShaderCode, size);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001448
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001449 free(vertShaderCode);
Chia-I Wu062ad152015-10-31 00:31:16 +08001450
1451 return demo->vert_shader_module;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001452}
1453
Chia-I Wu062ad152015-10-31 00:31:16 +08001454static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001455{
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001456 void *fragShaderCode;
1457 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001458
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001459 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001460
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001461 demo->frag_shader_module = demo_prepare_shader_module(demo,
Chia-I Wu062ad152015-10-31 00:31:16 +08001462 VK_SHADER_STAGE_FRAGMENT_BIT, fragShaderCode, size);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001463
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001464 free(fragShaderCode);
Chia-I Wu062ad152015-10-31 00:31:16 +08001465
1466 return demo->frag_shader_module;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001467}
1468
1469static void demo_prepare_pipeline(struct demo *demo)
1470{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001471 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001472 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand009e8652015-10-15 17:15:25 -07001473 VkPipelineVertexInputStateCreateInfo vi;
Tony Barboure307f582015-07-10 15:29:03 -06001474 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wu1f851912015-10-27 18:04:07 +08001475 VkPipelineRasterizationStateCreateInfo rs;
Tony Barboure307f582015-07-10 15:29:03 -06001476 VkPipelineColorBlendStateCreateInfo cb;
1477 VkPipelineDepthStencilStateCreateInfo ds;
1478 VkPipelineViewportStateCreateInfo vp;
1479 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wu1f851912015-10-27 18:04:07 +08001480 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniell811eb742015-09-29 13:01:09 -06001481 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbour22a30862015-04-22 09:02:32 -06001482 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001483
Piers Daniell811eb742015-09-29 13:01:09 -06001484 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1485 memset(&dynamicState, 0, sizeof dynamicState);
1486 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1487 dynamicState.pDynamicStates = dynamicStateEnables;
1488
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001489 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001490 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001491 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001492
Jason Ekstrand009e8652015-10-15 17:15:25 -07001493 memset(&vi, 0, sizeof(vi));
1494 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1495
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001496 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001497 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001498 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001499
1500 memset(&rs, 0, sizeof(rs));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001501 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1502 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wuce532f72015-10-26 17:32:47 +08001503 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wu1f851912015-10-27 18:04:07 +08001504 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001505 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001506 rs.rasterizerDiscardEnable = VK_FALSE;
1507 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001508
1509 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001510 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1511 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001512 memset(att_state, 0, sizeof(att_state));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001513 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001514 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001515 cb.attachmentCount = 1;
1516 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001517
Tony Barbourfa6cac72015-01-16 14:27:35 -07001518 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001519 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001520 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001521 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1522 vp.scissorCount = 1;
1523 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001524
1525 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001526 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001527 ds.depthTestEnable = VK_TRUE;
1528 ds.depthWriteEnable = VK_TRUE;
Chia-I Wu1f851912015-10-27 18:04:07 +08001529 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001530 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuc51b1212015-10-27 19:25:11 +08001531 ds.back.failOp = VK_STENCIL_OP_KEEP;
1532 ds.back.passOp = VK_STENCIL_OP_KEEP;
1533 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001534 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001535 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001536
Tony Barbourfa6cac72015-01-16 14:27:35 -07001537 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001538 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001539 ms.pSampleMask = NULL;
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001540 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001541
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001542 // Two stages: vs and fs
1543 pipeline.stageCount = 2;
1544 VkPipelineShaderStageCreateInfo shaderStages[2];
1545 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1546
1547 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu062ad152015-10-31 00:31:16 +08001548 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1549 shaderStages[0].module = demo_prepare_vs(demo);
1550 shaderStages[0].pName = "main";
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001551
1552 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu062ad152015-10-31 00:31:16 +08001553 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1554 shaderStages[1].module = demo_prepare_fs(demo);
1555 shaderStages[1].pName = "main";
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001556
Jon Ashburn0d60d272015-07-09 15:02:25 -06001557 memset(&pipelineCache, 0, sizeof(pipelineCache));
1558 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001559
Chia-I Wu69f40122015-10-26 21:10:41 +08001560 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001561 assert(!err);
Tony Barboure307f582015-07-10 15:29:03 -06001562
Jason Ekstrand009e8652015-10-15 17:15:25 -07001563 pipeline.pVertexInputState = &vi;
Tony Barboure307f582015-07-10 15:29:03 -06001564 pipeline.pInputAssemblyState = &ia;
Chia-I Wu1f851912015-10-27 18:04:07 +08001565 pipeline.pRasterizationState = &rs;
Tony Barboure307f582015-07-10 15:29:03 -06001566 pipeline.pColorBlendState = &cb;
1567 pipeline.pMultisampleState = &ms;
1568 pipeline.pViewportState = &vp;
1569 pipeline.pDepthStencilState = &ds;
1570 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001571 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001572 pipeline.pDynamicState = &dynamicState;
Tony Barboure307f582015-07-10 15:29:03 -06001573
Courtney Goeltzenleuchter14eb5ea2015-08-13 16:55:04 -06001574 pipeline.renderPass = demo->render_pass;
1575
Chia-I Wu69f40122015-10-26 21:10:41 +08001576 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1577 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001578 assert(!err);
1579
Chia-I Wu69f40122015-10-26 21:10:41 +08001580 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1581 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001582}
1583
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001584static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001585{
Chia-I Wuc51b1212015-10-27 19:25:11 +08001586 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001587 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001588 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001589 .descriptorCount = 1,
Chia-I Wuf8385062015-01-04 16:27:24 +08001590 },
1591 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001592 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001593 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wuf8385062015-01-04 16:27:24 +08001594 },
1595 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001596 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001597 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001598 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001599 .maxSets = 1,
Chia-I Wuc51b1212015-10-27 19:25:11 +08001600 .poolSizeCount = 2,
1601 .pPoolSizes = type_counts,
Chia-I Wuf8385062015-01-04 16:27:24 +08001602 };
Tony Barbour22a30862015-04-22 09:02:32 -06001603 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001604
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001605 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001606 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001607 assert(!err);
1608}
1609
1610static void demo_prepare_descriptor_set(struct demo *demo)
1611{
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001612 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001613 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001614 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001615 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001616
Chia-I Wu1f851912015-10-27 18:04:07 +08001617 VkDescriptorSetAllocateInfo alloc_info = {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001618 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO,
1619 .pNext = NULL,
1620 .descriptorPool = demo->desc_pool,
Chia-I Wu763a7492015-10-26 20:48:51 +08001621 .setLayoutCount = 1,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001622 .pSetLayouts = &demo->desc_layout
1623 };
Chia-I Wu1f851912015-10-27 18:04:07 +08001624 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northropc8aa4a52015-08-03 12:47:29 -06001625 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001626
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001627 memset(&tex_descs, 0, sizeof(tex_descs));
1628 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001629 tex_descs[i].sampler = demo->textures[i].sampler;
1630 tex_descs[i].imageView = demo->textures[i].view;
1631 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001632 }
1633
1634 memset(&writes, 0, sizeof(writes));
1635
1636 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu1f851912015-10-27 18:04:07 +08001637 writes[0].dstSet = demo->desc_set;
Chia-I Wu763a7492015-10-26 20:48:51 +08001638 writes[0].descriptorCount = 1;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001639 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001640 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001641
1642 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu1f851912015-10-27 18:04:07 +08001643 writes[1].dstSet = demo->desc_set;
1644 writes[1].dstBinding = 1;
Chia-I Wu763a7492015-10-26 20:48:51 +08001645 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001646 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001647 writes[1].pImageInfo = tex_descs;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001648
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001649 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001650}
1651
Chia-I Wu76cd4222015-07-08 13:34:24 +08001652static void demo_prepare_framebuffers(struct demo *demo)
1653{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001654 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001655 attachments[1] = demo->depth.view;
1656
Chia-I Wu76cd4222015-07-08 13:34:24 +08001657 const VkFramebufferCreateInfo fb_info = {
1658 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1659 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001660 .renderPass = demo->render_pass,
1661 .attachmentCount = 2,
1662 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001663 .width = demo->width,
1664 .height = demo->height,
1665 .layers = 1,
1666 };
1667 VkResult U_ASSERT_ONLY err;
1668 uint32_t i;
1669
Tony Barbour5aabff52015-10-08 14:26:24 -06001670 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1671 assert(demo->framebuffers);
1672
1673 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001674 attachments[0] = demo->buffers[i].view;
Chia-I Wu69f40122015-10-26 21:10:41 +08001675 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001676 assert(!err);
1677 }
1678}
1679
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001680static void demo_prepare(struct demo *demo)
1681{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001682 VkResult U_ASSERT_ONLY err;
1683
Chia-I Wu1f851912015-10-27 18:04:07 +08001684 const VkCommandPoolCreateInfo cmd_pool_info = {
1685 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001686 .pNext = NULL,
1687 .queueFamilyIndex = demo->graphics_queue_node_index,
1688 .flags = 0,
1689 };
Chia-I Wu69f40122015-10-26 21:10:41 +08001690 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop18ea11b2015-07-09 18:08:32 -06001691 assert(!err);
1692
Chia-I Wu1f851912015-10-27 18:04:07 +08001693 const VkCommandBufferAllocateInfo cmd = {
1694 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001695 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001696 .commandPool = demo->cmd_pool,
1697 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +08001698 .bufferCount = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001699 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001700
1701 demo_prepare_buffers(demo);
1702 demo_prepare_depth(demo);
1703 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001704 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001705
Chia-I Wuf8385062015-01-04 16:27:24 +08001706 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001707 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001708 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001709
Ian Elliott338dedb2015-08-21 15:09:33 -06001710 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08001711 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001712 assert(!err);
1713 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001714
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001715 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001716 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001717
Chia-I Wu76cd4222015-07-08 13:34:24 +08001718 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001719
Ian Elliott338dedb2015-08-21 15:09:33 -06001720 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001721 demo->current_buffer = i;
1722 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1723 }
1724
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001725 /*
1726 * Prepare functions above may generate pipeline commands
1727 * that need to be flushed before beginning the render loop.
1728 */
1729 demo_flush_init_cmd(demo);
1730
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001731 demo->current_buffer = 0;
Mike Stroyanb194be62015-10-28 09:46:57 -06001732 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001733}
1734
David Pinedoeeca2a22015-06-18 17:03:14 -06001735static void demo_cleanup(struct demo *demo)
1736{
1737 uint32_t i;
1738
1739 demo->prepared = false;
1740
Tony Barbour5aabff52015-10-08 14:26:24 -06001741 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001742 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbourde4124d2015-07-03 10:33:54 -06001743 }
Tony Barbour5aabff52015-10-08 14:26:24 -06001744 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08001745 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001746
Chia-I Wu69f40122015-10-26 21:10:41 +08001747 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1748 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1749 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1750 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1751 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001752
1753 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001754 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1755 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1756 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1757 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001758 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001759 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
David Pinedoeeca2a22015-06-18 17:03:14 -06001760
Chia-I Wu69f40122015-10-26 21:10:41 +08001761 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1762 vkDestroyImage(demo->device, demo->depth.image, NULL);
1763 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001764
Chia-I Wu69f40122015-10-26 21:10:41 +08001765 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1766 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001767
Ian Elliott338dedb2015-08-21 15:09:33 -06001768 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001769 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001770 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001771 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001772 free(demo->buffers);
David Pinedoeeca2a22015-06-18 17:03:14 -06001773
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001774 free(demo->queue_props);
1775
Chia-I Wu69f40122015-10-26 21:10:41 +08001776 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1777 vkDestroyDevice(demo->device, NULL);
Tony Barboura65ecc22015-06-30 14:14:19 -06001778 if (demo->validate) {
1779 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1780 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001781 vkDestroyInstance(demo->inst, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001782
1783#ifndef _WIN32
1784 xcb_destroy_window(demo->connection, demo->window);
1785 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001786 free(demo->atom_wm_delete_window);
David Pinedoeeca2a22015-06-18 17:03:14 -06001787#endif // _WIN32
1788}
1789
Ian Elliotte2688a52015-10-16 18:02:43 -06001790static void demo_resize(struct demo *demo)
1791{
1792 uint32_t i;
1793
Mike Stroyanb194be62015-10-28 09:46:57 -06001794 // Don't react to resize until after first initialization.
1795 if (!demo->prepared) {
1796 return;
1797 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001798 // In order to properly resize the window, we must re-create the swapchain
1799 // AND redo the command buffers, etc.
1800 //
1801 // First, perform part of the demo_cleanup() function:
1802 demo->prepared = false;
1803
1804 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001805 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001806 }
1807 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08001808 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001809
Chia-I Wu69f40122015-10-26 21:10:41 +08001810 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1811 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1812 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1813 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1814 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001815
1816 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001817 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1818 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1819 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1820 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001821 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001822
Chia-I Wu69f40122015-10-26 21:10:41 +08001823 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1824 vkDestroyImage(demo->device, demo->depth.image, NULL);
1825 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001826
Chia-I Wu69f40122015-10-26 21:10:41 +08001827 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1828 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001829
1830 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001831 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott74bb2eb2015-10-27 11:06:33 -06001832 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliotte2688a52015-10-16 18:02:43 -06001833 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001834 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001835 free(demo->buffers);
1836
Chia-I Wu69f40122015-10-26 21:10:41 +08001837 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliott74bb2eb2015-10-27 11:06:33 -06001838
Ian Elliotte2688a52015-10-16 18:02:43 -06001839
1840 // Second, re-perform the demo_prepare() function, which will re-create the
1841 // swapchain:
1842 demo_prepare(demo);
1843}
1844
David Pinedoeeca2a22015-06-18 17:03:14 -06001845// On MS-Windows, make this a global, so it's available to WndProc()
1846struct demo demo;
1847
Ian Elliotte14e9f92015-04-16 15:23:05 -06001848#ifdef _WIN32
1849static void demo_run(struct demo *demo)
1850{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001851 if (!demo->prepared)
1852 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001853 // Wait for work to finish before updating MVP.
1854 vkDeviceWaitIdle(demo->device);
1855 demo_update_data_buffer(demo);
1856
1857 demo_draw(demo);
1858
1859 // Wait for work to finish before updating MVP.
1860 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001861
David Pinedoeeca2a22015-06-18 17:03:14 -06001862 demo->curFrame++;
1863
1864 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1865 {
1866 demo->quit=true;
1867 demo_cleanup(demo);
1868 ExitProcess(0);
1869 }
1870
1871}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001872
1873// MS-Windows event handling function:
1874LRESULT CALLBACK WndProc(HWND hWnd,
1875 UINT uMsg,
1876 WPARAM wParam,
1877 LPARAM lParam)
1878{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001879 switch(uMsg)
1880 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001881 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001882 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001883 break;
Ian Elliotte36b2082015-07-06 14:27:58 -06001884 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001885 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001886 break;
Ian Elliottf3f80822015-10-21 15:14:07 -06001887 case WM_SIZE:
Mike Stroyanb194be62015-10-28 09:46:57 -06001888 demo.width = lParam & 0xffff;
1889 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliottf3f80822015-10-21 15:14:07 -06001890 demo_resize(&demo);
1891 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001892 default:
1893 break;
1894 }
1895 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1896}
1897
1898static void demo_create_window(struct demo *demo)
1899{
1900 WNDCLASSEX win_class;
1901
1902 // Initialize the window class structure:
1903 win_class.cbSize = sizeof(WNDCLASSEX);
1904 win_class.style = CS_HREDRAW | CS_VREDRAW;
1905 win_class.lpfnWndProc = WndProc;
1906 win_class.cbClsExtra = 0;
1907 win_class.cbWndExtra = 0;
1908 win_class.hInstance = demo->connection; // hInstance
1909 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1910 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1911 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1912 win_class.lpszMenuName = NULL;
1913 win_class.lpszClassName = demo->name;
1914 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1915 // Register window class:
1916 if (!RegisterClassEx(&win_class)) {
1917 // It didn't work, so try to give a useful error:
1918 printf("Unexpected error trying to start the application!\n");
1919 fflush(stdout);
1920 exit(1);
1921 }
1922 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001923 RECT wr = { 0, 0, demo->width, demo->height };
1924 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001925 demo->window = CreateWindowEx(0,
1926 demo->name, // class name
1927 demo->name, // app name
1928 WS_OVERLAPPEDWINDOW | // window style
1929 WS_VISIBLE |
1930 WS_SYSMENU,
1931 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06001932 wr.right-wr.left, // width
1933 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001934 NULL, // handle to parent
1935 NULL, // handle to menu
1936 demo->connection, // hInstance
1937 NULL); // no extra parameters
1938 if (!demo->window) {
1939 // It didn't work, so try to give a useful error:
1940 printf("Cannot create a window in which to draw!\n");
1941 fflush(stdout);
1942 exit(1);
1943 }
1944}
1945#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001946static void demo_handle_event(struct demo *demo,
1947 const xcb_generic_event_t *event)
1948{
Piers Daniell886be472015-02-23 16:23:13 -07001949 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001950 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001951 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001952 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001953 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001954 case XCB_CLIENT_MESSAGE:
1955 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1956 (*demo->atom_wm_delete_window).atom) {
1957 demo->quit = true;
1958 }
1959 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001960 case XCB_KEY_RELEASE:
1961 {
1962 const xcb_key_release_event_t *key =
1963 (const xcb_key_release_event_t *) event;
1964
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001965 switch (key->detail) {
1966 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001967 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001968 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001969 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001970 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001971 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001972 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001973 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001974 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001975 case 0x41:
1976 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001977 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001978 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001979 }
1980 break;
Ian Elliotte2688a52015-10-16 18:02:43 -06001981 case XCB_CONFIGURE_NOTIFY:
1982 {
1983 const xcb_configure_notify_event_t *cfg =
1984 (const xcb_configure_notify_event_t *) event;
1985 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
1986 demo_resize(demo);
1987 }
1988 }
1989 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001990 default:
1991 break;
1992 }
1993}
1994
1995static void demo_run(struct demo *demo)
1996{
1997 xcb_flush(demo->connection);
1998
1999 while (!demo->quit) {
2000 xcb_generic_event_t *event;
2001
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002002 if (demo->pause) {
2003 event = xcb_wait_for_event(demo->connection);
2004 } else {
2005 event = xcb_poll_for_event(demo->connection);
2006 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002007 if (event) {
2008 demo_handle_event(demo, event);
2009 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002010 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002011
2012 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002013 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002014 demo_update_data_buffer(demo);
2015
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002016 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07002017
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002018 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002019 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06002020 demo->curFrame++;
Tony Barboura26fb7d2015-09-21 15:17:33 -06002021 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedoeeca2a22015-06-18 17:03:14 -06002022 demo->quit = true;
2023
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002024 }
2025}
2026
2027static void demo_create_window(struct demo *demo)
2028{
2029 uint32_t value_mask, value_list[32];
2030
2031 demo->window = xcb_generate_id(demo->connection);
2032
2033 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2034 value_list[0] = demo->screen->black_pixel;
2035 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliotte2688a52015-10-16 18:02:43 -06002036 XCB_EVENT_MASK_EXPOSURE |
2037 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002038
2039 xcb_create_window(demo->connection,
2040 XCB_COPY_FROM_PARENT,
2041 demo->window, demo->screen->root,
2042 0, 0, demo->width, demo->height, 0,
2043 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2044 demo->screen->root_visual,
2045 value_mask, value_list);
2046
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002047 /* Magic code that will send notification when window is destroyed */
2048 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2049 "WM_PROTOCOLS");
2050 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2051
2052 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2053 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2054
2055 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2056 demo->window, (*reply).atom, 4, 32, 1,
2057 &(*demo->atom_wm_delete_window).atom);
2058 free(reply);
2059
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002060 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06002061
2062 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2063 const uint32_t coords[] = {100, 100};
2064 xcb_configure_window(demo->connection, demo->window,
2065 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002066}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002067#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002068
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002069/*
2070 * Return 1 (true) if all layer names specified in check_names
2071 * can be found in given layer properties.
2072 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002073static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002074 uint32_t layer_count, VkLayerProperties *layers)
2075{
2076 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002077 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002078 for (uint32_t j = 0; j < layer_count; j++) {
2079 if (!strcmp(check_names[i], layers[j].layerName)) {
2080 found = 1;
2081 }
2082 }
2083 if (!found) {
2084 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2085 return 0;
2086 }
2087 }
2088 return 1;
2089}
2090
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002091static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002092{
Tobin Ehlis3536b442015-04-16 18:04:57 -06002093 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002094 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002095 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06002096 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002097 VkLayerProperties *instance_layers;
2098 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002099 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002100 uint32_t instance_layer_count = 0;
2101 uint32_t enabled_extension_count = 0;
2102 uint32_t enabled_layer_count = 0;
2103
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002104 char *instance_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002105 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002106 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002107 "ObjectTracker",
2108 "DrawState",
2109 "ParamChecker",
2110 "ShaderChecker",
Ian Elliott329da012015-09-22 10:51:24 -06002111 "Swapchain",
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002112 "DeviceLimits",
2113 "Image",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002114 };
2115
2116 char *device_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002117 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002118 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002119 "ObjectTracker",
2120 "DrawState",
2121 "ParamChecker",
2122 "ShaderChecker",
Ian Elliott329da012015-09-22 10:51:24 -06002123 "Swapchain",
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002124 "DeviceLimits",
2125 "Image",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002126 };
2127
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002128 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002129 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002130 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002131 assert(!err);
2132
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002133 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002134 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002135 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002136
2137 if (demo->validate) {
2138 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2139 instance_layer_count, instance_layers);
2140 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002141 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002142 "required validation layer.\n\n"
2143 "Please look at the Getting Started guide for additional "
2144 "information.\n",
2145 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002146 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002147 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002148 }
2149
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002150 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002151 assert(!err);
2152
Tony Barbourd9955e42015-10-08 13:59:42 -06002153 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002154 memset(extension_names, 0, sizeof(extension_names));
2155 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002156 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002157 assert(!err);
2158 for (uint32_t i = 0; i < instance_extension_count; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08002159 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extensionName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06002160 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06002161 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002162 }
Chia-I Wu1f851912015-10-27 18:04:07 +08002163 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002164 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002165 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002166 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002167 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002168 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002169 }
Tony Barbourd9955e42015-10-08 13:59:42 -06002170 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002171 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06002172 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002173 "Vulkan installable client driver (ICD) installed?\nPlease "
2174 "look at the Getting Started guide for additional "
2175 "information.\n",
2176 "vkCreateInstance Failure");
2177 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002178 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002179 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002180 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08002181 .pApplicationName = APP_SHORT_NAME,
2182 .applicationVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002183 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002184 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002185 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002186 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002187 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002188 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002189 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08002190 .pApplicationInfo = &app,
Chia-I Wu763a7492015-10-26 20:48:51 +08002191 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002192 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu763a7492015-10-26 20:48:51 +08002193 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002194 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002195 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002196
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002197 uint32_t gpu_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002198
Chia-I Wu69f40122015-10-26 21:10:41 +08002199 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002200 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2201 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002202 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002203 "additional information.\n",
2204 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06002205 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002206 ERR_EXIT("Cannot find a specified extension library"
2207 ".\nMake sure your layers path is set appropriately\n",
2208 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002209 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002210 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2211 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002212 "the Getting Started guide for additional information.\n",
2213 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002214 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002215
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002216 free(instance_layers);
2217 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002218
Tobin Ehlis4f482a72015-09-07 15:16:39 -06002219 /* Make initial call to query gpu_count, then second call for gpu info*/
2220 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2221 assert(!err && gpu_count > 0);
2222 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2223 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2224 assert(!err);
2225 /* For cube demo we just grab the first physical device */
2226 demo->gpu = physical_devices[0];
2227 free(physical_devices);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002228
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002229 /* Look for validation layers */
2230 validation_found = 0;
2231 enabled_layer_count = 0;
2232 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002233 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002234 assert(!err);
2235
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002236 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002237 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002238 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002239
2240 if (demo->validate) {
2241 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2242 device_layer_count, device_layers);
2243 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002244 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002245 "a required validation layer.\n\n"
2246 "Please look at the Getting Started guide for additional "
2247 "information.\n",
2248 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002249 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002250 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002251 }
2252
2253 uint32_t device_extension_count = 0;
2254 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002255 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002256 demo->gpu, NULL, &device_extension_count, NULL);
2257 assert(!err);
2258
Tony Barbourd9955e42015-10-08 13:59:42 -06002259 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002260 enabled_extension_count = 0;
2261 memset(extension_names, 0, sizeof(extension_names));
2262 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002263 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002264 demo->gpu, NULL, &device_extension_count, device_extensions);
2265 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06002266
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002267 for (uint32_t i = 0; i < device_extension_count; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08002268 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extensionName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06002269 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06002270 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002271 }
2272 assert(enabled_extension_count < 64);
2273 }
Tony Barbourd9955e42015-10-08 13:59:42 -06002274 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002275 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06002276 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002277 "Vulkan installable client driver (ICD) installed?\nPlease "
2278 "look at the Getting Started guide for additional "
2279 "information.\n",
2280 "vkCreateInstance Failure");
2281 }
2282
Tony Barbour5685ad72015-04-29 16:19:20 -06002283 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06002284 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2285 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002286 if (!demo->dbgCreateMsgCallback) {
2287 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2288 "vkGetProcAddr Failure");
2289 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002290 if (!demo->dbgDestroyMsgCallback) {
2291 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2292 "vkGetProcAddr Failure");
2293 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002294 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2295 if (!demo->dbgBreakCallback) {
2296 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2297 "vkGetProcAddr Failure");
2298 }
2299
2300 PFN_vkDbgMsgCallback callback;
2301
2302 if (!demo->use_break) {
2303 callback = dbgFunc;
2304 } else {
2305 callback = demo->dbgBreakCallback;
2306 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002307 err = demo->dbgCreateMsgCallback(
2308 demo->inst,
2309 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002310 callback, NULL,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002311 &demo->msg_callback);
2312 switch (err) {
2313 case VK_SUCCESS:
2314 break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002315 case VK_ERROR_OUT_OF_HOST_MEMORY:
2316 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2317 "dbgCreateMsgCallback Failure");
2318 break;
2319 default:
2320 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2321 "dbgCreateMsgCallback Failure");
2322 break;
2323 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002324 }
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002325 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002326
2327 /* Call with NULL data to get count */
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002328 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002329 assert(demo->queue_count >= 1);
2330
2331 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002332 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002333 // Find a queue that supports gfx
2334 uint32_t gfx_queue_idx = 0;
2335 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2336 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2337 break;
2338 }
2339 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlis795460a2015-09-24 15:18:22 -06002340 // Query fine-grained feature support for this device.
2341 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis6454cd92015-09-29 11:22:37 -06002342 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002343 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlis795460a2015-09-24 15:18:22 -06002344
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06002345 float queue_priorities[1] = { 0.0 };
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002346 const VkDeviceQueueCreateInfo queue = {
2347 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2348 .pNext = NULL,
2349 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu763a7492015-10-26 20:48:51 +08002350 .queuePriorityCount = 1,
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06002351 .pQueuePriorities = queue_priorities
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002352 };
2353
2354 VkDeviceCreateInfo device = {
2355 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2356 .pNext = NULL,
Courtney Goeltzenleuchterdfd53f52015-10-15 16:58:44 -06002357 .requestedQueueCount = 1,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002358 .pRequestedQueues = &queue,
Chia-I Wu763a7492015-10-26 20:48:51 +08002359 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002360 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu763a7492015-10-26 20:48:51 +08002361 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002362 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlis795460a2015-09-24 15:18:22 -06002363 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002364 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002365
Chia-I Wu69f40122015-10-26 21:10:41 +08002366 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002367 assert(!err);
2368
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002369 free(device_layers);
2370
Ian Elliott338dedb2015-08-21 15:09:33 -06002371 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2372 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2373 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2374 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2375 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliott338dedb2015-08-21 15:09:33 -06002376 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2377 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2378 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2379 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002380}
2381
Tony Barbourd9955e42015-10-08 13:59:42 -06002382static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002383{
2384 VkResult err;
2385 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002386
Tony Barbourd9955e42015-10-08 13:59:42 -06002387 // Construct the surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06002388 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002389 demo->surface_description.pNext = NULL;
2390#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06002391 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002392 demo->surface_description.pPlatformHandle = demo->connection;
2393 demo->surface_description.pPlatformWindow = demo->window;
2394#else // _WIN32
2395 demo->platform_handle_xcb.connection = demo->connection;
2396 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06002397 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002398 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2399 demo->surface_description.pPlatformWindow = &demo->window;
2400#endif // _WIN32
2401
Tony Barbourd9955e42015-10-08 13:59:42 -06002402 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002403 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2404 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06002405 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2406 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06002407 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002408 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002409
2410 // Search for a graphics and a present queue in the array of queue
2411 // families, try to find one that supports both
2412 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2413 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002414 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002415 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2416 if (graphicsQueueNodeIndex == UINT32_MAX) {
2417 graphicsQueueNodeIndex = i;
2418 }
2419
2420 if (supportsPresent[i] == VK_TRUE) {
2421 graphicsQueueNodeIndex = i;
2422 presentQueueNodeIndex = i;
2423 break;
2424 }
2425 }
2426 }
2427 if (presentQueueNodeIndex == UINT32_MAX) {
2428 // If didn't find a queue that supports both graphics and present, then
2429 // find a separate present queue.
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002430 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002431 if (supportsPresent[i] == VK_TRUE) {
2432 presentQueueNodeIndex = i;
2433 break;
2434 }
2435 }
2436 }
2437 free(supportsPresent);
2438
2439 // Generate error if could not find both a graphics and a present queue
2440 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2441 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002442 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002443 }
2444
2445 // TODO: Add support for separate queues, including presentation,
2446 // synchronization, and appropriate tracking for QueueSubmit
2447 // While it is possible for an application to use a separate graphics and a
2448 // present queues, this demo program assumes it is only using one:
2449 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2450 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002451 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002452 }
2453
2454 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002455
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002456 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002457 0, &demo->queue);
Tony Barbour5685ad72015-04-29 16:19:20 -06002458
Ian Elliotte36b2082015-07-06 14:27:58 -06002459 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002460 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002461 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2462 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002463 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002464 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002465 VkSurfaceFormatKHR *surfFormats =
2466 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2467 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2468 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002469 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002470 assert(!err);
2471 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2472 // the surface has no preferred format. Otherwise, at least one
2473 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002474 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2475 {
2476 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2477 }
2478 else
2479 {
2480 assert(formatCount >= 1);
2481 demo->format = surfFormats[0].format;
2482 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002483 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002484
David Pinedoeeca2a22015-06-18 17:03:14 -06002485 demo->quit = false;
2486 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002487
2488 // Get Memory information and properties
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002489 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002490}
2491
2492static void demo_init_connection(struct demo *demo)
2493{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002494#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002495 const xcb_setup_t *setup;
2496 xcb_screen_iterator_t iter;
2497 int scr;
2498
2499 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002500 if (demo->connection == NULL) {
2501 printf("Cannot find a compatible Vulkan installable client driver "
2502 "(ICD).\nExiting ...\n");
2503 fflush(stdout);
2504 exit(1);
2505 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002506
2507 setup = xcb_get_setup(demo->connection);
2508 iter = xcb_setup_roots_iterator(setup);
2509 while (scr-- > 0)
2510 xcb_screen_next(&iter);
2511
2512 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002513#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002514}
2515
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002516static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002517{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002518 vec3 eye = {0.0f, 3.0f, 5.0f};
2519 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002520 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002521
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002522 memset(demo, 0, sizeof(*demo));
Tony Barboura26fb7d2015-09-21 15:17:33 -06002523 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002524
Piers Daniell886be472015-02-23 16:23:13 -07002525 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002526 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002527 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002528 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002529 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002530 if (strcmp(argv[i], "--break") == 0) {
2531 demo->use_break = true;
2532 continue;
2533 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002534 if (strcmp(argv[i], "--validate") == 0) {
2535 demo->validate = true;
2536 continue;
2537 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002538 if (strcmp(argv[i], "--c") == 0 &&
Tony Barboura26fb7d2015-09-21 15:17:33 -06002539 demo->frameCount == INT32_MAX &&
David Pinedoeeca2a22015-06-18 17:03:14 -06002540 i < argc-1 &&
2541 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2542 demo->frameCount >= 0)
2543 {
2544 i++;
2545 continue;
2546 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002547
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002548 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002549 fflush(stderr);
2550 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002551 }
2552
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002553 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002554 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002555
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002556 demo->width = 500;
2557 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002558
2559 demo->spin_angle = 0.01f;
2560 demo->spin_increment = 0.01f;
2561 demo->pause = false;
2562
Piers Daniell886be472015-02-23 16:23:13 -07002563 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002564 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2565 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002566}
2567
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002568
Ian Elliotte14e9f92015-04-16 15:23:05 -06002569#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002570extern int __getmainargs(
2571 int * _Argc,
2572 char *** _Argv,
2573 char *** _Env,
2574 int _DoWildCard,
2575 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002576
Ian Elliott421107f2015-04-28 15:50:36 -06002577int WINAPI WinMain(HINSTANCE hInstance,
2578 HINSTANCE hPrevInstance,
2579 LPSTR pCmdLine,
2580 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002581{
2582 MSG msg; // message
2583 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002584 int argc;
2585 char** argv;
2586 char** env;
2587 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002588
Tony Barbour5685ad72015-04-29 16:19:20 -06002589 __getmainargs(&argc,&argv,&env,0,&new_mode);
2590
2591 demo_init(&demo, argc, argv);
2592 demo.connection = hInstance;
2593 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002594 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002595 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002596
2597 demo_prepare(&demo);
2598
2599 done = false; //initialize loop condition variable
2600 /* main message loop*/
2601 while(!done)
2602 {
Ian Elliott421107f2015-04-28 15:50:36 -06002603 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002604 if (msg.message == WM_QUIT) //check for a quit message
2605 {
2606 done = true; //if found, quit app
2607 }
2608 else
2609 {
2610 /* Translate and dispatch to event queue*/
2611 TranslateMessage(&msg);
2612 DispatchMessage(&msg);
2613 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002614 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002615 }
2616
2617 demo_cleanup(&demo);
2618
Tony Barboura938abb2015-04-22 11:36:22 -06002619 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002620}
2621#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002622int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002623{
2624 struct demo demo;
2625
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002626 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002627 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002628 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002629
2630 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002631 demo_run(&demo);
2632
2633 demo_cleanup(&demo);
2634
2635 return 0;
2636}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002637#endif // _WIN32