blob: 30dc9fdbf2c62f3259aa4d94a5311348ccbea6ff [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Ian Elliotta0781972015-08-21 15:09:33 -060040#include <vk_ext_khr_swapchain.h>
41#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060042#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060043
Mark Lobodzinskif871f772015-09-01 09:00:16 -060044#include "vk_sdk_platform.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045#include "linmath.h"
46
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060047#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060048#define APP_SHORT_NAME "cube"
49#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060051#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
52
Tony Barbourfdc2d352015-04-22 09:02:32 -060053#if defined(NDEBUG) && defined(__GNUC__)
54#define U_ASSERT_ONLY __attribute__((unused))
55#else
56#define U_ASSERT_ONLY
57#endif
58
Ian Elliott07264132015-04-28 11:35:02 -060059#ifdef _WIN32
60#define ERR_EXIT(err_msg, err_class) \
61 do { \
62 MessageBox(NULL, err_msg, err_class, MB_OK); \
63 exit(1); \
64 } while (0)
65
Ian Elliott07264132015-04-28 11:35:02 -060066#else // _WIN32
67
68#define ERR_EXIT(err_msg, err_class) \
69 do { \
70 printf(err_msg); \
71 fflush(stdout); \
72 exit(1); \
73 } while (0)
74#endif // _WIN32
75
Ian Elliottaaae5352015-07-06 14:27:58 -060076#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
77{ \
78 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
79 if (demo->fp##entrypoint == NULL) { \
80 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
81 "vkGetInstanceProcAddr Failure"); \
82 } \
83}
84
Jon Ashburn7d8342c2015-10-08 15:58:23 -060085static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
86
Ian Elliott673898b2015-06-22 15:07:49 -060087#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
88{ \
Jon Ashburn7d8342c2015-10-08 15:58:23 -060089 if(!g_gdpa) \
90 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
91 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060092 if (demo->fp##entrypoint == NULL) { \
93 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
94 "vkGetDeviceProcAddr Failure"); \
95 } \
96}
97
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060098/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070099 * structure to track all objects related to a texture.
100 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600101struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600102 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700103
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600104 VkImage image;
105 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600106
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800107 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500108 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600109 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700110 int32_t tex_width, tex_height;
111};
112
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600113static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600114 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600115};
116
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600117struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600118 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600119 float mvp[4][4];
120 float position[12*3][4];
121 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600122};
123
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600124struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600125 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600126 float mvp[4][4];
127 float position[12*3][4];
128 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600130
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600131//--------------------------------------------------------------------------------------
132// Mesh and VertexFormat Data
133//--------------------------------------------------------------------------------------
134struct Vertex
135{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600136 float posX, posY, posZ, posW; // Position data
137 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600138};
139
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600140struct VertexPosTex
141{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600142 float posX, posY, posZ, posW; // Position data
143 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600144};
145
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600147#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600148
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600149static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800150 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600151 -1.0f,-1.0f, 1.0f,
152 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800153 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 -1.0f, 1.0f,-1.0f,
155 -1.0f,-1.0f,-1.0f,
156
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800157 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 1.0f, 1.0f,-1.0f,
159 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600162 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800164 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600166 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600169 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800171 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 -1.0f, 1.0f, 1.0f,
173 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600176 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800178 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 1.0f, 1.0f, 1.0f,
180 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 1.0f,-1.0f,-1.0f,
183 1.0f, 1.0f,-1.0f,
184
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600186 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600187 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 1.0f,-1.0f, 1.0f,
190 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600191};
192
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600193static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800194 0.0f, 0.0f, // -X side
195 1.0f, 0.0f,
196 1.0f, 1.0f,
197 1.0f, 1.0f,
198 0.0f, 1.0f,
199 0.0f, 0.0f,
200
201 1.0f, 0.0f, // -Z side
202 0.0f, 1.0f,
203 0.0f, 0.0f,
204 1.0f, 0.0f,
205 1.0f, 1.0f,
206 0.0f, 1.0f,
207
208 1.0f, 1.0f, // -Y side
209 1.0f, 0.0f,
210 0.0f, 0.0f,
211 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212 0.0f, 0.0f,
213 0.0f, 1.0f,
214
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800215 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600216 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800217 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600218 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600220 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 1.0f, 1.0f, // +X side
223 0.0f, 1.0f,
224 0.0f, 0.0f,
225 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600227 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800229 0.0f, 1.0f, // +Z side
230 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600232 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600235};
236
237void dumpMatrix(const char *note, mat4x4 MVP)
238{
239 int i;
240
241 printf("%s: \n", note);
242 for (i=0; i<4; i++) {
243 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
244 }
245 printf("\n");
246 fflush(stdout);
247}
248
249void dumpVec4(const char *note, vec4 vector)
250{
251 printf("%s: \n", note);
252 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
253 printf("\n");
254 fflush(stdout);
255}
256
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600257VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600258 VkFlags msgFlags,
259 VkDbgObjectType objType,
260 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600261 size_t location,
262 int32_t msgCode,
263 const char* pLayerPrefix,
264 const char* pMsg,
265 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600266{
267 char *message = (char *) malloc(strlen(pMsg)+100);
268
269 assert (message);
270
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600271 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
272 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
273 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600274 // We know that we're submitting queues without fences, ignore this warning
275 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600276 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600277 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600278 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600279 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600280 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600281 }
282
283#ifdef _WIN32
284 MessageBox(NULL, message, "Alert", MB_OK);
285#else
286 printf("%s\n",message);
287 fflush(stdout);
288#endif
289 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600290
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600291 /*
292 * false indicates that layer should not bail-out of an
293 * API call that had validation failures. This may mean that the
294 * app dies inside the driver due to invalid parameter(s).
295 * That's what would happen without validation layers, so we'll
296 * keep that behavior here.
297 */
298 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600299}
300
Ian Elliotta0781972015-08-21 15:09:33 -0600301typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600302 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800303 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600304 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600305} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600306
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600307struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600308#ifdef _WIN32
309#define APP_NAME_STR_LEN 80
310 HINSTANCE connection; // hInstance - Windows Instance
311 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
312 HWND window; // hWnd - window handle
313#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600314 xcb_connection_t *connection;
315 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800316 xcb_window_t window;
317 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotta0781972015-08-21 15:09:33 -0600318 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600319#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600320 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700321 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600323 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600324 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600325 VkDevice device;
326 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700327 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600328 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600329 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600330 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331
332 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600333 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600334 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
Ian Elliotta0781972015-08-21 15:09:33 -0600336 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
337 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
338 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
339 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
340 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
341 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
342 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
343 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
344 PFN_vkQueuePresentKHR fpQueuePresentKHR;
345 VkSurfaceDescriptionWindowKHR surface_description;
346 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600347 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600348 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800350 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
352 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600353 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600355 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800356 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500357 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600358 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600359 } depth;
360
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600361 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600362
363 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800365 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500366 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600367 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600368 } uniform_data;
369
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800370 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500371 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600372 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600373 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800374 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600377 mat4x4 projection_matrix;
378 mat4x4 view_matrix;
379 mat4x4 model_matrix;
380
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600381 float spin_angle;
382 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600383 bool pause;
384
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600385 VkShaderModule vert_shader_module;
386 VkShaderModule frag_shader_module;
387
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 VkDescriptorPool desc_pool;
389 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800390
Tony Barbourd8e504f2015-10-08 14:26:24 -0600391 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800392
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600393 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600394 int32_t curFrame;
395 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600396 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600397 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600398 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600399 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600400 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600401 VkDbgMsgCallback msg_callback;
402
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600403 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600404 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600405};
406
Ian Elliottcf074d32015-10-16 18:02:43 -0600407// Forward declaration:
408static void demo_resize(struct demo *demo);
409
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600410static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600411{
412 // Search memtypes to find first index with those properties
413 for (uint32_t i = 0; i < 32; i++) {
414 if ((typeBits & 1) == 1) {
415 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600416 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600417 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600418 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600419 }
420 }
421 typeBits >>= 1;
422 }
423 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600424 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600425}
426
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427static void demo_flush_init_cmd(struct demo *demo)
428{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600429 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432 return;
433
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600434 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435 assert(!err);
436
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800437 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800438 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600439 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800440 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
441 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800442 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600443 .pWaitSemaphores = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800444 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600445 .pCommandBuffers = cmd_bufs,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800446 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600447 .pSignalSemaphores = NULL
448 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600449
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600450 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451 assert(!err);
452
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 assert(!err);
455
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600456 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458}
459
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600460static void demo_set_image_layout(
461 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600462 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600463 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600464 VkImageLayout old_image_layout,
465 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600466{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600467 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800470 const VkCommandBufferAllocateInfo cmd = {
471 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800473 .commandPool = demo->cmd_pool,
474 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800475 .bufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476 };
477
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800478 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479 assert(!err);
480
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800481 VkCommandBufferBeginInfo cmd_buf_info = {
482 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600483 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600484 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800485 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600486 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800487 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600490 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491 }
492
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600493 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800496 .srcAccessMask = 0,
497 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 .oldLayout = old_image_layout,
499 .newLayout = new_image_layout,
500 .image = image,
Cody Northrop0e951672015-09-14 13:48:12 -0600501 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 };
503
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800504 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800506 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600507 }
508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600509 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510 /* Make sure any Copy or CPU writes to image are flushed */
Chia-I Wu19574622015-10-27 19:54:37 +0800511 image_memory_barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600512 }
513
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600514 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515
Tony Barbour50bbca42015-06-29 16:20:35 -0600516 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
517 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518
Chia-I Wu86365072015-10-26 17:08:33 +0800519 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600520}
521
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800522static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523{
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800524 const VkCommandBufferBeginInfo cmd_buf_info = {
525 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600526 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600527 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800528 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600529 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800530 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700531 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800532 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600533 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
534 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800535 };
536 const VkRenderPassBeginInfo rp_begin = {
537 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
538 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800539 .renderPass = demo->render_pass,
540 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800541 .renderArea.offset.x = 0,
542 .renderArea.offset.y = 0,
543 .renderArea.extent.width = demo->width,
544 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600545 .clearValueCount = 2,
546 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700547 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800548 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600550 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600551 assert(!err);
552
Chia-I Wuf051d262015-10-27 19:25:11 +0800553 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800554
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600555 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600556 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600557 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600558 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600560 VkViewport viewport;
561 memset(&viewport, 0, sizeof(viewport));
562 viewport.height = (float) demo->height;
563 viewport.width = (float) demo->width;
564 viewport.minDepth = (float) 0.0f;
565 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600566 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600567
568 VkRect2D scissor;
569 memset(&scissor, 0, sizeof(scissor));
570 scissor.extent.width = demo->width;
571 scissor.extent.height = demo->height;
572 scissor.offset.x = 0;
573 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600574 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600575
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600576 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800577 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578
Tony Barbour15a4ef62015-10-21 10:14:48 -0600579 VkImageMemoryBarrier prePresentBarrier = {
580 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
581 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800582 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
583 .dstAccessMask = 0,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600584 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
585 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
586 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800587 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600588 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
589 };
590
591 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
592 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wu082a6202015-10-31 00:31:16 +0800593 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu86365072015-10-26 17:08:33 +0800594 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600595
596
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600597 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600598 assert(!err);
599}
600
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600601
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600602void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600604 mat4x4 MVP, Model, VP;
605 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600606 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600607 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600608
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600609 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600611 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600612 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700613 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600614 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600615
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200616 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600617 assert(!err);
618
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600619 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600620
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600621 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622}
623
624static void demo_draw(struct demo *demo)
625{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600626 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600627 VkSemaphore presentCompleteSemaphore;
628 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
629 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
630 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600631 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600632 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800633 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600634
Ian Elliottaaae5352015-07-06 14:27:58 -0600635 err = vkCreateSemaphore(demo->device,
636 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +0800637 NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600638 &presentCompleteSemaphore);
639 assert(!err);
640
641 // Get the index of the next available swapchain image:
Ian Elliottcf074d32015-10-16 18:02:43 -0600642 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600643 UINT64_MAX,
644 presentCompleteSemaphore,
645 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600646 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
647 // demo->swapchain is out of date (e.g. the window was resized) and
648 // must be recreated:
649 demo_resize(demo);
650 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800651 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600652 return;
653 } else if (err == VK_SUBOPTIMAL_KHR) {
654 // demo->swapchain is not as optimal as it could be, but the platform's
655 // presentation engine will still present the image correctly.
656 } else {
657 assert(!err);
658 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600659
Tony Barbour15a4ef62015-10-21 10:14:48 -0600660 // Assume the command buffer has been run on current_buffer before so
661 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
662 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
663 VK_IMAGE_ASPECT_COLOR_BIT,
664 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
665 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
666 demo_flush_init_cmd(demo);
667
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600668 // Wait for the present complete semaphore to be signaled to ensure
669 // that the image won't be rendered to until the presentation
670 // engine has fully released ownership to the application, and it is
671 // okay to render to the image.
672
Ian Elliotta0781972015-08-21 15:09:33 -0600673// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600674 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800675 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
676 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800677 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600678 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800679 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600680 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800681 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600682 .pSignalSemaphores = NULL
683 };
684
685 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686 assert(!err);
687
Ian Elliotta0781972015-08-21 15:09:33 -0600688 VkPresentInfoKHR present = {
689 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600690 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600691 .swapchainCount = 1,
Ian Elliottcf074d32015-10-16 18:02:43 -0600692 .swapchains = &demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600693 .imageIndices = &demo->current_buffer,
694 };
695
696// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600697 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600698 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
699 // demo->swapchain is out of date (e.g. the window was resized) and
700 // must be recreated:
701 demo_resize(demo);
702 } else if (err == VK_SUBOPTIMAL_KHR) {
703 // demo->swapchain is not as optimal as it could be, but the platform's
704 // presentation engine will still present the image correctly.
705 } else {
706 assert(!err);
707 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600708
Chia-I Wucbb564e2015-04-16 22:02:10 +0800709 err = vkQueueWaitIdle(demo->queue);
710 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600711
Chia-I Wu63636062015-10-26 21:10:41 +0800712 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600713}
714
715static void demo_prepare_buffers(struct demo *demo)
716{
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600718 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600719
720 // Check the surface properties and formats
Ian Elliotta0781972015-08-21 15:09:33 -0600721 VkSurfacePropertiesKHR surfProperties;
722 err = demo->fpGetSurfacePropertiesKHR(demo->device,
723 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600724 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600725 assert(!err);
726
Ian Elliott8528eff2015-08-07 15:56:59 -0600727 uint32_t presentModeCount;
Ian Elliotta0781972015-08-21 15:09:33 -0600728 err = demo->fpGetSurfacePresentModesKHR(demo->device,
729 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600730 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600731 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600732 VkPresentModeKHR *presentModes =
733 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600734 assert(presentModes);
Ian Elliotta0781972015-08-21 15:09:33 -0600735 err = demo->fpGetSurfacePresentModesKHR(demo->device,
736 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600737 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600738 assert(!err);
739
Ian Elliotta0781972015-08-21 15:09:33 -0600740 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600741 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600742 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600743 {
744 // If the surface size is undefined, the size is set to
745 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600746 swapchainExtent.width = demo->width;
747 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600748 }
749 else
750 {
751 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600752 swapchainExtent = surfProperties.currentExtent;
Ian Elliottcf074d32015-10-16 18:02:43 -0600753 demo->width = surfProperties.currentExtent.width;
754 demo->height = surfProperties.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600755 }
756
757 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600758 // tearing mode. If not, try IMMEDIATE which will usually be available,
759 // and is fastest (though it tears). If not, fall back to FIFO which is
760 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600761 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600762 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600763 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
764 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600765 break;
766 }
Ian Elliotta0781972015-08-21 15:09:33 -0600767 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
768 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
769 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600770 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600771 }
772
773 // Determine the number of VkImage's to use in the swap chain (we desire to
774 // own only 1 image at a time, besides the images being displayed and
775 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600776 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600777 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600778 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600779 {
780 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600781 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600782 }
783
Tony Barbour9fd6d352015-09-16 15:01:32 -0600784 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600785 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
786 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600787 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600788 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 }
790
Ian Elliottcf074d32015-10-16 18:02:43 -0600791 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600792 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800793 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600794 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
795 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800796 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600797 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800798 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600799 .width = swapchainExtent.width,
800 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600802 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600803 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800804 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600805 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
806 .queueFamilyCount = 0,
807 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600808 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600809 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600810 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600811 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600812 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600813
Ian Elliottcf074d32015-10-16 18:02:43 -0600814 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800815 assert(!err);
816
Ian Elliottcf074d32015-10-16 18:02:43 -0600817 // If we just re-created an existing swapchain, we should destroy the old
818 // swapchain at this point.
819 // Note: destroying the swapchain also cleans up all its associated
820 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800821 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottcf074d32015-10-16 18:02:43 -0600822 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
823 }
824
825 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600826 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600827 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800828
Ian Elliotta0781972015-08-21 15:09:33 -0600829 VkImage* swapchainImages =
830 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
831 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600832 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600833 &demo->swapchainImageCount,
834 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600835 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600836
Ian Elliotta0781972015-08-21 15:09:33 -0600837 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600838 assert(demo->buffers);
839
Ian Elliotta0781972015-08-21 15:09:33 -0600840 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600841 VkImageViewCreateInfo color_image_view = {
842 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600843 .pNext = NULL,
844 .format = demo->format,
Chia-I Wub6d30c62015-10-27 19:00:15 +0800845 .components = {
Chia-I Wuf051d262015-10-27 19:25:11 +0800846 .r = VK_COMPONENT_SWIZZLE_R,
847 .g = VK_COMPONENT_SWIZZLE_G,
848 .b = VK_COMPONENT_SWIZZLE_B,
849 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600850 },
851 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600852 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600853 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800854 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600855 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800856 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600857 },
858 .viewType = VK_IMAGE_VIEW_TYPE_2D,
859 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600860 };
861
Ian Elliotta0781972015-08-21 15:09:33 -0600862 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600863
Tony Barbour15a4ef62015-10-21 10:14:48 -0600864 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
865 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600866 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600867 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600868 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600869 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600870
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600871 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600872
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600873 err = vkCreateImageView(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +0800874 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600875 assert(!err);
876 }
877}
878
879static void demo_prepare_depth(struct demo *demo)
880{
Tony Barbour72304ef2015-04-16 15:59:00 -0600881 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600882 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600883 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600884 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600885 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600886 .format = depth_format,
887 .extent = { demo->width, demo->height, 1 },
888 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600889 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800890 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600891 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600892 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600893 .flags = 0,
894 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200895
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600896 VkImageViewCreateInfo view = {
897 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600898 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800899 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600900 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600901 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600902 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600903 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800904 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600905 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800906 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600907 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600908 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600909 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600910 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600911
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500912 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600913 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600914 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600915
916 demo->depth.format = depth_format;
917
918 /* create image */
Chia-I Wu63636062015-10-26 21:10:41 +0800919 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600920 &demo->depth.image);
921 assert(!err);
922
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600923 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600924 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600925 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600926
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200927 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
928 demo->depth.mem_alloc.pNext = NULL;
929 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
930 demo->depth.mem_alloc.memoryTypeIndex = 0;
931
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600932 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600933 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600934 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200935 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600936 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600937
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500938 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800939 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +0800940 NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500941 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600942
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500943 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600944 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500945 demo->depth.mem, 0);
946 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600947
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600948 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600949 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600950 VK_IMAGE_LAYOUT_UNDEFINED,
951 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600952
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600953 /* create image view */
954 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800955 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600957}
958
Tony Barbour1a86d032015-09-21 15:17:33 -0600959/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700960bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600961 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600962 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600963{
Tony Barbour1a86d032015-09-21 15:17:33 -0600964 FILE *fPtr = fopen(filename,"rb");
965 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600966
Tony Barbour1a86d032015-09-21 15:17:33 -0600967 if (!fPtr)
968 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600969
Tony Barbour1a86d032015-09-21 15:17:33 -0600970 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600971 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
972 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600973 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600974 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600975
Tony Barbour1a86d032015-09-21 15:17:33 -0600976 do {
977 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600978 if (cPtr == NULL) {
979 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600980 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600981 }
Tony Barbour1a86d032015-09-21 15:17:33 -0600982 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600983
Tony Barbour1a86d032015-09-21 15:17:33 -0600984 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600985 if (rgba_data == NULL) {
986 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600987 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600988 }
Tony Barbour1a86d032015-09-21 15:17:33 -0600989 fgets(header, 256, fPtr); // Format
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600990 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
991 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600992 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600993 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600994
Tony Barbour1a86d032015-09-21 15:17:33 -0600995 for(int y = 0; y < *height; y++)
996 {
997 uint8_t *rowPtr = rgba_data;
998 for(int x = 0; x < *width; x++)
999 {
1000 fread(rowPtr, 3, 1, fPtr);
1001 rowPtr[3] = 255; /* Alpha of 1 */
1002 rowPtr += 4;
1003 }
1004 rgba_data += layout->rowPitch;
1005 }
1006 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001007 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001008}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001009
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001010static void demo_prepare_texture_image(struct demo *demo,
1011 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001012 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001013 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001014 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001015 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001017 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001018 int32_t tex_width;
1019 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001020 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001021 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001022
David Pinedo30bd71d2015-04-23 08:16:57 -06001023 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1024 {
1025 printf("Failed to load textures\n");
1026 fflush(stdout);
1027 exit(1);
1028 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001030 tex_obj->tex_width = tex_width;
1031 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001033 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001034 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001036 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001037 .format = tex_format,
1038 .extent = { tex_width, tex_height, 1 },
1039 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001040 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001041 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001042 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001043 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001044 .flags = 0,
1045 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001047 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048
Chia-I Wu63636062015-10-26 21:10:41 +08001049 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001050 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001051 assert(!err);
1052
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001053 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001054
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001055 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1056 tex_obj->mem_alloc.pNext = NULL;
1057 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1058 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001059
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001060 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1061 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001062
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001063 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001064 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001065 &(tex_obj->mem));
1066 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001068 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001069 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001070 tex_obj->mem, 0);
1071 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001073 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001074 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001075 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001077 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001079 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080 void *data;
1081
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001082 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001084 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085 assert(!err);
1086
1087 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1088 fprintf(stderr, "Error loading texture: %s\n", filename);
1089 }
1090
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001091 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001092 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001093
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001094 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001095 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001096 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001097 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001098 tex_obj->imageLayout);
1099 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001100}
1101
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001102static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103{
1104 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001105 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1106 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107}
1108
1109static void demo_prepare_textures(struct demo *demo)
1110{
Tony Barbour72304ef2015-04-16 15:59:00 -06001111 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001112 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001113 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001115 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001116
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001117 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001118 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119
FslNopper6a7e50e2015-05-06 21:42:01 +02001120 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121 /* Device can texture using linear textures */
1122 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001123 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001124 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001126 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127
1128 memset(&staging_texture, 0, sizeof(staging_texture));
1129 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001130 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001131
1132 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001133 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001134 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wuc42afd72015-10-27 18:53:22 +08001135 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001136
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001137 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001138 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001139 staging_texture.imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001140 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001141
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001142 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001143 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001144 demo->textures[i].imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001145 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001146
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001147 VkImageCopy copy_region = {
Tony Barbour466f0012015-11-02 11:47:51 -07001148 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001149 .srcOffset = { 0, 0, 0 },
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001150 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1151 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001152 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1153 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001154 vkCmdCopyImage(demo->cmd,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001155 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1156 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001157 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001158
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001159 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001160 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001161 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001162 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001164 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001166 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001168 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1169 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170 }
1171
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001172 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001173 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001174 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001175 .magFilter = VK_FILTER_NEAREST,
1176 .minFilter = VK_FILTER_NEAREST,
1177 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001178 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1179 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1180 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001181 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001183 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001184 .minLod = 0.0f,
1185 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001186 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001187 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001188 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001189
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001190 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001192 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001193 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001194 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195 .format = tex_format,
Chia-I Wub6d30c62015-10-27 19:00:15 +08001196 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuf051d262015-10-27 19:25:11 +08001197 VK_COMPONENT_SWIZZLE_G,
1198 VK_COMPONENT_SWIZZLE_B,
1199 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001200 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001201 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001202 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001203
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001204 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001205 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 &demo->textures[i].sampler);
1207 assert(!err);
1208
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209 /* create image view */
1210 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001211 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212 &demo->textures[i].view);
1213 assert(!err);
1214 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001215}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001216
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001217void demo_prepare_cube_data_buffer(struct demo *demo)
1218{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001219 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001220 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001221 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001222 int i;
1223 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001224 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001225 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001226 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001227
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001228 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001229 mat4x4_mul(MVP, VP, demo->model_matrix);
1230 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001231// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001232
1233 for (i=0; i<12*3; i++) {
1234 data.position[i][0] = g_vertex_buffer_data[i*3];
1235 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1236 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1237 data.position[i][3] = 1.0f;
1238 data.attr[i][0] = g_uv_buffer_data[2*i];
1239 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1240 data.attr[i][2] = 0;
1241 data.attr[i][3] = 0;
1242 }
1243
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001244 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001245 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001246 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001247 buf_info.size = sizeof(data);
Chia-I Wu63636062015-10-26 21:10:41 +08001248 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1249 &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001250 assert(!err);
1251
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001252 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001253
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001254 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1255 demo->uniform_data.mem_alloc.pNext = NULL;
1256 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1257 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1258
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001259 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001260 mem_reqs.memoryTypeBits,
1261 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001262 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001263 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001264
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001265 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +08001266 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001267 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001268
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001269 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001270 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001271
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001272 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001273
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001274 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001275
Tony Barbour155cce82015-07-03 10:33:54 -06001276 err = vkBindBufferMemory(demo->device,
1277 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001278 demo->uniform_data.mem, 0);
1279 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001280
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001281 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1282 demo->uniform_data.buffer_info.offset = 0;
1283 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001284}
1285
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001286static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001287{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001288 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001289 [0] = {
Chia-I Wued577562015-10-31 00:31:16 +08001290 .binding = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001291 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001292 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001293 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001294 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001295 },
1296 [1] = {
Chia-I Wued577562015-10-31 00:31:16 +08001297 .binding = 1,
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001298 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001299 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001300 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001301 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001302 },
1303 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001304 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001305 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001306 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001307 .bindingCount = 2,
Chia-I Wu0e76e3f2015-10-31 00:31:16 +08001308 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001309 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001310 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001312 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001313 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001314 assert(!err);
1315
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001316 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1317 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1318 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001319 .setLayoutCount = 1,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001320 .pSetLayouts = &demo->desc_layout,
1321 };
1322
1323 err = vkCreatePipelineLayout(demo->device,
1324 &pPipelineLayoutCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +08001325 NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001326 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001327 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328}
1329
Chia-I Wuf973e322015-07-08 13:34:24 +08001330static void demo_prepare_render_pass(struct demo *demo)
1331{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001332 const VkAttachmentDescription attachments[2] = {
1333 [0] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001334 .format = demo->format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001335 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001336 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1337 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1338 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1339 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1340 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1341 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1342 },
1343 [1] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001344 .format = demo->depth.format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001345 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001346 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1347 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1348 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1349 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1350 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1351 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1352 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001353 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001354 const VkAttachmentReference color_reference = {
1355 .attachment = 0,
1356 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1357 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001358 const VkAttachmentReference depth_reference = {
1359 .attachment = 1,
1360 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1361 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001362 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001363 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1364 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001365 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001366 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001367 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001368 .pColorAttachments = &color_reference,
1369 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001370 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001371 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001372 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001373 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001374 const VkRenderPassCreateInfo rp_info = {
1375 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1376 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001377 .attachmentCount = 2,
1378 .pAttachments = attachments,
1379 .subpassCount = 1,
1380 .pSubpasses = &subpass,
1381 .dependencyCount = 0,
1382 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001383 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001384 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001385
Chia-I Wu63636062015-10-26 21:10:41 +08001386 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001387 assert(!err);
1388}
1389
Chia-I Wu46176f12015-10-31 00:31:16 +08001390static VkShaderModule demo_prepare_shader_module(struct demo* demo,
1391 VkShaderStageFlagBits stage,
1392 const void* code,
1393 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001394{
Chia-I Wu46176f12015-10-31 00:31:16 +08001395 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001396 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001397 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001399 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1400 moduleCreateInfo.pNext = NULL;
1401
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001402 moduleCreateInfo.codeSize = size;
1403 moduleCreateInfo.pCode = code;
1404 moduleCreateInfo.flags = 0;
1405 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1406 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001407
1408 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001409}
1410
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001411char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001412{
1413 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001414 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001415 void *shader_code;
1416
1417 FILE *fp = fopen(filename, "rb");
1418 if (!fp) return NULL;
1419
1420 fseek(fp, 0L, SEEK_END);
1421 size = ftell(fp);
1422
1423 fseek(fp, 0L, SEEK_SET);
1424
1425 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001426 retval = fread(shader_code, size, 1, fp);
1427 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001428
1429 *psize = size;
1430
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001431 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001432 return shader_code;
1433}
1434
Chia-I Wu46176f12015-10-31 00:31:16 +08001435static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001436{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001437 void *vertShaderCode;
1438 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001439
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001440 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1441
1442 demo->vert_shader_module = demo_prepare_shader_module(demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001443 VK_SHADER_STAGE_VERTEX_BIT, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001444
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001445 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001446
1447 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001448}
1449
Chia-I Wu46176f12015-10-31 00:31:16 +08001450static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001451{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001452 void *fragShaderCode;
1453 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001454
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001455 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001456
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001457 demo->frag_shader_module = demo_prepare_shader_module(demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001458 VK_SHADER_STAGE_FRAGMENT_BIT, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001459
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001460 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001461
1462 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463}
1464
1465static void demo_prepare_pipeline(struct demo *demo)
1466{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001467 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001468 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001469 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001470 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001471 VkPipelineRasterizationStateCreateInfo rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001472 VkPipelineColorBlendStateCreateInfo cb;
1473 VkPipelineDepthStencilStateCreateInfo ds;
1474 VkPipelineViewportStateCreateInfo vp;
1475 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001476 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniellba839d12015-09-29 13:01:09 -06001477 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001478 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001479
Piers Daniellba839d12015-09-29 13:01:09 -06001480 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1481 memset(&dynamicState, 0, sizeof dynamicState);
1482 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1483 dynamicState.pDynamicStates = dynamicStateEnables;
1484
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001486 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001487 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001489 memset(&vi, 0, sizeof(vi));
1490 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1491
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001492 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001493 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001494 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001495
1496 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001497 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1498 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001499 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001500 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001501 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001502 rs.rasterizerDiscardEnable = VK_FALSE;
1503 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504
1505 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001506 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1507 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001508 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001509 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001510 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001511 cb.attachmentCount = 1;
1512 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001513
Tony Barbour29645d02015-01-16 14:27:35 -07001514 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001515 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001516 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001517 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1518 vp.scissorCount = 1;
1519 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001520
1521 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001522 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001523 ds.depthTestEnable = VK_TRUE;
1524 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001525 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001526 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001527 ds.back.failOp = VK_STENCIL_OP_KEEP;
1528 ds.back.passOp = VK_STENCIL_OP_KEEP;
1529 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001530 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001531 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532
Tony Barbour29645d02015-01-16 14:27:35 -07001533 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001534 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001535 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001536 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001538 // Two stages: vs and fs
1539 pipeline.stageCount = 2;
1540 VkPipelineShaderStageCreateInfo shaderStages[2];
1541 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1542
1543 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001544 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1545 shaderStages[0].module = demo_prepare_vs(demo);
1546 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001547
1548 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001549 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1550 shaderStages[1].module = demo_prepare_fs(demo);
1551 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001552
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001553 memset(&pipelineCache, 0, sizeof(pipelineCache));
1554 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001555
Chia-I Wu63636062015-10-26 21:10:41 +08001556 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001557 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001558
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001559 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001560 pipeline.pInputAssemblyState = &ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001561 pipeline.pRasterizationState = &rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001562 pipeline.pColorBlendState = &cb;
1563 pipeline.pMultisampleState = &ms;
1564 pipeline.pViewportState = &vp;
1565 pipeline.pDepthStencilState = &ds;
1566 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001567 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001568 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001569
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001570 pipeline.renderPass = demo->render_pass;
1571
Chia-I Wu63636062015-10-26 21:10:41 +08001572 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1573 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574 assert(!err);
1575
Chia-I Wu63636062015-10-26 21:10:41 +08001576 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1577 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001578}
1579
Chia-I Wu63ea9262015-03-26 13:14:16 +08001580static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001581{
Chia-I Wuf051d262015-10-27 19:25:11 +08001582 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001583 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001584 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001585 .descriptorCount = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001586 },
1587 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001588 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001589 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001590 },
1591 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001592 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001593 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001594 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001595 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001596 .poolSizeCount = 2,
1597 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001598 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001599 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001600
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001601 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001602 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001603 assert(!err);
1604}
1605
1606static void demo_prepare_descriptor_set(struct demo *demo)
1607{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001608 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001609 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001610 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001611 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001612
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001613 VkDescriptorSetAllocateInfo alloc_info = {
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001614 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO,
1615 .pNext = NULL,
1616 .descriptorPool = demo->desc_pool,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001617 .setLayoutCount = 1,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001618 .pSetLayouts = &demo->desc_layout
1619 };
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001620 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001621 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001622
Chia-I Wuae721ba2015-05-25 16:27:55 +08001623 memset(&tex_descs, 0, sizeof(tex_descs));
1624 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001625 tex_descs[i].sampler = demo->textures[i].sampler;
1626 tex_descs[i].imageView = demo->textures[i].view;
1627 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001628 }
1629
1630 memset(&writes, 0, sizeof(writes));
1631
1632 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001633 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001634 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001635 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001636 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001637
1638 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001639 writes[1].dstSet = demo->desc_set;
1640 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001641 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001642 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001643 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001644
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001645 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001646}
1647
Chia-I Wuf973e322015-07-08 13:34:24 +08001648static void demo_prepare_framebuffers(struct demo *demo)
1649{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001650 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001651 attachments[1] = demo->depth.view;
1652
Chia-I Wuf973e322015-07-08 13:34:24 +08001653 const VkFramebufferCreateInfo fb_info = {
1654 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1655 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001656 .renderPass = demo->render_pass,
1657 .attachmentCount = 2,
1658 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001659 .width = demo->width,
1660 .height = demo->height,
1661 .layers = 1,
1662 };
1663 VkResult U_ASSERT_ONLY err;
1664 uint32_t i;
1665
Tony Barbourd8e504f2015-10-08 14:26:24 -06001666 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1667 assert(demo->framebuffers);
1668
1669 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001670 attachments[0] = demo->buffers[i].view;
Chia-I Wu63636062015-10-26 21:10:41 +08001671 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001672 assert(!err);
1673 }
1674}
1675
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676static void demo_prepare(struct demo *demo)
1677{
Cody Northrop91e221b2015-07-09 18:08:32 -06001678 VkResult U_ASSERT_ONLY err;
1679
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001680 const VkCommandPoolCreateInfo cmd_pool_info = {
1681 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001682 .pNext = NULL,
1683 .queueFamilyIndex = demo->graphics_queue_node_index,
1684 .flags = 0,
1685 };
Chia-I Wu63636062015-10-26 21:10:41 +08001686 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001687 assert(!err);
1688
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001689 const VkCommandBufferAllocateInfo cmd = {
1690 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001692 .commandPool = demo->cmd_pool,
1693 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001694 .bufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001695 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001696
1697 demo_prepare_buffers(demo);
1698 demo_prepare_depth(demo);
1699 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001700 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001701
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001702 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001703 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001704 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001705
Ian Elliotta0781972015-08-21 15:09:33 -06001706 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001707 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001708 assert(!err);
1709 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001710
Chia-I Wu63ea9262015-03-26 13:14:16 +08001711 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001712 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001713
Chia-I Wuf973e322015-07-08 13:34:24 +08001714 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001715
Ian Elliotta0781972015-08-21 15:09:33 -06001716 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001717 demo->current_buffer = i;
1718 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1719 }
1720
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001721 /*
1722 * Prepare functions above may generate pipeline commands
1723 * that need to be flushed before beginning the render loop.
1724 */
1725 demo_flush_init_cmd(demo);
1726
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001727 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001728 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729}
1730
David Pinedo2bb7c932015-06-18 17:03:14 -06001731static void demo_cleanup(struct demo *demo)
1732{
1733 uint32_t i;
1734
1735 demo->prepared = false;
1736
Tony Barbourd8e504f2015-10-08 14:26:24 -06001737 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001738 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001739 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001740 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001741 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001742
Chia-I Wu63636062015-10-26 21:10:41 +08001743 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1744 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1745 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1746 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1747 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001748
1749 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001750 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1751 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1752 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1753 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001754 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001755 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001756
Chia-I Wu63636062015-10-26 21:10:41 +08001757 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1758 vkDestroyImage(demo->device, demo->depth.image, NULL);
1759 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001760
Chia-I Wu63636062015-10-26 21:10:41 +08001761 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1762 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001763
Ian Elliotta0781972015-08-21 15:09:33 -06001764 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001765 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001766 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001767 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001768 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001769
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001770 free(demo->queue_props);
1771
Chia-I Wu63636062015-10-26 21:10:41 +08001772 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1773 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001774 if (demo->validate) {
1775 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1776 }
Chia-I Wu63636062015-10-26 21:10:41 +08001777 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001778
1779#ifndef _WIN32
1780 xcb_destroy_window(demo->connection, demo->window);
1781 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001782 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001783#endif // _WIN32
1784}
1785
Ian Elliottcf074d32015-10-16 18:02:43 -06001786static void demo_resize(struct demo *demo)
1787{
1788 uint32_t i;
1789
Mike Stroyanbb60b382015-10-28 09:46:57 -06001790 // Don't react to resize until after first initialization.
1791 if (!demo->prepared) {
1792 return;
1793 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001794 // In order to properly resize the window, we must re-create the swapchain
1795 // AND redo the command buffers, etc.
1796 //
1797 // First, perform part of the demo_cleanup() function:
1798 demo->prepared = false;
1799
1800 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001801 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001802 }
1803 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001804 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001805
Chia-I Wu63636062015-10-26 21:10:41 +08001806 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1807 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1808 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1809 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1810 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001811
1812 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001813 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1814 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1815 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1816 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001817 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001818
Chia-I Wu63636062015-10-26 21:10:41 +08001819 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1820 vkDestroyImage(demo->device, demo->depth.image, NULL);
1821 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001822
Chia-I Wu63636062015-10-26 21:10:41 +08001823 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1824 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001825
1826 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001827 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001828 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001829 }
Chia-I Wu63636062015-10-26 21:10:41 +08001830 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001831 free(demo->buffers);
1832
Chia-I Wu63636062015-10-26 21:10:41 +08001833 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001834
Ian Elliottcf074d32015-10-16 18:02:43 -06001835
1836 // Second, re-perform the demo_prepare() function, which will re-create the
1837 // swapchain:
1838 demo_prepare(demo);
1839}
1840
David Pinedo2bb7c932015-06-18 17:03:14 -06001841// On MS-Windows, make this a global, so it's available to WndProc()
1842struct demo demo;
1843
Ian Elliott639ca472015-04-16 15:23:05 -06001844#ifdef _WIN32
1845static void demo_run(struct demo *demo)
1846{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001847 if (!demo->prepared)
1848 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001849 // Wait for work to finish before updating MVP.
1850 vkDeviceWaitIdle(demo->device);
1851 demo_update_data_buffer(demo);
1852
1853 demo_draw(demo);
1854
1855 // Wait for work to finish before updating MVP.
1856 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001857
David Pinedo2bb7c932015-06-18 17:03:14 -06001858 demo->curFrame++;
1859
1860 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1861 {
1862 demo->quit=true;
1863 demo_cleanup(demo);
1864 ExitProcess(0);
1865 }
1866
1867}
Ian Elliott639ca472015-04-16 15:23:05 -06001868
1869// MS-Windows event handling function:
1870LRESULT CALLBACK WndProc(HWND hWnd,
1871 UINT uMsg,
1872 WPARAM wParam,
1873 LPARAM lParam)
1874{
Ian Elliott639ca472015-04-16 15:23:05 -06001875 switch(uMsg)
1876 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001877 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001878 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001879 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001880 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001881 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001882 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001883 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001884 demo.width = lParam & 0xffff;
1885 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001886 demo_resize(&demo);
1887 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001888 default:
1889 break;
1890 }
1891 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1892}
1893
1894static void demo_create_window(struct demo *demo)
1895{
1896 WNDCLASSEX win_class;
1897
1898 // Initialize the window class structure:
1899 win_class.cbSize = sizeof(WNDCLASSEX);
1900 win_class.style = CS_HREDRAW | CS_VREDRAW;
1901 win_class.lpfnWndProc = WndProc;
1902 win_class.cbClsExtra = 0;
1903 win_class.cbWndExtra = 0;
1904 win_class.hInstance = demo->connection; // hInstance
1905 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1906 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1907 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1908 win_class.lpszMenuName = NULL;
1909 win_class.lpszClassName = demo->name;
1910 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1911 // Register window class:
1912 if (!RegisterClassEx(&win_class)) {
1913 // It didn't work, so try to give a useful error:
1914 printf("Unexpected error trying to start the application!\n");
1915 fflush(stdout);
1916 exit(1);
1917 }
1918 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001919 RECT wr = { 0, 0, demo->width, demo->height };
1920 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001921 demo->window = CreateWindowEx(0,
1922 demo->name, // class name
1923 demo->name, // app name
1924 WS_OVERLAPPEDWINDOW | // window style
1925 WS_VISIBLE |
1926 WS_SYSMENU,
1927 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001928 wr.right-wr.left, // width
1929 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001930 NULL, // handle to parent
1931 NULL, // handle to menu
1932 demo->connection, // hInstance
1933 NULL); // no extra parameters
1934 if (!demo->window) {
1935 // It didn't work, so try to give a useful error:
1936 printf("Cannot create a window in which to draw!\n");
1937 fflush(stdout);
1938 exit(1);
1939 }
1940}
1941#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001942static void demo_handle_event(struct demo *demo,
1943 const xcb_generic_event_t *event)
1944{
Piers Daniell735ee532015-02-23 16:23:13 -07001945 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001946 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001947 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001948 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001949 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001950 case XCB_CLIENT_MESSAGE:
1951 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1952 (*demo->atom_wm_delete_window).atom) {
1953 demo->quit = true;
1954 }
1955 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956 case XCB_KEY_RELEASE:
1957 {
1958 const xcb_key_release_event_t *key =
1959 (const xcb_key_release_event_t *) event;
1960
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001961 switch (key->detail) {
1962 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001963 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001964 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001965 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001966 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001967 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001968 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001969 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001970 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001971 case 0x41:
1972 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001973 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001974 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001975 }
1976 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06001977 case XCB_CONFIGURE_NOTIFY:
1978 {
1979 const xcb_configure_notify_event_t *cfg =
1980 (const xcb_configure_notify_event_t *) event;
1981 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
1982 demo_resize(demo);
1983 }
1984 }
1985 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001986 default:
1987 break;
1988 }
1989}
1990
1991static void demo_run(struct demo *demo)
1992{
1993 xcb_flush(demo->connection);
1994
1995 while (!demo->quit) {
1996 xcb_generic_event_t *event;
1997
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001998 if (demo->pause) {
1999 event = xcb_wait_for_event(demo->connection);
2000 } else {
2001 event = xcb_poll_for_event(demo->connection);
2002 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002003 if (event) {
2004 demo_handle_event(demo, event);
2005 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002006 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002007
2008 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002009 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002010 demo_update_data_buffer(demo);
2011
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002012 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002013
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002014 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002015 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002016 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002017 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002018 demo->quit = true;
2019
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002020 }
2021}
2022
2023static void demo_create_window(struct demo *demo)
2024{
2025 uint32_t value_mask, value_list[32];
2026
2027 demo->window = xcb_generate_id(demo->connection);
2028
2029 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2030 value_list[0] = demo->screen->black_pixel;
2031 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002032 XCB_EVENT_MASK_EXPOSURE |
2033 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002034
2035 xcb_create_window(demo->connection,
2036 XCB_COPY_FROM_PARENT,
2037 demo->window, demo->screen->root,
2038 0, 0, demo->width, demo->height, 0,
2039 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2040 demo->screen->root_visual,
2041 value_mask, value_list);
2042
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002043 /* Magic code that will send notification when window is destroyed */
2044 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2045 "WM_PROTOCOLS");
2046 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2047
2048 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2049 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2050
2051 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2052 demo->window, (*reply).atom, 4, 32, 1,
2053 &(*demo->atom_wm_delete_window).atom);
2054 free(reply);
2055
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002057
2058 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2059 const uint32_t coords[] = {100, 100};
2060 xcb_configure_window(demo->connection, demo->window,
2061 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002062}
Ian Elliott639ca472015-04-16 15:23:05 -06002063#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002065/*
2066 * Return 1 (true) if all layer names specified in check_names
2067 * can be found in given layer properties.
2068 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002069static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002070 uint32_t layer_count, VkLayerProperties *layers)
2071{
2072 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002073 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002074 for (uint32_t j = 0; j < layer_count; j++) {
2075 if (!strcmp(check_names[i], layers[j].layerName)) {
2076 found = 1;
2077 }
2078 }
2079 if (!found) {
2080 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2081 return 0;
2082 }
2083 }
2084 return 1;
2085}
2086
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002087static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002088{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002089 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002090 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002091 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002092 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002093 VkLayerProperties *instance_layers;
2094 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002095 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002096 uint32_t instance_layer_count = 0;
2097 uint32_t enabled_extension_count = 0;
2098 uint32_t enabled_layer_count = 0;
2099
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002100 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002101 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002102 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002103 "ObjectTracker",
2104 "DrawState",
2105 "ParamChecker",
2106 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002107 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002108 "DeviceLimits",
2109 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002110 };
2111
2112 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002113 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002114 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002115 "ObjectTracker",
2116 "DrawState",
2117 "ParamChecker",
2118 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002119 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002120 "DeviceLimits",
2121 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002122 };
2123
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002124 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002125 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002126 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002127 assert(!err);
2128
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002129 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002130 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002131 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002132
2133 if (demo->validate) {
2134 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2135 instance_layer_count, instance_layers);
2136 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002137 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002138 "required validation layer.\n\n"
2139 "Please look at the Getting Started guide for additional "
2140 "information.\n",
2141 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002142 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002143 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002144 }
2145
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002146 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002147 assert(!err);
2148
Tony Barbourcc69f452015-10-08 13:59:42 -06002149 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002150 memset(extension_names, 0, sizeof(extension_names));
2151 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002152 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002153 assert(!err);
2154 for (uint32_t i = 0; i < instance_extension_count; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002155 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002156 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002157 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002158 }
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002159 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002160 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002161 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002162 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002163 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002164 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002165 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002166 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002167 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002168 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002169 "Vulkan installable client driver (ICD) installed?\nPlease "
2170 "look at the Getting Started guide for additional "
2171 "information.\n",
2172 "vkCreateInstance Failure");
2173 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002174 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002175 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002176 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002177 .pApplicationName = APP_SHORT_NAME,
2178 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002179 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002180 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002181 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002182 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002183 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002184 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002185 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002186 .pApplicationInfo = &app,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002187 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002188 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002189 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002190 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002191 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002192
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002193 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002194
Chia-I Wu63636062015-10-26 21:10:41 +08002195 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002196 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2197 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002198 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002199 "additional information.\n",
2200 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002201 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002202 ERR_EXIT("Cannot find a specified extension library"
2203 ".\nMake sure your layers path is set appropriately\n",
2204 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002205 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002206 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2207 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002208 "the Getting Started guide for additional information.\n",
2209 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002210 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002211
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002212 free(instance_layers);
2213 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002214
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002215 /* Make initial call to query gpu_count, then second call for gpu info*/
2216 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2217 assert(!err && gpu_count > 0);
2218 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2219 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2220 assert(!err);
2221 /* For cube demo we just grab the first physical device */
2222 demo->gpu = physical_devices[0];
2223 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002224
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002225 /* Look for validation layers */
2226 validation_found = 0;
2227 enabled_layer_count = 0;
2228 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002229 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002230 assert(!err);
2231
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002232 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002233 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002234 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002235
2236 if (demo->validate) {
2237 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2238 device_layer_count, device_layers);
2239 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002240 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002241 "a required validation layer.\n\n"
2242 "Please look at the Getting Started guide for additional "
2243 "information.\n",
2244 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002245 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002246 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002247 }
2248
2249 uint32_t device_extension_count = 0;
2250 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002251 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002252 demo->gpu, NULL, &device_extension_count, NULL);
2253 assert(!err);
2254
Tony Barbourcc69f452015-10-08 13:59:42 -06002255 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002256 enabled_extension_count = 0;
2257 memset(extension_names, 0, sizeof(extension_names));
2258 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002259 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002260 demo->gpu, NULL, &device_extension_count, device_extensions);
2261 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002262
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002263 for (uint32_t i = 0; i < device_extension_count; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002264 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002265 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002266 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002267 }
2268 assert(enabled_extension_count < 64);
2269 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002270 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002271 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002272 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002273 "Vulkan installable client driver (ICD) installed?\nPlease "
2274 "look at the Getting Started guide for additional "
2275 "information.\n",
2276 "vkCreateInstance Failure");
2277 }
2278
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002279 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002280 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2281 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002282 if (!demo->dbgCreateMsgCallback) {
2283 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2284 "vkGetProcAddr Failure");
2285 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002286 if (!demo->dbgDestroyMsgCallback) {
2287 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2288 "vkGetProcAddr Failure");
2289 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002290 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2291 if (!demo->dbgBreakCallback) {
2292 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2293 "vkGetProcAddr Failure");
2294 }
2295
2296 PFN_vkDbgMsgCallback callback;
2297
2298 if (!demo->use_break) {
2299 callback = dbgFunc;
2300 } else {
2301 callback = demo->dbgBreakCallback;
2302 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002303 err = demo->dbgCreateMsgCallback(
2304 demo->inst,
2305 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002306 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002307 &demo->msg_callback);
2308 switch (err) {
2309 case VK_SUCCESS:
2310 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002311 case VK_ERROR_OUT_OF_HOST_MEMORY:
2312 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2313 "dbgCreateMsgCallback Failure");
2314 break;
2315 default:
2316 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2317 "dbgCreateMsgCallback Failure");
2318 break;
2319 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002320 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002321 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002322
2323 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002324 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002325 assert(demo->queue_count >= 1);
2326
2327 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002328 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002329 // Find a queue that supports gfx
2330 uint32_t gfx_queue_idx = 0;
2331 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2332 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2333 break;
2334 }
2335 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002336 // Query fine-grained feature support for this device.
2337 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002338 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002339 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002340
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002341 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002342 const VkDeviceQueueCreateInfo queue = {
2343 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2344 .pNext = NULL,
2345 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002346 .queuePriorityCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002347 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002348 };
2349
2350 VkDeviceCreateInfo device = {
2351 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2352 .pNext = NULL,
Courtney Goeltzenleuchter8ab618c2015-10-15 16:58:44 -06002353 .requestedQueueCount = 1,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002354 .pRequestedQueues = &queue,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002355 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002356 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002357 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002358 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002359 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002360 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002361
Chia-I Wu63636062015-10-26 21:10:41 +08002362 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002363 assert(!err);
2364
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002365 free(device_layers);
2366
Ian Elliotta0781972015-08-21 15:09:33 -06002367 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2368 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2369 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2370 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2371 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002372 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2373 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2374 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2375 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002376}
2377
Tony Barbourcc69f452015-10-08 13:59:42 -06002378static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002379{
2380 VkResult err;
2381 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002382
Tony Barbourcc69f452015-10-08 13:59:42 -06002383 // Construct the surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002384 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002385 demo->surface_description.pNext = NULL;
2386#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002387 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002388 demo->surface_description.pPlatformHandle = demo->connection;
2389 demo->surface_description.pPlatformWindow = demo->window;
2390#else // _WIN32
2391 demo->platform_handle_xcb.connection = demo->connection;
2392 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002393 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002394 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2395 demo->surface_description.pPlatformWindow = &demo->window;
2396#endif // _WIN32
2397
Tony Barbourcc69f452015-10-08 13:59:42 -06002398 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002399 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2400 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002401 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2402 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002403 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002404 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002405
2406 // Search for a graphics and a present queue in the array of queue
2407 // families, try to find one that supports both
2408 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2409 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002410 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002411 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2412 if (graphicsQueueNodeIndex == UINT32_MAX) {
2413 graphicsQueueNodeIndex = i;
2414 }
2415
2416 if (supportsPresent[i] == VK_TRUE) {
2417 graphicsQueueNodeIndex = i;
2418 presentQueueNodeIndex = i;
2419 break;
2420 }
2421 }
2422 }
2423 if (presentQueueNodeIndex == UINT32_MAX) {
2424 // If didn't find a queue that supports both graphics and present, then
2425 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002426 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002427 if (supportsPresent[i] == VK_TRUE) {
2428 presentQueueNodeIndex = i;
2429 break;
2430 }
2431 }
2432 }
2433 free(supportsPresent);
2434
2435 // Generate error if could not find both a graphics and a present queue
2436 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2437 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002438 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002439 }
2440
2441 // TODO: Add support for separate queues, including presentation,
2442 // synchronization, and appropriate tracking for QueueSubmit
2443 // While it is possible for an application to use a separate graphics and a
2444 // present queues, this demo program assumes it is only using one:
2445 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2446 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002447 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002448 }
2449
2450 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002451
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002452 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002453 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002454
Ian Elliottaaae5352015-07-06 14:27:58 -06002455 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002456 uint32_t formatCount;
Ian Elliotta0781972015-08-21 15:09:33 -06002457 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2458 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002459 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002460 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002461 VkSurfaceFormatKHR *surfFormats =
2462 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2463 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2464 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002465 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002466 assert(!err);
2467 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2468 // the surface has no preferred format. Otherwise, at least one
2469 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002470 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2471 {
2472 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2473 }
2474 else
2475 {
2476 assert(formatCount >= 1);
2477 demo->format = surfFormats[0].format;
2478 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002479 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002480
David Pinedo2bb7c932015-06-18 17:03:14 -06002481 demo->quit = false;
2482 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002483
2484 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002485 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002486}
2487
2488static void demo_init_connection(struct demo *demo)
2489{
Ian Elliott639ca472015-04-16 15:23:05 -06002490#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002491 const xcb_setup_t *setup;
2492 xcb_screen_iterator_t iter;
2493 int scr;
2494
2495 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002496 if (demo->connection == NULL) {
2497 printf("Cannot find a compatible Vulkan installable client driver "
2498 "(ICD).\nExiting ...\n");
2499 fflush(stdout);
2500 exit(1);
2501 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002502
2503 setup = xcb_get_setup(demo->connection);
2504 iter = xcb_setup_roots_iterator(setup);
2505 while (scr-- > 0)
2506 xcb_screen_next(&iter);
2507
2508 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002509#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002510}
2511
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002512static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002513{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002514 vec3 eye = {0.0f, 3.0f, 5.0f};
2515 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002516 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002517
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002518 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002519 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002520
Piers Daniell735ee532015-02-23 16:23:13 -07002521 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002522 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002523 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002524 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002525 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002526 if (strcmp(argv[i], "--break") == 0) {
2527 demo->use_break = true;
2528 continue;
2529 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002530 if (strcmp(argv[i], "--validate") == 0) {
2531 demo->validate = true;
2532 continue;
2533 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002534 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002535 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002536 i < argc-1 &&
2537 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2538 demo->frameCount >= 0)
2539 {
2540 i++;
2541 continue;
2542 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002543
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002544 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002545 fflush(stderr);
2546 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002547 }
2548
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002549 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002550 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002551
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002552 demo->width = 500;
2553 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002554
2555 demo->spin_angle = 0.01f;
2556 demo->spin_increment = 0.01f;
2557 demo->pause = false;
2558
Piers Daniell735ee532015-02-23 16:23:13 -07002559 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002560 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2561 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002562}
2563
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002564
Ian Elliott639ca472015-04-16 15:23:05 -06002565#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002566extern int __getmainargs(
2567 int * _Argc,
2568 char *** _Argv,
2569 char *** _Env,
2570 int _DoWildCard,
2571 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002572
Ian Elliotta748eaf2015-04-28 15:50:36 -06002573int WINAPI WinMain(HINSTANCE hInstance,
2574 HINSTANCE hPrevInstance,
2575 LPSTR pCmdLine,
2576 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002577{
2578 MSG msg; // message
2579 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002580 int argc;
2581 char** argv;
2582 char** env;
2583 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002584
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002585 __getmainargs(&argc,&argv,&env,0,&new_mode);
2586
2587 demo_init(&demo, argc, argv);
2588 demo.connection = hInstance;
2589 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002590 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002591 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002592
2593 demo_prepare(&demo);
2594
2595 done = false; //initialize loop condition variable
2596 /* main message loop*/
2597 while(!done)
2598 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002599 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002600 if (msg.message == WM_QUIT) //check for a quit message
2601 {
2602 done = true; //if found, quit app
2603 }
2604 else
2605 {
2606 /* Translate and dispatch to event queue*/
2607 TranslateMessage(&msg);
2608 DispatchMessage(&msg);
2609 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002610 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002611 }
2612
2613 demo_cleanup(&demo);
2614
Tony Barbourf9921732015-04-22 11:36:22 -06002615 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002616}
2617#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002618int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002619{
2620 struct demo demo;
2621
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002622 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002623 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002624 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002625
2626 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002627 demo_run(&demo);
2628
2629 demo_cleanup(&demo);
2630
2631 return 0;
2632}
Ian Elliott639ca472015-04-16 15:23:05 -06002633#endif // _WIN32