blob: b64023fc508a58030ac6e5e9587e0f433652700c [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
Cody Northropfe3d8bc2015-03-17 14:54:35 -060044#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045
Mark Lobodzinskif871f772015-09-01 09:00:16 -060046#include "vk_sdk_platform.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
62#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
67
Ian Elliott07264132015-04-28 11:35:02 -060068#else // _WIN32
69
70#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 printf(err_msg); \
73 fflush(stdout); \
74 exit(1); \
75 } while (0)
76#endif // _WIN32
77
Ian Elliottaaae5352015-07-06 14:27:58 -060078#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
79{ \
80 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
81 if (demo->fp##entrypoint == NULL) { \
82 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
83 "vkGetInstanceProcAddr Failure"); \
84 } \
85}
86
Jon Ashburn7d8342c2015-10-08 15:58:23 -060087static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
88
Ian Elliott673898b2015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Jon Ashburn7d8342c2015-10-08 15:58:23 -060091 if(!g_gdpa) \
92 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
93 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060094 if (demo->fp##entrypoint == NULL) { \
95 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
96 "vkGetDeviceProcAddr Failure"); \
97 } \
98}
99
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600100/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700101 * structure to track all objects related to a texture.
102 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600103struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600104 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700105
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600106 VkImage image;
107 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600108
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200109 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500110 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600111 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700112 int32_t tex_width, tex_height;
113};
114
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600115static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600116 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600117};
118
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600119struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600121 float mvp[4][4];
122 float position[12*3][4];
123 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600124};
125
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600126struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600128 float mvp[4][4];
129 float position[12*3][4];
130 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600131};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600132
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600133//--------------------------------------------------------------------------------------
134// Mesh and VertexFormat Data
135//--------------------------------------------------------------------------------------
136struct Vertex
137{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600138 float posX, posY, posZ, posW; // Position data
139 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600140};
141
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142struct VertexPosTex
143{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600144 float posX, posY, posZ, posW; // Position data
145 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146};
147
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600148#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600151static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800152 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600153 -1.0f,-1.0f, 1.0f,
154 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 -1.0f, 1.0f,-1.0f,
157 -1.0f,-1.0f,-1.0f,
158
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800159 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 1.0f, 1.0f,-1.0f,
161 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600164 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600171 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 -1.0f, 1.0f, 1.0f,
175 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600178 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 1.0f, 1.0f, 1.0f,
182 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 1.0f,-1.0f,-1.0f,
185 1.0f, 1.0f,-1.0f,
186
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600189 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 1.0f,-1.0f, 1.0f,
192 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600193};
194
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600195static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800196 0.0f, 0.0f, // -X side
197 1.0f, 0.0f,
198 1.0f, 1.0f,
199 1.0f, 1.0f,
200 0.0f, 1.0f,
201 0.0f, 0.0f,
202
203 1.0f, 0.0f, // -Z side
204 0.0f, 1.0f,
205 0.0f, 0.0f,
206 1.0f, 0.0f,
207 1.0f, 1.0f,
208 0.0f, 1.0f,
209
210 1.0f, 1.0f, // -Y side
211 1.0f, 0.0f,
212 0.0f, 0.0f,
213 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214 0.0f, 0.0f,
215 0.0f, 1.0f,
216
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800217 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600218 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600222 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800224 1.0f, 1.0f, // +X side
225 0.0f, 1.0f,
226 0.0f, 0.0f,
227 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600229 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600230
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800231 0.0f, 1.0f, // +Z side
232 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600234 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600237};
238
239void dumpMatrix(const char *note, mat4x4 MVP)
240{
241 int i;
242
243 printf("%s: \n", note);
244 for (i=0; i<4; i++) {
245 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
246 }
247 printf("\n");
248 fflush(stdout);
249}
250
251void dumpVec4(const char *note, vec4 vector)
252{
253 printf("%s: \n", note);
254 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
255 printf("\n");
256 fflush(stdout);
257}
258
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600259VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600260 VkFlags msgFlags,
261 VkDbgObjectType objType,
262 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600263 size_t location,
264 int32_t msgCode,
265 const char* pLayerPrefix,
266 const char* pMsg,
267 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600268{
269 char *message = (char *) malloc(strlen(pMsg)+100);
270
271 assert (message);
272
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600273 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
274 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
275 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600276 // We know that we're submitting queues without fences, ignore this warning
277 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600278 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600279 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600280 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600281 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600282 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600283 }
284
285#ifdef _WIN32
286 MessageBox(NULL, message, "Alert", MB_OK);
287#else
288 printf("%s\n",message);
289 fflush(stdout);
290#endif
291 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600292
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600293 /*
294 * false indicates that layer should not bail-out of an
295 * API call that had validation failures. This may mean that the
296 * app dies inside the driver due to invalid parameter(s).
297 * That's what would happen without validation layers, so we'll
298 * keep that behavior here.
299 */
300 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600301}
302
Ian Elliotta0781972015-08-21 15:09:33 -0600303typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600304 VkImage image;
305 VkCmdBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600306 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600307} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600308
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600310#ifdef _WIN32
311#define APP_NAME_STR_LEN 80
312 HINSTANCE connection; // hInstance - Windows Instance
313 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
314 HWND window; // hWnd - window handle
315#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316 xcb_connection_t *connection;
317 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800318 xcb_window_t window;
319 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotta0781972015-08-21 15:09:33 -0600320 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600321#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600322 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700323 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600324 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600326 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600327 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkDevice device;
329 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700330 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600331 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600332 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600333 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
335 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600336 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600337 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338
Ian Elliotta0781972015-08-21 15:09:33 -0600339 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
340 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
341 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
342 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
343 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
344 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
345 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
346 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
347 PFN_vkQueuePresentKHR fpQueuePresentKHR;
348 VkSurfaceDescriptionWindowKHR surface_description;
349 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600350 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600351 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352
Ian Elliottaaae5352015-07-06 14:27:58 -0600353 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
355 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkImage image;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200359 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500360 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600361 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600362 } depth;
363
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600364 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365
366 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkBuffer buf;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200368 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500369 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600370 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600371 } uniform_data;
372
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600373 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500374 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600376 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800377 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600380 mat4x4 projection_matrix;
381 mat4x4 view_matrix;
382 mat4x4 model_matrix;
383
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600384 float spin_angle;
385 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600386 bool pause;
387
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600388 VkShaderModule vert_shader_module;
389 VkShaderModule frag_shader_module;
390
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600391 VkDescriptorPool desc_pool;
392 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800393
Tony Barbourd8e504f2015-10-08 14:26:24 -0600394 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800395
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600396 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600397 int32_t curFrame;
398 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600399 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600400 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600401 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600402 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600403 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600404 VkDbgMsgCallback msg_callback;
405
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600406 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600407 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600408};
409
Ian Elliottcf074d32015-10-16 18:02:43 -0600410// Forward declaration:
411static void demo_resize(struct demo *demo);
412
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600413static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600414{
415 // Search memtypes to find first index with those properties
416 for (uint32_t i = 0; i < 32; i++) {
417 if ((typeBits & 1) == 1) {
418 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600419 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600420 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600421 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600422 }
423 }
424 typeBits >>= 1;
425 }
426 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600427 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600428}
429
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430static void demo_flush_init_cmd(struct demo *demo)
431{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600432 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600434 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435 return;
436
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 assert(!err);
439
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600440 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600441 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600442 VkSubmitInfo submit_info = {
443 .waitSemCount = 0,
444 .pWaitSemaphores = NULL,
445 .cmdBufferCount = 1,
446 .pCommandBuffers = cmd_bufs,
447 .signalSemCount = 0,
448 .pSignalSemaphores = NULL
449 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600451 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452 assert(!err);
453
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600454 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600455 assert(!err);
456
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600457 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600458 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459}
460
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461static void demo_set_image_layout(
462 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600463 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600464 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600465 VkImageLayout old_image_layout,
466 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600467{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600468 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600469
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600470 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600471 const VkCmdBufferAllocInfo cmd = {
472 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600474 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800475 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600476 .count = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 };
478
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600479 err = vkAllocCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600480 assert(!err);
481
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600482 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600484 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600485 .flags = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600486 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600487 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600488 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600489 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600491 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 }
493
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600494 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600495 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 .pNext = NULL,
497 .outputMask = 0,
498 .inputMask = 0,
499 .oldLayout = old_image_layout,
500 .newLayout = new_image_layout,
501 .image = image,
Cody Northrop0e951672015-09-14 13:48:12 -0600502 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503 };
504
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600505 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600506 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600507 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600508 }
509
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600510 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600512 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600513 }
514
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600515 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600516
Tony Barbour50bbca42015-06-29 16:20:35 -0600517 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
518 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600519
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600520 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600521}
522
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600523static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600524{
Chia-I Wuf973e322015-07-08 13:34:24 +0800525 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600527 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600528 .flags = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600529 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600530 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600531 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700532 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800533 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600534 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
535 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800536 };
537 const VkRenderPassBeginInfo rp_begin = {
538 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
539 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800540 .renderPass = demo->render_pass,
541 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800542 .renderArea.offset.x = 0,
543 .renderArea.offset.y = 0,
544 .renderArea.extent.width = demo->width,
545 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600546 .clearValueCount = 2,
547 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700548 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800549 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600551 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552 assert(!err);
553
Chia-I Wua74c5b22015-07-07 11:50:03 +0800554 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
555
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600556 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600557 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600558 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600559 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600560
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600561 VkViewport viewport;
562 memset(&viewport, 0, sizeof(viewport));
563 viewport.height = (float) demo->height;
564 viewport.width = (float) demo->width;
565 viewport.minDepth = (float) 0.0f;
566 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600567 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600568
569 VkRect2D scissor;
570 memset(&scissor, 0, sizeof(scissor));
571 scissor.extent.width = demo->width;
572 scissor.extent.height = demo->height;
573 scissor.offset.x = 0;
574 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600575 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600576
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600577 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800578 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579
Tony Barbour15a4ef62015-10-21 10:14:48 -0600580 VkImageMemoryBarrier prePresentBarrier = {
581 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
582 .pNext = NULL,
583 .outputMask = VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT,
584 .inputMask = 0,
585 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
586 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
587 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
588 .destQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
589 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
590 };
591
592 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
593 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
594 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_GPU_COMMANDS, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
595 VK_FALSE, 1, (const void * const*)&pmemory_barrier);
596
597
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600598 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600599 assert(!err);
600}
601
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600602
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600603void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600605 mat4x4 MVP, Model, VP;
606 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600607 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600608 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600609
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600610 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600611
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600612 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600613 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700614 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600615 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600616
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200617 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 -0600618 assert(!err);
619
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600620 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600621
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600622 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623}
624
625static void demo_draw(struct demo *demo)
626{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600627 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600628 VkSemaphore presentCompleteSemaphore;
629 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
630 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
631 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600632 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600633 };
Tony Barbour155cce82015-07-03 10:33:54 -0600634 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600635
Ian Elliottaaae5352015-07-06 14:27:58 -0600636 err = vkCreateSemaphore(demo->device,
637 &presentCompleteSemaphoreCreateInfo,
638 &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);
651 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
652 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 = {
675 .waitSemCount = 1,
676 .pWaitSemaphores = &presentCompleteSemaphore,
677 .cmdBufferCount = 1,
678 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
679 .signalSemCount = 0,
680 .pSignalSemaphores = NULL
681 };
682
683 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600684 assert(!err);
685
Ian Elliotta0781972015-08-21 15:09:33 -0600686 VkPresentInfoKHR present = {
687 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600688 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600689 .swapchainCount = 1,
Ian Elliottcf074d32015-10-16 18:02:43 -0600690 .swapchains = &demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600691 .imageIndices = &demo->current_buffer,
692 };
693
694// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600695 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600696 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
697 // demo->swapchain is out of date (e.g. the window was resized) and
698 // must be recreated:
699 demo_resize(demo);
700 } else if (err == VK_SUBOPTIMAL_KHR) {
701 // demo->swapchain is not as optimal as it could be, but the platform's
702 // presentation engine will still present the image correctly.
703 } else {
704 assert(!err);
705 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600706
Chia-I Wucbb564e2015-04-16 22:02:10 +0800707 err = vkQueueWaitIdle(demo->queue);
708 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600709
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600710 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600711}
712
713static void demo_prepare_buffers(struct demo *demo)
714{
Ian Elliottaaae5352015-07-06 14:27:58 -0600715 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600716 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600717
718 // Check the surface properties and formats
Ian Elliotta0781972015-08-21 15:09:33 -0600719 VkSurfacePropertiesKHR surfProperties;
720 err = demo->fpGetSurfacePropertiesKHR(demo->device,
721 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600722 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600723 assert(!err);
724
Ian Elliott8528eff2015-08-07 15:56:59 -0600725 uint32_t presentModeCount;
Ian Elliotta0781972015-08-21 15:09:33 -0600726 err = demo->fpGetSurfacePresentModesKHR(demo->device,
727 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600728 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600729 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600730 VkPresentModeKHR *presentModes =
731 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600732 assert(presentModes);
Ian Elliotta0781972015-08-21 15:09:33 -0600733 err = demo->fpGetSurfacePresentModesKHR(demo->device,
734 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600735 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600736 assert(!err);
737
Ian Elliotta0781972015-08-21 15:09:33 -0600738 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600739 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600740 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600741 {
742 // If the surface size is undefined, the size is set to
743 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600744 swapchainExtent.width = demo->width;
745 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600746 }
747 else
748 {
749 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600750 swapchainExtent = surfProperties.currentExtent;
Ian Elliottcf074d32015-10-16 18:02:43 -0600751 demo->width = surfProperties.currentExtent.width;
752 demo->height = surfProperties.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600753 }
754
755 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600756 // tearing mode. If not, try IMMEDIATE which will usually be available,
757 // and is fastest (though it tears). If not, fall back to FIFO which is
758 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600759 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600760 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600761 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
762 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600763 break;
764 }
Ian Elliotta0781972015-08-21 15:09:33 -0600765 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
766 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
767 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600768 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600769 }
770
771 // Determine the number of VkImage's to use in the swap chain (we desire to
772 // own only 1 image at a time, besides the images being displayed and
773 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600774 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600775 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600776 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600777 {
778 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600779 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600780 }
781
Tony Barbour9fd6d352015-09-16 15:01:32 -0600782 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600783 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
784 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600785 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600786 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600787 }
788
Ian Elliottcf074d32015-10-16 18:02:43 -0600789 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600790 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800791 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600792 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
793 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800794 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600795 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800796 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600797 .width = swapchainExtent.width,
798 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600799 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600800 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600801 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800802 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600803 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
804 .queueFamilyCount = 0,
805 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600806 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600807 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600808 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600809 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600810 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600811
Ian Elliottcf074d32015-10-16 18:02:43 -0600812 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800813 assert(!err);
814
Ian Elliottcf074d32015-10-16 18:02:43 -0600815 // If we just re-created an existing swapchain, we should destroy the old
816 // swapchain at this point.
817 // Note: destroying the swapchain also cleans up all its associated
818 // presentable images once the platform is done with them.
819 if (oldSwapchain.handle != VK_NULL_HANDLE) {
820 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
821 }
822
823 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600824 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600825 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800826
Ian Elliotta0781972015-08-21 15:09:33 -0600827 VkImage* swapchainImages =
828 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
829 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600830 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600831 &demo->swapchainImageCount,
832 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600833 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600834
Ian Elliotta0781972015-08-21 15:09:33 -0600835 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600836 assert(demo->buffers);
837
Ian Elliotta0781972015-08-21 15:09:33 -0600838 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600839 VkImageViewCreateInfo color_image_view = {
840 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600841 .pNext = NULL,
842 .format = demo->format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600843 .channels = {
844 .r = VK_CHANNEL_SWIZZLE_R,
845 .g = VK_CHANNEL_SWIZZLE_G,
846 .b = VK_CHANNEL_SWIZZLE_B,
847 .a = VK_CHANNEL_SWIZZLE_A,
848 },
849 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600850 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600851 .baseMipLevel = 0,
Courtney Goeltzenleuchtereae391e2015-10-16 09:46:00 -0600852 .numLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600853 .baseArrayLayer = 0,
Courtney Goeltzenleuchtereae391e2015-10-16 09:46:00 -0600854 .numLayers = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600855 },
856 .viewType = VK_IMAGE_VIEW_TYPE_2D,
857 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600858 };
859
Ian Elliotta0781972015-08-21 15:09:33 -0600860 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600861
Tony Barbour15a4ef62015-10-21 10:14:48 -0600862 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
863 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600864 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600865 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600866 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600867 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600868
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600869 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600870
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600871 err = vkCreateImageView(demo->device,
872 &color_image_view, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600873 assert(!err);
874 }
875}
876
877static void demo_prepare_depth(struct demo *demo)
878{
Tony Barbour72304ef2015-04-16 15:59:00 -0600879 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600880 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600881 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600882 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600883 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600884 .format = depth_format,
885 .extent = { demo->width, demo->height, 1 },
886 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600887 .arrayLayers = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600888 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600889 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600890 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600891 .flags = 0,
892 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200893
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600894 VkImageViewCreateInfo view = {
895 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600896 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600897 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600898 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600899 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600900 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600901 .baseMipLevel = 0,
Courtney Goeltzenleuchtereae391e2015-10-16 09:46:00 -0600902 .numLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600903 .baseArrayLayer = 0,
Courtney Goeltzenleuchtereae391e2015-10-16 09:46:00 -0600904 .numLayers = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600905 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600906 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600907 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600908 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600909
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500910 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600911 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600912 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600913
914 demo->depth.format = depth_format;
915
916 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600917 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600918 &demo->depth.image);
919 assert(!err);
920
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600921 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600922 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600923 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600924
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200925 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
926 demo->depth.mem_alloc.pNext = NULL;
927 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
928 demo->depth.mem_alloc.memoryTypeIndex = 0;
929
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600930 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600931 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600932 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200933 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600934 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600935
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500936 /* allocate memory */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200937 err = vkAllocMemory(demo->device, &demo->depth.mem_alloc, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500938 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600939
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500940 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600941 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500942 demo->depth.mem, 0);
943 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600945 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600946 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600947 VK_IMAGE_LAYOUT_UNDEFINED,
948 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600949
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950 /* create image view */
951 view.image = demo->depth.image;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600952 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600953 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954}
955
Tony Barbour1a86d032015-09-21 15:17:33 -0600956/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700957bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600958 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600959 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600960{
Tony Barbour1a86d032015-09-21 15:17:33 -0600961 FILE *fPtr = fopen(filename,"rb");
962 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600963
Tony Barbour1a86d032015-09-21 15:17:33 -0600964 if (!fPtr)
965 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600966
Tony Barbour1a86d032015-09-21 15:17:33 -0600967 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600968 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
969 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600970 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600971 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600972
Tony Barbour1a86d032015-09-21 15:17:33 -0600973 do {
974 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600975 if (cPtr == NULL) {
976 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600977 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600978 }
Tony Barbour1a86d032015-09-21 15:17:33 -0600979 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600980
Tony Barbour1a86d032015-09-21 15:17:33 -0600981 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600982 if (rgba_data == NULL) {
983 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600984 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600985 }
Tony Barbour1a86d032015-09-21 15:17:33 -0600986 fgets(header, 256, fPtr); // Format
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600987 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
988 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -0600989 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -0600990 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600991
Tony Barbour1a86d032015-09-21 15:17:33 -0600992 for(int y = 0; y < *height; y++)
993 {
994 uint8_t *rowPtr = rgba_data;
995 for(int x = 0; x < *width; x++)
996 {
997 fread(rowPtr, 3, 1, fPtr);
998 rowPtr[3] = 255; /* Alpha of 1 */
999 rowPtr += 4;
1000 }
1001 rgba_data += layout->rowPitch;
1002 }
1003 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001004 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001005}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001006
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007static void demo_prepare_texture_image(struct demo *demo,
1008 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001009 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001010 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001011 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001012 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001014 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001015 int32_t tex_width;
1016 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001017 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001018 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019
David Pinedo30bd71d2015-04-23 08:16:57 -06001020 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1021 {
1022 printf("Failed to load textures\n");
1023 fflush(stdout);
1024 exit(1);
1025 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001027 tex_obj->tex_width = tex_width;
1028 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001030 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001031 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001033 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001034 .format = tex_format,
1035 .extent = { tex_width, tex_height, 1 },
1036 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001037 .arrayLayers = 1,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038 .samples = 1,
1039 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001040 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041 .flags = 0,
1042 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001044 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001045
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001047 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048 assert(!err);
1049
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001050 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001051
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001052 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1053 tex_obj->mem_alloc.pNext = NULL;
1054 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1055 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001056
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001057 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1058 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001059
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001060 /* allocate memory */
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001061 err = vkAllocMemory(demo->device, &tex_obj->mem_alloc,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001062 &(tex_obj->mem));
1063 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001065 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001066 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001067 tex_obj->mem, 0);
1068 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001070 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001071 const VkImageSubresource subres = {
Courtney Goeltzenleuchter51041262015-10-21 17:00:51 -06001072 .aspect = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001074 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001076 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001077 void *data;
1078
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001079 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001081 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082 assert(!err);
1083
1084 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1085 fprintf(stderr, "Error loading texture: %s\n", filename);
1086 }
1087
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001088 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001089 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001090
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001091 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001092 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001093 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001094 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001095 tex_obj->imageLayout);
1096 /* 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 -07001097}
1098
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001099static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001100{
1101 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001103 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001104}
1105
1106static void demo_prepare_textures(struct demo *demo)
1107{
Tony Barbour72304ef2015-04-16 15:59:00 -06001108 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001109 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001110 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001112 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001113
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001115 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001116
FslNopper6a7e50e2015-05-06 21:42:01 +02001117 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118 /* Device can texture using linear textures */
1119 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001120 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001121 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001122 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001123 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001124
1125 memset(&staging_texture, 0, sizeof(staging_texture));
1126 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001127 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001128
1129 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001130 VK_IMAGE_TILING_OPTIMAL,
1131 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1132 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001134 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001135 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001136 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001137 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001139 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001140 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001141 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001142 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001144 VkImageCopy copy_region = {
Courtney Goeltzenleuchter51041262015-10-21 17:00:51 -06001145 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001146 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter51041262015-10-21 17:00:51 -06001147 .destSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148 .destOffset = { 0, 0, 0 },
1149 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1150 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001151 vkCmdCopyImage(demo->cmd,
1152 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1153 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001154 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001156 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001157 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001158 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001159 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001161 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001163 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001165 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1166 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167 }
1168
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001169 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001170 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001171 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001172 .magFilter = VK_TEX_FILTER_NEAREST,
1173 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001174 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter781da8a2015-09-10 14:08:50 -06001175 .addressModeU = VK_TEX_ADDRESS_MODE_CLAMP,
1176 .addressModeV = VK_TEX_ADDRESS_MODE_CLAMP,
1177 .addressModeW = VK_TEX_ADDRESS_MODE_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001178 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001179 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001180 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001181 .minLod = 0.0f,
1182 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001183 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001184 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001185 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001186
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001187 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001188 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001189 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001190 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001191 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001192 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001193 .channels = { VK_CHANNEL_SWIZZLE_R,
1194 VK_CHANNEL_SWIZZLE_G,
1195 VK_CHANNEL_SWIZZLE_B,
1196 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001197 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001198 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001200
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001202 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203 &demo->textures[i].sampler);
1204 assert(!err);
1205
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 /* create image view */
1207 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001208 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209 &demo->textures[i].view);
1210 assert(!err);
1211 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001213
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001214void demo_prepare_cube_data_buffer(struct demo *demo)
1215{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001216 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001217 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001218 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001219 int i;
1220 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001221 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001222 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001223 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001224
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001225 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001226 mat4x4_mul(MVP, VP, demo->model_matrix);
1227 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001228// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001229
1230 for (i=0; i<12*3; i++) {
1231 data.position[i][0] = g_vertex_buffer_data[i*3];
1232 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1233 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1234 data.position[i][3] = 1.0f;
1235 data.attr[i][0] = g_uv_buffer_data[2*i];
1236 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1237 data.attr[i][2] = 0;
1238 data.attr[i][3] = 0;
1239 }
1240
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001241 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001242 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001243 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001244 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001245 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001246 assert(!err);
1247
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001248 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001249
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001250 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1251 demo->uniform_data.mem_alloc.pNext = NULL;
1252 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1253 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1254
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001255 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001256 mem_reqs.memoryTypeBits,
1257 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001258 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001259 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001260
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001261 err = vkAllocMemory(demo->device, &demo->uniform_data.mem_alloc, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001262 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001263
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001264 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 -05001265 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001266
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001267 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001268
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001269 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001270
Tony Barbour155cce82015-07-03 10:33:54 -06001271 err = vkBindBufferMemory(demo->device,
1272 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001273 demo->uniform_data.mem, 0);
1274 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001275
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001276 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1277 demo->uniform_data.buffer_info.offset = 0;
1278 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001279}
1280
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001281static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001282{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001283 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001284 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001285 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001286 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001287 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001288 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001289 },
1290 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001291 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001292 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001293 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001294 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001295 },
1296 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001297 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001298 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001299 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001300 .count = 2,
1301 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001302 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001303 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001304
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001305 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001306 &descriptor_layout, &demo->desc_layout);
1307 assert(!err);
1308
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001309 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1310 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1311 .pNext = NULL,
1312 .descriptorSetCount = 1,
1313 .pSetLayouts = &demo->desc_layout,
1314 };
1315
1316 err = vkCreatePipelineLayout(demo->device,
1317 &pPipelineLayoutCreateInfo,
1318 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001319 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001320}
1321
Chia-I Wuf973e322015-07-08 13:34:24 +08001322static void demo_prepare_render_pass(struct demo *demo)
1323{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001324 const VkAttachmentDescription attachments[2] = {
1325 [0] = {
1326 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1327 .pNext = NULL,
1328 .format = demo->format,
1329 .samples = 1,
1330 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1331 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1332 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1333 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1334 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1335 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1336 },
1337 [1] = {
1338 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1339 .pNext = NULL,
1340 .format = demo->depth.format,
1341 .samples = 1,
1342 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1343 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1344 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1345 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1346 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1347 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1348 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001349 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001350 const VkAttachmentReference color_reference = {
1351 .attachment = 0,
1352 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1353 };
1354 const VkSubpassDescription subpass = {
1355 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1356 .pNext = NULL,
1357 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1358 .flags = 0,
1359 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001360 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001361 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001362 .pColorAttachments = &color_reference,
1363 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001364 .depthStencilAttachment = {
1365 .attachment = 1,
1366 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1367 },
1368 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001369 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001370 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001371 const VkRenderPassCreateInfo rp_info = {
1372 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1373 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001374 .attachmentCount = 2,
1375 .pAttachments = attachments,
1376 .subpassCount = 1,
1377 .pSubpasses = &subpass,
1378 .dependencyCount = 0,
1379 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001380 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001381 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001382
1383 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1384 assert(!err);
1385}
1386
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001387static VkShader demo_prepare_shader(struct demo* demo,
Courtney Goeltzenleuchter34d57d52015-10-21 17:08:06 -06001388 VkShaderStageFlagBits stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001389 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001390 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001391 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001393 VkShaderModuleCreateInfo moduleCreateInfo;
1394 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001395 VkShader shader;
1396 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001398
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001399 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1400 moduleCreateInfo.pNext = NULL;
1401
1402 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1403 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001404 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001405
Cody Northrop1fedb212015-05-28 11:27:16 -06001406 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001407 moduleCreateInfo.codeSize = size;
1408 moduleCreateInfo.pCode = code;
1409 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001410 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001411 assert(!err);
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001412
1413 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001414 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001415 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001416 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001417 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001418 assert(!err);
Cody Northrop1fedb212015-05-28 11:27:16 -06001419 } else {
1420 // Create fake SPV structure to feed GLSL
1421 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001422 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1423 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1424 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001425
1426 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001427 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1428 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1429 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1430 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001431
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001432 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001433 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001434 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001435 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001436
1437 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001438 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001439 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001440 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001441 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001442 assert(!err);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001443 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001444 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001445 return shader;
1446}
1447
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001448char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001449{
1450 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001451 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001452 void *shader_code;
1453
1454 FILE *fp = fopen(filename, "rb");
1455 if (!fp) return NULL;
1456
1457 fseek(fp, 0L, SEEK_END);
1458 size = ftell(fp);
1459
1460 fseek(fp, 0L, SEEK_SET);
1461
1462 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001463 retval = fread(shader_code, size, 1, fp);
1464 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001465
1466 *psize = size;
1467
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001468 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001469 return shader_code;
1470}
1471
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001472static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001473{
Cody Northrop1fedb212015-05-28 11:27:16 -06001474 if (!demo->use_glsl) {
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001475 VkShader shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001476 void *vertShaderCode;
1477 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001478
Cody Northrop1fedb212015-05-28 11:27:16 -06001479 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001480
Courtney Goeltzenleuchter34d57d52015-10-21 17:08:06 -06001481 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT, &demo->vert_shader_module,
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001482 vertShaderCode, size);
1483 free(vertShaderCode);
1484 return shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001485 } else {
1486 static const char *vertShaderText =
1487 "#version 140\n"
1488 "#extension GL_ARB_separate_shader_objects : enable\n"
1489 "#extension GL_ARB_shading_language_420pack : enable\n"
1490 "\n"
1491 "layout(binding = 0) uniform buf {\n"
1492 " mat4 MVP;\n"
1493 " vec4 position[12*3];\n"
1494 " vec4 attr[12*3];\n"
1495 "} ubuf;\n"
1496 "\n"
1497 "layout (location = 0) out vec4 texcoord;\n"
1498 "\n"
1499 "void main() \n"
1500 "{\n"
1501 " texcoord = ubuf.attr[gl_VertexID];\n"
1502 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1503 "\n"
1504 " // GL->VK conventions\n"
1505 " gl_Position.y = -gl_Position.y;\n"
1506 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1507 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001508
Courtney Goeltzenleuchter34d57d52015-10-21 17:08:06 -06001509 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001510 (const void *) vertShaderText,
1511 strlen(vertShaderText));
1512 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001513}
1514
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001515static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001516{
Cody Northrop1fedb212015-05-28 11:27:16 -06001517 if (!demo->use_glsl) {
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001518 VkShader shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001519 void *fragShaderCode;
1520 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001521
Cody Northrop1fedb212015-05-28 11:27:16 -06001522 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001523
Courtney Goeltzenleuchter34d57d52015-10-21 17:08:06 -06001524 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT, &demo->frag_shader_module,
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001525 fragShaderCode, size);
1526 free(fragShaderCode);
1527 return shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001528 } else {
1529 static const char *fragShaderText =
1530 "#version 140\n"
1531 "#extension GL_ARB_separate_shader_objects : enable\n"
1532 "#extension GL_ARB_shading_language_420pack : enable\n"
1533 "layout (binding = 1) uniform sampler2D tex;\n"
1534 "\n"
1535 "layout (location = 0) in vec4 texcoord;\n"
1536 "layout (location = 0) out vec4 uFragColor;\n"
1537 "void main() {\n"
1538 " uFragColor = texture(tex, texcoord.xy);\n"
1539 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001540
Courtney Goeltzenleuchter34d57d52015-10-21 17:08:06 -06001541 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001542 (const void *) fragShaderText,
1543 strlen(fragShaderText));
1544 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001545}
1546
1547static void demo_prepare_pipeline(struct demo *demo)
1548{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001549 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001550 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001551 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001552 VkPipelineInputAssemblyStateCreateInfo ia;
1553 VkPipelineRasterStateCreateInfo rs;
1554 VkPipelineColorBlendStateCreateInfo cb;
1555 VkPipelineDepthStencilStateCreateInfo ds;
1556 VkPipelineViewportStateCreateInfo vp;
1557 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniellba839d12015-09-29 13:01:09 -06001558 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1559 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001560 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561
Piers Daniellba839d12015-09-29 13:01:09 -06001562 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1563 memset(&dynamicState, 0, sizeof dynamicState);
1564 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1565 dynamicState.pDynamicStates = dynamicStateEnables;
1566
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001568 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001569 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001571 memset(&vi, 0, sizeof(vi));
1572 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1573
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001575 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001576 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001577
1578 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001579 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001580 rs.fillMode = VK_FILL_MODE_SOLID;
1581 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001582 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001583 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001584 rs.rasterizerDiscardEnable = VK_FALSE;
1585 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001586
1587 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001588 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1589 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001590 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001591 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001592 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001593 cb.attachmentCount = 1;
1594 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001595
Tony Barbour29645d02015-01-16 14:27:35 -07001596 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001597 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001598 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001599 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1600 vp.scissorCount = 1;
1601 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001602
1603 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001604 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001605 ds.depthTestEnable = VK_TRUE;
1606 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001607 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001608 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001609 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1610 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001611 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001612 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001613 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614
Tony Barbour29645d02015-01-16 14:27:35 -07001615 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001616 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001617 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001618 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001619
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001620 // Two stages: vs and fs
1621 pipeline.stageCount = 2;
1622 VkPipelineShaderStageCreateInfo shaderStages[2];
1623 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1624
1625 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001626 shaderStages[0].shader = demo_prepare_vs(demo);
1627
1628 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001629 shaderStages[1].shader = demo_prepare_fs(demo);
1630
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001631 memset(&pipelineCache, 0, sizeof(pipelineCache));
1632 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001633
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001634 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1635 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001636
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001637 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001638 pipeline.pInputAssemblyState = &ia;
1639 pipeline.pRasterState = &rs;
1640 pipeline.pColorBlendState = &cb;
1641 pipeline.pMultisampleState = &ms;
1642 pipeline.pViewportState = &vp;
1643 pipeline.pDepthStencilState = &ds;
1644 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001645 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001646 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001647
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001648 pipeline.renderPass = demo->render_pass;
1649
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001650 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001651 assert(!err);
1652
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001653 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001654 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001655 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001656 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1657 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001658}
1659
Chia-I Wu63ea9262015-03-26 13:14:16 +08001660static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001661{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001662 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001663 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001664 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001665 .count = 1,
1666 },
1667 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001668 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001669 .count = DEMO_TEXTURE_COUNT,
1670 },
1671 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001672 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001673 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001674 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001675 .maxSets = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001676 .count = 2,
1677 .pTypeCount = type_counts,
1678 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001679 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001680
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001681 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001682 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001683 assert(!err);
1684}
1685
1686static void demo_prepare_descriptor_set(struct demo *demo)
1687{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001688 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001689 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001690 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001691 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001692
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001693 VkDescriptorSetAllocInfo alloc_info = {
1694 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO,
1695 .pNext = NULL,
1696 .descriptorPool = demo->desc_pool,
1697 .count = 1,
1698 .pSetLayouts = &demo->desc_layout
1699 };
1700 err = vkAllocDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001701 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001702
Chia-I Wuae721ba2015-05-25 16:27:55 +08001703 memset(&tex_descs, 0, sizeof(tex_descs));
1704 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001705 tex_descs[i].sampler = demo->textures[i].sampler;
1706 tex_descs[i].imageView = demo->textures[i].view;
1707 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001708 }
1709
1710 memset(&writes, 0, sizeof(writes));
1711
1712 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1713 writes[0].destSet = demo->desc_set;
1714 writes[0].count = 1;
1715 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001716 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001717
1718 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1719 writes[1].destSet = demo->desc_set;
1720 writes[1].destBinding = 1;
1721 writes[1].count = DEMO_TEXTURE_COUNT;
1722 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001723 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001724
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001725 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001726}
1727
Chia-I Wuf973e322015-07-08 13:34:24 +08001728static void demo_prepare_framebuffers(struct demo *demo)
1729{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001730 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001731 attachments[1] = demo->depth.view;
1732
Chia-I Wuf973e322015-07-08 13:34:24 +08001733 const VkFramebufferCreateInfo fb_info = {
1734 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1735 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001736 .renderPass = demo->render_pass,
1737 .attachmentCount = 2,
1738 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001739 .width = demo->width,
1740 .height = demo->height,
1741 .layers = 1,
1742 };
1743 VkResult U_ASSERT_ONLY err;
1744 uint32_t i;
1745
Tony Barbourd8e504f2015-10-08 14:26:24 -06001746 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1747 assert(demo->framebuffers);
1748
1749 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001750 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001751 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1752 assert(!err);
1753 }
1754}
1755
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001756static void demo_prepare(struct demo *demo)
1757{
Cody Northrop91e221b2015-07-09 18:08:32 -06001758 VkResult U_ASSERT_ONLY err;
1759
1760 const VkCmdPoolCreateInfo cmd_pool_info = {
1761 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1762 .pNext = NULL,
1763 .queueFamilyIndex = demo->graphics_queue_node_index,
1764 .flags = 0,
1765 };
1766 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1767 assert(!err);
1768
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001769 const VkCmdBufferAllocInfo cmd = {
1770 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001771 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001772 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001773 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001774 .count = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001775 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001776
1777 demo_prepare_buffers(demo);
1778 demo_prepare_depth(demo);
1779 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001780 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001781
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001782 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001783 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001785
Ian Elliotta0781972015-08-21 15:09:33 -06001786 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001787 err = vkAllocCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001788 assert(!err);
1789 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001790
Chia-I Wu63ea9262015-03-26 13:14:16 +08001791 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001792 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001793
Chia-I Wuf973e322015-07-08 13:34:24 +08001794 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001795
Ian Elliotta0781972015-08-21 15:09:33 -06001796 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001797 demo->current_buffer = i;
1798 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1799 }
1800
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001801 /*
1802 * Prepare functions above may generate pipeline commands
1803 * that need to be flushed before beginning the render loop.
1804 */
1805 demo_flush_init_cmd(demo);
1806
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001807 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001808 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001809}
1810
David Pinedo2bb7c932015-06-18 17:03:14 -06001811static void demo_cleanup(struct demo *demo)
1812{
1813 uint32_t i;
1814
1815 demo->prepared = false;
1816
Tony Barbourd8e504f2015-10-08 14:26:24 -06001817 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001818 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1819 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001820 free(demo->framebuffers);
Tony Barbour155cce82015-07-03 10:33:54 -06001821 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001822
Tony Barbour155cce82015-07-03 10:33:54 -06001823 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001824 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001825 vkDestroyRenderPass(demo->device, demo->render_pass);
1826 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1827 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001828
1829 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001830 vkDestroyImageView(demo->device, demo->textures[i].view);
1831 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001832 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001833 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001834 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001835 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001836
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001837 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbour155cce82015-07-03 10:33:54 -06001838 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001839 vkFreeMemory(demo->device, demo->depth.mem);
1840
Tony Barbour155cce82015-07-03 10:33:54 -06001841 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001842 vkFreeMemory(demo->device, demo->uniform_data.mem);
1843
Ian Elliotta0781972015-08-21 15:09:33 -06001844 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001845 vkDestroyImageView(demo->device, demo->buffers[i].view);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001846 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001847 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001848 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001849
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001850 free(demo->queue_props);
1851
Cody Northrop91e221b2015-07-09 18:08:32 -06001852 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001853 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001854 if (demo->validate) {
1855 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1856 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001857 vkDestroyInstance(demo->inst);
1858
1859#ifndef _WIN32
1860 xcb_destroy_window(demo->connection, demo->window);
1861 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001862 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001863#endif // _WIN32
1864}
1865
Ian Elliottcf074d32015-10-16 18:02:43 -06001866static void demo_resize(struct demo *demo)
1867{
1868 uint32_t i;
1869
Mike Stroyanbb60b382015-10-28 09:46:57 -06001870 // Don't react to resize until after first initialization.
1871 if (!demo->prepared) {
1872 return;
1873 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001874 // In order to properly resize the window, we must re-create the swapchain
1875 // AND redo the command buffers, etc.
1876 //
1877 // First, perform part of the demo_cleanup() function:
1878 demo->prepared = false;
1879
1880 for (i = 0; i < demo->swapchainImageCount; i++) {
1881 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1882 }
1883 free(demo->framebuffers);
1884 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
1885
1886 vkDestroyPipeline(demo->device, demo->pipeline);
1887 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
1888 vkDestroyRenderPass(demo->device, demo->render_pass);
1889 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1890 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
1891
1892 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1893 vkDestroyImageView(demo->device, demo->textures[i].view);
1894 vkDestroyImage(demo->device, demo->textures[i].image);
1895 vkFreeMemory(demo->device, demo->textures[i].mem);
1896 vkDestroySampler(demo->device, demo->textures[i].sampler);
1897 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001898
1899 vkDestroyImageView(demo->device, demo->depth.view);
1900 vkDestroyImage(demo->device, demo->depth.image);
1901 vkFreeMemory(demo->device, demo->depth.mem);
1902
1903 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
1904 vkFreeMemory(demo->device, demo->uniform_data.mem);
1905
1906 for (i = 0; i < demo->swapchainImageCount; i++) {
1907 vkDestroyImageView(demo->device, demo->buffers[i].view);
Ian Elliott55234c42015-10-27 11:06:33 -06001908 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001909 }
Mark Lobodzinski331e4e42015-10-29 14:23:47 -06001910 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Ian Elliottcf074d32015-10-16 18:02:43 -06001911 free(demo->buffers);
1912
Ian Elliott55234c42015-10-27 11:06:33 -06001913 vkDestroyCommandPool(demo->device, demo->cmd_pool);
1914
Ian Elliottcf074d32015-10-16 18:02:43 -06001915
1916 // Second, re-perform the demo_prepare() function, which will re-create the
1917 // swapchain:
1918 demo_prepare(demo);
1919}
1920
David Pinedo2bb7c932015-06-18 17:03:14 -06001921// On MS-Windows, make this a global, so it's available to WndProc()
1922struct demo demo;
1923
Ian Elliott639ca472015-04-16 15:23:05 -06001924#ifdef _WIN32
1925static void demo_run(struct demo *demo)
1926{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001927 if (!demo->prepared)
1928 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001929 // Wait for work to finish before updating MVP.
1930 vkDeviceWaitIdle(demo->device);
1931 demo_update_data_buffer(demo);
1932
1933 demo_draw(demo);
1934
1935 // Wait for work to finish before updating MVP.
1936 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001937
David Pinedo2bb7c932015-06-18 17:03:14 -06001938 demo->curFrame++;
1939
1940 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1941 {
1942 demo->quit=true;
1943 demo_cleanup(demo);
1944 ExitProcess(0);
1945 }
1946
1947}
Ian Elliott639ca472015-04-16 15:23:05 -06001948
1949// MS-Windows event handling function:
1950LRESULT CALLBACK WndProc(HWND hWnd,
1951 UINT uMsg,
1952 WPARAM wParam,
1953 LPARAM lParam)
1954{
Ian Elliott639ca472015-04-16 15:23:05 -06001955 switch(uMsg)
1956 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001957 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001958 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001959 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001960 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001961 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001962 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001963 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001964 demo.width = lParam & 0xffff;
1965 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001966 demo_resize(&demo);
1967 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001968 default:
1969 break;
1970 }
1971 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1972}
1973
1974static void demo_create_window(struct demo *demo)
1975{
1976 WNDCLASSEX win_class;
1977
1978 // Initialize the window class structure:
1979 win_class.cbSize = sizeof(WNDCLASSEX);
1980 win_class.style = CS_HREDRAW | CS_VREDRAW;
1981 win_class.lpfnWndProc = WndProc;
1982 win_class.cbClsExtra = 0;
1983 win_class.cbWndExtra = 0;
1984 win_class.hInstance = demo->connection; // hInstance
1985 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1986 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1987 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1988 win_class.lpszMenuName = NULL;
1989 win_class.lpszClassName = demo->name;
1990 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1991 // Register window class:
1992 if (!RegisterClassEx(&win_class)) {
1993 // It didn't work, so try to give a useful error:
1994 printf("Unexpected error trying to start the application!\n");
1995 fflush(stdout);
1996 exit(1);
1997 }
1998 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001999 RECT wr = { 0, 0, demo->width, demo->height };
2000 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002001 demo->window = CreateWindowEx(0,
2002 demo->name, // class name
2003 demo->name, // app name
2004 WS_OVERLAPPEDWINDOW | // window style
2005 WS_VISIBLE |
2006 WS_SYSMENU,
2007 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06002008 wr.right-wr.left, // width
2009 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06002010 NULL, // handle to parent
2011 NULL, // handle to menu
2012 demo->connection, // hInstance
2013 NULL); // no extra parameters
2014 if (!demo->window) {
2015 // It didn't work, so try to give a useful error:
2016 printf("Cannot create a window in which to draw!\n");
2017 fflush(stdout);
2018 exit(1);
2019 }
2020}
2021#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002022static void demo_handle_event(struct demo *demo,
2023 const xcb_generic_event_t *event)
2024{
Piers Daniell735ee532015-02-23 16:23:13 -07002025 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002026 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002027 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002028 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002029 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002030 case XCB_CLIENT_MESSAGE:
2031 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2032 (*demo->atom_wm_delete_window).atom) {
2033 demo->quit = true;
2034 }
2035 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002036 case XCB_KEY_RELEASE:
2037 {
2038 const xcb_key_release_event_t *key =
2039 (const xcb_key_release_event_t *) event;
2040
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002041 switch (key->detail) {
2042 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002043 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002044 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002045 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002046 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002047 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002048 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002049 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002050 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002051 case 0x41:
2052 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002053 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002054 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002055 }
2056 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06002057 case XCB_CONFIGURE_NOTIFY:
2058 {
2059 const xcb_configure_notify_event_t *cfg =
2060 (const xcb_configure_notify_event_t *) event;
2061 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2062 demo_resize(demo);
2063 }
2064 }
2065 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002066 default:
2067 break;
2068 }
2069}
2070
2071static void demo_run(struct demo *demo)
2072{
2073 xcb_flush(demo->connection);
2074
2075 while (!demo->quit) {
2076 xcb_generic_event_t *event;
2077
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002078 if (demo->pause) {
2079 event = xcb_wait_for_event(demo->connection);
2080 } else {
2081 event = xcb_poll_for_event(demo->connection);
2082 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002083 if (event) {
2084 demo_handle_event(demo, event);
2085 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002086 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002087
2088 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002089 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002090 demo_update_data_buffer(demo);
2091
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002092 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002093
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002094 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002095 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002096 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002097 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002098 demo->quit = true;
2099
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002100 }
2101}
2102
2103static void demo_create_window(struct demo *demo)
2104{
2105 uint32_t value_mask, value_list[32];
2106
2107 demo->window = xcb_generate_id(demo->connection);
2108
2109 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2110 value_list[0] = demo->screen->black_pixel;
2111 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002112 XCB_EVENT_MASK_EXPOSURE |
2113 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002114
2115 xcb_create_window(demo->connection,
2116 XCB_COPY_FROM_PARENT,
2117 demo->window, demo->screen->root,
2118 0, 0, demo->width, demo->height, 0,
2119 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2120 demo->screen->root_visual,
2121 value_mask, value_list);
2122
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002123 /* Magic code that will send notification when window is destroyed */
2124 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2125 "WM_PROTOCOLS");
2126 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2127
2128 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2129 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2130
2131 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2132 demo->window, (*reply).atom, 4, 32, 1,
2133 &(*demo->atom_wm_delete_window).atom);
2134 free(reply);
2135
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002136 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002137
2138 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2139 const uint32_t coords[] = {100, 100};
2140 xcb_configure_window(demo->connection, demo->window,
2141 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002142}
Ian Elliott639ca472015-04-16 15:23:05 -06002143#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002144
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002145/*
2146 * Return 1 (true) if all layer names specified in check_names
2147 * can be found in given layer properties.
2148 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002149static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002150 uint32_t layer_count, VkLayerProperties *layers)
2151{
2152 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002153 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002154 for (uint32_t j = 0; j < layer_count; j++) {
2155 if (!strcmp(check_names[i], layers[j].layerName)) {
2156 found = 1;
2157 }
2158 }
2159 if (!found) {
2160 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2161 return 0;
2162 }
2163 }
2164 return 1;
2165}
2166
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002167static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002168{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002169 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002170 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002171 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002172 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002173 VkLayerProperties *instance_layers;
2174 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002175 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002176 uint32_t instance_layer_count = 0;
2177 uint32_t enabled_extension_count = 0;
2178 uint32_t enabled_layer_count = 0;
2179
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002180 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002181 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002182 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002183 "ObjectTracker",
2184 "DrawState",
2185 "ParamChecker",
2186 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002187 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002188 "DeviceLimits",
2189 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002190 };
2191
2192 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002193 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002194 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002195 "ObjectTracker",
2196 "DrawState",
2197 "ParamChecker",
2198 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002199 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002200 "DeviceLimits",
2201 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002202 };
2203
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002204 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002205 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002206 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002207 assert(!err);
2208
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002209 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002210 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002211 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002212
2213 if (demo->validate) {
2214 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2215 instance_layer_count, instance_layers);
2216 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002217 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002218 "required validation layer.\n\n"
2219 "Please look at the Getting Started guide for additional "
2220 "information.\n",
2221 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002222 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002223 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002224 }
2225
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002226 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002227 assert(!err);
2228
Tony Barbourcc69f452015-10-08 13:59:42 -06002229 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002230 memset(extension_names, 0, sizeof(extension_names));
2231 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002232 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002233 assert(!err);
2234 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002235 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002236 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002237 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002238 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002239 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002240 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002241 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002242 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002243 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002244 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002245 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002246 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002247 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002248 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002249 "Vulkan installable client driver (ICD) installed?\nPlease "
2250 "look at the Getting Started guide for additional "
2251 "information.\n",
2252 "vkCreateInstance Failure");
2253 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002254 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002255 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002256 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002257 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002258 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002259 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002260 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002261 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002262 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002263 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002264 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002265 .pNext = NULL,
2266 .pAppInfo = &app,
2267 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002268 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002269 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002270 .extensionCount = enabled_extension_count,
2271 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002272 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002273
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002274 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002275
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002276 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002277 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2278 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002279 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002280 "additional information.\n",
2281 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002282 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002283 ERR_EXIT("Cannot find a specified extension library"
2284 ".\nMake sure your layers path is set appropriately\n",
2285 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002286 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002287 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2288 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002289 "the Getting Started guide for additional information.\n",
2290 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002291 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002292
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002293 free(instance_layers);
2294 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002295
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002296 /* Make initial call to query gpu_count, then second call for gpu info*/
2297 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2298 assert(!err && gpu_count > 0);
2299 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2300 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2301 assert(!err);
2302 /* For cube demo we just grab the first physical device */
2303 demo->gpu = physical_devices[0];
2304 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002305
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002306 /* Look for validation layers */
2307 validation_found = 0;
2308 enabled_layer_count = 0;
2309 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002310 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002311 assert(!err);
2312
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002313 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002314 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002315 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002316
2317 if (demo->validate) {
2318 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2319 device_layer_count, device_layers);
2320 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002321 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002322 "a required validation layer.\n\n"
2323 "Please look at the Getting Started guide for additional "
2324 "information.\n",
2325 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002326 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002327 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002328 }
2329
2330 uint32_t device_extension_count = 0;
2331 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002332 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002333 demo->gpu, NULL, &device_extension_count, NULL);
2334 assert(!err);
2335
Tony Barbourcc69f452015-10-08 13:59:42 -06002336 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002337 enabled_extension_count = 0;
2338 memset(extension_names, 0, sizeof(extension_names));
2339 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002340 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002341 demo->gpu, NULL, &device_extension_count, device_extensions);
2342 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002343
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002344 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002345 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002346 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002347 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002348 }
2349 assert(enabled_extension_count < 64);
2350 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002351 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002352 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002353 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002354 "Vulkan installable client driver (ICD) installed?\nPlease "
2355 "look at the Getting Started guide for additional "
2356 "information.\n",
2357 "vkCreateInstance Failure");
2358 }
2359
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002360 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002361 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2362 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002363 if (!demo->dbgCreateMsgCallback) {
2364 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2365 "vkGetProcAddr Failure");
2366 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002367 if (!demo->dbgDestroyMsgCallback) {
2368 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2369 "vkGetProcAddr Failure");
2370 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002371 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2372 if (!demo->dbgBreakCallback) {
2373 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2374 "vkGetProcAddr Failure");
2375 }
2376
2377 PFN_vkDbgMsgCallback callback;
2378
2379 if (!demo->use_break) {
2380 callback = dbgFunc;
2381 } else {
2382 callback = demo->dbgBreakCallback;
2383 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002384 err = demo->dbgCreateMsgCallback(
2385 demo->inst,
2386 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002387 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002388 &demo->msg_callback);
2389 switch (err) {
2390 case VK_SUCCESS:
2391 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002392 case VK_ERROR_OUT_OF_HOST_MEMORY:
2393 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2394 "dbgCreateMsgCallback Failure");
2395 break;
2396 default:
2397 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2398 "dbgCreateMsgCallback Failure");
2399 break;
2400 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002401 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002402 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002403
2404 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002405 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002406 assert(demo->queue_count >= 1);
2407
2408 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002409 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002410 // Find a queue that supports gfx
2411 uint32_t gfx_queue_idx = 0;
2412 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2413 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2414 break;
2415 }
2416 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002417 // Query fine-grained feature support for this device.
2418 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002419 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002420 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002421
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002422 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002423 const VkDeviceQueueCreateInfo queue = {
2424 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2425 .pNext = NULL,
2426 .queueFamilyIndex = gfx_queue_idx,
2427 .queueCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002428 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002429 };
2430
2431 VkDeviceCreateInfo device = {
2432 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2433 .pNext = NULL,
Courtney Goeltzenleuchter8ab618c2015-10-15 16:58:44 -06002434 .requestedQueueCount = 1,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002435 .pRequestedQueues = &queue,
2436 .layerCount = enabled_layer_count,
2437 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
2438 .extensionCount = enabled_extension_count,
2439 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002440 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002441 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002442
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002443 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002444 assert(!err);
2445
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002446 free(device_layers);
2447
Ian Elliotta0781972015-08-21 15:09:33 -06002448 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2449 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2450 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2451 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2452 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002453 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2454 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2455 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2456 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002457}
2458
Tony Barbourcc69f452015-10-08 13:59:42 -06002459static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002460{
2461 VkResult err;
2462 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002463
Tony Barbourcc69f452015-10-08 13:59:42 -06002464 // Construct the surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002465 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002466 demo->surface_description.pNext = NULL;
2467#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002468 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002469 demo->surface_description.pPlatformHandle = demo->connection;
2470 demo->surface_description.pPlatformWindow = demo->window;
2471#else // _WIN32
2472 demo->platform_handle_xcb.connection = demo->connection;
2473 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002474 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002475 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2476 demo->surface_description.pPlatformWindow = &demo->window;
2477#endif // _WIN32
2478
Tony Barbourcc69f452015-10-08 13:59:42 -06002479 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002480 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2481 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002482 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2483 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002484 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002485 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002486
2487 // Search for a graphics and a present queue in the array of queue
2488 // families, try to find one that supports both
2489 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2490 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002491 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002492 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2493 if (graphicsQueueNodeIndex == UINT32_MAX) {
2494 graphicsQueueNodeIndex = i;
2495 }
2496
2497 if (supportsPresent[i] == VK_TRUE) {
2498 graphicsQueueNodeIndex = i;
2499 presentQueueNodeIndex = i;
2500 break;
2501 }
2502 }
2503 }
2504 if (presentQueueNodeIndex == UINT32_MAX) {
2505 // If didn't find a queue that supports both graphics and present, then
2506 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002507 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002508 if (supportsPresent[i] == VK_TRUE) {
2509 presentQueueNodeIndex = i;
2510 break;
2511 }
2512 }
2513 }
2514 free(supportsPresent);
2515
2516 // Generate error if could not find both a graphics and a present queue
2517 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2518 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002519 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002520 }
2521
2522 // TODO: Add support for separate queues, including presentation,
2523 // synchronization, and appropriate tracking for QueueSubmit
2524 // While it is possible for an application to use a separate graphics and a
2525 // present queues, this demo program assumes it is only using one:
2526 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2527 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002528 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002529 }
2530
2531 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002532
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002533 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002534 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002535
Ian Elliottaaae5352015-07-06 14:27:58 -06002536 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002537 uint32_t formatCount;
Ian Elliotta0781972015-08-21 15:09:33 -06002538 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2539 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002540 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002541 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002542 VkSurfaceFormatKHR *surfFormats =
2543 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2544 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2545 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002546 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002547 assert(!err);
2548 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2549 // the surface has no preferred format. Otherwise, at least one
2550 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002551 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2552 {
2553 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2554 }
2555 else
2556 {
2557 assert(formatCount >= 1);
2558 demo->format = surfFormats[0].format;
2559 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002560 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002561
David Pinedo2bb7c932015-06-18 17:03:14 -06002562 demo->quit = false;
2563 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002564
2565 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002566 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002567}
2568
2569static void demo_init_connection(struct demo *demo)
2570{
Ian Elliott639ca472015-04-16 15:23:05 -06002571#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002572 const xcb_setup_t *setup;
2573 xcb_screen_iterator_t iter;
2574 int scr;
2575
2576 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002577 if (demo->connection == NULL) {
2578 printf("Cannot find a compatible Vulkan installable client driver "
2579 "(ICD).\nExiting ...\n");
2580 fflush(stdout);
2581 exit(1);
2582 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002583
2584 setup = xcb_get_setup(demo->connection);
2585 iter = xcb_setup_roots_iterator(setup);
2586 while (scr-- > 0)
2587 xcb_screen_next(&iter);
2588
2589 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002590#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002591}
2592
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002593static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002594{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002595 vec3 eye = {0.0f, 3.0f, 5.0f};
2596 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002597 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002598
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002599 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002600 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002601
Piers Daniell735ee532015-02-23 16:23:13 -07002602 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002603 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002604 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002605 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002606 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002607 if (strcmp(argv[i], "--use_glsl") == 0) {
2608 demo->use_glsl = true;
2609 continue;
2610 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002611 if (strcmp(argv[i], "--break") == 0) {
2612 demo->use_break = true;
2613 continue;
2614 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002615 if (strcmp(argv[i], "--validate") == 0) {
2616 demo->validate = true;
2617 continue;
2618 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002619 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002620 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002621 i < argc-1 &&
2622 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2623 demo->frameCount >= 0)
2624 {
2625 i++;
2626 continue;
2627 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002628
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002629 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002630 fflush(stderr);
2631 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002632 }
2633
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002634 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002635 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002636
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002637 demo->width = 500;
2638 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002639
2640 demo->spin_angle = 0.01f;
2641 demo->spin_increment = 0.01f;
2642 demo->pause = false;
2643
Piers Daniell735ee532015-02-23 16:23:13 -07002644 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002645 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2646 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002647}
2648
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002649
Ian Elliott639ca472015-04-16 15:23:05 -06002650#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002651extern int __getmainargs(
2652 int * _Argc,
2653 char *** _Argv,
2654 char *** _Env,
2655 int _DoWildCard,
2656 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002657
Ian Elliotta748eaf2015-04-28 15:50:36 -06002658int WINAPI WinMain(HINSTANCE hInstance,
2659 HINSTANCE hPrevInstance,
2660 LPSTR pCmdLine,
2661 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002662{
2663 MSG msg; // message
2664 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002665 int argc;
2666 char** argv;
2667 char** env;
2668 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002669
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002670 __getmainargs(&argc,&argv,&env,0,&new_mode);
2671
2672 demo_init(&demo, argc, argv);
2673 demo.connection = hInstance;
2674 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002675 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002676 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002677
2678 demo_prepare(&demo);
2679
2680 done = false; //initialize loop condition variable
2681 /* main message loop*/
2682 while(!done)
2683 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002684 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002685 if (msg.message == WM_QUIT) //check for a quit message
2686 {
2687 done = true; //if found, quit app
2688 }
2689 else
2690 {
2691 /* Translate and dispatch to event queue*/
2692 TranslateMessage(&msg);
2693 DispatchMessage(&msg);
2694 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002695 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002696 }
2697
2698 demo_cleanup(&demo);
2699
Tony Barbourf9921732015-04-22 11:36:22 -06002700 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002701}
2702#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002703int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002704{
2705 struct demo demo;
2706
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002707 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002708 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002709 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710
2711 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002712 demo_run(&demo);
2713
2714 demo_cleanup(&demo);
2715
2716 return 0;
2717}
Ian Elliott639ca472015-04-16 15:23:05 -06002718#endif // _WIN32