blob: 12de7f54b9883a7b01320c0776d75a56bf0265e8 [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;
350 VkSwapchainKHR swap_chain;
351 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;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800370 VkDescriptorInfo desc;
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
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600410static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600411{
412 // Search memtypes to find first index with those properties
413 for (uint32_t i = 0; i < 32; i++) {
414 if ((typeBits & 1) == 1) {
415 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600416 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600417 *typeIndex = i;
418 return VK_SUCCESS;
419 }
420 }
421 typeBits >>= 1;
422 }
423 // No memory types matched, return failure
424 return VK_UNSUPPORTED;
425}
426
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427static void demo_flush_init_cmd(struct demo *demo)
428{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600429 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432 return;
433
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600434 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435 assert(!err);
436
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600437 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600438 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439
Tony Barbour155cce82015-07-03 10:33:54 -0600440 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600441 assert(!err);
442
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444 assert(!err);
445
Tony Barbour155cce82015-07-03 10:33:54 -0600446 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600447 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600448}
449
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450static void demo_set_image_layout(
451 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600452 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600453 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600454 VkImageLayout old_image_layout,
455 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600456{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600457 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600459 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600460 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600462 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600463 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800464 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 .flags = 0,
466 };
467
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600468 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600469 assert(!err);
470
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600471 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600474 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600475 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600476 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600477 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600478 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 }
482
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600483 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600484 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600485 .pNext = NULL,
486 .outputMask = 0,
487 .inputMask = 0,
488 .oldLayout = old_image_layout,
489 .newLayout = new_image_layout,
490 .image = image,
Cody Northrop0e951672015-09-14 13:48:12 -0600491 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 };
493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600496 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600497 }
498
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600499 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600500 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600501 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 }
503
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600504 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505
Tony Barbour50bbca42015-06-29 16:20:35 -0600506 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
507 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600508
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600509 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510}
511
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600512static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513{
Chia-I Wuf973e322015-07-08 13:34:24 +0800514 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600515 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600516 .pNext = NULL,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600517 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600518 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600519 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600520 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700521 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800522 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600523 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
524 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800525 };
526 const VkRenderPassBeginInfo rp_begin = {
527 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
528 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800529 .renderPass = demo->render_pass,
530 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800531 .renderArea.offset.x = 0,
532 .renderArea.offset.y = 0,
533 .renderArea.extent.width = demo->width,
534 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600535 .clearValueCount = 2,
536 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700537 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800538 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600540 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541 assert(!err);
542
Chia-I Wua74c5b22015-07-07 11:50:03 +0800543 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
544
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600545 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600547 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600548 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600550 VkViewport viewport;
551 memset(&viewport, 0, sizeof(viewport));
552 viewport.height = (float) demo->height;
553 viewport.width = (float) demo->width;
554 viewport.minDepth = (float) 0.0f;
555 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600556 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600557
558 VkRect2D scissor;
559 memset(&scissor, 0, sizeof(scissor));
560 scissor.extent.width = demo->width;
561 scissor.extent.height = demo->height;
562 scissor.offset.x = 0;
563 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600564 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600565
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600566 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800567 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600569 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570 assert(!err);
571}
572
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600573
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600574void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600576 mat4x4 MVP, Model, VP;
577 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600578 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600579 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600581 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600582
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600583 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600584 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700585 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600586 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600587
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200588 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 -0600589 assert(!err);
590
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600591 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600593 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600594}
595
596static void demo_draw(struct demo *demo)
597{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600598 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600599 VkSemaphore presentCompleteSemaphore;
600 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
601 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
602 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600603 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600604 };
Tony Barbour155cce82015-07-03 10:33:54 -0600605 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600606
Ian Elliottaaae5352015-07-06 14:27:58 -0600607 err = vkCreateSemaphore(demo->device,
608 &presentCompleteSemaphoreCreateInfo,
609 &presentCompleteSemaphore);
610 assert(!err);
611
612 // Get the index of the next available swapchain image:
Ian Elliotta0781972015-08-21 15:09:33 -0600613 err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600614 UINT64_MAX,
615 presentCompleteSemaphore,
616 &demo->current_buffer);
Ian Elliotta0781972015-08-21 15:09:33 -0600617 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600618 // return codes
619 assert(!err);
620
621 // Wait for the present complete semaphore to be signaled to ensure
622 // that the image won't be rendered to until the presentation
623 // engine has fully released ownership to the application, and it is
624 // okay to render to the image.
625 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
626
Ian Elliotta0781972015-08-21 15:09:33 -0600627// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600628 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600629 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600630 assert(!err);
631
Ian Elliotta0781972015-08-21 15:09:33 -0600632 VkPresentInfoKHR present = {
633 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600634 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600635 .swapchainCount = 1,
636 .swapchains = &demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600637 .imageIndices = &demo->current_buffer,
638 };
639
640// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600641 err = demo->fpQueuePresentKHR(demo->queue, &present);
642 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600643 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644 assert(!err);
645
Chia-I Wucbb564e2015-04-16 22:02:10 +0800646 err = vkQueueWaitIdle(demo->queue);
647 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600648
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600649 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650}
651
652static void demo_prepare_buffers(struct demo *demo)
653{
Ian Elliottaaae5352015-07-06 14:27:58 -0600654 VkResult U_ASSERT_ONLY err;
655
656 // Check the surface properties and formats
Ian Elliotta0781972015-08-21 15:09:33 -0600657 VkSurfacePropertiesKHR surfProperties;
658 err = demo->fpGetSurfacePropertiesKHR(demo->device,
659 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600660 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600661 assert(!err);
662
Ian Elliott8528eff2015-08-07 15:56:59 -0600663 uint32_t presentModeCount;
Ian Elliotta0781972015-08-21 15:09:33 -0600664 err = demo->fpGetSurfacePresentModesKHR(demo->device,
665 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600666 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600667 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600668 VkPresentModeKHR *presentModes =
669 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600670 assert(presentModes);
Ian Elliotta0781972015-08-21 15:09:33 -0600671 err = demo->fpGetSurfacePresentModesKHR(demo->device,
672 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600673 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600674 assert(!err);
675
Ian Elliotta0781972015-08-21 15:09:33 -0600676 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600677 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600678 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600679 {
680 // If the surface size is undefined, the size is set to
681 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600682 swapchainExtent.width = demo->width;
683 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600684 }
685 else
686 {
687 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600688 swapchainExtent = surfProperties.currentExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600689 }
690
691 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600692 // tearing mode. If not, try IMMEDIATE which will usually be available,
693 // and is fastest (though it tears). If not, fall back to FIFO which is
694 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600695 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600696 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600697 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
698 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600699 break;
700 }
Ian Elliotta0781972015-08-21 15:09:33 -0600701 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
702 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
703 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600704 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600705 }
706
707 // Determine the number of VkImage's to use in the swap chain (we desire to
708 // own only 1 image at a time, besides the images being displayed and
709 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600710 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600711 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600712 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600713 {
714 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600715 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600716 }
717
Tony Barbour9fd6d352015-09-16 15:01:32 -0600718 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600719 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
720 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600722 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600723 }
724
Ian Elliotta0781972015-08-21 15:09:33 -0600725 const VkSwapchainCreateInfoKHR swap_chain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600726 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800727 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600728 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
729 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800730 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600731 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800732 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600733 .width = swapchainExtent.width,
734 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600735 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600736 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600737 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800738 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600739 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
740 .queueFamilyCount = 0,
741 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600742 .presentMode = swapchainPresentMode,
743 .oldSwapchain.handle = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600744 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600745 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600746 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600747
Ian Elliotta0781972015-08-21 15:09:33 -0600748 err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800749 assert(!err);
750
Ian Elliotta0781972015-08-21 15:09:33 -0600751 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
752 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600753 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800754
Ian Elliotta0781972015-08-21 15:09:33 -0600755 VkImage* swapchainImages =
756 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
757 assert(swapchainImages);
758 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
759 &demo->swapchainImageCount,
760 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600761 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600762
Ian Elliotta0781972015-08-21 15:09:33 -0600763 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600764 assert(demo->buffers);
765
Ian Elliotta0781972015-08-21 15:09:33 -0600766 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600767 VkImageViewCreateInfo color_image_view = {
768 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600769 .pNext = NULL,
770 .format = demo->format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600771 .channels = {
772 .r = VK_CHANNEL_SWIZZLE_R,
773 .g = VK_CHANNEL_SWIZZLE_G,
774 .b = VK_CHANNEL_SWIZZLE_B,
775 .a = VK_CHANNEL_SWIZZLE_A,
776 },
777 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600778 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600779 .baseMipLevel = 0,
780 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600781 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600782 .arraySize = 1
783 },
784 .viewType = VK_IMAGE_VIEW_TYPE_2D,
785 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600786 };
787
Ian Elliotta0781972015-08-21 15:09:33 -0600788 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600789
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600790 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600791 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600792 VK_IMAGE_LAYOUT_UNDEFINED,
793 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600794
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600795 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600796
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600797 err = vkCreateImageView(demo->device,
798 &color_image_view, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600799 assert(!err);
800 }
801}
802
803static void demo_prepare_depth(struct demo *demo)
804{
Tony Barbour72304ef2015-04-16 15:59:00 -0600805 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600806 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600807 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600808 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600809 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600810 .format = depth_format,
811 .extent = { demo->width, demo->height, 1 },
812 .mipLevels = 1,
813 .arraySize = 1,
814 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600815 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600816 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600817 .flags = 0,
818 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200819
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600820 VkImageViewCreateInfo view = {
821 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600822 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600823 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600824 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600825 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600826 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600827 .baseMipLevel = 0,
828 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600829 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600830 .arraySize = 1
831 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600832 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600833 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600834 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600835
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500836 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600837 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600838
839 demo->depth.format = depth_format;
840
841 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600842 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600843 &demo->depth.image);
844 assert(!err);
845
Tony Barbour155cce82015-07-03 10:33:54 -0600846 err = vkGetImageMemoryRequirements(demo->device,
847 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600848
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200849 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
850 demo->depth.mem_alloc.pNext = NULL;
851 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
852 demo->depth.mem_alloc.memoryTypeIndex = 0;
853
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600854 err = memory_type_from_properties(demo,
855 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600856 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200857 &demo->depth.mem_alloc.memoryTypeIndex);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600858 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600859
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500860 /* allocate memory */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200861 err = vkAllocMemory(demo->device, &demo->depth.mem_alloc, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500862 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600863
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500864 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600865 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500866 demo->depth.mem, 0);
867 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600868
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600869 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600870 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600871 VK_IMAGE_LAYOUT_UNDEFINED,
872 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600873
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600874 /* create image view */
875 view.image = demo->depth.image;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600876 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600877 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600878}
879
Tony Barbour1a86d032015-09-21 15:17:33 -0600880/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700881bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600882 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600883 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600884{
Tony Barbour1a86d032015-09-21 15:17:33 -0600885 FILE *fPtr = fopen(filename,"rb");
886 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600887
Tony Barbour1a86d032015-09-21 15:17:33 -0600888 if (!fPtr)
889 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600890
Tony Barbour1a86d032015-09-21 15:17:33 -0600891 cPtr = fgets(header, 256, fPtr); // P6
892 if (cPtr == NULL || strncmp(header, "P6\n", 3))
893 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600894
Tony Barbour1a86d032015-09-21 15:17:33 -0600895 do {
896 cPtr = fgets(header, 256, fPtr);
897 if (cPtr == NULL)
898 return false;
899 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600900
Tony Barbour1a86d032015-09-21 15:17:33 -0600901 sscanf(header, "%u %u", height, width);
902 if (rgba_data == NULL)
903 return true;
904 fgets(header, 256, fPtr); // Format
905 if (cPtr == NULL || strncmp(header, "255\n", 3))
906 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600907
Tony Barbour1a86d032015-09-21 15:17:33 -0600908 for(int y = 0; y < *height; y++)
909 {
910 uint8_t *rowPtr = rgba_data;
911 for(int x = 0; x < *width; x++)
912 {
913 fread(rowPtr, 3, 1, fPtr);
914 rowPtr[3] = 255; /* Alpha of 1 */
915 rowPtr += 4;
916 }
917 rgba_data += layout->rowPitch;
918 }
919 fclose(fPtr);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600920 return true;
921}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600922
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700923static void demo_prepare_texture_image(struct demo *demo,
924 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600925 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600926 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600927 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600928 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600929{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600930 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600931 int32_t tex_width;
932 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600933 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934
David Pinedo30bd71d2015-04-23 08:16:57 -0600935 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
936 {
937 printf("Failed to load textures\n");
938 fflush(stdout);
939 exit(1);
940 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700941
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600942 tex_obj->tex_width = tex_width;
943 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700944
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600945 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600946 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700947 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600948 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700949 .format = tex_format,
950 .extent = { tex_width, tex_height, 1 },
951 .mipLevels = 1,
952 .arraySize = 1,
953 .samples = 1,
954 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600955 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956 .flags = 0,
957 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700958
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500959 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700960
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600961 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600962 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700963 assert(!err);
964
Tony Barbour155cce82015-07-03 10:33:54 -0600965 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600966 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700967
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200968 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
969 tex_obj->mem_alloc.pNext = NULL;
970 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
971 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700972
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600973 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600974 assert(!err);
975
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500976 /* allocate memory */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200977 err = vkAllocMemory(demo->device, &tex_obj->mem_alloc,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500978 &(tex_obj->mem));
979 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500981 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600982 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500983 tex_obj->mem, 0);
984 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600986 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600987 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600988 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600990 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600992 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700993 void *data;
994
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600995 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
996 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200998 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700999 assert(!err);
1000
1001 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1002 fprintf(stderr, "Error loading texture: %s\n", filename);
1003 }
1004
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001005 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001007
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001008 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001009 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001010 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001011 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001012 tex_obj->imageLayout);
1013 /* 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 -07001014}
1015
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001016static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017{
1018 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001019 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001020 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021}
1022
1023static void demo_prepare_textures(struct demo *demo)
1024{
Tony Barbour72304ef2015-04-16 15:59:00 -06001025 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001026 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001027 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001028 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001029
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001030 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001031 assert(!err);
1032
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001033 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001034
FslNopper6a7e50e2015-05-06 21:42:01 +02001035 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036 /* Device can texture using linear textures */
1037 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001038 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001039 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001041 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001042
1043 memset(&staging_texture, 0, sizeof(staging_texture));
1044 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001045 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046
1047 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001048 VK_IMAGE_TILING_OPTIMAL,
1049 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1050 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001051
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001052 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001053 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001054 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001056
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001057 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001058 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001059 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001060 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001062 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001063 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066 .destOffset = { 0, 0, 0 },
1067 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1068 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001069 vkCmdCopyImage(demo->cmd,
1070 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1071 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001072 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001074 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001075 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001076 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001077 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001079 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001081 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001083 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1084 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085 }
1086
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001087 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001088 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001089 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 .magFilter = VK_TEX_FILTER_NEAREST,
1091 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001092 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter781da8a2015-09-10 14:08:50 -06001093 .addressModeU = VK_TEX_ADDRESS_MODE_CLAMP,
1094 .addressModeV = VK_TEX_ADDRESS_MODE_CLAMP,
1095 .addressModeW = VK_TEX_ADDRESS_MODE_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001096 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001097 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001098 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001099 .minLod = 0.0f,
1100 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001101 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001102 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001103 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001104
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001105 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001106 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001107 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001108 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001109 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001110 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001111 .channels = { VK_CHANNEL_SWIZZLE_R,
1112 VK_CHANNEL_SWIZZLE_G,
1113 VK_CHANNEL_SWIZZLE_B,
1114 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001115 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001116 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001117 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001118
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001120 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001121 &demo->textures[i].sampler);
1122 assert(!err);
1123
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001124 /* create image view */
1125 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001126 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001127 &demo->textures[i].view);
1128 assert(!err);
1129 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001130}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001131
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001132void demo_prepare_cube_data_buffer(struct demo *demo)
1133{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001134 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001135 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001136 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137 int i;
1138 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001139 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001140 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001141
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001142 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001143 mat4x4_mul(MVP, VP, demo->model_matrix);
1144 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001145// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001146
1147 for (i=0; i<12*3; i++) {
1148 data.position[i][0] = g_vertex_buffer_data[i*3];
1149 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1150 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1151 data.position[i][3] = 1.0f;
1152 data.attr[i][0] = g_uv_buffer_data[2*i];
1153 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1154 data.attr[i][2] = 0;
1155 data.attr[i][3] = 0;
1156 }
1157
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001158 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001159 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001160 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001161 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001162 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001163 assert(!err);
1164
Tony Barbour155cce82015-07-03 10:33:54 -06001165 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001166 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001167
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001168 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1169 demo->uniform_data.mem_alloc.pNext = NULL;
1170 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1171 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1172
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001173 err = memory_type_from_properties(demo,
1174 mem_reqs.memoryTypeBits,
1175 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001176 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001177 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001178
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001179 err = vkAllocMemory(demo->device, &demo->uniform_data.mem_alloc, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001180 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001181
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001182 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 -05001183 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001184
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001185 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001186
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001187 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001188
Tony Barbour155cce82015-07-03 10:33:54 -06001189 err = vkBindBufferMemory(demo->device,
1190 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001191 demo->uniform_data.mem, 0);
1192 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001193
Courtney Goeltzenleuchter94415342015-09-14 14:14:21 -06001194 demo->uniform_data.desc.bufferInfo.buffer = demo->uniform_data.buf;
1195 demo->uniform_data.desc.bufferInfo.offset = 0;
1196 demo->uniform_data.desc.bufferInfo.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001197}
1198
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001199static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001201 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001202 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001203 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001204 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001205 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001206 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001207 },
1208 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001209 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001210 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001211 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001212 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001213 },
1214 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001215 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001216 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001217 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001218 .count = 2,
1219 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001220 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001221 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001222
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001223 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001224 &descriptor_layout, &demo->desc_layout);
1225 assert(!err);
1226
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001227 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1228 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1229 .pNext = NULL,
1230 .descriptorSetCount = 1,
1231 .pSetLayouts = &demo->desc_layout,
1232 };
1233
1234 err = vkCreatePipelineLayout(demo->device,
1235 &pPipelineLayoutCreateInfo,
1236 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001237 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238}
1239
Chia-I Wuf973e322015-07-08 13:34:24 +08001240static void demo_prepare_render_pass(struct demo *demo)
1241{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001242 const VkAttachmentDescription attachments[2] = {
1243 [0] = {
1244 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1245 .pNext = NULL,
1246 .format = demo->format,
1247 .samples = 1,
1248 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1249 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1250 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1251 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1252 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1253 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1254 },
1255 [1] = {
1256 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1257 .pNext = NULL,
1258 .format = demo->depth.format,
1259 .samples = 1,
1260 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1261 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1262 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1263 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1264 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1265 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1266 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001267 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001268 const VkAttachmentReference color_reference = {
1269 .attachment = 0,
1270 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1271 };
1272 const VkSubpassDescription subpass = {
1273 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1274 .pNext = NULL,
1275 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1276 .flags = 0,
1277 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001278 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001279 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001280 .pColorAttachments = &color_reference,
1281 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001282 .depthStencilAttachment = {
1283 .attachment = 1,
1284 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1285 },
1286 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001287 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001288 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001289 const VkRenderPassCreateInfo rp_info = {
1290 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1291 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001292 .attachmentCount = 2,
1293 .pAttachments = attachments,
1294 .subpassCount = 1,
1295 .pSubpasses = &subpass,
1296 .dependencyCount = 0,
1297 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001298 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001299 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001300
1301 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1302 assert(!err);
1303}
1304
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001305static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001306 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001307 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001308 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001309 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001311 VkShaderModuleCreateInfo moduleCreateInfo;
1312 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001313 VkShader shader;
1314 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001316
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001317 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1318 moduleCreateInfo.pNext = NULL;
1319
1320 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1321 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001322 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001323
Cody Northrop1fedb212015-05-28 11:27:16 -06001324 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001325 moduleCreateInfo.codeSize = size;
1326 moduleCreateInfo.pCode = code;
1327 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001328 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001329 assert(!err);
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001330
1331 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001332 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001333 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001334 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001335 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001336 } else {
1337 // Create fake SPV structure to feed GLSL
1338 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001339 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1340 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1341 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001342
1343 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001344 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1345 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1346 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1347 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001348
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001349 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001350 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001351 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001352 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001353
1354 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001355 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001356 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001357 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001358 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001359 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001360 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001361 return shader;
1362}
1363
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001364char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001365{
1366 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001367 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001368 void *shader_code;
1369
1370 FILE *fp = fopen(filename, "rb");
1371 if (!fp) return NULL;
1372
1373 fseek(fp, 0L, SEEK_END);
1374 size = ftell(fp);
1375
1376 fseek(fp, 0L, SEEK_SET);
1377
1378 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001379 retval = fread(shader_code, size, 1, fp);
1380 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001381
1382 *psize = size;
1383
1384 return shader_code;
1385}
1386
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001387static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388{
Cody Northrop1fedb212015-05-28 11:27:16 -06001389 if (!demo->use_glsl) {
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001390 VkShader shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001391 void *vertShaderCode;
1392 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001393
Cody Northrop1fedb212015-05-28 11:27:16 -06001394 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001395
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001396 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
1397 vertShaderCode, size);
1398 free(vertShaderCode);
1399 return shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001400 } else {
1401 static const char *vertShaderText =
1402 "#version 140\n"
1403 "#extension GL_ARB_separate_shader_objects : enable\n"
1404 "#extension GL_ARB_shading_language_420pack : enable\n"
1405 "\n"
1406 "layout(binding = 0) uniform buf {\n"
1407 " mat4 MVP;\n"
1408 " vec4 position[12*3];\n"
1409 " vec4 attr[12*3];\n"
1410 "} ubuf;\n"
1411 "\n"
1412 "layout (location = 0) out vec4 texcoord;\n"
1413 "\n"
1414 "void main() \n"
1415 "{\n"
1416 " texcoord = ubuf.attr[gl_VertexID];\n"
1417 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1418 "\n"
1419 " // GL->VK conventions\n"
1420 " gl_Position.y = -gl_Position.y;\n"
1421 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1422 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001423
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001424 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001425 (const void *) vertShaderText,
1426 strlen(vertShaderText));
1427 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428}
1429
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001430static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001431{
Cody Northrop1fedb212015-05-28 11:27:16 -06001432 if (!demo->use_glsl) {
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001433 VkShader shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001434 void *fragShaderCode;
1435 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001436
Cody Northrop1fedb212015-05-28 11:27:16 -06001437 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001438
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001439 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
1440 fragShaderCode, size);
1441 free(fragShaderCode);
1442 return shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001443 } else {
1444 static const char *fragShaderText =
1445 "#version 140\n"
1446 "#extension GL_ARB_separate_shader_objects : enable\n"
1447 "#extension GL_ARB_shading_language_420pack : enable\n"
1448 "layout (binding = 1) uniform sampler2D tex;\n"
1449 "\n"
1450 "layout (location = 0) in vec4 texcoord;\n"
1451 "layout (location = 0) out vec4 uFragColor;\n"
1452 "void main() {\n"
1453 " uFragColor = texture(tex, texcoord.xy);\n"
1454 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001455
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001456 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001457 (const void *) fragShaderText,
1458 strlen(fragShaderText));
1459 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001460}
1461
1462static void demo_prepare_pipeline(struct demo *demo)
1463{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001464 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001465 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001466 VkPipelineInputAssemblyStateCreateInfo ia;
1467 VkPipelineRasterStateCreateInfo rs;
1468 VkPipelineColorBlendStateCreateInfo cb;
1469 VkPipelineDepthStencilStateCreateInfo ds;
1470 VkPipelineViewportStateCreateInfo vp;
1471 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniellba839d12015-09-29 13:01:09 -06001472 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1473 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001474 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475
Piers Daniellba839d12015-09-29 13:01:09 -06001476 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1477 memset(&dynamicState, 0, sizeof dynamicState);
1478 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1479 dynamicState.pDynamicStates = dynamicStateEnables;
1480
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001481 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001482 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001483 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001484
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001486 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001487 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488
1489 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001490 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001491 rs.fillMode = VK_FILL_MODE_SOLID;
1492 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001493 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001494 rs.depthClipEnable = VK_TRUE;
Cody Northrop709c1742015-08-17 11:10:49 -06001495 rs.rasterizerDiscardEnable = VK_FALSE;
1496 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001497
1498 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001499 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1500 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001501 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001502 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001503 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001504 cb.attachmentCount = 1;
1505 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001506
Tony Barbour29645d02015-01-16 14:27:35 -07001507 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001508 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001509 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001510 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1511 vp.scissorCount = 1;
1512 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001513
1514 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001515 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 ds.depthTestEnable = VK_TRUE;
1517 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001518 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001519 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001520 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1521 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001522 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001523 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001524 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001525
Tony Barbour29645d02015-01-16 14:27:35 -07001526 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001527 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001528 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001529 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001530
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001531 // Two stages: vs and fs
1532 pipeline.stageCount = 2;
1533 VkPipelineShaderStageCreateInfo shaderStages[2];
1534 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1535
1536 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1537 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1538 shaderStages[0].shader = demo_prepare_vs(demo);
1539
1540 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1541 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1542 shaderStages[1].shader = demo_prepare_fs(demo);
1543
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001544 memset(&pipelineCache, 0, sizeof(pipelineCache));
1545 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001546
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001547 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1548 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001549
1550 pipeline.pVertexInputState = NULL;
1551 pipeline.pInputAssemblyState = &ia;
1552 pipeline.pRasterState = &rs;
1553 pipeline.pColorBlendState = &cb;
1554 pipeline.pMultisampleState = &ms;
1555 pipeline.pViewportState = &vp;
1556 pipeline.pDepthStencilState = &ds;
1557 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001558 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001559 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001560
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001561 pipeline.renderPass = demo->render_pass;
1562
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001563 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001564 assert(!err);
1565
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001566 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001567 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001568 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001569 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1570 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571}
1572
Chia-I Wu63ea9262015-03-26 13:14:16 +08001573static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001575 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001576 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001577 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001578 .count = 1,
1579 },
1580 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001581 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001582 .count = DEMO_TEXTURE_COUNT,
1583 },
1584 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001585 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001586 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001587 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001588 .poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT,
1589 .maxSets = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001590 .count = 2,
1591 .pTypeCount = type_counts,
1592 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001593 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001594
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001595 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001596 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001597 assert(!err);
1598}
1599
1600static void demo_prepare_descriptor_set(struct demo *demo)
1601{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001602 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1603 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001604 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001605 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001606
Mike Stroyanebae8322015-04-17 12:36:38 -06001607 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001608 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001609 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001610 &demo->desc_set);
1611 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001612
Chia-I Wuae721ba2015-05-25 16:27:55 +08001613 memset(&tex_descs, 0, sizeof(tex_descs));
1614 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1615 tex_descs[i].sampler = demo->textures[i].sampler;
1616 tex_descs[i].imageView = demo->textures[i].view;
1617 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1618 }
1619
1620 memset(&writes, 0, sizeof(writes));
1621
1622 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1623 writes[0].destSet = demo->desc_set;
1624 writes[0].count = 1;
1625 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1626 writes[0].pDescriptors = &demo->uniform_data.desc;
1627
1628 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1629 writes[1].destSet = demo->desc_set;
1630 writes[1].destBinding = 1;
1631 writes[1].count = DEMO_TEXTURE_COUNT;
1632 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1633 writes[1].pDescriptors = tex_descs;
1634
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001635 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001636}
1637
Chia-I Wuf973e322015-07-08 13:34:24 +08001638static void demo_prepare_framebuffers(struct demo *demo)
1639{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001640 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001641 attachments[1] = demo->depth.view;
1642
Chia-I Wuf973e322015-07-08 13:34:24 +08001643 const VkFramebufferCreateInfo fb_info = {
1644 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1645 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001646 .renderPass = demo->render_pass,
1647 .attachmentCount = 2,
1648 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001649 .width = demo->width,
1650 .height = demo->height,
1651 .layers = 1,
1652 };
1653 VkResult U_ASSERT_ONLY err;
1654 uint32_t i;
1655
Tony Barbourd8e504f2015-10-08 14:26:24 -06001656 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1657 assert(demo->framebuffers);
1658
1659 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001660 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001661 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1662 assert(!err);
1663 }
1664}
1665
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001666static void demo_prepare(struct demo *demo)
1667{
Cody Northrop91e221b2015-07-09 18:08:32 -06001668 VkResult U_ASSERT_ONLY err;
1669
1670 const VkCmdPoolCreateInfo cmd_pool_info = {
1671 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1672 .pNext = NULL,
1673 .queueFamilyIndex = demo->graphics_queue_node_index,
1674 .flags = 0,
1675 };
1676 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1677 assert(!err);
1678
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001679 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001680 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001681 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001682 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001683 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001684 .flags = 0,
1685 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686
1687 demo_prepare_buffers(demo);
1688 demo_prepare_depth(demo);
1689 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001690 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001692 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001693 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001695
Ian Elliotta0781972015-08-21 15:09:33 -06001696 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001697 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001698 assert(!err);
1699 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001700
Chia-I Wu63ea9262015-03-26 13:14:16 +08001701 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001702 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001703
Chia-I Wuf973e322015-07-08 13:34:24 +08001704 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001705
Ian Elliotta0781972015-08-21 15:09:33 -06001706 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001707 demo->current_buffer = i;
1708 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1709 }
1710
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001711 /*
1712 * Prepare functions above may generate pipeline commands
1713 * that need to be flushed before beginning the render loop.
1714 */
1715 demo_flush_init_cmd(demo);
1716
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001717 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001718 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719}
1720
David Pinedo2bb7c932015-06-18 17:03:14 -06001721static void demo_cleanup(struct demo *demo)
1722{
1723 uint32_t i;
1724
1725 demo->prepared = false;
1726
Tony Barbourd8e504f2015-10-08 14:26:24 -06001727 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001728 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1729 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001730 free(demo->framebuffers);
Tony Barbour155cce82015-07-03 10:33:54 -06001731 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001732
Tony Barbour155cce82015-07-03 10:33:54 -06001733 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001734 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001735 vkDestroyRenderPass(demo->device, demo->render_pass);
1736 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1737 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001738
1739 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001740 vkDestroyImageView(demo->device, demo->textures[i].view);
1741 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001742 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001743 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001744 }
Ian Elliotta0781972015-08-21 15:09:33 -06001745 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001746
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001747 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbour155cce82015-07-03 10:33:54 -06001748 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001749 vkFreeMemory(demo->device, demo->depth.mem);
1750
Tony Barbour155cce82015-07-03 10:33:54 -06001751 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001752 vkFreeMemory(demo->device, demo->uniform_data.mem);
1753
Ian Elliotta0781972015-08-21 15:09:33 -06001754 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001755 vkDestroyImageView(demo->device, demo->buffers[i].view);
Tony Barbour155cce82015-07-03 10:33:54 -06001756 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001757 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001758 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001759
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001760 free(demo->queue_props);
1761
Cody Northrop91e221b2015-07-09 18:08:32 -06001762 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001763 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001764 if (demo->validate) {
1765 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1766 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001767 vkDestroyInstance(demo->inst);
1768
1769#ifndef _WIN32
1770 xcb_destroy_window(demo->connection, demo->window);
1771 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001772 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001773#endif // _WIN32
1774}
1775
1776// On MS-Windows, make this a global, so it's available to WndProc()
1777struct demo demo;
1778
Ian Elliott639ca472015-04-16 15:23:05 -06001779#ifdef _WIN32
1780static void demo_run(struct demo *demo)
1781{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001782 if (!demo->prepared)
1783 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001784 // Wait for work to finish before updating MVP.
1785 vkDeviceWaitIdle(demo->device);
1786 demo_update_data_buffer(demo);
1787
1788 demo_draw(demo);
1789
1790 // Wait for work to finish before updating MVP.
1791 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001792
David Pinedo2bb7c932015-06-18 17:03:14 -06001793 demo->curFrame++;
1794
1795 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1796 {
1797 demo->quit=true;
1798 demo_cleanup(demo);
1799 ExitProcess(0);
1800 }
1801
1802}
Ian Elliott639ca472015-04-16 15:23:05 -06001803
1804// MS-Windows event handling function:
1805LRESULT CALLBACK WndProc(HWND hWnd,
1806 UINT uMsg,
1807 WPARAM wParam,
1808 LPARAM lParam)
1809{
Ian Elliott639ca472015-04-16 15:23:05 -06001810 switch(uMsg)
1811 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001812 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001813 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001814 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001815 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001816 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001817 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001818 default:
1819 break;
1820 }
1821 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1822}
1823
1824static void demo_create_window(struct demo *demo)
1825{
1826 WNDCLASSEX win_class;
1827
1828 // Initialize the window class structure:
1829 win_class.cbSize = sizeof(WNDCLASSEX);
1830 win_class.style = CS_HREDRAW | CS_VREDRAW;
1831 win_class.lpfnWndProc = WndProc;
1832 win_class.cbClsExtra = 0;
1833 win_class.cbWndExtra = 0;
1834 win_class.hInstance = demo->connection; // hInstance
1835 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1836 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1837 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1838 win_class.lpszMenuName = NULL;
1839 win_class.lpszClassName = demo->name;
1840 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1841 // Register window class:
1842 if (!RegisterClassEx(&win_class)) {
1843 // It didn't work, so try to give a useful error:
1844 printf("Unexpected error trying to start the application!\n");
1845 fflush(stdout);
1846 exit(1);
1847 }
1848 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001849 RECT wr = { 0, 0, demo->width, demo->height };
1850 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001851 demo->window = CreateWindowEx(0,
1852 demo->name, // class name
1853 demo->name, // app name
1854 WS_OVERLAPPEDWINDOW | // window style
1855 WS_VISIBLE |
1856 WS_SYSMENU,
1857 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001858 wr.right-wr.left, // width
1859 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001860 NULL, // handle to parent
1861 NULL, // handle to menu
1862 demo->connection, // hInstance
1863 NULL); // no extra parameters
1864 if (!demo->window) {
1865 // It didn't work, so try to give a useful error:
1866 printf("Cannot create a window in which to draw!\n");
1867 fflush(stdout);
1868 exit(1);
1869 }
1870}
1871#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001872static void demo_handle_event(struct demo *demo,
1873 const xcb_generic_event_t *event)
1874{
Piers Daniell735ee532015-02-23 16:23:13 -07001875 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001876 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001877 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001878 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001879 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001880 case XCB_CLIENT_MESSAGE:
1881 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1882 (*demo->atom_wm_delete_window).atom) {
1883 demo->quit = true;
1884 }
1885 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001886 case XCB_KEY_RELEASE:
1887 {
1888 const xcb_key_release_event_t *key =
1889 (const xcb_key_release_event_t *) event;
1890
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001891 switch (key->detail) {
1892 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001893 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001894 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001895 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001896 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001897 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001898 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001899 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001900 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001901 case 0x41:
1902 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001903 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001904 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001905 }
1906 break;
1907 default:
1908 break;
1909 }
1910}
1911
1912static void demo_run(struct demo *demo)
1913{
1914 xcb_flush(demo->connection);
1915
1916 while (!demo->quit) {
1917 xcb_generic_event_t *event;
1918
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001919 if (demo->pause) {
1920 event = xcb_wait_for_event(demo->connection);
1921 } else {
1922 event = xcb_poll_for_event(demo->connection);
1923 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001924 if (event) {
1925 demo_handle_event(demo, event);
1926 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001927 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001928
1929 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001930 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001931 demo_update_data_buffer(demo);
1932
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001933 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001934
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001935 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001936 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001937 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06001938 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06001939 demo->quit = true;
1940
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001941 }
1942}
1943
1944static void demo_create_window(struct demo *demo)
1945{
1946 uint32_t value_mask, value_list[32];
1947
1948 demo->window = xcb_generate_id(demo->connection);
1949
1950 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1951 value_list[0] = demo->screen->black_pixel;
1952 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1953 XCB_EVENT_MASK_EXPOSURE;
1954
1955 xcb_create_window(demo->connection,
1956 XCB_COPY_FROM_PARENT,
1957 demo->window, demo->screen->root,
1958 0, 0, demo->width, demo->height, 0,
1959 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1960 demo->screen->root_visual,
1961 value_mask, value_list);
1962
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001963 /* Magic code that will send notification when window is destroyed */
1964 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1965 "WM_PROTOCOLS");
1966 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1967
1968 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1969 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1970
1971 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1972 demo->window, (*reply).atom, 4, 32, 1,
1973 &(*demo->atom_wm_delete_window).atom);
1974 free(reply);
1975
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001976 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001977
1978 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1979 const uint32_t coords[] = {100, 100};
1980 xcb_configure_window(demo->connection, demo->window,
1981 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982}
Ian Elliott639ca472015-04-16 15:23:05 -06001983#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001984
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001985/*
1986 * Return 1 (true) if all layer names specified in check_names
1987 * can be found in given layer properties.
1988 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001989static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001990 uint32_t layer_count, VkLayerProperties *layers)
1991{
1992 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001993 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001994 for (uint32_t j = 0; j < layer_count; j++) {
1995 if (!strcmp(check_names[i], layers[j].layerName)) {
1996 found = 1;
1997 }
1998 }
1999 if (!found) {
2000 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2001 return 0;
2002 }
2003 }
2004 return 1;
2005}
2006
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002007static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002008{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002009 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002010 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002011 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002012 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002013 VkLayerProperties *instance_layers;
2014 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002015 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002016 uint32_t instance_layer_count = 0;
2017 uint32_t enabled_extension_count = 0;
2018 uint32_t enabled_layer_count = 0;
2019
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002020 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002021 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002022 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002023 "ObjectTracker",
2024 "DrawState",
2025 "ParamChecker",
2026 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002027 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002028 "DeviceLimits",
2029 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002030 };
2031
2032 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002033 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002034 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002035 "ObjectTracker",
2036 "DrawState",
2037 "ParamChecker",
2038 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002039 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002040 "DeviceLimits",
2041 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002042 };
2043
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002044 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002045 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002046 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002047 assert(!err);
2048
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002049 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002050 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002051 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002052
2053 if (demo->validate) {
2054 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2055 instance_layer_count, instance_layers);
2056 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002057 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002058 "required validation layer.\n\n"
2059 "Please look at the Getting Started guide for additional "
2060 "information.\n",
2061 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002062 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002063 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002064 }
2065
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002066 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002067 assert(!err);
2068
Tony Barbourcc69f452015-10-08 13:59:42 -06002069 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002070 memset(extension_names, 0, sizeof(extension_names));
2071 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002072 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002073 assert(!err);
2074 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002075 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002076 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002077 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002078 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002079 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002080 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002081 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002082 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002083 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002084 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002085 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002086 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002087 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002088 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002089 "Vulkan installable client driver (ICD) installed?\nPlease "
2090 "look at the Getting Started guide for additional "
2091 "information.\n",
2092 "vkCreateInstance Failure");
2093 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002094 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002095 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002096 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002097 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002098 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002099 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002100 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002101 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002103 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002104 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002105 .pNext = NULL,
2106 .pAppInfo = &app,
2107 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002108 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002109 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002110 .extensionCount = enabled_extension_count,
2111 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002112 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002113
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002114 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002115
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002116 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002117 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2118 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002119 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002120 "additional information.\n",
2121 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002122 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002123 ERR_EXIT("Cannot find a specified extension library"
2124 ".\nMake sure your layers path is set appropriately\n",
2125 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002126 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002127 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2128 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002129 "the Getting Started guide for additional information.\n",
2130 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002131 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002132
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002133 free(instance_layers);
2134 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002135
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002136 /* Make initial call to query gpu_count, then second call for gpu info*/
2137 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2138 assert(!err && gpu_count > 0);
2139 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2140 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2141 assert(!err);
2142 /* For cube demo we just grab the first physical device */
2143 demo->gpu = physical_devices[0];
2144 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002145
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002146 /* Look for validation layers */
2147 validation_found = 0;
2148 enabled_layer_count = 0;
2149 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002150 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002151 assert(!err);
2152
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002153 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002154 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002155 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002156
2157 if (demo->validate) {
2158 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2159 device_layer_count, device_layers);
2160 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002161 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002162 "a required validation layer.\n\n"
2163 "Please look at the Getting Started guide for additional "
2164 "information.\n",
2165 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002166 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002167 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002168 }
2169
2170 uint32_t device_extension_count = 0;
2171 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002172 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002173 demo->gpu, NULL, &device_extension_count, NULL);
2174 assert(!err);
2175
Tony Barbourcc69f452015-10-08 13:59:42 -06002176 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 enabled_extension_count = 0;
2178 memset(extension_names, 0, sizeof(extension_names));
2179 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002180 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002181 demo->gpu, NULL, &device_extension_count, device_extensions);
2182 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002183
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002184 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002185 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002186 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002187 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002188 }
2189 assert(enabled_extension_count < 64);
2190 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002191 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002192 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002193 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002194 "Vulkan installable client driver (ICD) installed?\nPlease "
2195 "look at the Getting Started guide for additional "
2196 "information.\n",
2197 "vkCreateInstance Failure");
2198 }
2199
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002200 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002201 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2202 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002203 if (!demo->dbgCreateMsgCallback) {
2204 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2205 "vkGetProcAddr Failure");
2206 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002207 if (!demo->dbgDestroyMsgCallback) {
2208 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2209 "vkGetProcAddr Failure");
2210 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002211 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2212 if (!demo->dbgBreakCallback) {
2213 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2214 "vkGetProcAddr Failure");
2215 }
2216
2217 PFN_vkDbgMsgCallback callback;
2218
2219 if (!demo->use_break) {
2220 callback = dbgFunc;
2221 } else {
2222 callback = demo->dbgBreakCallback;
2223 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002224 err = demo->dbgCreateMsgCallback(
2225 demo->inst,
2226 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002227 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002228 &demo->msg_callback);
2229 switch (err) {
2230 case VK_SUCCESS:
2231 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002232 case VK_ERROR_OUT_OF_HOST_MEMORY:
2233 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2234 "dbgCreateMsgCallback Failure");
2235 break;
2236 default:
2237 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2238 "dbgCreateMsgCallback Failure");
2239 break;
2240 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002241 }
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002242 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
2243 assert(!err);
2244
2245 /* Call with NULL data to get count */
2246 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
2247 assert(!err);
2248 assert(demo->queue_count >= 1);
2249
2250 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2251 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
2252 assert(!err);
2253 // Find a queue that supports gfx
2254 uint32_t gfx_queue_idx = 0;
2255 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2256 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2257 break;
2258 }
2259 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002260 // Query fine-grained feature support for this device.
2261 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002262 VkPhysicalDeviceFeatures physDevFeatures;
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002263 err = vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
2264 assert(!err);
2265
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002266 const VkDeviceQueueCreateInfo queue = {
2267 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2268 .pNext = NULL,
2269 .queueFamilyIndex = gfx_queue_idx,
2270 .queueCount = 1,
2271 };
2272
2273 VkDeviceCreateInfo device = {
2274 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2275 .pNext = NULL,
2276 .queueRecordCount = 1,
2277 .pRequestedQueues = &queue,
2278 .layerCount = enabled_layer_count,
2279 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
2280 .extensionCount = enabled_extension_count,
2281 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002282 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002283 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002284
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002285 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286 assert(!err);
2287
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002288 free(device_layers);
2289
Ian Elliotta0781972015-08-21 15:09:33 -06002290 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2291 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2292 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2293 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2294 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002295 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2296 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2297 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2298 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002299}
2300
Tony Barbourcc69f452015-10-08 13:59:42 -06002301static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002302{
2303 VkResult err;
2304 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002305
Tony Barbourcc69f452015-10-08 13:59:42 -06002306 // Construct the surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002307 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002308 demo->surface_description.pNext = NULL;
2309#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002310 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002311 demo->surface_description.pPlatformHandle = demo->connection;
2312 demo->surface_description.pPlatformWindow = demo->window;
2313#else // _WIN32
2314 demo->platform_handle_xcb.connection = demo->connection;
2315 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002316 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002317 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2318 demo->surface_description.pPlatformWindow = &demo->window;
2319#endif // _WIN32
2320
Tony Barbourcc69f452015-10-08 13:59:42 -06002321 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002322 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2323 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002324 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2325 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002326 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002327 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002328
2329 // Search for a graphics and a present queue in the array of queue
2330 // families, try to find one that supports both
2331 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2332 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002333 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002334 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2335 if (graphicsQueueNodeIndex == UINT32_MAX) {
2336 graphicsQueueNodeIndex = i;
2337 }
2338
2339 if (supportsPresent[i] == VK_TRUE) {
2340 graphicsQueueNodeIndex = i;
2341 presentQueueNodeIndex = i;
2342 break;
2343 }
2344 }
2345 }
2346 if (presentQueueNodeIndex == UINT32_MAX) {
2347 // If didn't find a queue that supports both graphics and present, then
2348 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002349 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002350 if (supportsPresent[i] == VK_TRUE) {
2351 presentQueueNodeIndex = i;
2352 break;
2353 }
2354 }
2355 }
2356 free(supportsPresent);
2357
2358 // Generate error if could not find both a graphics and a present queue
2359 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2360 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002361 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002362 }
2363
2364 // TODO: Add support for separate queues, including presentation,
2365 // synchronization, and appropriate tracking for QueueSubmit
2366 // While it is possible for an application to use a separate graphics and a
2367 // present queues, this demo program assumes it is only using one:
2368 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2369 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002370 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002371 }
2372
2373 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002374
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002375 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002376 0, &demo->queue);
2377 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002378
Ian Elliottaaae5352015-07-06 14:27:58 -06002379 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002380 uint32_t formatCount;
Ian Elliotta0781972015-08-21 15:09:33 -06002381 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2382 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002383 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002384 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002385 VkSurfaceFormatKHR *surfFormats =
2386 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2387 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2388 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002389 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002390 assert(!err);
2391 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2392 // the surface has no preferred format. Otherwise, at least one
2393 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002394 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2395 {
2396 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2397 }
2398 else
2399 {
2400 assert(formatCount >= 1);
2401 demo->format = surfFormats[0].format;
2402 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002403 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002404
David Pinedo2bb7c932015-06-18 17:03:14 -06002405 demo->quit = false;
2406 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002407
2408 // Get Memory information and properties
2409 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2410 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002411}
2412
2413static void demo_init_connection(struct demo *demo)
2414{
Ian Elliott639ca472015-04-16 15:23:05 -06002415#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002416 const xcb_setup_t *setup;
2417 xcb_screen_iterator_t iter;
2418 int scr;
2419
2420 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002421 if (demo->connection == NULL) {
2422 printf("Cannot find a compatible Vulkan installable client driver "
2423 "(ICD).\nExiting ...\n");
2424 fflush(stdout);
2425 exit(1);
2426 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002427
2428 setup = xcb_get_setup(demo->connection);
2429 iter = xcb_setup_roots_iterator(setup);
2430 while (scr-- > 0)
2431 xcb_screen_next(&iter);
2432
2433 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002434#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002435}
2436
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002437static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002438{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002439 vec3 eye = {0.0f, 3.0f, 5.0f};
2440 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002441 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002442
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002443 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002444 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002445
Piers Daniell735ee532015-02-23 16:23:13 -07002446 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002447 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002448 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002449 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002450 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002451 if (strcmp(argv[i], "--use_glsl") == 0) {
2452 demo->use_glsl = true;
2453 continue;
2454 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002455 if (strcmp(argv[i], "--break") == 0) {
2456 demo->use_break = true;
2457 continue;
2458 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002459 if (strcmp(argv[i], "--validate") == 0) {
2460 demo->validate = true;
2461 continue;
2462 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002463 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002464 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002465 i < argc-1 &&
2466 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2467 demo->frameCount >= 0)
2468 {
2469 i++;
2470 continue;
2471 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002472
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002473 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002474 fflush(stderr);
2475 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002476 }
2477
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002478 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002479 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002480
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002481 demo->width = 500;
2482 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002483
2484 demo->spin_angle = 0.01f;
2485 demo->spin_increment = 0.01f;
2486 demo->pause = false;
2487
Piers Daniell735ee532015-02-23 16:23:13 -07002488 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002489 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2490 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002491}
2492
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002493
Ian Elliott639ca472015-04-16 15:23:05 -06002494#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002495extern int __getmainargs(
2496 int * _Argc,
2497 char *** _Argv,
2498 char *** _Env,
2499 int _DoWildCard,
2500 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002501
Ian Elliotta748eaf2015-04-28 15:50:36 -06002502int WINAPI WinMain(HINSTANCE hInstance,
2503 HINSTANCE hPrevInstance,
2504 LPSTR pCmdLine,
2505 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002506{
2507 MSG msg; // message
2508 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002509 int argc;
2510 char** argv;
2511 char** env;
2512 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002513
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002514 __getmainargs(&argc,&argv,&env,0,&new_mode);
2515
2516 demo_init(&demo, argc, argv);
2517 demo.connection = hInstance;
2518 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002519 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002520 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002521
2522 demo_prepare(&demo);
2523
2524 done = false; //initialize loop condition variable
2525 /* main message loop*/
2526 while(!done)
2527 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002528 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002529 if (msg.message == WM_QUIT) //check for a quit message
2530 {
2531 done = true; //if found, quit app
2532 }
2533 else
2534 {
2535 /* Translate and dispatch to event queue*/
2536 TranslateMessage(&msg);
2537 DispatchMessage(&msg);
2538 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002539 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002540 }
2541
2542 demo_cleanup(&demo);
2543
Tony Barbourf9921732015-04-22 11:36:22 -06002544 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002545}
2546#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002547int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002548{
2549 struct demo demo;
2550
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002551 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002552 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002553 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002554
2555 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002556 demo_run(&demo);
2557
2558 demo_cleanup(&demo);
2559
2560 return 0;
2561}
Ian Elliott639ca472015-04-16 15:23:05 -06002562#endif // _WIN32