blob: 7a35e215247f3f52d80791480bdcc6155d2709dd [file] [log] [blame]
Ian Elliott421107f2015-04-28 15:50:36 -06001/*
Ian Elliott421107f2015-04-28 15:50:36 -06002 *
Courtney Goeltzenleuchter8a17da52015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Ian Elliott421107f2015-04-28 15:50:36 -06004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060023#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <stdbool.h>
28#include <assert.h>
29
Ian Elliotte14e9f92015-04-16 15:23:05 -060030#ifdef _WIN32
31#pragma comment(linker, "/subsystem:windows")
32#include <windows.h>
33#define APP_NAME_STR_LEN 80
34#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060035#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060036#endif // _WIN32
37
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060038#include <vulkan.h>
Ian Elliott338dedb2015-08-21 15:09:33 -060039#include <vk_ext_khr_swapchain.h>
40#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060041#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060042
Mark Lobodzinskieccbb372015-09-01 09:00:16 -060043#include "vk_sdk_platform.h"
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060044#include "linmath.h"
45
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060046#define DEMO_TEXTURE_COUNT 1
Ian Elliott4e19ed02015-04-28 10:52:52 -060047#define APP_SHORT_NAME "cube"
48#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060049
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -060050#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
51
Tony Barbour22a30862015-04-22 09:02:32 -060052#if defined(NDEBUG) && defined(__GNUC__)
53#define U_ASSERT_ONLY __attribute__((unused))
54#else
55#define U_ASSERT_ONLY
56#endif
57
Ian Elliottcaa9f272015-04-28 11:35:02 -060058#ifdef _WIN32
59#define ERR_EXIT(err_msg, err_class) \
60 do { \
61 MessageBox(NULL, err_msg, err_class, MB_OK); \
62 exit(1); \
63 } while (0)
64
Ian Elliottcaa9f272015-04-28 11:35:02 -060065#else // _WIN32
66
67#define ERR_EXIT(err_msg, err_class) \
68 do { \
69 printf(err_msg); \
70 fflush(stdout); \
71 exit(1); \
72 } while (0)
73#endif // _WIN32
74
Ian Elliotte36b2082015-07-06 14:27:58 -060075#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
76{ \
77 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
78 if (demo->fp##entrypoint == NULL) { \
79 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
80 "vkGetInstanceProcAddr Failure"); \
81 } \
82}
83
Jon Ashburnccfa8412015-10-08 15:58:23 -060084static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
85
Ian Elliott1b6de092015-06-22 15:07:49 -060086#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
87{ \
Jon Ashburnccfa8412015-10-08 15:58:23 -060088 if(!g_gdpa) \
89 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
90 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060091 if (demo->fp##entrypoint == NULL) { \
92 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
93 "vkGetDeviceProcAddr Failure"); \
94 } \
95}
96
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060097/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070098 * structure to track all objects related to a texture.
99 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600100struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600101 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700102
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600103 VkImage image;
104 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600105
Chia-I Wu1f851912015-10-27 18:04:07 +0800106 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500107 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600108 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700109 int32_t tex_width, tex_height;
110};
111
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600112static char *tex_files[] = {
Tony Barboura26fb7d2015-09-21 15:17:33 -0600113 "lunarg.ppm"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600114};
115
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600116struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600117 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600118 float mvp[4][4];
119 float position[12*3][4];
120 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600121};
122
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600123struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600124 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600125 float mvp[4][4];
126 float position[12*3][4];
127 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600128};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600129
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600130//--------------------------------------------------------------------------------------
131// Mesh and VertexFormat Data
132//--------------------------------------------------------------------------------------
133struct Vertex
134{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600137};
138
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600139struct VertexPosTex
140{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600141 float posX, posY, posZ, posW; // Position data
142 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600143};
144
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600145#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600146#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600147
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600148static const float g_vertex_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800149 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600150 -1.0f,-1.0f, 1.0f,
151 -1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800152 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600153 -1.0f, 1.0f,-1.0f,
154 -1.0f,-1.0f,-1.0f,
155
Chia-I Wuc3487c22015-04-22 14:56:17 +0800156 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600157 1.0f, 1.0f,-1.0f,
158 1.0f,-1.0f,-1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800159 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600160 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600161 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600162
Chia-I Wuc3487c22015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600164 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600165 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600167 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600168 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600169
Chia-I Wuc3487c22015-04-22 14:56:17 +0800170 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600171 -1.0f, 1.0f, 1.0f,
172 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800173 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600174 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600175 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600176
Chia-I Wuc3487c22015-04-22 14:56:17 +0800177 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600178 1.0f, 1.0f, 1.0f,
179 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800180 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600181 1.0f,-1.0f,-1.0f,
182 1.0f, 1.0f,-1.0f,
183
Chia-I Wuc3487c22015-04-22 14:56:17 +0800184 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600185 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600186 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800187 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600188 1.0f,-1.0f, 1.0f,
189 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600190};
191
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600192static const float g_uv_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800193 0.0f, 0.0f, // -X side
194 1.0f, 0.0f,
195 1.0f, 1.0f,
196 1.0f, 1.0f,
197 0.0f, 1.0f,
198 0.0f, 0.0f,
199
200 1.0f, 0.0f, // -Z side
201 0.0f, 1.0f,
202 0.0f, 0.0f,
203 1.0f, 0.0f,
204 1.0f, 1.0f,
205 0.0f, 1.0f,
206
207 1.0f, 1.0f, // -Y side
208 1.0f, 0.0f,
209 0.0f, 0.0f,
210 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600211 0.0f, 0.0f,
212 0.0f, 1.0f,
213
Chia-I Wuc3487c22015-04-22 14:56:17 +0800214 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600215 0.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800216 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600217 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600218 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600219 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600220
Chia-I Wuc3487c22015-04-22 14:56:17 +0800221 1.0f, 1.0f, // +X side
222 0.0f, 1.0f,
223 0.0f, 0.0f,
224 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600225 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600226 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600227
Chia-I Wuc3487c22015-04-22 14:56:17 +0800228 0.0f, 1.0f, // +Z side
229 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600230 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600231 0.0f, 0.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600234};
235
236void dumpMatrix(const char *note, mat4x4 MVP)
237{
238 int i;
239
240 printf("%s: \n", note);
241 for (i=0; i<4; i++) {
242 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
243 }
244 printf("\n");
245 fflush(stdout);
246}
247
248void dumpVec4(const char *note, vec4 vector)
249{
250 printf("%s: \n", note);
251 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
252 printf("\n");
253 fflush(stdout);
254}
255
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600256VkBool32 dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600257 VkFlags msgFlags,
258 VkDbgObjectType objType,
259 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600260 size_t location,
261 int32_t msgCode,
262 const char* pLayerPrefix,
263 const char* pMsg,
264 void* pUserData)
Tony Barbour5685ad72015-04-29 16:19:20 -0600265{
266 char *message = (char *) malloc(strlen(pMsg)+100);
267
268 assert (message);
269
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600270 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
271 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
272 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barboura65ecc22015-06-30 14:14:19 -0600273 // We know that we're submitting queues without fences, ignore this warning
274 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600275 return false;
Tony Barboura65ecc22015-06-30 14:14:19 -0600276 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600277 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour5685ad72015-04-29 16:19:20 -0600278 } else {
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600279 return false;
Tony Barbour5685ad72015-04-29 16:19:20 -0600280 }
281
282#ifdef _WIN32
283 MessageBox(NULL, message, "Alert", MB_OK);
284#else
285 printf("%s\n",message);
286 fflush(stdout);
287#endif
288 free(message);
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600289
Courtney Goeltzenleuchtere4c43132015-09-08 16:57:22 -0600290 /*
291 * false indicates that layer should not bail-out of an
292 * API call that had validation failures. This may mean that the
293 * app dies inside the driver due to invalid parameter(s).
294 * That's what would happen without validation layers, so we'll
295 * keep that behavior here.
296 */
297 return false;
Tony Barbour5685ad72015-04-29 16:19:20 -0600298}
299
Ian Elliott338dedb2015-08-21 15:09:33 -0600300typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600301 VkImage image;
Chia-I Wu1f851912015-10-27 18:04:07 +0800302 VkCommandBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600303 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600304} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600305
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600306struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600307#ifdef _WIN32
308#define APP_NAME_STR_LEN 80
309 HINSTANCE connection; // hInstance - Windows Instance
310 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
311 HWND window; // hWnd - window handle
312#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600313 xcb_connection_t *connection;
314 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800315 xcb_window_t window;
316 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott338dedb2015-08-21 15:09:33 -0600317 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour7910de72015-07-13 16:37:21 -0600318#endif // _WIN32
Cody Northrop75db0322015-05-28 11:27:16 -0600319 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700320 bool use_staging_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600322 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600323 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600324 VkDevice device;
325 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700326 uint32_t graphics_queue_node_index;
Tony Barbour426b9052015-06-24 16:06:58 -0600327 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600328 VkQueueFamilyProperties *queue_props;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600329 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600330
331 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600332 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600333 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600334
Ian Elliott338dedb2015-08-21 15:09:33 -0600335 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
336 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
337 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
338 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
339 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
340 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
341 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
342 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
343 PFN_vkQueuePresentKHR fpQueuePresentKHR;
344 VkSurfaceDescriptionWindowKHR surface_description;
345 uint32_t swapchainImageCount;
Ian Elliotte2688a52015-10-16 18:02:43 -0600346 VkSwapchainKHR swapchain;
Ian Elliott338dedb2015-08-21 15:09:33 -0600347 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600348
Chia-I Wu1f851912015-10-27 18:04:07 +0800349 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600350
351 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600352 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600353
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600354 VkImage image;
Chia-I Wu1f851912015-10-27 18:04:07 +0800355 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500356 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600357 VkImageView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600358 } depth;
359
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600360 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600361
362 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600363 VkBuffer buf;
Chia-I Wu1f851912015-10-27 18:04:07 +0800364 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500365 VkDeviceMemory mem;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600366 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600367 } uniform_data;
368
Chia-I Wu1f851912015-10-27 18:04:07 +0800369 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500370 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600371 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600372 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800373 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600374 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600375
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600376 mat4x4 projection_matrix;
377 mat4x4 view_matrix;
378 mat4x4 model_matrix;
379
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600380 float spin_angle;
381 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600382 bool pause;
383
Tony Barbour9400f092015-07-21 09:04:41 -0600384 VkShaderModule vert_shader_module;
385 VkShaderModule frag_shader_module;
386
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600387 VkDescriptorPool desc_pool;
388 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800389
Tony Barbour5aabff52015-10-08 14:26:24 -0600390 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800391
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600392 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600393 int32_t curFrame;
394 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600395 bool validate;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600396 bool use_break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600397 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600398 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600399 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600400 VkDbgMsgCallback msg_callback;
401
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600402 uint32_t current_buffer;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -0600403 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600404};
405
Ian Elliotte2688a52015-10-16 18:02:43 -0600406// Forward declaration:
407static void demo_resize(struct demo *demo);
408
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600409static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinski72346292015-07-02 16:49:40 -0600410{
411 // Search memtypes to find first index with those properties
412 for (uint32_t i = 0; i < 32; i++) {
413 if ((typeBits & 1) == 1) {
414 // Type is available, does it match user properties?
Tony Barbourf1eceb92015-10-15 12:42:56 -0600415 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinski72346292015-07-02 16:49:40 -0600416 *typeIndex = i;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600417 return true;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600418 }
419 }
420 typeBits >>= 1;
421 }
422 // No memory types matched, return failure
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600423 return false;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600424}
425
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600426static void demo_flush_init_cmd(struct demo *demo)
427{
Tony Barbour22a30862015-04-22 09:02:32 -0600428 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600429
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600430 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600431 return;
432
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600433 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600434 assert(!err);
435
Chia-I Wu1f851912015-10-27 18:04:07 +0800436 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wue420a332015-10-26 20:04:44 +0800437 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600438 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800439 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
440 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800441 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600442 .pWaitSemaphores = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800443 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600444 .pCommandBuffers = cmd_bufs,
Chia-I Wu763a7492015-10-26 20:48:51 +0800445 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600446 .pSignalSemaphores = NULL
447 };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600448
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600449 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600450 assert(!err);
451
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600452 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600453 assert(!err);
454
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600455 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600456 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600457}
458
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600459static void demo_set_image_layout(
460 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600461 VkImage image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600462 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600463 VkImageLayout old_image_layout,
464 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600465{
Tony Barbour22a30862015-04-22 09:02:32 -0600466 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600467
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600468 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800469 const VkCommandBufferAllocateInfo cmd = {
470 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600471 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +0800472 .commandPool = demo->cmd_pool,
473 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +0800474 .bufferCount = 1,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600475 };
476
Chia-I Wu1f851912015-10-27 18:04:07 +0800477 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600478 assert(!err);
479
Chia-I Wu1f851912015-10-27 18:04:07 +0800480 VkCommandBufferBeginInfo cmd_buf_info = {
481 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600482 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600483 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800484 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600485 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800486 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600487 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600488 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourc1098272015-10-23 10:53:30 -0600489 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600490 }
491
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600492 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600493 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600494 .pNext = NULL,
Chia-I Wu989de842015-10-27 19:54:37 +0800495 .srcAccessMask = 0,
496 .dstAccessMask = 0,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600497 .oldLayout = old_image_layout,
498 .newLayout = new_image_layout,
499 .image = image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600500 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600501 };
502
Chia-I Wu1f851912015-10-27 18:04:07 +0800503 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600504 /* Make sure anything that was copying from this image has completed */
Chia-I Wu989de842015-10-27 19:54:37 +0800505 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600506 }
507
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600508 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600509 /* Make sure any Copy or CPU writes to image are flushed */
Chia-I Wu989de842015-10-27 19:54:37 +0800510 image_memory_barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600511 }
512
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600513 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600514
Tony Barbourc2e987e2015-06-29 16:20:35 -0600515 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
516 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600517
Chia-I Wu3ffcd732015-10-26 17:08:33 +0800518 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600519}
520
Chia-I Wu1f851912015-10-27 18:04:07 +0800521static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600522{
Chia-I Wu1f851912015-10-27 18:04:07 +0800523 const VkCommandBufferBeginInfo cmd_buf_info = {
524 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600525 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600526 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800527 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600528 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800529 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700530 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800531 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600532 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
533 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800534 };
535 const VkRenderPassBeginInfo rp_begin = {
536 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
537 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800538 .renderPass = demo->render_pass,
539 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800540 .renderArea.offset.x = 0,
541 .renderArea.offset.y = 0,
542 .renderArea.extent.width = demo->width,
543 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600544 .clearValueCount = 2,
545 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700546 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800547 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600548
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600549 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600550 assert(!err);
551
Chia-I Wuc51b1212015-10-27 19:25:11 +0800552 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wuc278df82015-07-07 11:50:03 +0800553
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600554 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600555 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600556 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600557 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600558
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600559 VkViewport viewport;
560 memset(&viewport, 0, sizeof(viewport));
561 viewport.height = (float) demo->height;
562 viewport.width = (float) demo->width;
563 viewport.minDepth = (float) 0.0f;
564 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600565 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600566
567 VkRect2D scissor;
568 memset(&scissor, 0, sizeof(scissor));
569 scissor.extent.width = demo->width;
570 scissor.extent.height = demo->height;
571 scissor.offset.x = 0;
572 scissor.offset.y = 0;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600573 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600574
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600575 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800576 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600577
Tony Barbour40292ae2015-10-21 10:14:48 -0600578 VkImageMemoryBarrier prePresentBarrier = {
579 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
580 .pNext = NULL,
Chia-I Wu989de842015-10-27 19:54:37 +0800581 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
582 .dstAccessMask = 0,
Tony Barbour40292ae2015-10-21 10:14:48 -0600583 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
584 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
585 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wu1f851912015-10-27 18:04:07 +0800586 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600587 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
588 };
589
590 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
591 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wucba6cea2015-10-31 00:31:16 +0800592 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu3ffcd732015-10-26 17:08:33 +0800593 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour40292ae2015-10-21 10:14:48 -0600594
595
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600596 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600597 assert(!err);
598}
599
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600600
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600601void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600602{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600603 mat4x4 MVP, Model, VP;
604 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600605 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600606 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600607
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600608 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600609
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600610 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600611 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700612 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600613 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600614
Dominik Witczake82d5b62015-09-22 18:25:33 +0200615 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600616 assert(!err);
617
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600618 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600619
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600620 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600621}
622
623static void demo_draw(struct demo *demo)
624{
Tony Barbour22a30862015-04-22 09:02:32 -0600625 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600626 VkSemaphore presentCompleteSemaphore;
627 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
628 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
629 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600630 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600631 };
Chia-I Wue420a332015-10-26 20:04:44 +0800632 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600633
Ian Elliotte36b2082015-07-06 14:27:58 -0600634 err = vkCreateSemaphore(demo->device,
635 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +0800636 NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600637 &presentCompleteSemaphore);
638 assert(!err);
639
640 // Get the index of the next available swapchain image:
Ian Elliotte2688a52015-10-16 18:02:43 -0600641 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600642 UINT64_MAX,
643 presentCompleteSemaphore,
644 &demo->current_buffer);
Ian Elliotte2688a52015-10-16 18:02:43 -0600645 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
646 // demo->swapchain is out of date (e.g. the window was resized) and
647 // must be recreated:
648 demo_resize(demo);
649 demo_draw(demo);
Chia-I Wu69f40122015-10-26 21:10:41 +0800650 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -0600651 return;
652 } else if (err == VK_SUBOPTIMAL_KHR) {
653 // demo->swapchain is not as optimal as it could be, but the platform's
654 // presentation engine will still present the image correctly.
655 } else {
656 assert(!err);
657 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600658
Tony Barbour40292ae2015-10-21 10:14:48 -0600659 // Assume the command buffer has been run on current_buffer before so
660 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
661 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
662 VK_IMAGE_ASPECT_COLOR_BIT,
663 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
664 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
665 demo_flush_init_cmd(demo);
666
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600667 // Wait for the present complete semaphore to be signaled to ensure
668 // that the image won't be rendered to until the presentation
669 // engine has fully released ownership to the application, and it is
670 // okay to render to the image.
671
Ian Elliott338dedb2015-08-21 15:09:33 -0600672// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600673 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800674 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
675 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800676 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600677 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu763a7492015-10-26 20:48:51 +0800678 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600679 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu763a7492015-10-26 20:48:51 +0800680 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600681 .pSignalSemaphores = NULL
682 };
683
684 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600685 assert(!err);
686
Ian Elliott338dedb2015-08-21 15:09:33 -0600687 VkPresentInfoKHR present = {
688 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600689 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600690 .swapchainCount = 1,
Ian Elliotte2688a52015-10-16 18:02:43 -0600691 .swapchains = &demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600692 .imageIndices = &demo->current_buffer,
693 };
694
695// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600696 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliotte2688a52015-10-16 18:02:43 -0600697 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
698 // demo->swapchain is out of date (e.g. the window was resized) and
699 // must be recreated:
700 demo_resize(demo);
701 } else if (err == VK_SUBOPTIMAL_KHR) {
702 // demo->swapchain is not as optimal as it could be, but the platform's
703 // presentation engine will still present the image correctly.
704 } else {
705 assert(!err);
706 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600707
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800708 err = vkQueueWaitIdle(demo->queue);
709 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600710
Chia-I Wu69f40122015-10-26 21:10:41 +0800711 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600712}
713
714static void demo_prepare_buffers(struct demo *demo)
715{
Ian Elliotte36b2082015-07-06 14:27:58 -0600716 VkResult U_ASSERT_ONLY err;
Ian Elliotte2688a52015-10-16 18:02:43 -0600717 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600718
719 // Check the surface properties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600720 VkSurfacePropertiesKHR surfProperties;
721 err = demo->fpGetSurfacePropertiesKHR(demo->device,
722 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600723 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600724 assert(!err);
725
Ian Elliott7fe115d2015-08-07 15:56:59 -0600726 uint32_t presentModeCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600727 err = demo->fpGetSurfacePresentModesKHR(demo->device,
728 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600729 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600730 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600731 VkPresentModeKHR *presentModes =
732 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600733 assert(presentModes);
Ian Elliott338dedb2015-08-21 15:09:33 -0600734 err = demo->fpGetSurfacePresentModesKHR(demo->device,
735 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600736 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600737 assert(!err);
738
Ian Elliott338dedb2015-08-21 15:09:33 -0600739 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600740 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600741 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600742 {
743 // If the surface size is undefined, the size is set to
744 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600745 swapchainExtent.width = demo->width;
746 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600747 }
748 else
749 {
750 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -0600751 swapchainExtent = surfProperties.currentExtent;
Ian Elliotte2688a52015-10-16 18:02:43 -0600752 demo->width = surfProperties.currentExtent.width;
753 demo->height = surfProperties.currentExtent.height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600754 }
755
756 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3fc49c22015-07-27 13:53:11 -0600757 // tearing mode. If not, try IMMEDIATE which will usually be available,
758 // and is fastest (though it tears). If not, fall back to FIFO which is
759 // always available.
Ian Elliott338dedb2015-08-21 15:09:33 -0600760 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600761 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -0600762 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
763 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600764 break;
765 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600766 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
767 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
768 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3fc49c22015-07-27 13:53:11 -0600769 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600770 }
771
772 // Determine the number of VkImage's to use in the swap chain (we desire to
773 // own only 1 image at a time, besides the images being displayed and
774 // queued for display):
Ian Elliott338dedb2015-08-21 15:09:33 -0600775 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600776 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600777 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600778 {
779 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600780 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600781 }
782
Tony Barbourd161c542015-09-16 15:01:32 -0600783 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliott338dedb2015-08-21 15:09:33 -0600784 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
785 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600786 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600787 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600788 }
789
Ian Elliotte2688a52015-10-16 18:02:43 -0600790 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600791 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800792 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600793 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
794 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800795 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600796 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800797 .imageExtent = {
Ian Elliott338dedb2015-08-21 15:09:33 -0600798 .width = swapchainExtent.width,
799 .height = swapchainExtent.height,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600800 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600801 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600802 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800803 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600804 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
805 .queueFamilyCount = 0,
806 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600807 .presentMode = swapchainPresentMode,
Ian Elliotte2688a52015-10-16 18:02:43 -0600808 .oldSwapchain = oldSwapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600809 .clipped = true,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600810 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600811 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600812
Ian Elliotte2688a52015-10-16 18:02:43 -0600813 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800814 assert(!err);
815
Ian Elliotte2688a52015-10-16 18:02:43 -0600816 // If we just re-created an existing swapchain, we should destroy the old
817 // swapchain at this point.
818 // Note: destroying the swapchain also cleans up all its associated
819 // presentable images once the platform is done with them.
Chia-I Wue420a332015-10-26 20:04:44 +0800820 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliotte2688a52015-10-16 18:02:43 -0600821 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
822 }
823
824 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600825 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600826 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800827
Ian Elliott338dedb2015-08-21 15:09:33 -0600828 VkImage* swapchainImages =
829 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
830 assert(swapchainImages);
Ian Elliotte2688a52015-10-16 18:02:43 -0600831 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600832 &demo->swapchainImageCount,
833 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600834 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -0600835
Ian Elliott338dedb2015-08-21 15:09:33 -0600836 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600837 assert(demo->buffers);
838
Ian Elliott338dedb2015-08-21 15:09:33 -0600839 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600840 VkImageViewCreateInfo color_image_view = {
841 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600842 .pNext = NULL,
843 .format = demo->format,
Chia-I Wu4291d882015-10-27 19:00:15 +0800844 .components = {
Chia-I Wuc51b1212015-10-27 19:25:11 +0800845 .r = VK_COMPONENT_SWIZZLE_R,
846 .g = VK_COMPONENT_SWIZZLE_G,
847 .b = VK_COMPONENT_SWIZZLE_B,
848 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600849 },
850 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600851 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600852 .baseMipLevel = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800853 .levelCount = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600854 .baseArrayLayer = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800855 .layerCount = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600856 },
857 .viewType = VK_IMAGE_VIEW_TYPE_2D,
858 .flags = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600859 };
860
Ian Elliott338dedb2015-08-21 15:09:33 -0600861 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600862
Tony Barbour40292ae2015-10-21 10:14:48 -0600863 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
864 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600865 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -0600866 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600867 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600868 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600869
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600870 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600871
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600872 err = vkCreateImageView(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +0800873 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600874 assert(!err);
875 }
876}
877
878static void demo_prepare_depth(struct demo *demo)
879{
Tony Barbour8205d902015-04-16 15:59:00 -0600880 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600881 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600882 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600883 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600884 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600885 .format = depth_format,
886 .extent = { demo->width, demo->height, 1 },
887 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600888 .arrayLayers = 1,
Chia-I Wu3138d6a2015-10-31 00:31:16 +0800889 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour8205d902015-04-16 15:59:00 -0600890 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600891 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600892 .flags = 0,
893 };
Dominik Witczake82d5b62015-09-22 18:25:33 +0200894
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600895 VkImageViewCreateInfo view = {
896 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600897 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +0800898 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4ab40c42015-07-15 17:41:38 -0600899 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600900 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600901 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600902 .baseMipLevel = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800903 .levelCount = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600904 .baseArrayLayer = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800905 .layerCount = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600906 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600907 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600908 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600909 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600910
Mark Lobodzinski23182612015-05-29 09:32:35 -0500911 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600912 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600913 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600914
915 demo->depth.format = depth_format;
916
917 /* create image */
Chia-I Wu69f40122015-10-26 21:10:41 +0800918 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600919 &demo->depth.image);
920 assert(!err);
921
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600922 vkGetImageMemoryRequirements(demo->device,
Tony Barbourde4124d2015-07-03 10:33:54 -0600923 demo->depth.image, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -0600924 assert(!err);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600925
Dominik Witczake82d5b62015-09-22 18:25:33 +0200926 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
927 demo->depth.mem_alloc.pNext = NULL;
928 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
929 demo->depth.mem_alloc.memoryTypeIndex = 0;
930
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600931 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600932 mem_reqs.memoryTypeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600933 0, /* No requirements */
Dominik Witczake82d5b62015-09-22 18:25:33 +0200934 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600935 assert(pass);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600936
Mark Lobodzinski23182612015-05-29 09:32:35 -0500937 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +0800938 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu69f40122015-10-26 21:10:41 +0800939 NULL, &demo->depth.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500940 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600941
Mark Lobodzinski23182612015-05-29 09:32:35 -0500942 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600943 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500944 demo->depth.mem, 0);
945 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600946
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600947 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600948 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600949 VK_IMAGE_LAYOUT_UNDEFINED,
950 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600951
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600952 /* create image view */
953 view.image = demo->depth.image;
Chia-I Wu69f40122015-10-26 21:10:41 +0800954 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600955 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600956}
957
Tony Barboura26fb7d2015-09-21 15:17:33 -0600958/* Load a ppm file into memory */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700959bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600960 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600961 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600962{
Tony Barboura26fb7d2015-09-21 15:17:33 -0600963 FILE *fPtr = fopen(filename,"rb");
964 char header[256], *cPtr;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600965
Tony Barboura26fb7d2015-09-21 15:17:33 -0600966 if (!fPtr)
967 return false;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600968
Tony Barboura26fb7d2015-09-21 15:17:33 -0600969 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan85180252015-10-28 11:15:46 -0600970 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
971 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600972 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600973 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600974
Tony Barboura26fb7d2015-09-21 15:17:33 -0600975 do {
976 cPtr = fgets(header, 256, fPtr);
Mike Stroyan85180252015-10-28 11:15:46 -0600977 if (cPtr == NULL) {
978 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600979 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600980 }
Tony Barboura26fb7d2015-09-21 15:17:33 -0600981 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600982
Tony Barboura26fb7d2015-09-21 15:17:33 -0600983 sscanf(header, "%u %u", height, width);
Mike Stroyan85180252015-10-28 11:15:46 -0600984 if (rgba_data == NULL) {
985 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600986 return true;
Mike Stroyan85180252015-10-28 11:15:46 -0600987 }
Tony Barboura26fb7d2015-09-21 15:17:33 -0600988 fgets(header, 256, fPtr); // Format
Mike Stroyan85180252015-10-28 11:15:46 -0600989 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
990 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600991 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600992 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600993
Tony Barboura26fb7d2015-09-21 15:17:33 -0600994 for(int y = 0; y < *height; y++)
995 {
996 uint8_t *rowPtr = rgba_data;
997 for(int x = 0; x < *width; x++)
998 {
999 fread(rowPtr, 3, 1, fPtr);
1000 rowPtr[3] = 255; /* Alpha of 1 */
1001 rowPtr += 4;
1002 }
1003 rgba_data += layout->rowPitch;
1004 }
1005 fclose(fPtr);
Mike Stroyan85180252015-10-28 11:15:46 -06001006 return true;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001007}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001008
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001009static void demo_prepare_texture_image(struct demo *demo,
1010 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001011 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001012 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001013 VkImageUsageFlags usage,
Tony Barbourf1eceb92015-10-15 12:42:56 -06001014 VkFlags required_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001015{
Mike Stroyande24a6f2015-06-15 14:21:03 -06001016 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001017 int32_t tex_width;
1018 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -06001019 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001020 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001021
David Pinedo61e77382015-04-23 08:16:57 -06001022 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1023 {
1024 printf("Failed to load textures\n");
1025 fflush(stdout);
1026 exit(1);
1027 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001028
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001029 tex_obj->tex_width = tex_width;
1030 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001031
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001032 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001033 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001034 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -06001035 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001036 .format = tex_format,
1037 .extent = { tex_width, tex_height, 1 },
1038 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -06001039 .arrayLayers = 1,
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001040 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001041 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001042 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001043 .flags = 0,
1044 };
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001045
Mark Lobodzinski23182612015-05-29 09:32:35 -05001046 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001047
Chia-I Wu69f40122015-10-26 21:10:41 +08001048 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001049 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001050 assert(!err);
1051
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001052 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell886be472015-02-23 16:23:13 -07001053
Dominik Witczake82d5b62015-09-22 18:25:33 +02001054 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1055 tex_obj->mem_alloc.pNext = NULL;
1056 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1057 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001058
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001059 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1060 assert(pass);
Mark Lobodzinski72346292015-07-02 16:49:40 -06001061
Mark Lobodzinski23182612015-05-29 09:32:35 -05001062 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +08001063 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001064 &(tex_obj->mem));
1065 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001066
Mark Lobodzinski23182612015-05-29 09:32:35 -05001067 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -06001068 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001069 tex_obj->mem, 0);
1070 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001071
Tony Barbourf1eceb92015-10-15 12:42:56 -06001072 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001073 const VkImageSubresource subres = {
Chia-I Wu195c9e12015-10-27 19:55:05 +08001074 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001075 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -06001076 .arrayLayer = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001077 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001078 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001079 void *data;
1080
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001081 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001082
Dominik Witczake82d5b62015-09-22 18:25:33 +02001083 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001084 assert(!err);
1085
1086 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1087 fprintf(stderr, "Error loading texture: %s\n", filename);
1088 }
1089
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001090 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001091 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001092
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001093 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001094 demo_set_image_layout(demo, tex_obj->image,
Tony Barbour983984f2015-10-12 11:07:58 -06001095 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001096 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001097 tex_obj->imageLayout);
1098 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001099}
1100
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001101static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001102{
1103 /* clean up staging resources */
Chia-I Wu69f40122015-10-26 21:10:41 +08001104 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1105 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001106}
1107
1108static void demo_prepare_textures(struct demo *demo)
1109{
Tony Barbour8205d902015-04-16 15:59:00 -06001110 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001111 VkFormatProperties props;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001112 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001113
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001114 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001115
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001116 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001117 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001118
FslNopper434db6a2015-05-06 21:42:01 +02001119 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001120 /* Device can texture using linear textures */
1121 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001122 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001123 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001124 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001125 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001126
1127 memset(&staging_texture, 0, sizeof(staging_texture));
1128 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wu1f851912015-10-27 18:04:07 +08001129 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001130
1131 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001132 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001133 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wu27d8ed92015-10-27 18:53:22 +08001134 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001135
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001136 demo_set_image_layout(demo, staging_texture.image,
Tony Barbour983984f2015-10-12 11:07:58 -06001137 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001138 staging_texture.imageLayout,
Chia-I Wu1f851912015-10-27 18:04:07 +08001139 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001140
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001141 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -06001142 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001143 demo->textures[i].imageLayout,
Chia-I Wu1f851912015-10-27 18:04:07 +08001144 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001145
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001146 VkImageCopy copy_region = {
Tony Barbourca5c4eb2015-11-02 11:47:51 -07001147 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001148 .srcOffset = { 0, 0, 0 },
Chia-I Wu1f851912015-10-27 18:04:07 +08001149 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1150 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001151 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1152 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001153 vkCmdCopyImage(demo->cmd,
Chia-I Wu1f851912015-10-27 18:04:07 +08001154 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1155 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001156 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001157
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001158 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -06001159 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wu1f851912015-10-27 18:04:07 +08001160 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001161 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001162
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001163 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001164
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001165 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001166 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001167 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1168 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001169 }
1170
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001171 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001172 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001173 .pNext = NULL,
Chia-I Wu3603b082015-10-26 16:49:32 +08001174 .magFilter = VK_FILTER_NEAREST,
1175 .minFilter = VK_FILTER_NEAREST,
1176 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wuce532f72015-10-26 17:32:47 +08001177 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1178 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1179 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001180 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001181 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001182 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001183 .minLod = 0.0f,
1184 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001185 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -06001186 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001187 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001188
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001189 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001190 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001191 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +08001192 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001193 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001194 .format = tex_format,
Chia-I Wu4291d882015-10-27 19:00:15 +08001195 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuc51b1212015-10-27 19:25:11 +08001196 VK_COMPONENT_SWIZZLE_G,
1197 VK_COMPONENT_SWIZZLE_B,
1198 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -06001199 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001200 .flags = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001201 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001202
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001203 /* create sampler */
Chia-I Wu69f40122015-10-26 21:10:41 +08001204 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001205 &demo->textures[i].sampler);
1206 assert(!err);
1207
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001208 /* create image view */
1209 view.image = demo->textures[i].image;
Chia-I Wu69f40122015-10-26 21:10:41 +08001210 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001211 &demo->textures[i].view);
1212 assert(!err);
1213 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001214}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001215
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001216void demo_prepare_cube_data_buffer(struct demo *demo)
1217{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001218 VkBufferCreateInfo buf_info;
Mark Lobodzinski23182612015-05-29 09:32:35 -05001219 VkMemoryRequirements mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001220 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001221 int i;
1222 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001223 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001224 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001225 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001226
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001227 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001228 mat4x4_mul(MVP, VP, demo->model_matrix);
1229 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001230// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001231
1232 for (i=0; i<12*3; i++) {
1233 data.position[i][0] = g_vertex_buffer_data[i*3];
1234 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1235 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1236 data.position[i][3] = 1.0f;
1237 data.attr[i][0] = g_uv_buffer_data[2*i];
1238 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1239 data.attr[i][2] = 0;
1240 data.attr[i][3] = 0;
1241 }
1242
Chia-I Wu714df452015-01-01 07:55:04 +08001243 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001244 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001245 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop86049392015-07-16 10:29:32 -06001246 buf_info.size = sizeof(data);
Chia-I Wu69f40122015-10-26 21:10:41 +08001247 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1248 &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001249 assert(!err);
1250
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001251 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +08001252
Dominik Witczake82d5b62015-09-22 18:25:33 +02001253 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1254 demo->uniform_data.mem_alloc.pNext = NULL;
1255 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1256 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1257
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001258 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001259 mem_reqs.memoryTypeBits,
1260 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczake82d5b62015-09-22 18:25:33 +02001261 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001262 assert(pass);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001263
Chia-I Wu1f851912015-10-27 18:04:07 +08001264 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu69f40122015-10-26 21:10:41 +08001265 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski23182612015-05-29 09:32:35 -05001266 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001267
Dominik Witczake82d5b62015-09-22 18:25:33 +02001268 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001269 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001270
Mark Lobodzinski23182612015-05-29 09:32:35 -05001271 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001272
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001273 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001274
Tony Barbourde4124d2015-07-03 10:33:54 -06001275 err = vkBindBufferMemory(demo->device,
1276 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001277 demo->uniform_data.mem, 0);
1278 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001279
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001280 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1281 demo->uniform_data.buffer_info.offset = 0;
1282 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001283}
1284
Chia-I Wuf8385062015-01-04 16:27:24 +08001285static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001286{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001287 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001288 [0] = {
Chia-I Wub5689ee2015-10-31 00:31:16 +08001289 .binding = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001290 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001291 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001292 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001293 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001294 },
1295 [1] = {
Chia-I Wub5689ee2015-10-31 00:31:16 +08001296 .binding = 1,
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001297 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001298 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001299 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001300 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001301 },
1302 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001303 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001304 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001305 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001306 .bindingCount = 2,
Chia-I Wuf03cbf72015-10-31 00:31:16 +08001307 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001308 };
Tony Barbour22a30862015-04-22 09:02:32 -06001309 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001310
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001311 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001312 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001313 assert(!err);
1314
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001315 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1316 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1317 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001318 .setLayoutCount = 1,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001319 .pSetLayouts = &demo->desc_layout,
1320 };
1321
1322 err = vkCreatePipelineLayout(demo->device,
1323 &pPipelineLayoutCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +08001324 NULL,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001325 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001326 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001327}
1328
Chia-I Wu76cd4222015-07-08 13:34:24 +08001329static void demo_prepare_render_pass(struct demo *demo)
1330{
Chia-I Wuc278df82015-07-07 11:50:03 +08001331 const VkAttachmentDescription attachments[2] = {
1332 [0] = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001333 .format = demo->format,
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001334 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wuc278df82015-07-07 11:50:03 +08001335 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1336 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1337 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1338 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1339 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1340 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1341 },
1342 [1] = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001343 .format = demo->depth.format,
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001344 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wuc278df82015-07-07 11:50:03 +08001345 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1346 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1347 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1348 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1349 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1350 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1351 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001352 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001353 const VkAttachmentReference color_reference = {
1354 .attachment = 0,
1355 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1356 };
Chia-I Wuce532f72015-10-26 17:32:47 +08001357 const VkAttachmentReference depth_reference = {
1358 .attachment = 1,
1359 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1360 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001361 const VkSubpassDescription subpass = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001362 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1363 .flags = 0,
Chia-I Wu763a7492015-10-26 20:48:51 +08001364 .inputAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001365 .pInputAttachments = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001366 .colorAttachmentCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001367 .pColorAttachments = &color_reference,
1368 .pResolveAttachments = NULL,
Chia-I Wuce532f72015-10-26 17:32:47 +08001369 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu763a7492015-10-26 20:48:51 +08001370 .preserveAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001371 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001372 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001373 const VkRenderPassCreateInfo rp_info = {
1374 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1375 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001376 .attachmentCount = 2,
1377 .pAttachments = attachments,
1378 .subpassCount = 1,
1379 .pSubpasses = &subpass,
1380 .dependencyCount = 0,
1381 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001382 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001383 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001384
Chia-I Wu69f40122015-10-26 21:10:41 +08001385 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001386 assert(!err);
1387}
1388
Chia-I Wu062ad152015-10-31 00:31:16 +08001389static VkShaderModule demo_prepare_shader_module(struct demo* demo,
1390 VkShaderStageFlagBits stage,
1391 const void* code,
1392 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001393{
Chia-I Wu062ad152015-10-31 00:31:16 +08001394 VkShaderModule module;
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001395 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001396 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001397
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001398 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1399 moduleCreateInfo.pNext = NULL;
1400
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001401 moduleCreateInfo.codeSize = size;
1402 moduleCreateInfo.pCode = code;
1403 moduleCreateInfo.flags = 0;
1404 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1405 assert(!err);
Chia-I Wu062ad152015-10-31 00:31:16 +08001406
1407 return module;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001408}
1409
Cody Northropacfb0492015-03-17 15:55:58 -06001410char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001411{
1412 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001413 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001414 void *shader_code;
1415
1416 FILE *fp = fopen(filename, "rb");
1417 if (!fp) return NULL;
1418
1419 fseek(fp, 0L, SEEK_END);
1420 size = ftell(fp);
1421
1422 fseek(fp, 0L, SEEK_SET);
1423
1424 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001425 retval = fread(shader_code, size, 1, fp);
1426 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001427
1428 *psize = size;
1429
Mike Stroyan85180252015-10-28 11:15:46 -06001430 fclose(fp);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001431 return shader_code;
1432}
1433
Chia-I Wu062ad152015-10-31 00:31:16 +08001434static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001435{
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001436 void *vertShaderCode;
1437 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001438
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001439 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1440
1441 demo->vert_shader_module = demo_prepare_shader_module(demo,
Chia-I Wu062ad152015-10-31 00:31:16 +08001442 VK_SHADER_STAGE_VERTEX_BIT, vertShaderCode, size);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001443
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001444 free(vertShaderCode);
Chia-I Wu062ad152015-10-31 00:31:16 +08001445
1446 return demo->vert_shader_module;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001447}
1448
Chia-I Wu062ad152015-10-31 00:31:16 +08001449static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001450{
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001451 void *fragShaderCode;
1452 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001453
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001454 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001455
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001456 demo->frag_shader_module = demo_prepare_shader_module(demo,
Chia-I Wu062ad152015-10-31 00:31:16 +08001457 VK_SHADER_STAGE_FRAGMENT_BIT, fragShaderCode, size);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001458
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001459 free(fragShaderCode);
Chia-I Wu062ad152015-10-31 00:31:16 +08001460
1461 return demo->frag_shader_module;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001462}
1463
1464static void demo_prepare_pipeline(struct demo *demo)
1465{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001466 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001467 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand009e8652015-10-15 17:15:25 -07001468 VkPipelineVertexInputStateCreateInfo vi;
Tony Barboure307f582015-07-10 15:29:03 -06001469 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wu1f851912015-10-27 18:04:07 +08001470 VkPipelineRasterizationStateCreateInfo rs;
Tony Barboure307f582015-07-10 15:29:03 -06001471 VkPipelineColorBlendStateCreateInfo cb;
1472 VkPipelineDepthStencilStateCreateInfo ds;
1473 VkPipelineViewportStateCreateInfo vp;
1474 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wu1f851912015-10-27 18:04:07 +08001475 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniell811eb742015-09-29 13:01:09 -06001476 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbour22a30862015-04-22 09:02:32 -06001477 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001478
Piers Daniell811eb742015-09-29 13:01:09 -06001479 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1480 memset(&dynamicState, 0, sizeof dynamicState);
1481 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1482 dynamicState.pDynamicStates = dynamicStateEnables;
1483
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001484 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001485 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001486 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001487
Jason Ekstrand009e8652015-10-15 17:15:25 -07001488 memset(&vi, 0, sizeof(vi));
1489 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1490
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001491 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001492 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001493 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001494
1495 memset(&rs, 0, sizeof(rs));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001496 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1497 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wuce532f72015-10-26 17:32:47 +08001498 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wu1f851912015-10-27 18:04:07 +08001499 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001500 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001501 rs.rasterizerDiscardEnable = VK_FALSE;
1502 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001503
1504 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001505 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1506 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001507 memset(att_state, 0, sizeof(att_state));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001508 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001509 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001510 cb.attachmentCount = 1;
1511 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001512
Tony Barbourfa6cac72015-01-16 14:27:35 -07001513 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001514 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001515 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001516 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1517 vp.scissorCount = 1;
1518 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001519
1520 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001521 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001522 ds.depthTestEnable = VK_TRUE;
1523 ds.depthWriteEnable = VK_TRUE;
Chia-I Wu1f851912015-10-27 18:04:07 +08001524 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001525 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuc51b1212015-10-27 19:25:11 +08001526 ds.back.failOp = VK_STENCIL_OP_KEEP;
1527 ds.back.passOp = VK_STENCIL_OP_KEEP;
1528 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001529 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001530 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001531
Tony Barbourfa6cac72015-01-16 14:27:35 -07001532 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001533 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001534 ms.pSampleMask = NULL;
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001535 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001536
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001537 // Two stages: vs and fs
1538 pipeline.stageCount = 2;
1539 VkPipelineShaderStageCreateInfo shaderStages[2];
1540 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1541
1542 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu062ad152015-10-31 00:31:16 +08001543 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1544 shaderStages[0].module = demo_prepare_vs(demo);
1545 shaderStages[0].pName = "main";
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001546
1547 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu062ad152015-10-31 00:31:16 +08001548 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1549 shaderStages[1].module = demo_prepare_fs(demo);
1550 shaderStages[1].pName = "main";
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001551
Jon Ashburn0d60d272015-07-09 15:02:25 -06001552 memset(&pipelineCache, 0, sizeof(pipelineCache));
1553 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001554
Chia-I Wu69f40122015-10-26 21:10:41 +08001555 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001556 assert(!err);
Tony Barboure307f582015-07-10 15:29:03 -06001557
Jason Ekstrand009e8652015-10-15 17:15:25 -07001558 pipeline.pVertexInputState = &vi;
Tony Barboure307f582015-07-10 15:29:03 -06001559 pipeline.pInputAssemblyState = &ia;
Chia-I Wu1f851912015-10-27 18:04:07 +08001560 pipeline.pRasterizationState = &rs;
Tony Barboure307f582015-07-10 15:29:03 -06001561 pipeline.pColorBlendState = &cb;
1562 pipeline.pMultisampleState = &ms;
1563 pipeline.pViewportState = &vp;
1564 pipeline.pDepthStencilState = &ds;
1565 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001566 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001567 pipeline.pDynamicState = &dynamicState;
Tony Barboure307f582015-07-10 15:29:03 -06001568
Courtney Goeltzenleuchter14eb5ea2015-08-13 16:55:04 -06001569 pipeline.renderPass = demo->render_pass;
1570
Chia-I Wu69f40122015-10-26 21:10:41 +08001571 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1572 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001573 assert(!err);
1574
Chia-I Wu69f40122015-10-26 21:10:41 +08001575 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1576 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001577}
1578
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001579static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001580{
Chia-I Wuc51b1212015-10-27 19:25:11 +08001581 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001582 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001583 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001584 .descriptorCount = 1,
Chia-I Wuf8385062015-01-04 16:27:24 +08001585 },
1586 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001587 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001588 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wuf8385062015-01-04 16:27:24 +08001589 },
1590 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001591 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001592 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001593 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001594 .maxSets = 1,
Chia-I Wuc51b1212015-10-27 19:25:11 +08001595 .poolSizeCount = 2,
1596 .pPoolSizes = type_counts,
Chia-I Wuf8385062015-01-04 16:27:24 +08001597 };
Tony Barbour22a30862015-04-22 09:02:32 -06001598 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001599
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001600 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001601 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001602 assert(!err);
1603}
1604
1605static void demo_prepare_descriptor_set(struct demo *demo)
1606{
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001607 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001608 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001609 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001610 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001611
Chia-I Wu1f851912015-10-27 18:04:07 +08001612 VkDescriptorSetAllocateInfo alloc_info = {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001613 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO,
1614 .pNext = NULL,
1615 .descriptorPool = demo->desc_pool,
Chia-I Wu763a7492015-10-26 20:48:51 +08001616 .setLayoutCount = 1,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001617 .pSetLayouts = &demo->desc_layout
1618 };
Chia-I Wu1f851912015-10-27 18:04:07 +08001619 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northropc8aa4a52015-08-03 12:47:29 -06001620 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001621
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001622 memset(&tex_descs, 0, sizeof(tex_descs));
1623 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001624 tex_descs[i].sampler = demo->textures[i].sampler;
1625 tex_descs[i].imageView = demo->textures[i].view;
1626 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001627 }
1628
1629 memset(&writes, 0, sizeof(writes));
1630
1631 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu1f851912015-10-27 18:04:07 +08001632 writes[0].dstSet = demo->desc_set;
Chia-I Wu763a7492015-10-26 20:48:51 +08001633 writes[0].descriptorCount = 1;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001634 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001635 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001636
1637 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu1f851912015-10-27 18:04:07 +08001638 writes[1].dstSet = demo->desc_set;
1639 writes[1].dstBinding = 1;
Chia-I Wu763a7492015-10-26 20:48:51 +08001640 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001641 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001642 writes[1].pImageInfo = tex_descs;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001643
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001644 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001645}
1646
Chia-I Wu76cd4222015-07-08 13:34:24 +08001647static void demo_prepare_framebuffers(struct demo *demo)
1648{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001649 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001650 attachments[1] = demo->depth.view;
1651
Chia-I Wu76cd4222015-07-08 13:34:24 +08001652 const VkFramebufferCreateInfo fb_info = {
1653 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1654 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001655 .renderPass = demo->render_pass,
1656 .attachmentCount = 2,
1657 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001658 .width = demo->width,
1659 .height = demo->height,
1660 .layers = 1,
1661 };
1662 VkResult U_ASSERT_ONLY err;
1663 uint32_t i;
1664
Tony Barbour5aabff52015-10-08 14:26:24 -06001665 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1666 assert(demo->framebuffers);
1667
1668 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001669 attachments[0] = demo->buffers[i].view;
Chia-I Wu69f40122015-10-26 21:10:41 +08001670 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001671 assert(!err);
1672 }
1673}
1674
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001675static void demo_prepare(struct demo *demo)
1676{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001677 VkResult U_ASSERT_ONLY err;
1678
Chia-I Wu1f851912015-10-27 18:04:07 +08001679 const VkCommandPoolCreateInfo cmd_pool_info = {
1680 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001681 .pNext = NULL,
1682 .queueFamilyIndex = demo->graphics_queue_node_index,
1683 .flags = 0,
1684 };
Chia-I Wu69f40122015-10-26 21:10:41 +08001685 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop18ea11b2015-07-09 18:08:32 -06001686 assert(!err);
1687
Chia-I Wu1f851912015-10-27 18:04:07 +08001688 const VkCommandBufferAllocateInfo cmd = {
1689 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001690 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001691 .commandPool = demo->cmd_pool,
1692 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +08001693 .bufferCount = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001694 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001695
1696 demo_prepare_buffers(demo);
1697 demo_prepare_depth(demo);
1698 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001699 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001700
Chia-I Wuf8385062015-01-04 16:27:24 +08001701 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001702 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001703 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001704
Ian Elliott338dedb2015-08-21 15:09:33 -06001705 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08001706 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001707 assert(!err);
1708 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001709
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001710 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001711 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001712
Chia-I Wu76cd4222015-07-08 13:34:24 +08001713 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001714
Ian Elliott338dedb2015-08-21 15:09:33 -06001715 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001716 demo->current_buffer = i;
1717 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1718 }
1719
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001720 /*
1721 * Prepare functions above may generate pipeline commands
1722 * that need to be flushed before beginning the render loop.
1723 */
1724 demo_flush_init_cmd(demo);
1725
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001726 demo->current_buffer = 0;
Mike Stroyanb194be62015-10-28 09:46:57 -06001727 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001728}
1729
David Pinedoeeca2a22015-06-18 17:03:14 -06001730static void demo_cleanup(struct demo *demo)
1731{
1732 uint32_t i;
1733
1734 demo->prepared = false;
1735
Tony Barbour5aabff52015-10-08 14:26:24 -06001736 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001737 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbourde4124d2015-07-03 10:33:54 -06001738 }
Tony Barbour5aabff52015-10-08 14:26:24 -06001739 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08001740 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001741
Chia-I Wu69f40122015-10-26 21:10:41 +08001742 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1743 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1744 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1745 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1746 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001747
1748 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001749 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1750 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1751 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1752 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001753 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001754 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
David Pinedoeeca2a22015-06-18 17:03:14 -06001755
Chia-I Wu69f40122015-10-26 21:10:41 +08001756 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1757 vkDestroyImage(demo->device, demo->depth.image, NULL);
1758 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001759
Chia-I Wu69f40122015-10-26 21:10:41 +08001760 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1761 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001762
Ian Elliott338dedb2015-08-21 15:09:33 -06001763 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001764 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001765 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001766 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001767 free(demo->buffers);
David Pinedoeeca2a22015-06-18 17:03:14 -06001768
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001769 free(demo->queue_props);
1770
Chia-I Wu69f40122015-10-26 21:10:41 +08001771 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1772 vkDestroyDevice(demo->device, NULL);
Tony Barboura65ecc22015-06-30 14:14:19 -06001773 if (demo->validate) {
1774 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1775 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001776 vkDestroyInstance(demo->inst, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001777
1778#ifndef _WIN32
1779 xcb_destroy_window(demo->connection, demo->window);
1780 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001781 free(demo->atom_wm_delete_window);
David Pinedoeeca2a22015-06-18 17:03:14 -06001782#endif // _WIN32
1783}
1784
Ian Elliotte2688a52015-10-16 18:02:43 -06001785static void demo_resize(struct demo *demo)
1786{
1787 uint32_t i;
1788
Mike Stroyanb194be62015-10-28 09:46:57 -06001789 // Don't react to resize until after first initialization.
1790 if (!demo->prepared) {
1791 return;
1792 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001793 // In order to properly resize the window, we must re-create the swapchain
1794 // AND redo the command buffers, etc.
1795 //
1796 // First, perform part of the demo_cleanup() function:
1797 demo->prepared = false;
1798
1799 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001800 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001801 }
1802 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08001803 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001804
Chia-I Wu69f40122015-10-26 21:10:41 +08001805 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1806 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1807 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1808 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1809 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001810
1811 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001812 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1813 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1814 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1815 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001816 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001817
Chia-I Wu69f40122015-10-26 21:10:41 +08001818 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1819 vkDestroyImage(demo->device, demo->depth.image, NULL);
1820 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001821
Chia-I Wu69f40122015-10-26 21:10:41 +08001822 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1823 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001824
1825 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001826 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott74bb2eb2015-10-27 11:06:33 -06001827 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliotte2688a52015-10-16 18:02:43 -06001828 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001829 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001830 free(demo->buffers);
1831
Chia-I Wu69f40122015-10-26 21:10:41 +08001832 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliott74bb2eb2015-10-27 11:06:33 -06001833
Ian Elliotte2688a52015-10-16 18:02:43 -06001834
1835 // Second, re-perform the demo_prepare() function, which will re-create the
1836 // swapchain:
1837 demo_prepare(demo);
1838}
1839
David Pinedoeeca2a22015-06-18 17:03:14 -06001840// On MS-Windows, make this a global, so it's available to WndProc()
1841struct demo demo;
1842
Ian Elliotte14e9f92015-04-16 15:23:05 -06001843#ifdef _WIN32
1844static void demo_run(struct demo *demo)
1845{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001846 if (!demo->prepared)
1847 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001848 // Wait for work to finish before updating MVP.
1849 vkDeviceWaitIdle(demo->device);
1850 demo_update_data_buffer(demo);
1851
1852 demo_draw(demo);
1853
1854 // Wait for work to finish before updating MVP.
1855 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001856
David Pinedoeeca2a22015-06-18 17:03:14 -06001857 demo->curFrame++;
1858
1859 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1860 {
1861 demo->quit=true;
1862 demo_cleanup(demo);
1863 ExitProcess(0);
1864 }
1865
1866}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001867
1868// MS-Windows event handling function:
1869LRESULT CALLBACK WndProc(HWND hWnd,
1870 UINT uMsg,
1871 WPARAM wParam,
1872 LPARAM lParam)
1873{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001874 switch(uMsg)
1875 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001876 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001877 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001878 break;
Ian Elliotte36b2082015-07-06 14:27:58 -06001879 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001880 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001881 break;
Ian Elliottf3f80822015-10-21 15:14:07 -06001882 case WM_SIZE:
Mike Stroyanb194be62015-10-28 09:46:57 -06001883 demo.width = lParam & 0xffff;
1884 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliottf3f80822015-10-21 15:14:07 -06001885 demo_resize(&demo);
1886 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001887 default:
1888 break;
1889 }
1890 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1891}
1892
1893static void demo_create_window(struct demo *demo)
1894{
1895 WNDCLASSEX win_class;
1896
1897 // Initialize the window class structure:
1898 win_class.cbSize = sizeof(WNDCLASSEX);
1899 win_class.style = CS_HREDRAW | CS_VREDRAW;
1900 win_class.lpfnWndProc = WndProc;
1901 win_class.cbClsExtra = 0;
1902 win_class.cbWndExtra = 0;
1903 win_class.hInstance = demo->connection; // hInstance
1904 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1905 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1906 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1907 win_class.lpszMenuName = NULL;
1908 win_class.lpszClassName = demo->name;
1909 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1910 // Register window class:
1911 if (!RegisterClassEx(&win_class)) {
1912 // It didn't work, so try to give a useful error:
1913 printf("Unexpected error trying to start the application!\n");
1914 fflush(stdout);
1915 exit(1);
1916 }
1917 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001918 RECT wr = { 0, 0, demo->width, demo->height };
1919 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001920 demo->window = CreateWindowEx(0,
1921 demo->name, // class name
1922 demo->name, // app name
1923 WS_OVERLAPPEDWINDOW | // window style
1924 WS_VISIBLE |
1925 WS_SYSMENU,
1926 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06001927 wr.right-wr.left, // width
1928 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001929 NULL, // handle to parent
1930 NULL, // handle to menu
1931 demo->connection, // hInstance
1932 NULL); // no extra parameters
1933 if (!demo->window) {
1934 // It didn't work, so try to give a useful error:
1935 printf("Cannot create a window in which to draw!\n");
1936 fflush(stdout);
1937 exit(1);
1938 }
1939}
1940#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001941static void demo_handle_event(struct demo *demo,
1942 const xcb_generic_event_t *event)
1943{
Piers Daniell886be472015-02-23 16:23:13 -07001944 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001945 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001946 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001947 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001948 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001949 case XCB_CLIENT_MESSAGE:
1950 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1951 (*demo->atom_wm_delete_window).atom) {
1952 demo->quit = true;
1953 }
1954 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001955 case XCB_KEY_RELEASE:
1956 {
1957 const xcb_key_release_event_t *key =
1958 (const xcb_key_release_event_t *) event;
1959
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001960 switch (key->detail) {
1961 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001962 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001963 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001964 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001965 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001966 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001967 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001968 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001969 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001970 case 0x41:
1971 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001972 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001973 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001974 }
1975 break;
Ian Elliotte2688a52015-10-16 18:02:43 -06001976 case XCB_CONFIGURE_NOTIFY:
1977 {
1978 const xcb_configure_notify_event_t *cfg =
1979 (const xcb_configure_notify_event_t *) event;
1980 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
1981 demo_resize(demo);
1982 }
1983 }
1984 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001985 default:
1986 break;
1987 }
1988}
1989
1990static void demo_run(struct demo *demo)
1991{
1992 xcb_flush(demo->connection);
1993
1994 while (!demo->quit) {
1995 xcb_generic_event_t *event;
1996
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001997 if (demo->pause) {
1998 event = xcb_wait_for_event(demo->connection);
1999 } else {
2000 event = xcb_poll_for_event(demo->connection);
2001 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002002 if (event) {
2003 demo_handle_event(demo, event);
2004 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002005 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002006
2007 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002008 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002009 demo_update_data_buffer(demo);
2010
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002011 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07002012
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002013 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002014 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06002015 demo->curFrame++;
Tony Barboura26fb7d2015-09-21 15:17:33 -06002016 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedoeeca2a22015-06-18 17:03:14 -06002017 demo->quit = true;
2018
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002019 }
2020}
2021
2022static void demo_create_window(struct demo *demo)
2023{
2024 uint32_t value_mask, value_list[32];
2025
2026 demo->window = xcb_generate_id(demo->connection);
2027
2028 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2029 value_list[0] = demo->screen->black_pixel;
2030 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliotte2688a52015-10-16 18:02:43 -06002031 XCB_EVENT_MASK_EXPOSURE |
2032 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002033
2034 xcb_create_window(demo->connection,
2035 XCB_COPY_FROM_PARENT,
2036 demo->window, demo->screen->root,
2037 0, 0, demo->width, demo->height, 0,
2038 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2039 demo->screen->root_visual,
2040 value_mask, value_list);
2041
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002042 /* Magic code that will send notification when window is destroyed */
2043 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2044 "WM_PROTOCOLS");
2045 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2046
2047 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2048 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2049
2050 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2051 demo->window, (*reply).atom, 4, 32, 1,
2052 &(*demo->atom_wm_delete_window).atom);
2053 free(reply);
2054
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002055 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06002056
2057 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2058 const uint32_t coords[] = {100, 100};
2059 xcb_configure_window(demo->connection, demo->window,
2060 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002061}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002062#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002063
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002064/*
2065 * Return 1 (true) if all layer names specified in check_names
2066 * can be found in given layer properties.
2067 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002068static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002069 uint32_t layer_count, VkLayerProperties *layers)
2070{
2071 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002072 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002073 for (uint32_t j = 0; j < layer_count; j++) {
2074 if (!strcmp(check_names[i], layers[j].layerName)) {
2075 found = 1;
2076 }
2077 }
2078 if (!found) {
2079 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2080 return 0;
2081 }
2082 }
2083 return 1;
2084}
2085
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002086static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002087{
Tobin Ehlis3536b442015-04-16 18:04:57 -06002088 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002089 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002090 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06002091 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002092 VkLayerProperties *instance_layers;
2093 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002094 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002095 uint32_t instance_layer_count = 0;
2096 uint32_t enabled_extension_count = 0;
2097 uint32_t enabled_layer_count = 0;
2098
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002099 char *instance_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002100 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002101 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002102 "ObjectTracker",
2103 "DrawState",
2104 "ParamChecker",
2105 "ShaderChecker",
Ian Elliott329da012015-09-22 10:51:24 -06002106 "Swapchain",
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002107 "DeviceLimits",
2108 "Image",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002109 };
2110
2111 char *device_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002112 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002113 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002114 "ObjectTracker",
2115 "DrawState",
2116 "ParamChecker",
2117 "ShaderChecker",
Ian Elliott329da012015-09-22 10:51:24 -06002118 "Swapchain",
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002119 "DeviceLimits",
2120 "Image",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002121 };
2122
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002123 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002124 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002125 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002126 assert(!err);
2127
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002128 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002129 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002130 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002131
2132 if (demo->validate) {
2133 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2134 instance_layer_count, instance_layers);
2135 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002136 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002137 "required validation layer.\n\n"
2138 "Please look at the Getting Started guide for additional "
2139 "information.\n",
2140 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002141 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002142 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002143 }
2144
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002145 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002146 assert(!err);
2147
Tony Barbourd9955e42015-10-08 13:59:42 -06002148 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002149 memset(extension_names, 0, sizeof(extension_names));
2150 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002151 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002152 assert(!err);
2153 for (uint32_t i = 0; i < instance_extension_count; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08002154 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extensionName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06002155 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06002156 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002157 }
Chia-I Wu1f851912015-10-27 18:04:07 +08002158 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002159 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002160 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002161 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002162 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002163 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002164 }
Tony Barbourd9955e42015-10-08 13:59:42 -06002165 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002166 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06002167 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002168 "Vulkan installable client driver (ICD) installed?\nPlease "
2169 "look at the Getting Started guide for additional "
2170 "information.\n",
2171 "vkCreateInstance Failure");
2172 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002173 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002174 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002175 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08002176 .pApplicationName = APP_SHORT_NAME,
2177 .applicationVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002178 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002179 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002180 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002181 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002182 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002183 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002184 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08002185 .pApplicationInfo = &app,
Chia-I Wu763a7492015-10-26 20:48:51 +08002186 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002187 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu763a7492015-10-26 20:48:51 +08002188 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002189 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002190 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002191
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002192 uint32_t gpu_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002193
Chia-I Wu69f40122015-10-26 21:10:41 +08002194 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002195 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2196 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002197 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002198 "additional information.\n",
2199 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06002200 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002201 ERR_EXIT("Cannot find a specified extension library"
2202 ".\nMake sure your layers path is set appropriately\n",
2203 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002204 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002205 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2206 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002207 "the Getting Started guide for additional information.\n",
2208 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002209 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002210
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002211 free(instance_layers);
2212 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002213
Tobin Ehlis4f482a72015-09-07 15:16:39 -06002214 /* Make initial call to query gpu_count, then second call for gpu info*/
2215 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2216 assert(!err && gpu_count > 0);
2217 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2218 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2219 assert(!err);
2220 /* For cube demo we just grab the first physical device */
2221 demo->gpu = physical_devices[0];
2222 free(physical_devices);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002223
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002224 /* Look for validation layers */
2225 validation_found = 0;
2226 enabled_layer_count = 0;
2227 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002228 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002229 assert(!err);
2230
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002231 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002232 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002233 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002234
2235 if (demo->validate) {
2236 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2237 device_layer_count, device_layers);
2238 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002239 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002240 "a required validation layer.\n\n"
2241 "Please look at the Getting Started guide for additional "
2242 "information.\n",
2243 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002244 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002245 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002246 }
2247
2248 uint32_t device_extension_count = 0;
2249 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002250 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002251 demo->gpu, NULL, &device_extension_count, NULL);
2252 assert(!err);
2253
Tony Barbourd9955e42015-10-08 13:59:42 -06002254 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002255 enabled_extension_count = 0;
2256 memset(extension_names, 0, sizeof(extension_names));
2257 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002258 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002259 demo->gpu, NULL, &device_extension_count, device_extensions);
2260 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06002261
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002262 for (uint32_t i = 0; i < device_extension_count; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08002263 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extensionName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06002264 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06002265 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002266 }
2267 assert(enabled_extension_count < 64);
2268 }
Tony Barbourd9955e42015-10-08 13:59:42 -06002269 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002270 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06002271 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002272 "Vulkan installable client driver (ICD) installed?\nPlease "
2273 "look at the Getting Started guide for additional "
2274 "information.\n",
2275 "vkCreateInstance Failure");
2276 }
2277
Tony Barbour5685ad72015-04-29 16:19:20 -06002278 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06002279 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2280 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002281 if (!demo->dbgCreateMsgCallback) {
2282 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2283 "vkGetProcAddr Failure");
2284 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002285 if (!demo->dbgDestroyMsgCallback) {
2286 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2287 "vkGetProcAddr Failure");
2288 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002289 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2290 if (!demo->dbgBreakCallback) {
2291 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2292 "vkGetProcAddr Failure");
2293 }
2294
2295 PFN_vkDbgMsgCallback callback;
2296
2297 if (!demo->use_break) {
2298 callback = dbgFunc;
2299 } else {
2300 callback = demo->dbgBreakCallback;
2301 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002302 err = demo->dbgCreateMsgCallback(
2303 demo->inst,
2304 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002305 callback, NULL,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002306 &demo->msg_callback);
2307 switch (err) {
2308 case VK_SUCCESS:
2309 break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002310 case VK_ERROR_OUT_OF_HOST_MEMORY:
2311 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2312 "dbgCreateMsgCallback Failure");
2313 break;
2314 default:
2315 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2316 "dbgCreateMsgCallback Failure");
2317 break;
2318 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002319 }
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002320 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002321
2322 /* Call with NULL data to get count */
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002323 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002324 assert(demo->queue_count >= 1);
2325
2326 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002327 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002328 // Find a queue that supports gfx
2329 uint32_t gfx_queue_idx = 0;
2330 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2331 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2332 break;
2333 }
2334 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlis795460a2015-09-24 15:18:22 -06002335 // Query fine-grained feature support for this device.
2336 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis6454cd92015-09-29 11:22:37 -06002337 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002338 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlis795460a2015-09-24 15:18:22 -06002339
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06002340 float queue_priorities[1] = { 0.0 };
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002341 const VkDeviceQueueCreateInfo queue = {
2342 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2343 .pNext = NULL,
2344 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu763a7492015-10-26 20:48:51 +08002345 .queuePriorityCount = 1,
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06002346 .pQueuePriorities = queue_priorities
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002347 };
2348
2349 VkDeviceCreateInfo device = {
2350 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2351 .pNext = NULL,
Courtney Goeltzenleuchterdfd53f52015-10-15 16:58:44 -06002352 .requestedQueueCount = 1,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002353 .pRequestedQueues = &queue,
Chia-I Wu763a7492015-10-26 20:48:51 +08002354 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002355 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu763a7492015-10-26 20:48:51 +08002356 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002357 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlis795460a2015-09-24 15:18:22 -06002358 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002359 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002360
Chia-I Wu69f40122015-10-26 21:10:41 +08002361 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002362 assert(!err);
2363
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002364 free(device_layers);
2365
Ian Elliott338dedb2015-08-21 15:09:33 -06002366 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2367 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2368 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2369 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2370 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliott338dedb2015-08-21 15:09:33 -06002371 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2372 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2373 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2374 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002375}
2376
Tony Barbourd9955e42015-10-08 13:59:42 -06002377static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002378{
2379 VkResult err;
2380 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002381
Tony Barbourd9955e42015-10-08 13:59:42 -06002382 // Construct the surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06002383 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002384 demo->surface_description.pNext = NULL;
2385#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06002386 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002387 demo->surface_description.pPlatformHandle = demo->connection;
2388 demo->surface_description.pPlatformWindow = demo->window;
2389#else // _WIN32
2390 demo->platform_handle_xcb.connection = demo->connection;
2391 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06002392 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002393 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2394 demo->surface_description.pPlatformWindow = &demo->window;
2395#endif // _WIN32
2396
Tony Barbourd9955e42015-10-08 13:59:42 -06002397 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002398 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2399 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06002400 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2401 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06002402 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002403 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002404
2405 // Search for a graphics and a present queue in the array of queue
2406 // families, try to find one that supports both
2407 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2408 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002409 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002410 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2411 if (graphicsQueueNodeIndex == UINT32_MAX) {
2412 graphicsQueueNodeIndex = i;
2413 }
2414
2415 if (supportsPresent[i] == VK_TRUE) {
2416 graphicsQueueNodeIndex = i;
2417 presentQueueNodeIndex = i;
2418 break;
2419 }
2420 }
2421 }
2422 if (presentQueueNodeIndex == UINT32_MAX) {
2423 // If didn't find a queue that supports both graphics and present, then
2424 // find a separate present queue.
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002425 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002426 if (supportsPresent[i] == VK_TRUE) {
2427 presentQueueNodeIndex = i;
2428 break;
2429 }
2430 }
2431 }
2432 free(supportsPresent);
2433
2434 // Generate error if could not find both a graphics and a present queue
2435 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2436 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002437 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002438 }
2439
2440 // TODO: Add support for separate queues, including presentation,
2441 // synchronization, and appropriate tracking for QueueSubmit
2442 // While it is possible for an application to use a separate graphics and a
2443 // present queues, this demo program assumes it is only using one:
2444 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2445 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002446 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002447 }
2448
2449 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002450
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002451 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002452 0, &demo->queue);
Tony Barbour5685ad72015-04-29 16:19:20 -06002453
Ian Elliotte36b2082015-07-06 14:27:58 -06002454 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002455 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002456 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2457 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002458 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002459 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002460 VkSurfaceFormatKHR *surfFormats =
2461 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2462 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2463 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002464 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002465 assert(!err);
2466 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2467 // the surface has no preferred format. Otherwise, at least one
2468 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002469 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2470 {
2471 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2472 }
2473 else
2474 {
2475 assert(formatCount >= 1);
2476 demo->format = surfFormats[0].format;
2477 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002478 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002479
David Pinedoeeca2a22015-06-18 17:03:14 -06002480 demo->quit = false;
2481 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002482
2483 // Get Memory information and properties
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002484 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002485}
2486
2487static void demo_init_connection(struct demo *demo)
2488{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002489#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002490 const xcb_setup_t *setup;
2491 xcb_screen_iterator_t iter;
2492 int scr;
2493
2494 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002495 if (demo->connection == NULL) {
2496 printf("Cannot find a compatible Vulkan installable client driver "
2497 "(ICD).\nExiting ...\n");
2498 fflush(stdout);
2499 exit(1);
2500 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002501
2502 setup = xcb_get_setup(demo->connection);
2503 iter = xcb_setup_roots_iterator(setup);
2504 while (scr-- > 0)
2505 xcb_screen_next(&iter);
2506
2507 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002508#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002509}
2510
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002511static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002512{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002513 vec3 eye = {0.0f, 3.0f, 5.0f};
2514 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002515 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002516
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002517 memset(demo, 0, sizeof(*demo));
Tony Barboura26fb7d2015-09-21 15:17:33 -06002518 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002519
Piers Daniell886be472015-02-23 16:23:13 -07002520 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002521 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002522 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002523 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002524 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002525 if (strcmp(argv[i], "--break") == 0) {
2526 demo->use_break = true;
2527 continue;
2528 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002529 if (strcmp(argv[i], "--validate") == 0) {
2530 demo->validate = true;
2531 continue;
2532 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002533 if (strcmp(argv[i], "--c") == 0 &&
Tony Barboura26fb7d2015-09-21 15:17:33 -06002534 demo->frameCount == INT32_MAX &&
David Pinedoeeca2a22015-06-18 17:03:14 -06002535 i < argc-1 &&
2536 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2537 demo->frameCount >= 0)
2538 {
2539 i++;
2540 continue;
2541 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002542
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002543 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002544 fflush(stderr);
2545 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002546 }
2547
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002548 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002549 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002550
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002551 demo->width = 500;
2552 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002553
2554 demo->spin_angle = 0.01f;
2555 demo->spin_increment = 0.01f;
2556 demo->pause = false;
2557
Piers Daniell886be472015-02-23 16:23:13 -07002558 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002559 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2560 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002561}
2562
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002563
Ian Elliotte14e9f92015-04-16 15:23:05 -06002564#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002565extern int __getmainargs(
2566 int * _Argc,
2567 char *** _Argv,
2568 char *** _Env,
2569 int _DoWildCard,
2570 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002571
Ian Elliott421107f2015-04-28 15:50:36 -06002572int WINAPI WinMain(HINSTANCE hInstance,
2573 HINSTANCE hPrevInstance,
2574 LPSTR pCmdLine,
2575 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002576{
2577 MSG msg; // message
2578 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002579 int argc;
2580 char** argv;
2581 char** env;
2582 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002583
Tony Barbour5685ad72015-04-29 16:19:20 -06002584 __getmainargs(&argc,&argv,&env,0,&new_mode);
2585
2586 demo_init(&demo, argc, argv);
2587 demo.connection = hInstance;
2588 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002589 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002590 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002591
2592 demo_prepare(&demo);
2593
2594 done = false; //initialize loop condition variable
2595 /* main message loop*/
2596 while(!done)
2597 {
Ian Elliott421107f2015-04-28 15:50:36 -06002598 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002599 if (msg.message == WM_QUIT) //check for a quit message
2600 {
2601 done = true; //if found, quit app
2602 }
2603 else
2604 {
2605 /* Translate and dispatch to event queue*/
2606 TranslateMessage(&msg);
2607 DispatchMessage(&msg);
2608 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002609 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002610 }
2611
2612 demo_cleanup(&demo);
2613
Tony Barboura938abb2015-04-22 11:36:22 -06002614 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002615}
2616#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002617int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002618{
2619 struct demo demo;
2620
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002621 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002622 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002623 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002624
2625 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002626 demo_run(&demo);
2627
2628 demo_cleanup(&demo);
2629
2630 return 0;
2631}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002632#endif // _WIN32