blob: f0e393adc39c1c25a63556da08ed09928d28b5f8 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
Ian Elliotta748eaf2015-04-28 15:50:36 -06002 *
Courtney Goeltzenleuchterbb387b72015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Ian Elliotta748eaf2015-04-28 15:50:36 -06004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060022 *
23 * Author: Chia-I Wu <olv@lunarg.com>
24 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
25 * Author: Ian Elliott <ian@LunarG.com>
26 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060027 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdbool.h>
33#include <assert.h>
34
Ian Elliott639ca472015-04-16 15:23:05 -060035#ifdef _WIN32
36#pragma comment(linker, "/subsystem:windows")
37#include <windows.h>
38#define APP_NAME_STR_LEN 80
39#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060040#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060041#endif // _WIN32
42
David Pinedoc5193c12015-11-06 12:54:48 -070043#include <vulkan/vulkan.h>
Ian Elliottf4a4e442015-11-17 17:29:40 -070044
45#include <vulkan/VK_KHR_surface.h>
46#include <vulkan/VK_KHR_swapchain.h>
47#include "vulkan/vk_lunarg_debug_report.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048
David Pinedo541526f2015-11-24 09:00:24 -070049#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060050#include "linmath.h"
51
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060053#define APP_SHORT_NAME "cube"
54#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060055
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060056#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
57
Tony Barbourfdc2d352015-04-22 09:02:32 -060058#if defined(NDEBUG) && defined(__GNUC__)
59#define U_ASSERT_ONLY __attribute__((unused))
60#else
61#define U_ASSERT_ONLY
62#endif
63
Ian Elliott07264132015-04-28 11:35:02 -060064#ifdef _WIN32
65#define ERR_EXIT(err_msg, err_class) \
66 do { \
67 MessageBox(NULL, err_msg, err_class, MB_OK); \
68 exit(1); \
69 } while (0)
70
Ian Elliott07264132015-04-28 11:35:02 -060071#else // _WIN32
72
73#define ERR_EXIT(err_msg, err_class) \
74 do { \
75 printf(err_msg); \
76 fflush(stdout); \
77 exit(1); \
78 } while (0)
79#endif // _WIN32
80
Ian Elliottaaae5352015-07-06 14:27:58 -060081#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
82{ \
83 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
84 if (demo->fp##entrypoint == NULL) { \
85 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
86 "vkGetInstanceProcAddr Failure"); \
87 } \
88}
89
Jon Ashburn7d8342c2015-10-08 15:58:23 -060090static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
91
Ian Elliott673898b2015-06-22 15:07:49 -060092#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
93{ \
Jon Ashburn7d8342c2015-10-08 15:58:23 -060094 if(!g_gdpa) \
95 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
96 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060097 if (demo->fp##entrypoint == NULL) { \
98 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
99 "vkGetDeviceProcAddr Failure"); \
100 } \
101}
102
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600103/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700104 * structure to track all objects related to a texture.
105 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600106struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600107 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700108
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600109 VkImage image;
110 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600111
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800112 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500113 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600114 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700115 int32_t tex_width, tex_height;
116};
117
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600118static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600119 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600120};
121
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600122struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600123 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600124 float mvp[4][4];
125 float position[12*3][4];
126 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
128
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600129struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600130 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600131 float mvp[4][4];
132 float position[12*3][4];
133 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600134};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600135
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600136//--------------------------------------------------------------------------------------
137// Mesh and VertexFormat Data
138//--------------------------------------------------------------------------------------
139struct Vertex
140{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600141 float posX, posY, posZ, posW; // Position data
142 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600143};
144
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600145struct VertexPosTex
146{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600147 float posX, posY, posZ, posW; // Position data
148 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149};
150
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600151#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600152#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600153
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600154static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 -1.0f,-1.0f, 1.0f,
157 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 -1.0f, 1.0f,-1.0f,
160 -1.0f,-1.0f,-1.0f,
161
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 1.0f, 1.0f,-1.0f,
164 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600171 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 -1.0f, 1.0f, 1.0f,
178 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600181 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 1.0f, 1.0f, 1.0f,
185 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f,-1.0f,-1.0f,
188 1.0f, 1.0f,-1.0f,
189
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600192 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600194 1.0f,-1.0f, 1.0f,
195 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600196};
197
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600198static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800199 0.0f, 0.0f, // -X side
200 1.0f, 0.0f,
201 1.0f, 1.0f,
202 1.0f, 1.0f,
203 0.0f, 1.0f,
204 0.0f, 0.0f,
205
206 1.0f, 0.0f, // -Z side
207 0.0f, 1.0f,
208 0.0f, 0.0f,
209 1.0f, 0.0f,
210 1.0f, 1.0f,
211 0.0f, 1.0f,
212
213 1.0f, 1.0f, // -Y side
214 1.0f, 0.0f,
215 0.0f, 0.0f,
216 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 0.0f, 0.0f,
218 0.0f, 1.0f,
219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600221 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600223 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 1.0f, 1.0f, // +X side
228 0.0f, 1.0f,
229 0.0f, 0.0f,
230 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600232 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 0.0f, 1.0f, // +Z side
235 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600237 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800238 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600239 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600240};
241
242void dumpMatrix(const char *note, mat4x4 MVP)
243{
244 int i;
245
246 printf("%s: \n", note);
247 for (i=0; i<4; i++) {
248 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
249 }
250 printf("\n");
251 fflush(stdout);
252}
253
254void dumpVec4(const char *note, vec4 vector)
255{
256 printf("%s: \n", note);
257 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
258 printf("\n");
259 fflush(stdout);
260}
261
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600262VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600263 VkFlags msgFlags,
264 VkDbgObjectType objType,
265 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600266 size_t location,
267 int32_t msgCode,
268 const char* pLayerPrefix,
269 const char* pMsg,
270 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600271{
272 char *message = (char *) malloc(strlen(pMsg)+100);
273
274 assert (message);
275
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600276 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
277 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
278 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600279 // We know that we're submitting queues without fences, ignore this warning
280 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600281 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600282 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600283 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600284 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600285 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600286 }
287
288#ifdef _WIN32
289 MessageBox(NULL, message, "Alert", MB_OK);
290#else
291 printf("%s\n",message);
292 fflush(stdout);
293#endif
294 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600295
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600296 /*
297 * false indicates that layer should not bail-out of an
298 * API call that had validation failures. This may mean that the
299 * app dies inside the driver due to invalid parameter(s).
300 * That's what would happen without validation layers, so we'll
301 * keep that behavior here.
302 */
303 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600304}
305
Ian Elliotta0781972015-08-21 15:09:33 -0600306typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600307 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800308 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600309 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600310} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600311
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600313#ifdef _WIN32
314#define APP_NAME_STR_LEN 80
315 HINSTANCE connection; // hInstance - Windows Instance
316 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
317 HWND window; // hWnd - window handle
318#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319 xcb_connection_t *connection;
320 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800321 xcb_window_t window;
322 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotta0781972015-08-21 15:09:33 -0600323 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600324#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600325 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700326 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600329 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkDevice device;
331 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700332 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600333 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600334 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600335 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336
337 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600339 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600340
Ian Elliotta0781972015-08-21 15:09:33 -0600341 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
Ian Elliottf4a4e442015-11-17 17:29:40 -0700342 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
343 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
344 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600345 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
346 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
347 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
348 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
349 PFN_vkQueuePresentKHR fpQueuePresentKHR;
350 VkSurfaceDescriptionWindowKHR surface_description;
351 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600352 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600353 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800355 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600356
357 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800361 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500362 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600363 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364 } depth;
365
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600366 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367
368 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800370 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500371 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600372 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600373 } uniform_data;
374
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800375 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500376 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600378 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800379 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600382 mat4x4 projection_matrix;
383 mat4x4 view_matrix;
384 mat4x4 model_matrix;
385
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600386 float spin_angle;
387 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600388 bool pause;
389
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600390 VkShaderModule vert_shader_module;
391 VkShaderModule frag_shader_module;
392
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 VkDescriptorPool desc_pool;
394 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800395
Tony Barbourd8e504f2015-10-08 14:26:24 -0600396 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800397
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600399 int32_t curFrame;
400 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600401 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600402 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600403 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600404 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600405 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600406 VkDbgMsgCallback msg_callback;
407
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600408 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600409 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600410};
411
Ian Elliottcf074d32015-10-16 18:02:43 -0600412// Forward declaration:
413static void demo_resize(struct demo *demo);
414
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600415static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600416{
417 // Search memtypes to find first index with those properties
418 for (uint32_t i = 0; i < 32; i++) {
419 if ((typeBits & 1) == 1) {
420 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600421 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600422 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600423 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600424 }
425 }
426 typeBits >>= 1;
427 }
428 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600429 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600430}
431
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432static void demo_flush_init_cmd(struct demo *demo)
433{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600434 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437 return;
438
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600439 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600440 assert(!err);
441
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800442 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800443 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600444 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800445 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
446 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800447 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600448 .pWaitSemaphores = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800449 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600450 .pCommandBuffers = cmd_bufs,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800451 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600452 .pSignalSemaphores = NULL
453 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600455 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600456 assert(!err);
457
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600458 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459 assert(!err);
460
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600461 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600462 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600463}
464
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465static void demo_set_image_layout(
466 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600467 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600468 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600469 VkImageLayout old_image_layout,
470 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600472 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800475 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800476 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800478 .commandPool = demo->cmd_pool,
479 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800480 .bufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 };
482
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800483 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600484 assert(!err);
485
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800486 VkCommandBufferBeginInfo cmd_buf_info = {
487 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600489 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800490 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600491 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800492 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800493 .occlusionQueryEnable = VK_FALSE,
494 .queryFlags = 0,
495 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600498 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499 }
500
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600501 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800504 .srcAccessMask = 0,
505 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600506 .oldLayout = old_image_layout,
507 .newLayout = new_image_layout,
508 .image = image,
Jeremy Hayes105ca502015-11-17 18:19:55 -0700509 .subresourceRange = { aspectMask, 0, 1, 0, 1 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510 };
511
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800512 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600513 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800514 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515 }
516
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700517 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
518 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT;
519 }
520
521 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
522 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;
523 }
524
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600525 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526 /* Make sure any Copy or CPU writes to image are flushed */
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700527 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528 }
529
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600530 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600531
Tony Barbour50bbca42015-06-29 16:20:35 -0600532 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
533 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600534
Chia-I Wu86365072015-10-26 17:08:33 +0800535 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600536}
537
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800538static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539{
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800540 const VkCommandBufferBeginInfo cmd_buf_info = {
541 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600542 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600543 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800544 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600545 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800546 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800547 .occlusionQueryEnable = VK_FALSE,
548 .queryFlags = 0,
549 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700550 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800551 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600552 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
553 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800554 };
555 const VkRenderPassBeginInfo rp_begin = {
556 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
557 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800558 .renderPass = demo->render_pass,
559 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800560 .renderArea.offset.x = 0,
561 .renderArea.offset.y = 0,
562 .renderArea.extent.width = demo->width,
563 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600564 .clearValueCount = 2,
565 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700566 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800567 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600569 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570 assert(!err);
571
Chia-I Wuf051d262015-10-27 19:25:11 +0800572 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800573
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600574 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600576 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600577 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600579 VkViewport viewport;
580 memset(&viewport, 0, sizeof(viewport));
581 viewport.height = (float) demo->height;
582 viewport.width = (float) demo->width;
583 viewport.minDepth = (float) 0.0f;
584 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600585 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600586
587 VkRect2D scissor;
588 memset(&scissor, 0, sizeof(scissor));
589 scissor.extent.width = demo->width;
590 scissor.extent.height = demo->height;
591 scissor.offset.x = 0;
592 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600593 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600594
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600595 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800596 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600597
Tony Barbour15a4ef62015-10-21 10:14:48 -0600598 VkImageMemoryBarrier prePresentBarrier = {
599 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
600 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800601 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
602 .dstAccessMask = 0,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600603 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700604 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600605 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800606 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600607 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
608 };
609
610 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
611 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wu082a6202015-10-31 00:31:16 +0800612 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu86365072015-10-26 17:08:33 +0800613 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600614
615
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600616 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600617 assert(!err);
618}
619
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600620
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600621void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600623 mat4x4 MVP, Model, VP;
624 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600625 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600626 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600627
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600628 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600629
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600630 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600631 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700632 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600633 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600634
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200635 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600636 assert(!err);
637
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600638 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600639
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600640 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600641}
642
643static void demo_draw(struct demo *demo)
644{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600645 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600646 VkSemaphore presentCompleteSemaphore;
647 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
648 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
649 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600650 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600651 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800652 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653
Ian Elliottaaae5352015-07-06 14:27:58 -0600654 err = vkCreateSemaphore(demo->device,
655 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +0800656 NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600657 &presentCompleteSemaphore);
658 assert(!err);
659
660 // Get the index of the next available swapchain image:
Ian Elliottcf074d32015-10-16 18:02:43 -0600661 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600662 UINT64_MAX,
663 presentCompleteSemaphore,
664 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600665 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
666 // demo->swapchain is out of date (e.g. the window was resized) and
667 // must be recreated:
668 demo_resize(demo);
669 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800670 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600671 return;
672 } else if (err == VK_SUBOPTIMAL_KHR) {
673 // demo->swapchain is not as optimal as it could be, but the platform's
674 // presentation engine will still present the image correctly.
675 } else {
676 assert(!err);
677 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600678
Tony Barbour15a4ef62015-10-21 10:14:48 -0600679 // Assume the command buffer has been run on current_buffer before so
680 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
681 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
682 VK_IMAGE_ASPECT_COLOR_BIT,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700683 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600684 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
685 demo_flush_init_cmd(demo);
686
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600687 // Wait for the present complete semaphore to be signaled to ensure
688 // that the image won't be rendered to until the presentation
689 // engine has fully released ownership to the application, and it is
690 // okay to render to the image.
691
Ian Elliottf4a4e442015-11-17 17:29:40 -0700692// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600693 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800694 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
695 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800696 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600697 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800698 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600699 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800700 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600701 .pSignalSemaphores = NULL
702 };
703
704 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600705 assert(!err);
706
Ian Elliotta0781972015-08-21 15:09:33 -0600707 VkPresentInfoKHR present = {
708 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600709 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600710 .swapchainCount = 1,
Ian Elliottcf074d32015-10-16 18:02:43 -0600711 .swapchains = &demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 .imageIndices = &demo->current_buffer,
713 };
714
715// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600716 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600717 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
718 // demo->swapchain is out of date (e.g. the window was resized) and
719 // must be recreated:
720 demo_resize(demo);
721 } else if (err == VK_SUBOPTIMAL_KHR) {
722 // demo->swapchain is not as optimal as it could be, but the platform's
723 // presentation engine will still present the image correctly.
724 } else {
725 assert(!err);
726 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600727
Chia-I Wucbb564e2015-04-16 22:02:10 +0800728 err = vkQueueWaitIdle(demo->queue);
729 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600730
Chia-I Wu63636062015-10-26 21:10:41 +0800731 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600732}
733
734static void demo_prepare_buffers(struct demo *demo)
735{
Ian Elliottaaae5352015-07-06 14:27:58 -0600736 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600737 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600738
739 // Check the surface properties and formats
Ian Elliottf4a4e442015-11-17 17:29:40 -0700740 VkSurfaceCapabilitiesKHR surfProperties;
741 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->device,
Ian Elliotta0781972015-08-21 15:09:33 -0600742 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600743 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600744 assert(!err);
745
Ian Elliott8528eff2015-08-07 15:56:59 -0600746 uint32_t presentModeCount;
Ian Elliottf4a4e442015-11-17 17:29:40 -0700747 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->device,
Ian Elliotta0781972015-08-21 15:09:33 -0600748 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600749 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600750 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600751 VkPresentModeKHR *presentModes =
752 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600753 assert(presentModes);
Ian Elliottf4a4e442015-11-17 17:29:40 -0700754 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->device,
Ian Elliotta0781972015-08-21 15:09:33 -0600755 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600756 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600757 assert(!err);
758
Ian Elliotta0781972015-08-21 15:09:33 -0600759 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600760 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600761 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600762 {
763 // If the surface size is undefined, the size is set to
764 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600765 swapchainExtent.width = demo->width;
766 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600767 }
768 else
769 {
770 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600771 swapchainExtent = surfProperties.currentExtent;
Ian Elliottcf074d32015-10-16 18:02:43 -0600772 demo->width = surfProperties.currentExtent.width;
773 demo->height = surfProperties.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600774 }
775
776 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600777 // tearing mode. If not, try IMMEDIATE which will usually be available,
778 // and is fastest (though it tears). If not, fall back to FIFO which is
779 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600780 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600781 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600782 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
783 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600784 break;
785 }
Ian Elliotta0781972015-08-21 15:09:33 -0600786 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
787 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
788 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600789 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600790 }
791
792 // Determine the number of VkImage's to use in the swap chain (we desire to
793 // own only 1 image at a time, besides the images being displayed and
794 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600795 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600796 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600797 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600798 {
799 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600800 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600801 }
802
Tony Barbour9fd6d352015-09-16 15:01:32 -0600803 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600804 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
805 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600806 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600807 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600808 }
809
Ian Elliottcf074d32015-10-16 18:02:43 -0600810 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600811 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800812 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600813 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
814 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800815 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600816 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800817 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600818 .width = swapchainExtent.width,
819 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600820 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600821 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600822 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800823 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600824 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
825 .queueFamilyCount = 0,
826 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600827 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600828 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600829 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600831 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600832
Ian Elliottcf074d32015-10-16 18:02:43 -0600833 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800834 assert(!err);
835
Ian Elliottcf074d32015-10-16 18:02:43 -0600836 // If we just re-created an existing swapchain, we should destroy the old
837 // swapchain at this point.
838 // Note: destroying the swapchain also cleans up all its associated
839 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800840 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottcf074d32015-10-16 18:02:43 -0600841 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
842 }
843
844 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600845 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600846 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800847
Ian Elliotta0781972015-08-21 15:09:33 -0600848 VkImage* swapchainImages =
849 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
850 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600851 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600852 &demo->swapchainImageCount,
853 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600854 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600855
Ian Elliotta0781972015-08-21 15:09:33 -0600856 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600857 assert(demo->buffers);
858
Ian Elliotta0781972015-08-21 15:09:33 -0600859 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600860 VkImageViewCreateInfo color_image_view = {
861 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600862 .pNext = NULL,
863 .format = demo->format,
Chia-I Wub6d30c62015-10-27 19:00:15 +0800864 .components = {
Chia-I Wuf051d262015-10-27 19:25:11 +0800865 .r = VK_COMPONENT_SWIZZLE_R,
866 .g = VK_COMPONENT_SWIZZLE_G,
867 .b = VK_COMPONENT_SWIZZLE_B,
868 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600869 },
870 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600871 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600872 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800873 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600874 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800875 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600876 },
877 .viewType = VK_IMAGE_VIEW_TYPE_2D,
878 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600879 };
880
Ian Elliotta0781972015-08-21 15:09:33 -0600881 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600882
Ian Elliottf4a4e442015-11-17 17:29:40 -0700883 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Tony Barbour15a4ef62015-10-21 10:14:48 -0600884 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600885 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600886 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600887 VK_IMAGE_LAYOUT_UNDEFINED,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700888 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600889
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600890 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600891
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600892 err = vkCreateImageView(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +0800893 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600894 assert(!err);
895 }
896}
897
898static void demo_prepare_depth(struct demo *demo)
899{
Tony Barbour72304ef2015-04-16 15:59:00 -0600900 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600901 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600902 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600903 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600904 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600905 .format = depth_format,
906 .extent = { demo->width, demo->height, 1 },
907 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600908 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800909 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600910 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600911 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600912 .flags = 0,
913 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200914
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600915 VkImageViewCreateInfo view = {
916 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600917 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800918 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600919 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600920 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600921 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600922 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800923 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600924 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800925 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600926 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600927 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600928 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600929 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600930
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500931 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600932 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600933 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600934
935 demo->depth.format = depth_format;
936
937 /* create image */
Chia-I Wu63636062015-10-26 21:10:41 +0800938 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600939 &demo->depth.image);
940 assert(!err);
941
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600942 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600943 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600944 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600945
Chia-I Wuf48700b2015-11-10 16:21:09 +0800946 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200947 demo->depth.mem_alloc.pNext = NULL;
948 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
949 demo->depth.mem_alloc.memoryTypeIndex = 0;
950
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600951 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600952 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600953 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200954 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600955 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500957 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800958 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +0800959 NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500960 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500962 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600963 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500964 demo->depth.mem, 0);
965 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600966
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600967 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600968 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600969 VK_IMAGE_LAYOUT_UNDEFINED,
970 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600971
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972 /* create image view */
973 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800974 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600975 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600976}
977
Tony Barbour1a86d032015-09-21 15:17:33 -0600978/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700979bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600980 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600981 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600982{
Tony Barbour1a86d032015-09-21 15:17:33 -0600983 FILE *fPtr = fopen(filename,"rb");
984 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600985
Tony Barbour1a86d032015-09-21 15:17:33 -0600986 if (!fPtr)
987 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600988
Tony Barbour1a86d032015-09-21 15:17:33 -0600989 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600990 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
991 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600992 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600993 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600994
Tony Barbour1a86d032015-09-21 15:17:33 -0600995 do {
996 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600997 if (cPtr == NULL) {
998 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600999 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001000 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001001 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001002
Tony Barbour1a86d032015-09-21 15:17:33 -06001003 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001004 if (rgba_data == NULL) {
1005 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001006 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001007 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001008 fgets(header, 256, fPtr); // Format
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001009 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1010 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001011 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001012 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001013
Tony Barbour1a86d032015-09-21 15:17:33 -06001014 for(int y = 0; y < *height; y++)
1015 {
1016 uint8_t *rowPtr = rgba_data;
1017 for(int x = 0; x < *width; x++)
1018 {
1019 fread(rowPtr, 3, 1, fPtr);
1020 rowPtr[3] = 255; /* Alpha of 1 */
1021 rowPtr += 4;
1022 }
1023 rgba_data += layout->rowPitch;
1024 }
1025 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001026 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001027}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001028
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029static void demo_prepare_texture_image(struct demo *demo,
1030 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001031 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001032 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001033 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001034 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001036 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001037 int32_t tex_width;
1038 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001039 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001040 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041
David Pinedo30bd71d2015-04-23 08:16:57 -06001042 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1043 {
1044 printf("Failed to load textures\n");
1045 fflush(stdout);
1046 exit(1);
1047 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001049 tex_obj->tex_width = tex_width;
1050 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001051
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001052 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001053 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001054 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001055 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001056 .format = tex_format,
1057 .extent = { tex_width, tex_height, 1 },
1058 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001059 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001060 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001062 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063 .flags = 0,
1064 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001066 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067
Chia-I Wu63636062015-10-26 21:10:41 +08001068 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001069 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001070 assert(!err);
1071
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001072 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001073
Chia-I Wuf48700b2015-11-10 16:21:09 +08001074 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001075 tex_obj->mem_alloc.pNext = NULL;
1076 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1077 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001079 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1080 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001081
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001082 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001083 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001084 &(tex_obj->mem));
1085 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001087 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001088 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001089 tex_obj->mem, 0);
1090 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001091
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001092 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001093 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001094 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001095 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001096 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001097 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001098 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001099 void *data;
1100
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001101 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001102
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001103 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001104 assert(!err);
1105
1106 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1107 fprintf(stderr, "Error loading texture: %s\n", filename);
1108 }
1109
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001110 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001112
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001113 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001114 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001115 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001116 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001117 tex_obj->imageLayout);
1118 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119}
1120
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001121static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001122{
1123 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001124 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1125 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001126}
1127
1128static void demo_prepare_textures(struct demo *demo)
1129{
Tony Barbour72304ef2015-04-16 15:59:00 -06001130 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001131 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001132 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001133
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001134 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001135
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001136 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001137 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138
FslNopper6a7e50e2015-05-06 21:42:01 +02001139 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001140 /* Device can texture using linear textures */
1141 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001142 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001143 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001145 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001146
1147 memset(&staging_texture, 0, sizeof(staging_texture));
1148 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001149 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001150
1151 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001152 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001153 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wuc42afd72015-10-27 18:53:22 +08001154 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001156 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001157 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001158 staging_texture.imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001159 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001161 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001162 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001163 demo->textures[i].imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001164 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001166 VkImageCopy copy_region = {
Tony Barbour466f0012015-11-02 11:47:51 -07001167 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168 .srcOffset = { 0, 0, 0 },
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001169 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1170 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001171 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1172 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001173 vkCmdCopyImage(demo->cmd,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001174 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1175 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001176 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001177
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001178 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001179 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001180 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001181 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001183 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001185 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001186 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001187 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1188 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001189 }
1190
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001191 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001192 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001193 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001194 .magFilter = VK_FILTER_NEAREST,
1195 .minFilter = VK_FILTER_NEAREST,
1196 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001197 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1198 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1199 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001202 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203 .minLod = 0.0f,
1204 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001205 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001206 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001207 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001208
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001209 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001210 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001211 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001212 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001213 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001214 .format = tex_format,
Chia-I Wub6d30c62015-10-27 19:00:15 +08001215 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuf051d262015-10-27 19:25:11 +08001216 VK_COMPONENT_SWIZZLE_G,
1217 VK_COMPONENT_SWIZZLE_B,
1218 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001219 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001220 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001221 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001222
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001223 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001224 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 &demo->textures[i].sampler);
1226 assert(!err);
1227
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228 /* create image view */
1229 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001230 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001231 &demo->textures[i].view);
1232 assert(!err);
1233 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001234}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001236void demo_prepare_cube_data_buffer(struct demo *demo)
1237{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001238 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001239 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001240 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001241 int i;
1242 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001243 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001244 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001245 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001246
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001247 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001248 mat4x4_mul(MVP, VP, demo->model_matrix);
1249 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001250// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001251
1252 for (i=0; i<12*3; i++) {
1253 data.position[i][0] = g_vertex_buffer_data[i*3];
1254 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1255 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1256 data.position[i][3] = 1.0f;
1257 data.attr[i][0] = g_uv_buffer_data[2*i];
1258 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1259 data.attr[i][2] = 0;
1260 data.attr[i][3] = 0;
1261 }
1262
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001263 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001264 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001265 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001266 buf_info.size = sizeof(data);
Chia-I Wu63636062015-10-26 21:10:41 +08001267 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1268 &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001269 assert(!err);
1270
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001271 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001272
Chia-I Wuf48700b2015-11-10 16:21:09 +08001273 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001274 demo->uniform_data.mem_alloc.pNext = NULL;
1275 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1276 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1277
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001278 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001279 mem_reqs.memoryTypeBits,
1280 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001281 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001282 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001283
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001284 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +08001285 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001286 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001287
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001288 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001289 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001290
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001291 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001292
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001293 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001294
Tony Barbour155cce82015-07-03 10:33:54 -06001295 err = vkBindBufferMemory(demo->device,
1296 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001297 demo->uniform_data.mem, 0);
1298 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001299
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001300 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1301 demo->uniform_data.buffer_info.offset = 0;
1302 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001303}
1304
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001305static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001307 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001308 [0] = {
Chia-I Wued577562015-10-31 00:31:16 +08001309 .binding = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001310 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001311 .descriptorCount = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001312 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001313 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001314 },
1315 [1] = {
Chia-I Wued577562015-10-31 00:31:16 +08001316 .binding = 1,
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001317 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001318 .descriptorCount = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001319 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001320 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001321 },
1322 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001323 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001324 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001325 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001326 .bindingCount = 2,
Chia-I Wu0e76e3f2015-10-31 00:31:16 +08001327 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001328 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001329 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001330
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001331 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001332 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001333 assert(!err);
1334
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001335 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1336 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1337 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001338 .setLayoutCount = 1,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001339 .pSetLayouts = &demo->desc_layout,
1340 };
1341
1342 err = vkCreatePipelineLayout(demo->device,
1343 &pPipelineLayoutCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +08001344 NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001345 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347}
1348
Chia-I Wuf973e322015-07-08 13:34:24 +08001349static void demo_prepare_render_pass(struct demo *demo)
1350{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001351 const VkAttachmentDescription attachments[2] = {
1352 [0] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001353 .format = demo->format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001354 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001355 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1356 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1357 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1358 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1359 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1360 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1361 },
1362 [1] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001363 .format = demo->depth.format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001364 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001365 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1366 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1367 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1368 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1369 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1370 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1371 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001372 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001373 const VkAttachmentReference color_reference = {
1374 .attachment = 0,
1375 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1376 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001377 const VkAttachmentReference depth_reference = {
1378 .attachment = 1,
1379 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1380 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001381 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001382 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1383 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001384 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001385 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001386 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001387 .pColorAttachments = &color_reference,
1388 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001389 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001390 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001391 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001392 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001393 const VkRenderPassCreateInfo rp_info = {
1394 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1395 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001396 .attachmentCount = 2,
1397 .pAttachments = attachments,
1398 .subpassCount = 1,
1399 .pSubpasses = &subpass,
1400 .dependencyCount = 0,
1401 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001402 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001403 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001404
Chia-I Wu63636062015-10-26 21:10:41 +08001405 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001406 assert(!err);
1407}
1408
Chia-I Wu46176f12015-10-31 00:31:16 +08001409static VkShaderModule demo_prepare_shader_module(struct demo* demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001410 const void* code,
1411 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412{
Chia-I Wu46176f12015-10-31 00:31:16 +08001413 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001414 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001415 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001417 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1418 moduleCreateInfo.pNext = NULL;
1419
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001420 moduleCreateInfo.codeSize = size;
1421 moduleCreateInfo.pCode = code;
1422 moduleCreateInfo.flags = 0;
1423 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1424 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001425
1426 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427}
1428
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001429char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001430{
1431 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001432 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001433 void *shader_code;
1434
1435 FILE *fp = fopen(filename, "rb");
1436 if (!fp) return NULL;
1437
1438 fseek(fp, 0L, SEEK_END);
1439 size = ftell(fp);
1440
1441 fseek(fp, 0L, SEEK_SET);
1442
1443 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001444 retval = fread(shader_code, size, 1, fp);
1445 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001446
1447 *psize = size;
1448
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001449 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001450 return shader_code;
1451}
1452
Chia-I Wu46176f12015-10-31 00:31:16 +08001453static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001455 void *vertShaderCode;
1456 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001457
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001458 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1459
Tony Barbourf331eb32015-11-06 10:10:37 -07001460 demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001461
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001462 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001463
1464 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001465}
1466
Chia-I Wu46176f12015-10-31 00:31:16 +08001467static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001469 void *fragShaderCode;
1470 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001471
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001472 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001473
Tony Barbourf331eb32015-11-06 10:10:37 -07001474 demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001475
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001476 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001477
1478 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001479}
1480
1481static void demo_prepare_pipeline(struct demo *demo)
1482{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001483 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001484 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001485 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001486 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001487 VkPipelineRasterizationStateCreateInfo rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001488 VkPipelineColorBlendStateCreateInfo cb;
1489 VkPipelineDepthStencilStateCreateInfo ds;
1490 VkPipelineViewportStateCreateInfo vp;
1491 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001492 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniellba839d12015-09-29 13:01:09 -06001493 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001494 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001495
Piers Daniellba839d12015-09-29 13:01:09 -06001496 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1497 memset(&dynamicState, 0, sizeof dynamicState);
1498 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1499 dynamicState.pDynamicStates = dynamicStateEnables;
1500
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001502 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001503 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001505 memset(&vi, 0, sizeof(vi));
1506 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1507
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001508 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001509 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001510 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511
1512 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001513 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1514 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001515 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001516 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001517 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001518 rs.rasterizerDiscardEnable = VK_FALSE;
1519 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520
1521 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001522 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1523 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001524 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001525 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001526 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001527 cb.attachmentCount = 1;
1528 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001529
Tony Barbour29645d02015-01-16 14:27:35 -07001530 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001531 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001532 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001533 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1534 vp.scissorCount = 1;
1535 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001536
1537 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001538 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001539 ds.depthTestEnable = VK_TRUE;
1540 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001541 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001542 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001543 ds.back.failOp = VK_STENCIL_OP_KEEP;
1544 ds.back.passOp = VK_STENCIL_OP_KEEP;
1545 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001546 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001547 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548
Tony Barbour29645d02015-01-16 14:27:35 -07001549 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001550 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001551 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001552 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001553
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001554 // Two stages: vs and fs
1555 pipeline.stageCount = 2;
1556 VkPipelineShaderStageCreateInfo shaderStages[2];
1557 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1558
1559 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001560 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1561 shaderStages[0].module = demo_prepare_vs(demo);
1562 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001563
1564 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001565 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1566 shaderStages[1].module = demo_prepare_fs(demo);
1567 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001568
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001569 memset(&pipelineCache, 0, sizeof(pipelineCache));
1570 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571
Chia-I Wu63636062015-10-26 21:10:41 +08001572 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001573 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001574
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001575 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001576 pipeline.pInputAssemblyState = &ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001577 pipeline.pRasterizationState = &rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001578 pipeline.pColorBlendState = &cb;
1579 pipeline.pMultisampleState = &ms;
1580 pipeline.pViewportState = &vp;
1581 pipeline.pDepthStencilState = &ds;
1582 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001583 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001584 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001585
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001586 pipeline.renderPass = demo->render_pass;
1587
Chia-I Wu63636062015-10-26 21:10:41 +08001588 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1589 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001590 assert(!err);
1591
Chia-I Wu63636062015-10-26 21:10:41 +08001592 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1593 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001594}
1595
Chia-I Wu63ea9262015-03-26 13:14:16 +08001596static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001597{
Chia-I Wuf051d262015-10-27 19:25:11 +08001598 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001599 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001600 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001601 .descriptorCount = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001602 },
1603 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001604 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001605 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001606 },
1607 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001608 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001609 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001610 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001611 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001612 .poolSizeCount = 2,
1613 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001614 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001615 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001616
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001617 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001618 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001619 assert(!err);
1620}
1621
1622static void demo_prepare_descriptor_set(struct demo *demo)
1623{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001624 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001625 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001626 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001627 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001628
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001629 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001630 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001631 .pNext = NULL,
1632 .descriptorPool = demo->desc_pool,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001633 .setLayoutCount = 1,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001634 .pSetLayouts = &demo->desc_layout
1635 };
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001636 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001637 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001638
Chia-I Wuae721ba2015-05-25 16:27:55 +08001639 memset(&tex_descs, 0, sizeof(tex_descs));
1640 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001641 tex_descs[i].sampler = demo->textures[i].sampler;
1642 tex_descs[i].imageView = demo->textures[i].view;
1643 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001644 }
1645
1646 memset(&writes, 0, sizeof(writes));
1647
1648 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001649 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001650 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001651 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001652 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001653
1654 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001655 writes[1].dstSet = demo->desc_set;
1656 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001657 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001658 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001659 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001660
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001661 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001662}
1663
Chia-I Wuf973e322015-07-08 13:34:24 +08001664static void demo_prepare_framebuffers(struct demo *demo)
1665{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001666 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001667 attachments[1] = demo->depth.view;
1668
Chia-I Wuf973e322015-07-08 13:34:24 +08001669 const VkFramebufferCreateInfo fb_info = {
1670 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1671 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001672 .renderPass = demo->render_pass,
1673 .attachmentCount = 2,
1674 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001675 .width = demo->width,
1676 .height = demo->height,
1677 .layers = 1,
1678 };
1679 VkResult U_ASSERT_ONLY err;
1680 uint32_t i;
1681
Tony Barbourd8e504f2015-10-08 14:26:24 -06001682 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1683 assert(demo->framebuffers);
1684
1685 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001686 attachments[0] = demo->buffers[i].view;
Chia-I Wu63636062015-10-26 21:10:41 +08001687 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001688 assert(!err);
1689 }
1690}
1691
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001692static void demo_prepare(struct demo *demo)
1693{
Cody Northrop91e221b2015-07-09 18:08:32 -06001694 VkResult U_ASSERT_ONLY err;
1695
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001696 const VkCommandPoolCreateInfo cmd_pool_info = {
1697 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001698 .pNext = NULL,
1699 .queueFamilyIndex = demo->graphics_queue_node_index,
1700 .flags = 0,
1701 };
Chia-I Wu63636062015-10-26 21:10:41 +08001702 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001703 assert(!err);
1704
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001705 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001706 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001708 .commandPool = demo->cmd_pool,
1709 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001710 .bufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001712
1713 demo_prepare_buffers(demo);
1714 demo_prepare_depth(demo);
1715 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001716 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001718 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001719 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001721
Ian Elliotta0781972015-08-21 15:09:33 -06001722 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001723 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001724 assert(!err);
1725 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001726
Chia-I Wu63ea9262015-03-26 13:14:16 +08001727 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001728 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001729
Chia-I Wuf973e322015-07-08 13:34:24 +08001730 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001731
Ian Elliotta0781972015-08-21 15:09:33 -06001732 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001733 demo->current_buffer = i;
1734 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1735 }
1736
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001737 /*
1738 * Prepare functions above may generate pipeline commands
1739 * that need to be flushed before beginning the render loop.
1740 */
1741 demo_flush_init_cmd(demo);
1742
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001743 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001744 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745}
1746
David Pinedo2bb7c932015-06-18 17:03:14 -06001747static void demo_cleanup(struct demo *demo)
1748{
1749 uint32_t i;
1750
1751 demo->prepared = false;
1752
Tony Barbourd8e504f2015-10-08 14:26:24 -06001753 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001754 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001755 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001756 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001757 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001758
Chia-I Wu63636062015-10-26 21:10:41 +08001759 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1760 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1761 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1762 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1763 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001764
1765 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001766 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1767 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1768 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1769 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001770 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001771 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001772
Chia-I Wu63636062015-10-26 21:10:41 +08001773 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1774 vkDestroyImage(demo->device, demo->depth.image, NULL);
1775 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001776
Chia-I Wu63636062015-10-26 21:10:41 +08001777 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1778 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001779
Ian Elliotta0781972015-08-21 15:09:33 -06001780 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001781 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001782 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001783 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001784 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001785
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001786 free(demo->queue_props);
1787
Chia-I Wu63636062015-10-26 21:10:41 +08001788 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1789 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001790 if (demo->validate) {
1791 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1792 }
Chia-I Wu63636062015-10-26 21:10:41 +08001793 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001794
1795#ifndef _WIN32
1796 xcb_destroy_window(demo->connection, demo->window);
1797 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001798 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001799#endif // _WIN32
1800}
1801
Ian Elliottcf074d32015-10-16 18:02:43 -06001802static void demo_resize(struct demo *demo)
1803{
1804 uint32_t i;
1805
Mike Stroyanbb60b382015-10-28 09:46:57 -06001806 // Don't react to resize until after first initialization.
1807 if (!demo->prepared) {
1808 return;
1809 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001810 // In order to properly resize the window, we must re-create the swapchain
1811 // AND redo the command buffers, etc.
1812 //
1813 // First, perform part of the demo_cleanup() function:
1814 demo->prepared = false;
1815
1816 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001817 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001818 }
1819 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001820 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001821
Chia-I Wu63636062015-10-26 21:10:41 +08001822 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1823 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1824 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1825 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1826 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001827
1828 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001829 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1830 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1831 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1832 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001833 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001834
Chia-I Wu63636062015-10-26 21:10:41 +08001835 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1836 vkDestroyImage(demo->device, demo->depth.image, NULL);
1837 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001838
Chia-I Wu63636062015-10-26 21:10:41 +08001839 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1840 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001841
1842 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001843 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001844 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001845 }
Chia-I Wu63636062015-10-26 21:10:41 +08001846 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001847 free(demo->buffers);
1848
Chia-I Wu63636062015-10-26 21:10:41 +08001849 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001850
Ian Elliottcf074d32015-10-16 18:02:43 -06001851
1852 // Second, re-perform the demo_prepare() function, which will re-create the
1853 // swapchain:
1854 demo_prepare(demo);
1855}
1856
David Pinedo2bb7c932015-06-18 17:03:14 -06001857// On MS-Windows, make this a global, so it's available to WndProc()
1858struct demo demo;
1859
Ian Elliott639ca472015-04-16 15:23:05 -06001860#ifdef _WIN32
1861static void demo_run(struct demo *demo)
1862{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001863 if (!demo->prepared)
1864 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001865 // Wait for work to finish before updating MVP.
1866 vkDeviceWaitIdle(demo->device);
1867 demo_update_data_buffer(demo);
1868
1869 demo_draw(demo);
1870
1871 // Wait for work to finish before updating MVP.
1872 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001873
David Pinedo2bb7c932015-06-18 17:03:14 -06001874 demo->curFrame++;
1875
1876 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1877 {
1878 demo->quit=true;
1879 demo_cleanup(demo);
1880 ExitProcess(0);
1881 }
1882
1883}
Ian Elliott639ca472015-04-16 15:23:05 -06001884
1885// MS-Windows event handling function:
1886LRESULT CALLBACK WndProc(HWND hWnd,
1887 UINT uMsg,
1888 WPARAM wParam,
1889 LPARAM lParam)
1890{
Ian Elliott639ca472015-04-16 15:23:05 -06001891 switch(uMsg)
1892 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001893 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001894 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001895 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001896 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001897 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001898 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001899 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001900 demo.width = lParam & 0xffff;
1901 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001902 demo_resize(&demo);
1903 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001904 default:
1905 break;
1906 }
1907 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1908}
1909
1910static void demo_create_window(struct demo *demo)
1911{
1912 WNDCLASSEX win_class;
1913
1914 // Initialize the window class structure:
1915 win_class.cbSize = sizeof(WNDCLASSEX);
1916 win_class.style = CS_HREDRAW | CS_VREDRAW;
1917 win_class.lpfnWndProc = WndProc;
1918 win_class.cbClsExtra = 0;
1919 win_class.cbWndExtra = 0;
1920 win_class.hInstance = demo->connection; // hInstance
1921 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1922 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1923 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1924 win_class.lpszMenuName = NULL;
1925 win_class.lpszClassName = demo->name;
1926 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1927 // Register window class:
1928 if (!RegisterClassEx(&win_class)) {
1929 // It didn't work, so try to give a useful error:
1930 printf("Unexpected error trying to start the application!\n");
1931 fflush(stdout);
1932 exit(1);
1933 }
1934 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001935 RECT wr = { 0, 0, demo->width, demo->height };
1936 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001937 demo->window = CreateWindowEx(0,
1938 demo->name, // class name
1939 demo->name, // app name
1940 WS_OVERLAPPEDWINDOW | // window style
1941 WS_VISIBLE |
1942 WS_SYSMENU,
1943 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001944 wr.right-wr.left, // width
1945 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001946 NULL, // handle to parent
1947 NULL, // handle to menu
1948 demo->connection, // hInstance
1949 NULL); // no extra parameters
1950 if (!demo->window) {
1951 // It didn't work, so try to give a useful error:
1952 printf("Cannot create a window in which to draw!\n");
1953 fflush(stdout);
1954 exit(1);
1955 }
1956}
1957#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001958static void demo_handle_event(struct demo *demo,
1959 const xcb_generic_event_t *event)
1960{
Piers Daniell735ee532015-02-23 16:23:13 -07001961 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001962 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001963 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001964 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001965 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001966 case XCB_CLIENT_MESSAGE:
1967 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1968 (*demo->atom_wm_delete_window).atom) {
1969 demo->quit = true;
1970 }
1971 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972 case XCB_KEY_RELEASE:
1973 {
1974 const xcb_key_release_event_t *key =
1975 (const xcb_key_release_event_t *) event;
1976
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001977 switch (key->detail) {
1978 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001979 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001980 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001981 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001982 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001983 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001984 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001985 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001986 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001987 case 0x41:
1988 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001989 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001990 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001991 }
1992 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06001993 case XCB_CONFIGURE_NOTIFY:
1994 {
1995 const xcb_configure_notify_event_t *cfg =
1996 (const xcb_configure_notify_event_t *) event;
1997 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
1998 demo_resize(demo);
1999 }
2000 }
2001 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002002 default:
2003 break;
2004 }
2005}
2006
2007static void demo_run(struct demo *demo)
2008{
2009 xcb_flush(demo->connection);
2010
2011 while (!demo->quit) {
2012 xcb_generic_event_t *event;
2013
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002014 if (demo->pause) {
2015 event = xcb_wait_for_event(demo->connection);
2016 } else {
2017 event = xcb_poll_for_event(demo->connection);
2018 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002019 if (event) {
2020 demo_handle_event(demo, event);
2021 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002022 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002023
2024 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002025 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002026 demo_update_data_buffer(demo);
2027
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002028 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002029
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002030 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002031 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002032 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002033 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002034 demo->quit = true;
2035
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002036 }
2037}
2038
2039static void demo_create_window(struct demo *demo)
2040{
2041 uint32_t value_mask, value_list[32];
2042
2043 demo->window = xcb_generate_id(demo->connection);
2044
2045 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2046 value_list[0] = demo->screen->black_pixel;
2047 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002048 XCB_EVENT_MASK_EXPOSURE |
2049 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050
2051 xcb_create_window(demo->connection,
2052 XCB_COPY_FROM_PARENT,
2053 demo->window, demo->screen->root,
2054 0, 0, demo->width, demo->height, 0,
2055 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2056 demo->screen->root_visual,
2057 value_mask, value_list);
2058
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002059 /* Magic code that will send notification when window is destroyed */
2060 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2061 "WM_PROTOCOLS");
2062 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2063
2064 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2065 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2066
2067 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2068 demo->window, (*reply).atom, 4, 32, 1,
2069 &(*demo->atom_wm_delete_window).atom);
2070 free(reply);
2071
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002072 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002073
2074 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2075 const uint32_t coords[] = {100, 100};
2076 xcb_configure_window(demo->connection, demo->window,
2077 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002078}
Ian Elliott639ca472015-04-16 15:23:05 -06002079#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002081/*
2082 * Return 1 (true) if all layer names specified in check_names
2083 * can be found in given layer properties.
2084 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002085static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002086 uint32_t layer_count, VkLayerProperties *layers)
2087{
2088 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002089 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002090 for (uint32_t j = 0; j < layer_count; j++) {
2091 if (!strcmp(check_names[i], layers[j].layerName)) {
2092 found = 1;
2093 }
2094 }
2095 if (!found) {
2096 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2097 return 0;
2098 }
2099 }
2100 return 1;
2101}
2102
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002103static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002104{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002105 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002106 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002107 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002108 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002109 VkLayerProperties *instance_layers;
2110 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002111 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002112 uint32_t instance_layer_count = 0;
2113 uint32_t enabled_extension_count = 0;
2114 uint32_t enabled_layer_count = 0;
2115
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002116 char *instance_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002117 "VK_LAYER_LUNARG_Threading",
2118 "VK_LAYER_LUNARG_MemTracker",
2119 "VK_LAYER_LUNARG_ObjectTracker",
2120 "VK_LAYER_LUNARG_DrawState",
2121 "VK_LAYER_LUNARG_ParamChecker",
2122 "VK_LAYER_LUNARG_ShaderChecker",
2123 "VK_LAYER_LUNARG_Swapchain",
2124 "VK_LAYER_LUNARG_DeviceLimits",
2125 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002126 };
2127
2128 char *device_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002129 "VK_LAYER_LUNARG_Threading",
2130 "VK_LAYER_LUNARG_MemTracker",
2131 "VK_LAYER_LUNARG_ObjectTracker",
2132 "VK_LAYER_LUNARG_DrawState",
2133 "VK_LAYER_LUNARG_ParamChecker",
2134 "VK_LAYER_LUNARG_ShaderChecker",
2135 "VK_LAYER_LUNARG_Swapchain",
2136 "VK_LAYER_LUNARG_DeviceLimits",
2137 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002138 };
2139
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002140 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002141 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002142 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002143 assert(!err);
2144
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002145 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002146 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002147 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002148
2149 if (demo->validate) {
2150 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2151 instance_layer_count, instance_layers);
2152 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002153 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002154 "required validation layer.\n\n"
2155 "Please look at the Getting Started guide for additional "
2156 "information.\n",
2157 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002158 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002159 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002160 }
2161
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002162 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002163 assert(!err);
2164
Tony Barbourcc69f452015-10-08 13:59:42 -06002165 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002166 memset(extension_names, 0, sizeof(extension_names));
2167 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002168 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002169 assert(!err);
2170 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002171 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002172 swapchainExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002173 extension_names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002174 }
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002175 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002176 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002177 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002178 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002179 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002180 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002181 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002182 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002183 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002184 VK_KHR_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002185 "Vulkan installable client driver (ICD) installed?\nPlease "
2186 "look at the Getting Started guide for additional "
2187 "information.\n",
2188 "vkCreateInstance Failure");
2189 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002190 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002191 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002192 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002193 .pApplicationName = APP_SHORT_NAME,
2194 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002195 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002196 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002197 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002198 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002199 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002200 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002201 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002202 .pApplicationInfo = &app,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002203 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002204 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002205 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002206 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002207 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002208
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002209 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002210
Chia-I Wu63636062015-10-26 21:10:41 +08002211 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002212 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2213 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002214 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002215 "additional information.\n",
2216 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002217 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002218 ERR_EXIT("Cannot find a specified extension library"
2219 ".\nMake sure your layers path is set appropriately\n",
2220 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002221 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002222 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2223 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002224 "the Getting Started guide for additional information.\n",
2225 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002226 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002227
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002228 free(instance_layers);
2229 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002230
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002231 /* Make initial call to query gpu_count, then second call for gpu info*/
2232 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2233 assert(!err && gpu_count > 0);
2234 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2235 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2236 assert(!err);
2237 /* For cube demo we just grab the first physical device */
2238 demo->gpu = physical_devices[0];
2239 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002240
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002241 /* Look for validation layers */
2242 validation_found = 0;
2243 enabled_layer_count = 0;
2244 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002245 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002246 assert(!err);
2247
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002248 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002249 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002250 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002251
2252 if (demo->validate) {
2253 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2254 device_layer_count, device_layers);
2255 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002256 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002257 "a required validation layer.\n\n"
2258 "Please look at the Getting Started guide for additional "
2259 "information.\n",
2260 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002261 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002262 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002263 }
2264
2265 uint32_t device_extension_count = 0;
2266 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002267 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002268 demo->gpu, NULL, &device_extension_count, NULL);
2269 assert(!err);
2270
Tony Barbourcc69f452015-10-08 13:59:42 -06002271 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002272 enabled_extension_count = 0;
2273 memset(extension_names, 0, sizeof(extension_names));
2274 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002275 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002276 demo->gpu, NULL, &device_extension_count, device_extensions);
2277 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002278
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002279 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002280 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002281 swapchainExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002282 extension_names[enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002283 }
2284 assert(enabled_extension_count < 64);
2285 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002286 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002287 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002288 VK_KHR_SWAPCHAIN_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002289 "Vulkan installable client driver (ICD) installed?\nPlease "
2290 "look at the Getting Started guide for additional "
2291 "information.\n",
2292 "vkCreateInstance Failure");
2293 }
2294
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002295 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002296 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2297 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002298 if (!demo->dbgCreateMsgCallback) {
2299 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2300 "vkGetProcAddr Failure");
2301 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002302 if (!demo->dbgDestroyMsgCallback) {
2303 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2304 "vkGetProcAddr Failure");
2305 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002306 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2307 if (!demo->dbgBreakCallback) {
2308 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2309 "vkGetProcAddr Failure");
2310 }
2311
2312 PFN_vkDbgMsgCallback callback;
2313
2314 if (!demo->use_break) {
2315 callback = dbgFunc;
2316 } else {
2317 callback = demo->dbgBreakCallback;
2318 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002319 err = demo->dbgCreateMsgCallback(
2320 demo->inst,
2321 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002322 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002323 &demo->msg_callback);
2324 switch (err) {
2325 case VK_SUCCESS:
2326 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002327 case VK_ERROR_OUT_OF_HOST_MEMORY:
2328 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2329 "dbgCreateMsgCallback Failure");
2330 break;
2331 default:
2332 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2333 "dbgCreateMsgCallback Failure");
2334 break;
2335 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002336 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002337 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002338
2339 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002340 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002341 assert(demo->queue_count >= 1);
2342
2343 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002344 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002345 // Find a queue that supports gfx
2346 uint32_t gfx_queue_idx = 0;
2347 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2348 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2349 break;
2350 }
2351 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002352 // Query fine-grained feature support for this device.
2353 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002354 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002355 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002356
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002357 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002358 const VkDeviceQueueCreateInfo queue = {
2359 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2360 .pNext = NULL,
2361 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu8b497442015-11-06 06:42:02 +08002362 .queueCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002363 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002364 };
2365
2366 VkDeviceCreateInfo device = {
2367 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2368 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002369 .queueCreateInfoCount = 1,
2370 .pQueueCreateInfos = &queue,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002371 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002372 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002373 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002374 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002375 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002376 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002377
Chia-I Wu63636062015-10-26 21:10:41 +08002378 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002379 assert(!err);
2380
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002381 free(device_layers);
2382
Ian Elliotta0781972015-08-21 15:09:33 -06002383 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
Ian Elliottf4a4e442015-11-17 17:29:40 -07002384 GET_DEVICE_PROC_ADDR(demo->device, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2385 GET_DEVICE_PROC_ADDR(demo->device, GetPhysicalDeviceSurfaceFormatsKHR);
2386 GET_DEVICE_PROC_ADDR(demo->device, GetPhysicalDeviceSurfacePresentModesKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002387 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002388 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2389 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2390 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2391 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002392}
2393
Tony Barbourcc69f452015-10-08 13:59:42 -06002394static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002395{
2396 VkResult err;
2397 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002398
Tony Barbourcc69f452015-10-08 13:59:42 -06002399 // Construct the surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002400 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002401 demo->surface_description.pNext = NULL;
2402#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002403 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002404 demo->surface_description.pPlatformHandle = demo->connection;
2405 demo->surface_description.pPlatformWindow = demo->window;
2406#else // _WIN32
2407 demo->platform_handle_xcb.connection = demo->connection;
2408 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002409 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002410 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2411 demo->surface_description.pPlatformWindow = &demo->window;
2412#endif // _WIN32
2413
Tony Barbourcc69f452015-10-08 13:59:42 -06002414 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002415 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2416 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002417 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2418 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002419 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002420 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002421
2422 // Search for a graphics and a present queue in the array of queue
2423 // families, try to find one that supports both
2424 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2425 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002426 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002427 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2428 if (graphicsQueueNodeIndex == UINT32_MAX) {
2429 graphicsQueueNodeIndex = i;
2430 }
2431
2432 if (supportsPresent[i] == VK_TRUE) {
2433 graphicsQueueNodeIndex = i;
2434 presentQueueNodeIndex = i;
2435 break;
2436 }
2437 }
2438 }
2439 if (presentQueueNodeIndex == UINT32_MAX) {
2440 // If didn't find a queue that supports both graphics and present, then
2441 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002442 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002443 if (supportsPresent[i] == VK_TRUE) {
2444 presentQueueNodeIndex = i;
2445 break;
2446 }
2447 }
2448 }
2449 free(supportsPresent);
2450
2451 // Generate error if could not find both a graphics and a present queue
2452 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2453 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002454 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002455 }
2456
2457 // TODO: Add support for separate queues, including presentation,
2458 // synchronization, and appropriate tracking for QueueSubmit
2459 // While it is possible for an application to use a separate graphics and a
2460 // present queues, this demo program assumes it is only using one:
2461 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2462 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002463 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002464 }
2465
2466 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002467
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002468 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002469 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002470
Ian Elliottaaae5352015-07-06 14:27:58 -06002471 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002472 uint32_t formatCount;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002473 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->device,
Ian Elliotta0781972015-08-21 15:09:33 -06002474 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002475 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002476 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002477 VkSurfaceFormatKHR *surfFormats =
2478 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Ian Elliottf4a4e442015-11-17 17:29:40 -07002479 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->device,
Ian Elliotta0781972015-08-21 15:09:33 -06002480 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002481 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002482 assert(!err);
2483 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2484 // the surface has no preferred format. Otherwise, at least one
2485 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002486 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2487 {
2488 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2489 }
2490 else
2491 {
2492 assert(formatCount >= 1);
2493 demo->format = surfFormats[0].format;
2494 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002495 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002496
David Pinedo2bb7c932015-06-18 17:03:14 -06002497 demo->quit = false;
2498 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002499
2500 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002501 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002502}
2503
2504static void demo_init_connection(struct demo *demo)
2505{
Ian Elliott639ca472015-04-16 15:23:05 -06002506#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002507 const xcb_setup_t *setup;
2508 xcb_screen_iterator_t iter;
2509 int scr;
2510
2511 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002512 if (demo->connection == NULL) {
2513 printf("Cannot find a compatible Vulkan installable client driver "
2514 "(ICD).\nExiting ...\n");
2515 fflush(stdout);
2516 exit(1);
2517 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002518
2519 setup = xcb_get_setup(demo->connection);
2520 iter = xcb_setup_roots_iterator(setup);
2521 while (scr-- > 0)
2522 xcb_screen_next(&iter);
2523
2524 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002525#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002526}
2527
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002528static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002529{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002530 vec3 eye = {0.0f, 3.0f, 5.0f};
2531 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002532 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002533
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002534 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002535 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002536
Piers Daniell735ee532015-02-23 16:23:13 -07002537 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002538 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002539 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002540 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002541 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002542 if (strcmp(argv[i], "--break") == 0) {
2543 demo->use_break = true;
2544 continue;
2545 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002546 if (strcmp(argv[i], "--validate") == 0) {
2547 demo->validate = true;
2548 continue;
2549 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002550 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002551 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002552 i < argc-1 &&
2553 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2554 demo->frameCount >= 0)
2555 {
2556 i++;
2557 continue;
2558 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002559
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002560 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002561 fflush(stderr);
2562 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002563 }
2564
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002565 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002566 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002567
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002568 demo->width = 500;
2569 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002570
2571 demo->spin_angle = 0.01f;
2572 demo->spin_increment = 0.01f;
2573 demo->pause = false;
2574
Piers Daniell735ee532015-02-23 16:23:13 -07002575 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002576 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2577 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002578}
2579
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002580
Ian Elliott639ca472015-04-16 15:23:05 -06002581#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002582extern int __getmainargs(
2583 int * _Argc,
2584 char *** _Argv,
2585 char *** _Env,
2586 int _DoWildCard,
2587 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002588
Ian Elliotta748eaf2015-04-28 15:50:36 -06002589int WINAPI WinMain(HINSTANCE hInstance,
2590 HINSTANCE hPrevInstance,
2591 LPSTR pCmdLine,
2592 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002593{
2594 MSG msg; // message
2595 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002596 int argc;
2597 char** argv;
2598 char** env;
2599 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002600
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002601 __getmainargs(&argc,&argv,&env,0,&new_mode);
2602
2603 demo_init(&demo, argc, argv);
2604 demo.connection = hInstance;
2605 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002606 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002607 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002608
2609 demo_prepare(&demo);
2610
2611 done = false; //initialize loop condition variable
2612 /* main message loop*/
2613 while(!done)
2614 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002615 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002616 if (msg.message == WM_QUIT) //check for a quit message
2617 {
2618 done = true; //if found, quit app
2619 }
2620 else
2621 {
2622 /* Translate and dispatch to event queue*/
2623 TranslateMessage(&msg);
2624 DispatchMessage(&msg);
2625 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002626 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002627 }
2628
2629 demo_cleanup(&demo);
2630
Tony Barbourf9921732015-04-22 11:36:22 -06002631 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002632}
2633#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002634int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002635{
2636 struct demo demo;
2637
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002638 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002639 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002640 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002641
2642 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002643 demo_run(&demo);
2644
2645 demo_cleanup(&demo);
2646
2647 return 0;
2648}
Ian Elliott639ca472015-04-16 15:23:05 -06002649#endif // _WIN32