blob: 4c1cc3190bb1bee537326a66ee0c148adca77da9 [file] [log] [blame]
Ian Elliott421107f2015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-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 Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060039#include <vulkan.h>
Ian Elliott338dedb2015-08-21 15:09:33 -060040#include <vk_ext_khr_swapchain.h>
41#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060042#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060043
Cody Northropd4e020a2015-03-17 14:54:35 -060044#include "icd-spv.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060045
Mark Lobodzinskieccbb372015-09-01 09:00:16 -060046#include "vk_sdk_platform.h"
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott4e19ed02015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbour22a30862015-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 Elliottcaa9f272015-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 Elliottcaa9f272015-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 Elliotte36b2082015-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 Ashburnccfa8412015-10-08 15:58:23 -060087static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
88
Ian Elliott1b6de092015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Jon Ashburnccfa8412015-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 Elliott1b6de092015-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 Goeltzenleuchter7e334982014-10-30 15:14:16 -0600100/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700101 * structure to track all objects related to a texture.
102 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600103struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600104 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700105
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600106 VkImage image;
107 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600108
Dominik Witczake82d5b62015-09-22 18:25:33 +0200109 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500110 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600111 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700112 int32_t tex_width, tex_height;
113};
114
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600115static char *tex_files[] = {
Tony Barboura26fb7d2015-09-21 15:17:33 -0600116 "lunarg.ppm"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600117};
118
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600119struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600120 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600121 float mvp[4][4];
122 float position[12*3][4];
123 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600124};
125
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600126struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600127 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600128 float mvp[4][4];
129 float position[12*3][4];
130 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600131};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600132
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600133//--------------------------------------------------------------------------------------
134// Mesh and VertexFormat Data
135//--------------------------------------------------------------------------------------
136struct Vertex
137{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600138 float posX, posY, posZ, posW; // Position data
139 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600140};
141
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600142struct VertexPosTex
143{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600144 float posX, posY, posZ, posW; // Position data
145 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600146};
147
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600148#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600149#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600150
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600151static const float g_vertex_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800152 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600153 -1.0f,-1.0f, 1.0f,
154 -1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800155 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600156 -1.0f, 1.0f,-1.0f,
157 -1.0f,-1.0f,-1.0f,
158
Chia-I Wuc3487c22015-04-22 14:56:17 +0800159 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600160 1.0f, 1.0f,-1.0f,
161 1.0f,-1.0f,-1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600163 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600164 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600165
Chia-I Wuc3487c22015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600167 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600168 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600170 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600171 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600172
Chia-I Wuc3487c22015-04-22 14:56:17 +0800173 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600174 -1.0f, 1.0f, 1.0f,
175 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800176 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600177 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600178 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600179
Chia-I Wuc3487c22015-04-22 14:56:17 +0800180 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600181 1.0f, 1.0f, 1.0f,
182 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800183 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600184 1.0f,-1.0f,-1.0f,
185 1.0f, 1.0f,-1.0f,
186
Chia-I Wuc3487c22015-04-22 14:56:17 +0800187 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600188 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600189 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800190 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600191 1.0f,-1.0f, 1.0f,
192 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600193};
194
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600195static const float g_uv_buffer_data[] = {
Chia-I Wuc3487c22015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600214 0.0f, 0.0f,
215 0.0f, 1.0f,
216
Chia-I Wuc3487c22015-04-22 14:56:17 +0800217 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600218 0.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800219 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600221 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600222 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600223
Chia-I Wuc3487c22015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600228 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600229 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600230
Chia-I Wuc3487c22015-04-22 14:56:17 +0800231 0.0f, 1.0f, // +Z side
232 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600234 0.0f, 0.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800235 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-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 Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600259VkBool32 dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600260 VkFlags msgFlags,
261 VkDbgObjectType objType,
262 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-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 Barbour5685ad72015-04-29 16:19:20 -0600268{
269 char *message = (char *) malloc(strlen(pMsg)+100);
270
271 assert (message);
272
Courtney Goeltzenleuchter1c7c65d2015-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 Barboura65ecc22015-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 Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600278 return false;
Tony Barboura65ecc22015-06-30 14:14:19 -0600279 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600280 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour5685ad72015-04-29 16:19:20 -0600281 } else {
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600282 return false;
Tony Barbour5685ad72015-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 Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600292
Courtney Goeltzenleuchtere4c43132015-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 Barbour5685ad72015-04-29 16:19:20 -0600301}
302
Ian Elliott338dedb2015-08-21 15:09:33 -0600303typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600304 VkImage image;
305 VkCmdBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600306 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600307} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600308
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600309struct demo {
Ian Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600316 xcb_connection_t *connection;
317 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800318 xcb_window_t window;
319 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott338dedb2015-08-21 15:09:33 -0600320 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour7910de72015-07-13 16:37:21 -0600321#endif // _WIN32
Cody Northrop75db0322015-05-28 11:27:16 -0600322 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700323 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600324 bool use_glsl;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600325
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600326 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600327 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600328 VkDevice device;
329 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700330 uint32_t graphics_queue_node_index;
Tony Barbour426b9052015-06-24 16:06:58 -0600331 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600332 VkQueueFamilyProperties *queue_props;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600333 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600334
335 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600336 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600337 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600338
Ian Elliott338dedb2015-08-21 15:09:33 -0600339 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
340 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
341 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
342 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
343 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
344 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
345 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
346 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
347 PFN_vkQueuePresentKHR fpQueuePresentKHR;
348 VkSurfaceDescriptionWindowKHR surface_description;
349 uint32_t swapchainImageCount;
Ian Elliotte2688a52015-10-16 18:02:43 -0600350 VkSwapchainKHR swapchain;
Ian Elliott338dedb2015-08-21 15:09:33 -0600351 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600352
Ian Elliotte36b2082015-07-06 14:27:58 -0600353 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600354
355 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600356 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600357
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600358 VkImage image;
Dominik Witczake82d5b62015-09-22 18:25:33 +0200359 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500360 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600361 VkImageView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600362 } depth;
363
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600364 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600365
366 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600367 VkBuffer buf;
Dominik Witczake82d5b62015-09-22 18:25:33 +0200368 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500369 VkDeviceMemory mem;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -0600370 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600371 } uniform_data;
372
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600373 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500374 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600375 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600376 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800377 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600378 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600379
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600380 mat4x4 projection_matrix;
381 mat4x4 view_matrix;
382 mat4x4 model_matrix;
383
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600384 float spin_angle;
385 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600386 bool pause;
387
Tony Barbour9400f092015-07-21 09:04:41 -0600388 VkShaderModule vert_shader_module;
389 VkShaderModule frag_shader_module;
390
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600391 VkDescriptorPool desc_pool;
392 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800393
Tony Barbour5aabff52015-10-08 14:26:24 -0600394 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800395
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600396 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600397 int32_t curFrame;
398 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600399 bool validate;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600400 bool use_break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600401 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600402 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600403 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600404 VkDbgMsgCallback msg_callback;
405
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600406 uint32_t current_buffer;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -0600407 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600408};
409
Ian Elliotte2688a52015-10-16 18:02:43 -0600410// Forward declaration:
411static void demo_resize(struct demo *demo);
412
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600413static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinski72346292015-07-02 16:49:40 -0600414{
415 // Search memtypes to find first index with those properties
416 for (uint32_t i = 0; i < 32; i++) {
417 if ((typeBits & 1) == 1) {
418 // Type is available, does it match user properties?
Tony Barbourf1eceb92015-10-15 12:42:56 -0600419 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinski72346292015-07-02 16:49:40 -0600420 *typeIndex = i;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600421 return true;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600422 }
423 }
424 typeBits >>= 1;
425 }
426 // No memory types matched, return failure
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600427 return false;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600428}
429
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600430static void demo_flush_init_cmd(struct demo *demo)
431{
Tony Barbour22a30862015-04-22 09:02:32 -0600432 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600433
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600434 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600435 return;
436
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600437 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600438 assert(!err);
439
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600440 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wue420a332015-10-26 20:04:44 +0800441 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600442 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800443 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
444 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800445 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600446 .pWaitSemaphores = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800447 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600448 .pCommandBuffers = cmd_bufs,
Chia-I Wu763a7492015-10-26 20:48:51 +0800449 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600450 .pSignalSemaphores = NULL
451 };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600452
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600453 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600454 assert(!err);
455
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600456 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600457 assert(!err);
458
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600459 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600460 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600461}
462
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600463static void demo_set_image_layout(
464 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600465 VkImage image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600466 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600467 VkImageLayout old_image_layout,
468 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600469{
Tony Barbour22a30862015-04-22 09:02:32 -0600470 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600471
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600472 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600473 const VkCmdBufferAllocInfo cmd = {
474 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600475 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600476 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800477 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +0800478 .bufferCount = 1,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600479 };
480
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600481 err = vkAllocCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600482 assert(!err);
483
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600484 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600485 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600486 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600487 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800488 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600489 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800490 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600491 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600492 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourc1098272015-10-23 10:53:30 -0600493 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600494 }
495
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600496 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600497 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600498 .pNext = NULL,
499 .outputMask = 0,
500 .inputMask = 0,
501 .oldLayout = old_image_layout,
502 .newLayout = new_image_layout,
503 .image = image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600504 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600505 };
506
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600507 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600508 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600509 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600510 }
511
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600512 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600513 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600514 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600515 }
516
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600517 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600518
Tony Barbourc2e987e2015-06-29 16:20:35 -0600519 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
520 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600521
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600522 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600523}
524
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600525static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600526{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800527 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600528 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600529 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600530 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800531 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600532 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800533 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700534 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800535 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600536 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
537 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800538 };
539 const VkRenderPassBeginInfo rp_begin = {
540 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
541 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800542 .renderPass = demo->render_pass,
543 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800544 .renderArea.offset.x = 0,
545 .renderArea.offset.y = 0,
546 .renderArea.extent.width = demo->width,
547 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600548 .clearValueCount = 2,
549 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700550 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800551 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600552
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600553 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600554 assert(!err);
555
Chia-I Wuc278df82015-07-07 11:50:03 +0800556 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
557
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600558 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600559 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600560 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600561 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600562
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600563 VkViewport viewport;
564 memset(&viewport, 0, sizeof(viewport));
565 viewport.height = (float) demo->height;
566 viewport.width = (float) demo->width;
567 viewport.minDepth = (float) 0.0f;
568 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600569 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600570
571 VkRect2D scissor;
572 memset(&scissor, 0, sizeof(scissor));
573 scissor.extent.width = demo->width;
574 scissor.extent.height = demo->height;
575 scissor.offset.x = 0;
576 scissor.offset.y = 0;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600577 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600578
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600579 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800580 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600581
Tony Barbour40292ae2015-10-21 10:14:48 -0600582 VkImageMemoryBarrier prePresentBarrier = {
583 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
584 .pNext = NULL,
585 .outputMask = VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT,
586 .inputMask = 0,
587 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
588 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
589 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
590 .destQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
591 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
592 };
593
594 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
595 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
596 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_GPU_COMMANDS, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
597 VK_FALSE, 1, (const void * const*)&pmemory_barrier);
598
599
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600600 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600601 assert(!err);
602}
603
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600604
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600605void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600606{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600607 mat4x4 MVP, Model, VP;
608 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600609 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600610 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600611
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600612 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600613
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600614 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600615 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700616 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600617 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600618
Dominik Witczake82d5b62015-09-22 18:25:33 +0200619 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600620 assert(!err);
621
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600622 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600623
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600624 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600625}
626
627static void demo_draw(struct demo *demo)
628{
Tony Barbour22a30862015-04-22 09:02:32 -0600629 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600630 VkSemaphore presentCompleteSemaphore;
631 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
632 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
633 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600634 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600635 };
Chia-I Wue420a332015-10-26 20:04:44 +0800636 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600637
Ian Elliotte36b2082015-07-06 14:27:58 -0600638 err = vkCreateSemaphore(demo->device,
639 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +0800640 NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600641 &presentCompleteSemaphore);
642 assert(!err);
643
644 // Get the index of the next available swapchain image:
Ian Elliotte2688a52015-10-16 18:02:43 -0600645 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600646 UINT64_MAX,
647 presentCompleteSemaphore,
648 &demo->current_buffer);
Ian Elliotte2688a52015-10-16 18:02:43 -0600649 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
650 // demo->swapchain is out of date (e.g. the window was resized) and
651 // must be recreated:
652 demo_resize(demo);
653 demo_draw(demo);
Chia-I Wu69f40122015-10-26 21:10:41 +0800654 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -0600655 return;
656 } else if (err == VK_SUBOPTIMAL_KHR) {
657 // demo->swapchain is not as optimal as it could be, but the platform's
658 // presentation engine will still present the image correctly.
659 } else {
660 assert(!err);
661 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600662
Tony Barbour40292ae2015-10-21 10:14:48 -0600663 // Assume the command buffer has been run on current_buffer before so
664 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
665 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
666 VK_IMAGE_ASPECT_COLOR_BIT,
667 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
668 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
669 demo_flush_init_cmd(demo);
670
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600671 // Wait for the present complete semaphore to be signaled to ensure
672 // that the image won't be rendered to until the presentation
673 // engine has fully released ownership to the application, and it is
674 // okay to render to the image.
675
Ian Elliott338dedb2015-08-21 15:09:33 -0600676// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600677 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800678 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
679 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800680 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600681 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu763a7492015-10-26 20:48:51 +0800682 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600683 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu763a7492015-10-26 20:48:51 +0800684 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600685 .pSignalSemaphores = NULL
686 };
687
688 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600689 assert(!err);
690
Ian Elliott338dedb2015-08-21 15:09:33 -0600691 VkPresentInfoKHR present = {
692 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600693 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600694 .swapchainCount = 1,
Ian Elliotte2688a52015-10-16 18:02:43 -0600695 .swapchains = &demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600696 .imageIndices = &demo->current_buffer,
697 };
698
699// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600700 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliotte2688a52015-10-16 18:02:43 -0600701 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
702 // demo->swapchain is out of date (e.g. the window was resized) and
703 // must be recreated:
704 demo_resize(demo);
705 } else if (err == VK_SUBOPTIMAL_KHR) {
706 // demo->swapchain is not as optimal as it could be, but the platform's
707 // presentation engine will still present the image correctly.
708 } else {
709 assert(!err);
710 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600711
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800712 err = vkQueueWaitIdle(demo->queue);
713 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600714
Chia-I Wu69f40122015-10-26 21:10:41 +0800715 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600716}
717
718static void demo_prepare_buffers(struct demo *demo)
719{
Ian Elliotte36b2082015-07-06 14:27:58 -0600720 VkResult U_ASSERT_ONLY err;
Ian Elliotte2688a52015-10-16 18:02:43 -0600721 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600722
723 // Check the surface properties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600724 VkSurfacePropertiesKHR surfProperties;
725 err = demo->fpGetSurfacePropertiesKHR(demo->device,
726 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600727 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600728 assert(!err);
729
Ian Elliott7fe115d2015-08-07 15:56:59 -0600730 uint32_t presentModeCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600731 err = demo->fpGetSurfacePresentModesKHR(demo->device,
732 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600733 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600734 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600735 VkPresentModeKHR *presentModes =
736 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600737 assert(presentModes);
Ian Elliott338dedb2015-08-21 15:09:33 -0600738 err = demo->fpGetSurfacePresentModesKHR(demo->device,
739 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600740 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600741 assert(!err);
742
Ian Elliott338dedb2015-08-21 15:09:33 -0600743 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600744 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600745 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600746 {
747 // If the surface size is undefined, the size is set to
748 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600749 swapchainExtent.width = demo->width;
750 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600751 }
752 else
753 {
754 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -0600755 swapchainExtent = surfProperties.currentExtent;
Ian Elliotte2688a52015-10-16 18:02:43 -0600756 demo->width = surfProperties.currentExtent.width;
757 demo->height = surfProperties.currentExtent.height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600758 }
759
760 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3fc49c22015-07-27 13:53:11 -0600761 // tearing mode. If not, try IMMEDIATE which will usually be available,
762 // and is fastest (though it tears). If not, fall back to FIFO which is
763 // always available.
Ian Elliott338dedb2015-08-21 15:09:33 -0600764 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600765 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -0600766 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
767 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600768 break;
769 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600770 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
771 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
772 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3fc49c22015-07-27 13:53:11 -0600773 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600774 }
775
776 // Determine the number of VkImage's to use in the swap chain (we desire to
777 // own only 1 image at a time, besides the images being displayed and
778 // queued for display):
Ian Elliott338dedb2015-08-21 15:09:33 -0600779 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600780 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600781 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600782 {
783 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600784 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600785 }
786
Tony Barbourd161c542015-09-16 15:01:32 -0600787 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliott338dedb2015-08-21 15:09:33 -0600788 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
789 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600790 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600791 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600792 }
793
Ian Elliotte2688a52015-10-16 18:02:43 -0600794 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600795 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800796 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600797 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
798 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800799 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600800 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800801 .imageExtent = {
Ian Elliott338dedb2015-08-21 15:09:33 -0600802 .width = swapchainExtent.width,
803 .height = swapchainExtent.height,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600804 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600805 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600806 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800807 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600808 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
809 .queueFamilyCount = 0,
810 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600811 .presentMode = swapchainPresentMode,
Ian Elliotte2688a52015-10-16 18:02:43 -0600812 .oldSwapchain = oldSwapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600813 .clipped = true,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600814 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600815 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600816
Ian Elliotte2688a52015-10-16 18:02:43 -0600817 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800818 assert(!err);
819
Ian Elliotte2688a52015-10-16 18:02:43 -0600820 // If we just re-created an existing swapchain, we should destroy the old
821 // swapchain at this point.
822 // Note: destroying the swapchain also cleans up all its associated
823 // presentable images once the platform is done with them.
Chia-I Wue420a332015-10-26 20:04:44 +0800824 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliotte2688a52015-10-16 18:02:43 -0600825 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
826 }
827
828 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600829 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600830 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800831
Ian Elliott338dedb2015-08-21 15:09:33 -0600832 VkImage* swapchainImages =
833 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
834 assert(swapchainImages);
Ian Elliotte2688a52015-10-16 18:02:43 -0600835 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600836 &demo->swapchainImageCount,
837 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600838 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -0600839
Ian Elliott338dedb2015-08-21 15:09:33 -0600840 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600841 assert(demo->buffers);
842
Ian Elliott338dedb2015-08-21 15:09:33 -0600843 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600844 VkImageViewCreateInfo color_image_view = {
845 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600846 .pNext = NULL,
847 .format = demo->format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600848 .channels = {
849 .r = VK_CHANNEL_SWIZZLE_R,
850 .g = VK_CHANNEL_SWIZZLE_G,
851 .b = VK_CHANNEL_SWIZZLE_B,
852 .a = VK_CHANNEL_SWIZZLE_A,
853 },
854 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600855 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600856 .baseMipLevel = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600857 .numLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600858 .baseArrayLayer = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600859 .numLayers = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600860 },
861 .viewType = VK_IMAGE_VIEW_TYPE_2D,
862 .flags = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600863 };
864
Ian Elliott338dedb2015-08-21 15:09:33 -0600865 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600866
Tony Barbour40292ae2015-10-21 10:14:48 -0600867 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
868 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600869 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -0600870 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600871 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600872 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600873
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600874 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600875
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600876 err = vkCreateImageView(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +0800877 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600878 assert(!err);
879 }
880}
881
882static void demo_prepare_depth(struct demo *demo)
883{
Tony Barbour8205d902015-04-16 15:59:00 -0600884 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600885 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600886 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600887 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600888 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600889 .format = depth_format,
890 .extent = { demo->width, demo->height, 1 },
891 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600892 .arrayLayers = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600893 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600894 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600895 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600896 .flags = 0,
897 };
Dominik Witczake82d5b62015-09-22 18:25:33 +0200898
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600899 VkImageViewCreateInfo view = {
900 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600901 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +0800902 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4ab40c42015-07-15 17:41:38 -0600903 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600904 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600905 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600906 .baseMipLevel = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600907 .numLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600908 .baseArrayLayer = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600909 .numLayers = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600910 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600911 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600912 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600913 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600914
Mark Lobodzinski23182612015-05-29 09:32:35 -0500915 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600916 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600917 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600918
919 demo->depth.format = depth_format;
920
921 /* create image */
Chia-I Wu69f40122015-10-26 21:10:41 +0800922 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600923 &demo->depth.image);
924 assert(!err);
925
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600926 vkGetImageMemoryRequirements(demo->device,
Tony Barbourde4124d2015-07-03 10:33:54 -0600927 demo->depth.image, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -0600928 assert(!err);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600929
Dominik Witczake82d5b62015-09-22 18:25:33 +0200930 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
931 demo->depth.mem_alloc.pNext = NULL;
932 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
933 demo->depth.mem_alloc.memoryTypeIndex = 0;
934
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600935 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600936 mem_reqs.memoryTypeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600937 0, /* No requirements */
Dominik Witczake82d5b62015-09-22 18:25:33 +0200938 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600939 assert(pass);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600940
Mark Lobodzinski23182612015-05-29 09:32:35 -0500941 /* allocate memory */
Chia-I Wu69f40122015-10-26 21:10:41 +0800942 err = vkAllocMemory(demo->device, &demo->depth.mem_alloc,
943 NULL, &demo->depth.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500944 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600945
Mark Lobodzinski23182612015-05-29 09:32:35 -0500946 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600947 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500948 demo->depth.mem, 0);
949 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600950
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600951 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600952 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600953 VK_IMAGE_LAYOUT_UNDEFINED,
954 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600955
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600956 /* create image view */
957 view.image = demo->depth.image;
Chia-I Wu69f40122015-10-26 21:10:41 +0800958 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600959 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600960}
961
Tony Barboura26fb7d2015-09-21 15:17:33 -0600962/* Load a ppm file into memory */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700963bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600964 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600965 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600966{
Tony Barboura26fb7d2015-09-21 15:17:33 -0600967 FILE *fPtr = fopen(filename,"rb");
968 char header[256], *cPtr;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600969
Tony Barboura26fb7d2015-09-21 15:17:33 -0600970 if (!fPtr)
971 return false;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600972
Tony Barboura26fb7d2015-09-21 15:17:33 -0600973 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan85180252015-10-28 11:15:46 -0600974 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
975 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600976 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600977 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600978
Tony Barboura26fb7d2015-09-21 15:17:33 -0600979 do {
980 cPtr = fgets(header, 256, fPtr);
Mike Stroyan85180252015-10-28 11:15:46 -0600981 if (cPtr == NULL) {
982 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600983 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600984 }
Tony Barboura26fb7d2015-09-21 15:17:33 -0600985 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600986
Tony Barboura26fb7d2015-09-21 15:17:33 -0600987 sscanf(header, "%u %u", height, width);
Mike Stroyan85180252015-10-28 11:15:46 -0600988 if (rgba_data == NULL) {
989 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600990 return true;
Mike Stroyan85180252015-10-28 11:15:46 -0600991 }
Tony Barboura26fb7d2015-09-21 15:17:33 -0600992 fgets(header, 256, fPtr); // Format
Mike Stroyan85180252015-10-28 11:15:46 -0600993 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
994 fclose(fPtr);
Tony Barboura26fb7d2015-09-21 15:17:33 -0600995 return false;
Mike Stroyan85180252015-10-28 11:15:46 -0600996 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600997
Tony Barboura26fb7d2015-09-21 15:17:33 -0600998 for(int y = 0; y < *height; y++)
999 {
1000 uint8_t *rowPtr = rgba_data;
1001 for(int x = 0; x < *width; x++)
1002 {
1003 fread(rowPtr, 3, 1, fPtr);
1004 rowPtr[3] = 255; /* Alpha of 1 */
1005 rowPtr += 4;
1006 }
1007 rgba_data += layout->rowPitch;
1008 }
1009 fclose(fPtr);
Mike Stroyan85180252015-10-28 11:15:46 -06001010 return true;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001011}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001012
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001013static void demo_prepare_texture_image(struct demo *demo,
1014 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001015 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001016 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001017 VkImageUsageFlags usage,
Tony Barbourf1eceb92015-10-15 12:42:56 -06001018 VkFlags required_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001019{
Mike Stroyande24a6f2015-06-15 14:21:03 -06001020 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001021 int32_t tex_width;
1022 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -06001023 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001024 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001025
David Pinedo61e77382015-04-23 08:16:57 -06001026 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1027 {
1028 printf("Failed to load textures\n");
1029 fflush(stdout);
1030 exit(1);
1031 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001032
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001033 tex_obj->tex_width = tex_width;
1034 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001035
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001036 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001037 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001038 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -06001039 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001040 .format = tex_format,
1041 .extent = { tex_width, tex_height, 1 },
1042 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -06001043 .arrayLayers = 1,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001044 .samples = 1,
1045 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001046 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001047 .flags = 0,
1048 };
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001049
Mark Lobodzinski23182612015-05-29 09:32:35 -05001050 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001051
Chia-I Wu69f40122015-10-26 21:10:41 +08001052 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001053 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001054 assert(!err);
1055
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001056 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell886be472015-02-23 16:23:13 -07001057
Dominik Witczake82d5b62015-09-22 18:25:33 +02001058 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1059 tex_obj->mem_alloc.pNext = NULL;
1060 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1061 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001062
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001063 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1064 assert(pass);
Mark Lobodzinski72346292015-07-02 16:49:40 -06001065
Mark Lobodzinski23182612015-05-29 09:32:35 -05001066 /* allocate memory */
Chia-I Wu69f40122015-10-26 21:10:41 +08001067 err = vkAllocMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001068 &(tex_obj->mem));
1069 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001070
Mark Lobodzinski23182612015-05-29 09:32:35 -05001071 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -06001072 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001073 tex_obj->mem, 0);
1074 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001075
Tony Barbourf1eceb92015-10-15 12:42:56 -06001076 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001077 const VkImageSubresource subres = {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -06001078 .aspect = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001079 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -06001080 .arrayLayer = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001081 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001082 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001083 void *data;
1084
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001085 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001086
Dominik Witczake82d5b62015-09-22 18:25:33 +02001087 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001088 assert(!err);
1089
1090 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1091 fprintf(stderr, "Error loading texture: %s\n", filename);
1092 }
1093
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001094 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001095 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001096
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001097 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001098 demo_set_image_layout(demo, tex_obj->image,
Tony Barbour983984f2015-10-12 11:07:58 -06001099 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001100 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001101 tex_obj->imageLayout);
1102 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001103}
1104
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001105static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001106{
1107 /* clean up staging resources */
Chia-I Wu69f40122015-10-26 21:10:41 +08001108 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1109 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001110}
1111
1112static void demo_prepare_textures(struct demo *demo)
1113{
Tony Barbour8205d902015-04-16 15:59:00 -06001114 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001115 VkFormatProperties props;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001116 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001117
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001118 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001119
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001120 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001121 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001122
FslNopper434db6a2015-05-06 21:42:01 +02001123 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001124 /* Device can texture using linear textures */
1125 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001126 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001127 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001128 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001129 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001130
1131 memset(&staging_texture, 0, sizeof(staging_texture));
1132 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001133 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001134
1135 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001136 VK_IMAGE_TILING_OPTIMAL,
1137 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1138 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001139
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001140 demo_set_image_layout(demo, staging_texture.image,
Tony Barbour983984f2015-10-12 11:07:58 -06001141 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001142 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001143 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001144
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001145 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -06001146 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001147 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001148 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001149
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001150 VkImageCopy copy_region = {
Tony Barbourca5c4eb2015-11-02 11:47:51 -07001151 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001152 .srcOffset = { 0, 0, 0 },
Tony Barbourca5c4eb2015-11-02 11:47:51 -07001153 .destSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001154 .destOffset = { 0, 0, 0 },
1155 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1156 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001157 vkCmdCopyImage(demo->cmd,
1158 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1159 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001160 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001161
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001162 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbour983984f2015-10-12 11:07:58 -06001163 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001164 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001165 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001166
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001167 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001168
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001169 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001170 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001171 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1172 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001173 }
1174
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001175 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001176 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001177 .pNext = NULL,
Chia-I Wu3603b082015-10-26 16:49:32 +08001178 .magFilter = VK_FILTER_NEAREST,
1179 .minFilter = VK_FILTER_NEAREST,
1180 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
1181 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP,
1182 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP,
1183 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001184 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001185 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001186 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001187 .minLod = 0.0f,
1188 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001189 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -06001190 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001191 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001192
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001193 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001194 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001195 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +08001196 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001197 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001198 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001199 .channels = { VK_CHANNEL_SWIZZLE_R,
1200 VK_CHANNEL_SWIZZLE_G,
1201 VK_CHANNEL_SWIZZLE_B,
1202 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -06001203 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001204 .flags = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001205 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001206
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001207 /* create sampler */
Chia-I Wu69f40122015-10-26 21:10:41 +08001208 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001209 &demo->textures[i].sampler);
1210 assert(!err);
1211
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001212 /* create image view */
1213 view.image = demo->textures[i].image;
Chia-I Wu69f40122015-10-26 21:10:41 +08001214 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001215 &demo->textures[i].view);
1216 assert(!err);
1217 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001218}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001219
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001220void demo_prepare_cube_data_buffer(struct demo *demo)
1221{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001222 VkBufferCreateInfo buf_info;
Mark Lobodzinski23182612015-05-29 09:32:35 -05001223 VkMemoryRequirements mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001224 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001225 int i;
1226 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001227 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001228 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001229 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001230
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001231 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001232 mat4x4_mul(MVP, VP, demo->model_matrix);
1233 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001234// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001235
1236 for (i=0; i<12*3; i++) {
1237 data.position[i][0] = g_vertex_buffer_data[i*3];
1238 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1239 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1240 data.position[i][3] = 1.0f;
1241 data.attr[i][0] = g_uv_buffer_data[2*i];
1242 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1243 data.attr[i][2] = 0;
1244 data.attr[i][3] = 0;
1245 }
1246
Chia-I Wu714df452015-01-01 07:55:04 +08001247 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001248 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001249 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop86049392015-07-16 10:29:32 -06001250 buf_info.size = sizeof(data);
Chia-I Wu69f40122015-10-26 21:10:41 +08001251 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1252 &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001253 assert(!err);
1254
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001255 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +08001256
Dominik Witczake82d5b62015-09-22 18:25:33 +02001257 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1258 demo->uniform_data.mem_alloc.pNext = NULL;
1259 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1260 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1261
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001262 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001263 mem_reqs.memoryTypeBits,
1264 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczake82d5b62015-09-22 18:25:33 +02001265 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001266 assert(pass);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001267
Chia-I Wu69f40122015-10-26 21:10:41 +08001268 err = vkAllocMemory(demo->device, &demo->uniform_data.mem_alloc,
1269 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski23182612015-05-29 09:32:35 -05001270 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001271
Dominik Witczake82d5b62015-09-22 18:25:33 +02001272 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001273 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001274
Mark Lobodzinski23182612015-05-29 09:32:35 -05001275 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001276
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001277 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001278
Tony Barbourde4124d2015-07-03 10:33:54 -06001279 err = vkBindBufferMemory(demo->device,
1280 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001281 demo->uniform_data.mem, 0);
1282 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001283
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001284 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1285 demo->uniform_data.buffer_info.offset = 0;
1286 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001287}
1288
Chia-I Wuf8385062015-01-04 16:27:24 +08001289static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001290{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001291 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001292 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001293 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001294 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001295 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001296 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001297 },
1298 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001299 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001300 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001301 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001302 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001303 },
1304 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001305 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001306 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001307 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001308 .bindingCount = 2,
1309 .pBindings = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001310 };
Tony Barbour22a30862015-04-22 09:02:32 -06001311 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001312
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001313 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001314 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001315 assert(!err);
1316
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001317 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1318 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1319 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001320 .setLayoutCount = 1,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001321 .pSetLayouts = &demo->desc_layout,
1322 };
1323
1324 err = vkCreatePipelineLayout(demo->device,
1325 &pPipelineLayoutCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +08001326 NULL,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001327 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001328 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001329}
1330
Chia-I Wu76cd4222015-07-08 13:34:24 +08001331static void demo_prepare_render_pass(struct demo *demo)
1332{
Chia-I Wuc278df82015-07-07 11:50:03 +08001333 const VkAttachmentDescription attachments[2] = {
1334 [0] = {
1335 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1336 .pNext = NULL,
1337 .format = demo->format,
1338 .samples = 1,
1339 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1340 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1341 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1342 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1343 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1344 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1345 },
1346 [1] = {
1347 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1348 .pNext = NULL,
1349 .format = demo->depth.format,
1350 .samples = 1,
1351 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1352 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1353 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1354 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1355 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1356 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1357 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001358 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001359 const VkAttachmentReference color_reference = {
1360 .attachment = 0,
1361 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1362 };
1363 const VkSubpassDescription subpass = {
1364 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1365 .pNext = NULL,
1366 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1367 .flags = 0,
Chia-I Wu763a7492015-10-26 20:48:51 +08001368 .inputAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001369 .pInputAttachments = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001370 .colorAttachmentCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001371 .pColorAttachments = &color_reference,
1372 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001373 .depthStencilAttachment = {
1374 .attachment = 1,
1375 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1376 },
Chia-I Wu763a7492015-10-26 20:48:51 +08001377 .preserveAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001378 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001379 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001380 const VkRenderPassCreateInfo rp_info = {
1381 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1382 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001383 .attachmentCount = 2,
1384 .pAttachments = attachments,
1385 .subpassCount = 1,
1386 .pSubpasses = &subpass,
1387 .dependencyCount = 0,
1388 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001389 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001390 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001391
Chia-I Wu69f40122015-10-26 21:10:41 +08001392 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001393 assert(!err);
1394}
1395
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001396static VkShader demo_prepare_shader(struct demo* demo,
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001397 VkShaderStageFlagBits stage,
Tony Barbour9400f092015-07-21 09:04:41 -06001398 VkShaderModule* pShaderModule,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001399 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001400 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001401{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001402 VkShaderModuleCreateInfo moduleCreateInfo;
1403 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001404 VkShader shader;
1405 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001406
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001407
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001408 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1409 moduleCreateInfo.pNext = NULL;
1410
1411 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1412 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001413 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001414
Cody Northrop75db0322015-05-28 11:27:16 -06001415 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001416 moduleCreateInfo.codeSize = size;
1417 moduleCreateInfo.pCode = code;
1418 moduleCreateInfo.flags = 0;
Chia-I Wu69f40122015-10-26 21:10:41 +08001419 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, pShaderModule);
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001420 assert(!err);
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001421
1422 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001423 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001424 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001425 shaderCreateInfo.stage = stage;
Chia-I Wu69f40122015-10-26 21:10:41 +08001426 err = vkCreateShader(demo->device, &shaderCreateInfo, NULL, &shader);
Tony Barbourc1098272015-10-23 10:53:30 -06001427 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001428 } else {
1429 // Create fake SPV structure to feed GLSL
1430 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001431 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1432 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1433 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001434
1435 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001436 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1437 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1438 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1439 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001440
Chia-I Wu69f40122015-10-26 21:10:41 +08001441 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001442 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001443 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001444 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001445
1446 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001447 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001448 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001449 shaderCreateInfo.stage = stage;
Chia-I Wu69f40122015-10-26 21:10:41 +08001450 err = vkCreateShader(demo->device, &shaderCreateInfo, NULL, &shader);
Tony Barbourc1098272015-10-23 10:53:30 -06001451 assert(!err);
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001452 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001453 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001454 return shader;
1455}
1456
Cody Northropacfb0492015-03-17 15:55:58 -06001457char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001458{
1459 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001460 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001461 void *shader_code;
1462
1463 FILE *fp = fopen(filename, "rb");
1464 if (!fp) return NULL;
1465
1466 fseek(fp, 0L, SEEK_END);
1467 size = ftell(fp);
1468
1469 fseek(fp, 0L, SEEK_SET);
1470
1471 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001472 retval = fread(shader_code, size, 1, fp);
1473 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001474
1475 *psize = size;
1476
Mike Stroyan85180252015-10-28 11:15:46 -06001477 fclose(fp);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001478 return shader_code;
1479}
1480
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001481static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001482{
Cody Northrop75db0322015-05-28 11:27:16 -06001483 if (!demo->use_glsl) {
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001484 VkShader shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001485 void *vertShaderCode;
1486 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001487
Cody Northrop75db0322015-05-28 11:27:16 -06001488 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001489
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001490 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT, &demo->vert_shader_module,
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001491 vertShaderCode, size);
1492 free(vertShaderCode);
1493 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001494 } else {
1495 static const char *vertShaderText =
1496 "#version 140\n"
1497 "#extension GL_ARB_separate_shader_objects : enable\n"
1498 "#extension GL_ARB_shading_language_420pack : enable\n"
1499 "\n"
1500 "layout(binding = 0) uniform buf {\n"
1501 " mat4 MVP;\n"
1502 " vec4 position[12*3];\n"
1503 " vec4 attr[12*3];\n"
1504 "} ubuf;\n"
1505 "\n"
1506 "layout (location = 0) out vec4 texcoord;\n"
1507 "\n"
1508 "void main() \n"
1509 "{\n"
1510 " texcoord = ubuf.attr[gl_VertexID];\n"
1511 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1512 "\n"
1513 " // GL->VK conventions\n"
1514 " gl_Position.y = -gl_Position.y;\n"
1515 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1516 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001517
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001518 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT, &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001519 (const void *) vertShaderText,
1520 strlen(vertShaderText));
1521 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001522}
1523
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001524static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001525{
Cody Northrop75db0322015-05-28 11:27:16 -06001526 if (!demo->use_glsl) {
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001527 VkShader shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001528 void *fragShaderCode;
1529 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001530
Cody Northrop75db0322015-05-28 11:27:16 -06001531 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001532
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001533 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT, &demo->frag_shader_module,
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001534 fragShaderCode, size);
1535 free(fragShaderCode);
1536 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001537 } else {
1538 static const char *fragShaderText =
1539 "#version 140\n"
1540 "#extension GL_ARB_separate_shader_objects : enable\n"
1541 "#extension GL_ARB_shading_language_420pack : enable\n"
1542 "layout (binding = 1) uniform sampler2D tex;\n"
1543 "\n"
1544 "layout (location = 0) in vec4 texcoord;\n"
1545 "layout (location = 0) out vec4 uFragColor;\n"
1546 "void main() {\n"
1547 " uFragColor = texture(tex, texcoord.xy);\n"
1548 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001549
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001550 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT, &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001551 (const void *) fragShaderText,
1552 strlen(fragShaderText));
1553 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001554}
1555
1556static void demo_prepare_pipeline(struct demo *demo)
1557{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001558 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001559 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand009e8652015-10-15 17:15:25 -07001560 VkPipelineVertexInputStateCreateInfo vi;
Tony Barboure307f582015-07-10 15:29:03 -06001561 VkPipelineInputAssemblyStateCreateInfo ia;
1562 VkPipelineRasterStateCreateInfo rs;
1563 VkPipelineColorBlendStateCreateInfo cb;
1564 VkPipelineDepthStencilStateCreateInfo ds;
1565 VkPipelineViewportStateCreateInfo vp;
1566 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniell811eb742015-09-29 13:01:09 -06001567 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1568 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbour22a30862015-04-22 09:02:32 -06001569 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001570
Piers Daniell811eb742015-09-29 13:01:09 -06001571 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1572 memset(&dynamicState, 0, sizeof dynamicState);
1573 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1574 dynamicState.pDynamicStates = dynamicStateEnables;
1575
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001576 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001577 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001578 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001579
Jason Ekstrand009e8652015-10-15 17:15:25 -07001580 memset(&vi, 0, sizeof(vi));
1581 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1582
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001583 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001584 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001585 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001586
1587 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001588 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001589 rs.fillMode = VK_FILL_MODE_SOLID;
1590 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001591 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001592 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001593 rs.rasterizerDiscardEnable = VK_FALSE;
1594 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001595
1596 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001597 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1598 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001599 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001600 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001601 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001602 cb.attachmentCount = 1;
1603 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001604
Tony Barbourfa6cac72015-01-16 14:27:35 -07001605 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001606 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001607 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001608 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1609 vp.scissorCount = 1;
1610 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001611
1612 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001613 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001614 ds.depthTestEnable = VK_TRUE;
1615 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001616 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001617 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001618 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1619 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001620 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001621 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001622 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001623
Tony Barbourfa6cac72015-01-16 14:27:35 -07001624 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001625 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001626 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001627 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001628
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001629 // Two stages: vs and fs
1630 pipeline.stageCount = 2;
1631 VkPipelineShaderStageCreateInfo shaderStages[2];
1632 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1633
1634 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001635 shaderStages[0].shader = demo_prepare_vs(demo);
1636
1637 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001638 shaderStages[1].shader = demo_prepare_fs(demo);
1639
Jon Ashburn0d60d272015-07-09 15:02:25 -06001640 memset(&pipelineCache, 0, sizeof(pipelineCache));
1641 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001642
Chia-I Wu69f40122015-10-26 21:10:41 +08001643 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001644 assert(!err);
Tony Barboure307f582015-07-10 15:29:03 -06001645
Jason Ekstrand009e8652015-10-15 17:15:25 -07001646 pipeline.pVertexInputState = &vi;
Tony Barboure307f582015-07-10 15:29:03 -06001647 pipeline.pInputAssemblyState = &ia;
1648 pipeline.pRasterState = &rs;
1649 pipeline.pColorBlendState = &cb;
1650 pipeline.pMultisampleState = &ms;
1651 pipeline.pViewportState = &vp;
1652 pipeline.pDepthStencilState = &ds;
1653 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001654 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001655 pipeline.pDynamicState = &dynamicState;
Tony Barboure307f582015-07-10 15:29:03 -06001656
Courtney Goeltzenleuchter14eb5ea2015-08-13 16:55:04 -06001657 pipeline.renderPass = demo->render_pass;
1658
Chia-I Wu69f40122015-10-26 21:10:41 +08001659 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1660 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001661 assert(!err);
1662
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001663 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001664 vkDestroyShader(demo->device, shaderStages[i].shader, NULL);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001665 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001666 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1667 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001668}
1669
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001670static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001671{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001672 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001673 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001674 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001675 .descriptorCount = 1,
Chia-I Wuf8385062015-01-04 16:27:24 +08001676 },
1677 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001678 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001679 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wuf8385062015-01-04 16:27:24 +08001680 },
1681 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001682 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001683 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001684 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001685 .maxSets = 1,
Chia-I Wu763a7492015-10-26 20:48:51 +08001686 .typeCount = 2,
1687 .pTypeCounts = type_counts,
Chia-I Wuf8385062015-01-04 16:27:24 +08001688 };
Tony Barbour22a30862015-04-22 09:02:32 -06001689 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001690
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001691 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001692 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001693 assert(!err);
1694}
1695
1696static void demo_prepare_descriptor_set(struct demo *demo)
1697{
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001698 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001699 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001700 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001701 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001702
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001703 VkDescriptorSetAllocInfo alloc_info = {
1704 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO,
1705 .pNext = NULL,
1706 .descriptorPool = demo->desc_pool,
Chia-I Wu763a7492015-10-26 20:48:51 +08001707 .setLayoutCount = 1,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001708 .pSetLayouts = &demo->desc_layout
1709 };
1710 err = vkAllocDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northropc8aa4a52015-08-03 12:47:29 -06001711 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001712
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001713 memset(&tex_descs, 0, sizeof(tex_descs));
1714 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001715 tex_descs[i].sampler = demo->textures[i].sampler;
1716 tex_descs[i].imageView = demo->textures[i].view;
1717 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001718 }
1719
1720 memset(&writes, 0, sizeof(writes));
1721
1722 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1723 writes[0].destSet = demo->desc_set;
Chia-I Wu763a7492015-10-26 20:48:51 +08001724 writes[0].descriptorCount = 1;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001725 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001726 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001727
1728 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1729 writes[1].destSet = demo->desc_set;
1730 writes[1].destBinding = 1;
Chia-I Wu763a7492015-10-26 20:48:51 +08001731 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001732 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001733 writes[1].pImageInfo = tex_descs;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001734
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001735 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001736}
1737
Chia-I Wu76cd4222015-07-08 13:34:24 +08001738static void demo_prepare_framebuffers(struct demo *demo)
1739{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001740 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001741 attachments[1] = demo->depth.view;
1742
Chia-I Wu76cd4222015-07-08 13:34:24 +08001743 const VkFramebufferCreateInfo fb_info = {
1744 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1745 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001746 .renderPass = demo->render_pass,
1747 .attachmentCount = 2,
1748 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001749 .width = demo->width,
1750 .height = demo->height,
1751 .layers = 1,
1752 };
1753 VkResult U_ASSERT_ONLY err;
1754 uint32_t i;
1755
Tony Barbour5aabff52015-10-08 14:26:24 -06001756 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1757 assert(demo->framebuffers);
1758
1759 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001760 attachments[0] = demo->buffers[i].view;
Chia-I Wu69f40122015-10-26 21:10:41 +08001761 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001762 assert(!err);
1763 }
1764}
1765
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001766static void demo_prepare(struct demo *demo)
1767{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001768 VkResult U_ASSERT_ONLY err;
1769
1770 const VkCmdPoolCreateInfo cmd_pool_info = {
1771 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1772 .pNext = NULL,
1773 .queueFamilyIndex = demo->graphics_queue_node_index,
1774 .flags = 0,
1775 };
Chia-I Wu69f40122015-10-26 21:10:41 +08001776 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop18ea11b2015-07-09 18:08:32 -06001777 assert(!err);
1778
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001779 const VkCmdBufferAllocInfo cmd = {
1780 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001781 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001782 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001783 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +08001784 .bufferCount = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001785 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001786
1787 demo_prepare_buffers(demo);
1788 demo_prepare_depth(demo);
1789 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001790 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001791
Chia-I Wuf8385062015-01-04 16:27:24 +08001792 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001793 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001794 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001795
Ian Elliott338dedb2015-08-21 15:09:33 -06001796 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001797 err = vkAllocCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001798 assert(!err);
1799 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001800
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001801 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001802 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001803
Chia-I Wu76cd4222015-07-08 13:34:24 +08001804 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001805
Ian Elliott338dedb2015-08-21 15:09:33 -06001806 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001807 demo->current_buffer = i;
1808 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1809 }
1810
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001811 /*
1812 * Prepare functions above may generate pipeline commands
1813 * that need to be flushed before beginning the render loop.
1814 */
1815 demo_flush_init_cmd(demo);
1816
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001817 demo->current_buffer = 0;
Mike Stroyanb194be62015-10-28 09:46:57 -06001818 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001819}
1820
David Pinedoeeca2a22015-06-18 17:03:14 -06001821static void demo_cleanup(struct demo *demo)
1822{
1823 uint32_t i;
1824
1825 demo->prepared = false;
1826
Tony Barbour5aabff52015-10-08 14:26:24 -06001827 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001828 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbourde4124d2015-07-03 10:33:54 -06001829 }
Tony Barbour5aabff52015-10-08 14:26:24 -06001830 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08001831 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001832
Chia-I Wu69f40122015-10-26 21:10:41 +08001833 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1834 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1835 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1836 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1837 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001838
1839 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001840 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1841 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1842 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1843 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001844 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001845 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
David Pinedoeeca2a22015-06-18 17:03:14 -06001846
Chia-I Wu69f40122015-10-26 21:10:41 +08001847 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1848 vkDestroyImage(demo->device, demo->depth.image, NULL);
1849 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001850
Chia-I Wu69f40122015-10-26 21:10:41 +08001851 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1852 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001853
Ian Elliott338dedb2015-08-21 15:09:33 -06001854 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001855 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001856 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001857 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001858 free(demo->buffers);
David Pinedoeeca2a22015-06-18 17:03:14 -06001859
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001860 free(demo->queue_props);
1861
Chia-I Wu69f40122015-10-26 21:10:41 +08001862 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1863 vkDestroyDevice(demo->device, NULL);
Tony Barboura65ecc22015-06-30 14:14:19 -06001864 if (demo->validate) {
1865 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1866 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001867 vkDestroyInstance(demo->inst, NULL);
David Pinedoeeca2a22015-06-18 17:03:14 -06001868
1869#ifndef _WIN32
1870 xcb_destroy_window(demo->connection, demo->window);
1871 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter283f4a32015-09-24 17:16:48 -06001872 free(demo->atom_wm_delete_window);
David Pinedoeeca2a22015-06-18 17:03:14 -06001873#endif // _WIN32
1874}
1875
Ian Elliotte2688a52015-10-16 18:02:43 -06001876static void demo_resize(struct demo *demo)
1877{
1878 uint32_t i;
1879
Mike Stroyanb194be62015-10-28 09:46:57 -06001880 // Don't react to resize until after first initialization.
1881 if (!demo->prepared) {
1882 return;
1883 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001884 // In order to properly resize the window, we must re-create the swapchain
1885 // AND redo the command buffers, etc.
1886 //
1887 // First, perform part of the demo_cleanup() function:
1888 demo->prepared = false;
1889
1890 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001891 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001892 }
1893 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08001894 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001895
Chia-I Wu69f40122015-10-26 21:10:41 +08001896 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1897 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1898 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1899 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1900 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001901
1902 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001903 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1904 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1905 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1906 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001907 }
Ian Elliotte2688a52015-10-16 18:02:43 -06001908
Chia-I Wu69f40122015-10-26 21:10:41 +08001909 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1910 vkDestroyImage(demo->device, demo->depth.image, NULL);
1911 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001912
Chia-I Wu69f40122015-10-26 21:10:41 +08001913 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1914 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001915
1916 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001917 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott74bb2eb2015-10-27 11:06:33 -06001918 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliotte2688a52015-10-16 18:02:43 -06001919 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001920 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06001921 free(demo->buffers);
1922
Chia-I Wu69f40122015-10-26 21:10:41 +08001923 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliott74bb2eb2015-10-27 11:06:33 -06001924
Ian Elliotte2688a52015-10-16 18:02:43 -06001925
1926 // Second, re-perform the demo_prepare() function, which will re-create the
1927 // swapchain:
1928 demo_prepare(demo);
1929}
1930
David Pinedoeeca2a22015-06-18 17:03:14 -06001931// On MS-Windows, make this a global, so it's available to WndProc()
1932struct demo demo;
1933
Ian Elliotte14e9f92015-04-16 15:23:05 -06001934#ifdef _WIN32
1935static void demo_run(struct demo *demo)
1936{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001937 if (!demo->prepared)
1938 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001939 // Wait for work to finish before updating MVP.
1940 vkDeviceWaitIdle(demo->device);
1941 demo_update_data_buffer(demo);
1942
1943 demo_draw(demo);
1944
1945 // Wait for work to finish before updating MVP.
1946 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001947
David Pinedoeeca2a22015-06-18 17:03:14 -06001948 demo->curFrame++;
1949
1950 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1951 {
1952 demo->quit=true;
1953 demo_cleanup(demo);
1954 ExitProcess(0);
1955 }
1956
1957}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001958
1959// MS-Windows event handling function:
1960LRESULT CALLBACK WndProc(HWND hWnd,
1961 UINT uMsg,
1962 WPARAM wParam,
1963 LPARAM lParam)
1964{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001965 switch(uMsg)
1966 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001967 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001968 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001969 break;
Ian Elliotte36b2082015-07-06 14:27:58 -06001970 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001971 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001972 break;
Ian Elliottf3f80822015-10-21 15:14:07 -06001973 case WM_SIZE:
Mike Stroyanb194be62015-10-28 09:46:57 -06001974 demo.width = lParam & 0xffff;
1975 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliottf3f80822015-10-21 15:14:07 -06001976 demo_resize(&demo);
1977 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001978 default:
1979 break;
1980 }
1981 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1982}
1983
1984static void demo_create_window(struct demo *demo)
1985{
1986 WNDCLASSEX win_class;
1987
1988 // Initialize the window class structure:
1989 win_class.cbSize = sizeof(WNDCLASSEX);
1990 win_class.style = CS_HREDRAW | CS_VREDRAW;
1991 win_class.lpfnWndProc = WndProc;
1992 win_class.cbClsExtra = 0;
1993 win_class.cbWndExtra = 0;
1994 win_class.hInstance = demo->connection; // hInstance
1995 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1996 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1997 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1998 win_class.lpszMenuName = NULL;
1999 win_class.lpszClassName = demo->name;
2000 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2001 // Register window class:
2002 if (!RegisterClassEx(&win_class)) {
2003 // It didn't work, so try to give a useful error:
2004 printf("Unexpected error trying to start the application!\n");
2005 fflush(stdout);
2006 exit(1);
2007 }
2008 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06002009 RECT wr = { 0, 0, demo->width, demo->height };
2010 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002011 demo->window = CreateWindowEx(0,
2012 demo->name, // class name
2013 demo->name, // app name
2014 WS_OVERLAPPEDWINDOW | // window style
2015 WS_VISIBLE |
2016 WS_SYSMENU,
2017 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06002018 wr.right-wr.left, // width
2019 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06002020 NULL, // handle to parent
2021 NULL, // handle to menu
2022 demo->connection, // hInstance
2023 NULL); // no extra parameters
2024 if (!demo->window) {
2025 // It didn't work, so try to give a useful error:
2026 printf("Cannot create a window in which to draw!\n");
2027 fflush(stdout);
2028 exit(1);
2029 }
2030}
2031#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002032static void demo_handle_event(struct demo *demo,
2033 const xcb_generic_event_t *event)
2034{
Piers Daniell886be472015-02-23 16:23:13 -07002035 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002036 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002037 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002038 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002039 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002040 case XCB_CLIENT_MESSAGE:
2041 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2042 (*demo->atom_wm_delete_window).atom) {
2043 demo->quit = true;
2044 }
2045 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002046 case XCB_KEY_RELEASE:
2047 {
2048 const xcb_key_release_event_t *key =
2049 (const xcb_key_release_event_t *) event;
2050
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002051 switch (key->detail) {
2052 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002053 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002054 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002055 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002056 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002057 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002058 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002059 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002060 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002061 case 0x41:
2062 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07002063 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002064 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002065 }
2066 break;
Ian Elliotte2688a52015-10-16 18:02:43 -06002067 case XCB_CONFIGURE_NOTIFY:
2068 {
2069 const xcb_configure_notify_event_t *cfg =
2070 (const xcb_configure_notify_event_t *) event;
2071 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2072 demo_resize(demo);
2073 }
2074 }
2075 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002076 default:
2077 break;
2078 }
2079}
2080
2081static void demo_run(struct demo *demo)
2082{
2083 xcb_flush(demo->connection);
2084
2085 while (!demo->quit) {
2086 xcb_generic_event_t *event;
2087
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002088 if (demo->pause) {
2089 event = xcb_wait_for_event(demo->connection);
2090 } else {
2091 event = xcb_poll_for_event(demo->connection);
2092 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002093 if (event) {
2094 demo_handle_event(demo, event);
2095 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002096 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002097
2098 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002099 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002100 demo_update_data_buffer(demo);
2101
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002102 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07002103
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002104 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002105 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06002106 demo->curFrame++;
Tony Barboura26fb7d2015-09-21 15:17:33 -06002107 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedoeeca2a22015-06-18 17:03:14 -06002108 demo->quit = true;
2109
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002110 }
2111}
2112
2113static void demo_create_window(struct demo *demo)
2114{
2115 uint32_t value_mask, value_list[32];
2116
2117 demo->window = xcb_generate_id(demo->connection);
2118
2119 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2120 value_list[0] = demo->screen->black_pixel;
2121 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliotte2688a52015-10-16 18:02:43 -06002122 XCB_EVENT_MASK_EXPOSURE |
2123 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002124
2125 xcb_create_window(demo->connection,
2126 XCB_COPY_FROM_PARENT,
2127 demo->window, demo->screen->root,
2128 0, 0, demo->width, demo->height, 0,
2129 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2130 demo->screen->root_visual,
2131 value_mask, value_list);
2132
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002133 /* Magic code that will send notification when window is destroyed */
2134 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2135 "WM_PROTOCOLS");
2136 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2137
2138 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2139 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2140
2141 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2142 demo->window, (*reply).atom, 4, 32, 1,
2143 &(*demo->atom_wm_delete_window).atom);
2144 free(reply);
2145
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002146 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06002147
2148 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2149 const uint32_t coords[] = {100, 100};
2150 xcb_configure_window(demo->connection, demo->window,
2151 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002152}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002153#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002154
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002155/*
2156 * Return 1 (true) if all layer names specified in check_names
2157 * can be found in given layer properties.
2158 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002159static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002160 uint32_t layer_count, VkLayerProperties *layers)
2161{
2162 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002163 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002164 for (uint32_t j = 0; j < layer_count; j++) {
2165 if (!strcmp(check_names[i], layers[j].layerName)) {
2166 found = 1;
2167 }
2168 }
2169 if (!found) {
2170 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2171 return 0;
2172 }
2173 }
2174 return 1;
2175}
2176
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002177static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002178{
Tobin Ehlis3536b442015-04-16 18:04:57 -06002179 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002180 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002181 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06002182 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002183 VkLayerProperties *instance_layers;
2184 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002185 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002186 uint32_t instance_layer_count = 0;
2187 uint32_t enabled_extension_count = 0;
2188 uint32_t enabled_layer_count = 0;
2189
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002190 char *instance_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002191 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002192 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002193 "ObjectTracker",
2194 "DrawState",
2195 "ParamChecker",
2196 "ShaderChecker",
Ian Elliott329da012015-09-22 10:51:24 -06002197 "Swapchain",
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002198 "DeviceLimits",
2199 "Image",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002200 };
2201
2202 char *device_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002203 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002204 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002205 "ObjectTracker",
2206 "DrawState",
2207 "ParamChecker",
2208 "ShaderChecker",
Ian Elliott329da012015-09-22 10:51:24 -06002209 "Swapchain",
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002210 "DeviceLimits",
2211 "Image",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002212 };
2213
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002214 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002215 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002216 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002217 assert(!err);
2218
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002219 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002220 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002221 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002222
2223 if (demo->validate) {
2224 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2225 instance_layer_count, instance_layers);
2226 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002227 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002228 "required validation layer.\n\n"
2229 "Please look at the Getting Started guide for additional "
2230 "information.\n",
2231 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002232 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002233 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002234 }
2235
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002236 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002237 assert(!err);
2238
Tony Barbourd9955e42015-10-08 13:59:42 -06002239 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002240 memset(extension_names, 0, sizeof(extension_names));
2241 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002242 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002243 assert(!err);
2244 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06002245 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06002246 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06002247 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002248 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002249 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002250 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002251 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002252 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002253 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002254 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002255 }
Tony Barbourd9955e42015-10-08 13:59:42 -06002256 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002257 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06002258 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002259 "Vulkan installable client driver (ICD) installed?\nPlease "
2260 "look at the Getting Started guide for additional "
2261 "information.\n",
2262 "vkCreateInstance Failure");
2263 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002264 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002265 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002266 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002267 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002268 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002269 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002270 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002271 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002272 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002273 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002274 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002275 .pNext = NULL,
2276 .pAppInfo = &app,
Chia-I Wu763a7492015-10-26 20:48:51 +08002277 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002278 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu763a7492015-10-26 20:48:51 +08002279 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002280 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002281 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002282
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002283 uint32_t gpu_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002284
Chia-I Wu69f40122015-10-26 21:10:41 +08002285 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002286 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2287 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002288 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002289 "additional information.\n",
2290 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06002291 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002292 ERR_EXIT("Cannot find a specified extension library"
2293 ".\nMake sure your layers path is set appropriately\n",
2294 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002295 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002296 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2297 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002298 "the Getting Started guide for additional information.\n",
2299 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002300 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002301
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002302 free(instance_layers);
2303 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002304
Tobin Ehlis4f482a72015-09-07 15:16:39 -06002305 /* Make initial call to query gpu_count, then second call for gpu info*/
2306 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2307 assert(!err && gpu_count > 0);
2308 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2309 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2310 assert(!err);
2311 /* For cube demo we just grab the first physical device */
2312 demo->gpu = physical_devices[0];
2313 free(physical_devices);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002314
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002315 /* Look for validation layers */
2316 validation_found = 0;
2317 enabled_layer_count = 0;
2318 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002319 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002320 assert(!err);
2321
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002322 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002323 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002324 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002325
2326 if (demo->validate) {
2327 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2328 device_layer_count, device_layers);
2329 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002330 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002331 "a required validation layer.\n\n"
2332 "Please look at the Getting Started guide for additional "
2333 "information.\n",
2334 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002335 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002336 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002337 }
2338
2339 uint32_t device_extension_count = 0;
2340 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002341 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002342 demo->gpu, NULL, &device_extension_count, NULL);
2343 assert(!err);
2344
Tony Barbourd9955e42015-10-08 13:59:42 -06002345 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002346 enabled_extension_count = 0;
2347 memset(extension_names, 0, sizeof(extension_names));
2348 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002349 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002350 demo->gpu, NULL, &device_extension_count, device_extensions);
2351 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06002352
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002353 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06002354 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06002355 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06002356 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002357 }
2358 assert(enabled_extension_count < 64);
2359 }
Tony Barbourd9955e42015-10-08 13:59:42 -06002360 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06002361 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06002362 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002363 "Vulkan installable client driver (ICD) installed?\nPlease "
2364 "look at the Getting Started guide for additional "
2365 "information.\n",
2366 "vkCreateInstance Failure");
2367 }
2368
Tony Barbour5685ad72015-04-29 16:19:20 -06002369 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06002370 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2371 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002372 if (!demo->dbgCreateMsgCallback) {
2373 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2374 "vkGetProcAddr Failure");
2375 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002376 if (!demo->dbgDestroyMsgCallback) {
2377 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2378 "vkGetProcAddr Failure");
2379 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002380 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2381 if (!demo->dbgBreakCallback) {
2382 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2383 "vkGetProcAddr Failure");
2384 }
2385
2386 PFN_vkDbgMsgCallback callback;
2387
2388 if (!demo->use_break) {
2389 callback = dbgFunc;
2390 } else {
2391 callback = demo->dbgBreakCallback;
2392 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002393 err = demo->dbgCreateMsgCallback(
2394 demo->inst,
2395 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002396 callback, NULL,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002397 &demo->msg_callback);
2398 switch (err) {
2399 case VK_SUCCESS:
2400 break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002401 case VK_ERROR_OUT_OF_HOST_MEMORY:
2402 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2403 "dbgCreateMsgCallback Failure");
2404 break;
2405 default:
2406 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2407 "dbgCreateMsgCallback Failure");
2408 break;
2409 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002410 }
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002411 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002412
2413 /* Call with NULL data to get count */
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002414 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002415 assert(demo->queue_count >= 1);
2416
2417 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002418 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002419 // Find a queue that supports gfx
2420 uint32_t gfx_queue_idx = 0;
2421 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2422 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2423 break;
2424 }
2425 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlis795460a2015-09-24 15:18:22 -06002426 // Query fine-grained feature support for this device.
2427 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis6454cd92015-09-29 11:22:37 -06002428 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002429 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlis795460a2015-09-24 15:18:22 -06002430
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06002431 float queue_priorities[1] = { 0.0 };
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002432 const VkDeviceQueueCreateInfo queue = {
2433 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2434 .pNext = NULL,
2435 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu763a7492015-10-26 20:48:51 +08002436 .queuePriorityCount = 1,
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06002437 .pQueuePriorities = queue_priorities
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002438 };
2439
2440 VkDeviceCreateInfo device = {
2441 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2442 .pNext = NULL,
Courtney Goeltzenleuchterdfd53f52015-10-15 16:58:44 -06002443 .requestedQueueCount = 1,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002444 .pRequestedQueues = &queue,
Chia-I Wu763a7492015-10-26 20:48:51 +08002445 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002446 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu763a7492015-10-26 20:48:51 +08002447 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002448 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlis795460a2015-09-24 15:18:22 -06002449 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis3bd5dc42015-09-22 13:52:37 -06002450 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002451
Chia-I Wu69f40122015-10-26 21:10:41 +08002452 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002453 assert(!err);
2454
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002455 free(device_layers);
2456
Ian Elliott338dedb2015-08-21 15:09:33 -06002457 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2458 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2459 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2460 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2461 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliott338dedb2015-08-21 15:09:33 -06002462 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2463 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2464 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2465 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002466}
2467
Tony Barbourd9955e42015-10-08 13:59:42 -06002468static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002469{
2470 VkResult err;
2471 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002472
Tony Barbourd9955e42015-10-08 13:59:42 -06002473 // Construct the surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06002474 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002475 demo->surface_description.pNext = NULL;
2476#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06002477 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002478 demo->surface_description.pPlatformHandle = demo->connection;
2479 demo->surface_description.pPlatformWindow = demo->window;
2480#else // _WIN32
2481 demo->platform_handle_xcb.connection = demo->connection;
2482 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06002483 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002484 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2485 demo->surface_description.pPlatformWindow = &demo->window;
2486#endif // _WIN32
2487
Tony Barbourd9955e42015-10-08 13:59:42 -06002488 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002489 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2490 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06002491 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2492 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06002493 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002494 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002495
2496 // Search for a graphics and a present queue in the array of queue
2497 // families, try to find one that supports both
2498 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2499 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002500 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002501 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2502 if (graphicsQueueNodeIndex == UINT32_MAX) {
2503 graphicsQueueNodeIndex = i;
2504 }
2505
2506 if (supportsPresent[i] == VK_TRUE) {
2507 graphicsQueueNodeIndex = i;
2508 presentQueueNodeIndex = i;
2509 break;
2510 }
2511 }
2512 }
2513 if (presentQueueNodeIndex == UINT32_MAX) {
2514 // If didn't find a queue that supports both graphics and present, then
2515 // find a separate present queue.
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002516 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002517 if (supportsPresent[i] == VK_TRUE) {
2518 presentQueueNodeIndex = i;
2519 break;
2520 }
2521 }
2522 }
2523 free(supportsPresent);
2524
2525 // Generate error if could not find both a graphics and a present queue
2526 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2527 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002528 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002529 }
2530
2531 // TODO: Add support for separate queues, including presentation,
2532 // synchronization, and appropriate tracking for QueueSubmit
2533 // While it is possible for an application to use a separate graphics and a
2534 // present queues, this demo program assumes it is only using one:
2535 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2536 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002537 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002538 }
2539
2540 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002541
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002542 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002543 0, &demo->queue);
Tony Barbour5685ad72015-04-29 16:19:20 -06002544
Ian Elliotte36b2082015-07-06 14:27:58 -06002545 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002546 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002547 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2548 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002549 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002550 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002551 VkSurfaceFormatKHR *surfFormats =
2552 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2553 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2554 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002555 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002556 assert(!err);
2557 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2558 // the surface has no preferred format. Otherwise, at least one
2559 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002560 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2561 {
2562 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2563 }
2564 else
2565 {
2566 assert(formatCount >= 1);
2567 demo->format = surfFormats[0].format;
2568 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002569 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002570
David Pinedoeeca2a22015-06-18 17:03:14 -06002571 demo->quit = false;
2572 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002573
2574 // Get Memory information and properties
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002575 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002576}
2577
2578static void demo_init_connection(struct demo *demo)
2579{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002580#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002581 const xcb_setup_t *setup;
2582 xcb_screen_iterator_t iter;
2583 int scr;
2584
2585 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002586 if (demo->connection == NULL) {
2587 printf("Cannot find a compatible Vulkan installable client driver "
2588 "(ICD).\nExiting ...\n");
2589 fflush(stdout);
2590 exit(1);
2591 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002592
2593 setup = xcb_get_setup(demo->connection);
2594 iter = xcb_setup_roots_iterator(setup);
2595 while (scr-- > 0)
2596 xcb_screen_next(&iter);
2597
2598 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002599#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002600}
2601
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002602static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002603{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002604 vec3 eye = {0.0f, 3.0f, 5.0f};
2605 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002606 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002607
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002608 memset(demo, 0, sizeof(*demo));
Tony Barboura26fb7d2015-09-21 15:17:33 -06002609 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002610
Piers Daniell886be472015-02-23 16:23:13 -07002611 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002612 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002613 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002614 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002615 }
Cody Northrop75db0322015-05-28 11:27:16 -06002616 if (strcmp(argv[i], "--use_glsl") == 0) {
2617 demo->use_glsl = true;
2618 continue;
2619 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002620 if (strcmp(argv[i], "--break") == 0) {
2621 demo->use_break = true;
2622 continue;
2623 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002624 if (strcmp(argv[i], "--validate") == 0) {
2625 demo->validate = true;
2626 continue;
2627 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002628 if (strcmp(argv[i], "--c") == 0 &&
Tony Barboura26fb7d2015-09-21 15:17:33 -06002629 demo->frameCount == INT32_MAX &&
David Pinedoeeca2a22015-06-18 17:03:14 -06002630 i < argc-1 &&
2631 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2632 demo->frameCount >= 0)
2633 {
2634 i++;
2635 continue;
2636 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002637
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002638 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002639 fflush(stderr);
2640 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002641 }
2642
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002643 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002644 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002645
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002646 demo->width = 500;
2647 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002648
2649 demo->spin_angle = 0.01f;
2650 demo->spin_increment = 0.01f;
2651 demo->pause = false;
2652
Piers Daniell886be472015-02-23 16:23:13 -07002653 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002654 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2655 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002656}
2657
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002658
Ian Elliotte14e9f92015-04-16 15:23:05 -06002659#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002660extern int __getmainargs(
2661 int * _Argc,
2662 char *** _Argv,
2663 char *** _Env,
2664 int _DoWildCard,
2665 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002666
Ian Elliott421107f2015-04-28 15:50:36 -06002667int WINAPI WinMain(HINSTANCE hInstance,
2668 HINSTANCE hPrevInstance,
2669 LPSTR pCmdLine,
2670 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002671{
2672 MSG msg; // message
2673 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002674 int argc;
2675 char** argv;
2676 char** env;
2677 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002678
Tony Barbour5685ad72015-04-29 16:19:20 -06002679 __getmainargs(&argc,&argv,&env,0,&new_mode);
2680
2681 demo_init(&demo, argc, argv);
2682 demo.connection = hInstance;
2683 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002684 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002685 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002686
2687 demo_prepare(&demo);
2688
2689 done = false; //initialize loop condition variable
2690 /* main message loop*/
2691 while(!done)
2692 {
Ian Elliott421107f2015-04-28 15:50:36 -06002693 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002694 if (msg.message == WM_QUIT) //check for a quit message
2695 {
2696 done = true; //if found, quit app
2697 }
2698 else
2699 {
2700 /* Translate and dispatch to event queue*/
2701 TranslateMessage(&msg);
2702 DispatchMessage(&msg);
2703 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002704 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002705 }
2706
2707 demo_cleanup(&demo);
2708
Tony Barboura938abb2015-04-22 11:36:22 -06002709 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002710}
2711#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002712int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002713{
2714 struct demo demo;
2715
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002716 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002717 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002718 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002719
2720 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002721 demo_run(&demo);
2722
2723 demo_cleanup(&demo);
2724
2725 return 0;
2726}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002727#endif // _WIN32