blob: 6437a71e028e3235d413d0b8accfb0ad728972f8 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Ian Elliotta0781972015-08-21 15:09:33 -060040#include <vk_ext_khr_swapchain.h>
41#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060042#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060043
Cody Northropfe3d8bc2015-03-17 14:54:35 -060044#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045
Mark Lobodzinskif871f772015-09-01 09:00:16 -060046#include "vk_sdk_platform.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
62#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
67
Ian Elliott07264132015-04-28 11:35:02 -060068#else // _WIN32
69
70#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 printf(err_msg); \
73 fflush(stdout); \
74 exit(1); \
75 } while (0)
76#endif // _WIN32
77
Ian Elliottaaae5352015-07-06 14:27:58 -060078#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
79{ \
80 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
81 if (demo->fp##entrypoint == NULL) { \
82 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
83 "vkGetInstanceProcAddr Failure"); \
84 } \
85}
86
Ian Elliott673898b2015-06-22 15:07:49 -060087#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
88{ \
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -060089 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060090 if (demo->fp##entrypoint == NULL) { \
91 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
92 "vkGetDeviceProcAddr Failure"); \
93 } \
94}
95
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060096/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070097 * structure to track all objects related to a texture.
98 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060099struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600100 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700101
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600102 VkImage image;
103 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600104
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200105 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500106 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600107 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700108 int32_t tex_width, tex_height;
109};
110
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600111static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600112 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600113};
114
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600115struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600116 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600117 float mvp[4][4];
118 float position[12*3][4];
119 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120};
121
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600122struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600123 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600124 float mvp[4][4];
125 float position[12*3][4];
126 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600128
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600129//--------------------------------------------------------------------------------------
130// Mesh and VertexFormat Data
131//--------------------------------------------------------------------------------------
132struct Vertex
133{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600134 float posX, posY, posZ, posW; // Position data
135 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600136};
137
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600138struct VertexPosTex
139{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600140 float posX, posY, posZ, posW; // Position data
141 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142};
143
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600144#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600145#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600147static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800148 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600149 -1.0f,-1.0f, 1.0f,
150 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800151 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 -1.0f, 1.0f,-1.0f,
153 -1.0f,-1.0f,-1.0f,
154
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 1.0f, 1.0f,-1.0f,
157 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600160 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600164 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 -1.0f, 1.0f, 1.0f,
171 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 1.0f, 1.0f, 1.0f,
178 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f,-1.0f,-1.0f,
181 1.0f, 1.0f,-1.0f,
182
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600185 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f,-1.0f, 1.0f,
188 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600189};
190
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600191static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 0.0f, 0.0f, // -X side
193 1.0f, 0.0f,
194 1.0f, 1.0f,
195 1.0f, 1.0f,
196 0.0f, 1.0f,
197 0.0f, 0.0f,
198
199 1.0f, 0.0f, // -Z side
200 0.0f, 1.0f,
201 0.0f, 0.0f,
202 1.0f, 0.0f,
203 1.0f, 1.0f,
204 0.0f, 1.0f,
205
206 1.0f, 1.0f, // -Y side
207 1.0f, 0.0f,
208 0.0f, 0.0f,
209 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 0.0f, 0.0f,
211 0.0f, 1.0f,
212
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600214 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800215 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600216 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600218 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 1.0f, 1.0f, // +X side
221 0.0f, 1.0f,
222 0.0f, 0.0f,
223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 0.0f, 1.0f, // +Z side
228 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800231 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600233};
234
235void dumpMatrix(const char *note, mat4x4 MVP)
236{
237 int i;
238
239 printf("%s: \n", note);
240 for (i=0; i<4; i++) {
241 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
242 }
243 printf("\n");
244 fflush(stdout);
245}
246
247void dumpVec4(const char *note, vec4 vector)
248{
249 printf("%s: \n", note);
250 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
251 printf("\n");
252 fflush(stdout);
253}
254
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600255VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600256 VkFlags msgFlags,
257 VkDbgObjectType objType,
258 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600259 size_t location,
260 int32_t msgCode,
261 const char* pLayerPrefix,
262 const char* pMsg,
263 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600264{
265 char *message = (char *) malloc(strlen(pMsg)+100);
266
267 assert (message);
268
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600269 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
270 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
271 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600272 // We know that we're submitting queues without fences, ignore this warning
273 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600274 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600275 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600276 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600277 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600278 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600279 }
280
281#ifdef _WIN32
282 MessageBox(NULL, message, "Alert", MB_OK);
283#else
284 printf("%s\n",message);
285 fflush(stdout);
286#endif
287 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600288
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600289 /*
290 * false indicates that layer should not bail-out of an
291 * API call that had validation failures. This may mean that the
292 * app dies inside the driver due to invalid parameter(s).
293 * That's what would happen without validation layers, so we'll
294 * keep that behavior here.
295 */
296 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600297}
298
Ian Elliotta0781972015-08-21 15:09:33 -0600299typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600300 VkImage image;
301 VkCmdBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600302 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600303} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600304
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600305struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600306#ifdef _WIN32
307#define APP_NAME_STR_LEN 80
308 HINSTANCE connection; // hInstance - Windows Instance
309 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
310 HWND window; // hWnd - window handle
311#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312 xcb_connection_t *connection;
313 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800314 xcb_window_t window;
315 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotta0781972015-08-21 15:09:33 -0600316 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600317#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600318 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700319 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600320 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600323 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkDevice device;
325 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700326 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600327 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600328 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600329 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330
331 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600333 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
Ian Elliotta0781972015-08-21 15:09:33 -0600335 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
336 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
337 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
338 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
339 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
340 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
341 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
342 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
343 PFN_vkQueuePresentKHR fpQueuePresentKHR;
344 VkSurfaceDescriptionWindowKHR surface_description;
345 uint32_t swapchainImageCount;
346 VkSwapchainKHR swap_chain;
347 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600348
Ian Elliottaaae5352015-07-06 14:27:58 -0600349 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350
351 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600352 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600354 VkImage image;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200355 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500356 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600357 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600358 } depth;
359
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600360 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361
362 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600363 VkBuffer buf;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200364 VkMemoryAllocInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500365 VkDeviceMemory mem;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800366 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600367 } uniform_data;
368
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500370 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600371 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600372 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800373 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600376 mat4x4 projection_matrix;
377 mat4x4 view_matrix;
378 mat4x4 model_matrix;
379
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600380 float spin_angle;
381 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600382 bool pause;
383
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600384 VkShaderModule vert_shader_module;
385 VkShaderModule frag_shader_module;
386
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387 VkDescriptorPool desc_pool;
388 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800389
Tony Barbourd8e504f2015-10-08 14:26:24 -0600390 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800391
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600392 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600393 int32_t curFrame;
394 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600395 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600396 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600397 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600398 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600399 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600400 VkDbgMsgCallback msg_callback;
401
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600402 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600403 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404};
405
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600406static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
407{
408 // Search memtypes to find first index with those properties
409 for (uint32_t i = 0; i < 32; i++) {
410 if ((typeBits & 1) == 1) {
411 // Type is available, does it match user properties?
412 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
413 *typeIndex = i;
414 return VK_SUCCESS;
415 }
416 }
417 typeBits >>= 1;
418 }
419 // No memory types matched, return failure
420 return VK_UNSUPPORTED;
421}
422
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600423static void demo_flush_init_cmd(struct demo *demo)
424{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600425 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600426
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600427 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428 return;
429
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 assert(!err);
432
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600433 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600434 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435
Tony Barbour155cce82015-07-03 10:33:54 -0600436 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437 assert(!err);
438
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600439 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600440 assert(!err);
441
Tony Barbour155cce82015-07-03 10:33:54 -0600442 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444}
445
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600446static void demo_set_image_layout(
447 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600448 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600449 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600450 VkImageLayout old_image_layout,
451 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600453 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600456 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600459 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800460 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461 .flags = 0,
462 };
463
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 assert(!err);
466
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600467 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600468 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600469 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600470 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600471 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600472 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600473 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600474 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600475 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 }
478
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600479 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 .pNext = NULL,
482 .outputMask = 0,
483 .inputMask = 0,
484 .oldLayout = old_image_layout,
485 .newLayout = new_image_layout,
486 .image = image,
Cody Northrop0e951672015-09-14 13:48:12 -0600487 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488 };
489
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600492 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600493 }
494
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600495 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600497 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 }
499
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600500 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600501
Tony Barbour50bbca42015-06-29 16:20:35 -0600502 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
503 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600504
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600505 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600506}
507
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600508static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509{
Chia-I Wuf973e322015-07-08 13:34:24 +0800510 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600511 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600512 .pNext = NULL,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600513 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600514 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600515 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600516 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700517 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800518 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600519 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
520 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800521 };
522 const VkRenderPassBeginInfo rp_begin = {
523 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
524 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800525 .renderPass = demo->render_pass,
526 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800527 .renderArea.offset.x = 0,
528 .renderArea.offset.y = 0,
529 .renderArea.extent.width = demo->width,
530 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600531 .clearValueCount = 2,
532 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700533 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800534 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600536 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537 assert(!err);
538
Chia-I Wua74c5b22015-07-07 11:50:03 +0800539 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
540
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600543 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600544 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600545
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600546 VkViewport viewport;
547 memset(&viewport, 0, sizeof(viewport));
548 viewport.height = (float) demo->height;
549 viewport.width = (float) demo->width;
550 viewport.minDepth = (float) 0.0f;
551 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600552 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600553
554 VkRect2D scissor;
555 memset(&scissor, 0, sizeof(scissor));
556 scissor.extent.width = demo->width;
557 scissor.extent.height = demo->height;
558 scissor.offset.x = 0;
559 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600560 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600561
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600562 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800563 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600565 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566 assert(!err);
567}
568
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600569
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600570void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600571{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600572 mat4x4 MVP, Model, VP;
573 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600574 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600575 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600576
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600577 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600579 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600580 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700581 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600582 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600583
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200584 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600585 assert(!err);
586
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600587 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600588
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600589 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590}
591
592static void demo_draw(struct demo *demo)
593{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600594 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600595 VkSemaphore presentCompleteSemaphore;
596 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
597 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
598 .pNext = NULL,
599 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
600 };
Tony Barbour155cce82015-07-03 10:33:54 -0600601 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600602
Ian Elliottaaae5352015-07-06 14:27:58 -0600603 err = vkCreateSemaphore(demo->device,
604 &presentCompleteSemaphoreCreateInfo,
605 &presentCompleteSemaphore);
606 assert(!err);
607
608 // Get the index of the next available swapchain image:
Ian Elliotta0781972015-08-21 15:09:33 -0600609 err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600610 UINT64_MAX,
611 presentCompleteSemaphore,
612 &demo->current_buffer);
Ian Elliotta0781972015-08-21 15:09:33 -0600613 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600614 // return codes
615 assert(!err);
616
617 // Wait for the present complete semaphore to be signaled to ensure
618 // that the image won't be rendered to until the presentation
619 // engine has fully released ownership to the application, and it is
620 // okay to render to the image.
621 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
622
Ian Elliotta0781972015-08-21 15:09:33 -0600623// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600624 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600625 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626 assert(!err);
627
Ian Elliotta0781972015-08-21 15:09:33 -0600628 VkPresentInfoKHR present = {
629 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600630 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600631 .swapchainCount = 1,
632 .swapchains = &demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600633 .imageIndices = &demo->current_buffer,
634 };
635
636// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600637 err = demo->fpQueuePresentKHR(demo->queue, &present);
638 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600639 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600640 assert(!err);
641
Chia-I Wucbb564e2015-04-16 22:02:10 +0800642 err = vkQueueWaitIdle(demo->queue);
643 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600644
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600645 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600646}
647
648static void demo_prepare_buffers(struct demo *demo)
649{
Ian Elliottaaae5352015-07-06 14:27:58 -0600650 VkResult U_ASSERT_ONLY err;
651
652 // Check the surface properties and formats
Ian Elliotta0781972015-08-21 15:09:33 -0600653 VkSurfacePropertiesKHR surfProperties;
654 err = demo->fpGetSurfacePropertiesKHR(demo->device,
655 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600656 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600657 assert(!err);
658
Ian Elliott8528eff2015-08-07 15:56:59 -0600659 uint32_t presentModeCount;
Ian Elliotta0781972015-08-21 15:09:33 -0600660 err = demo->fpGetSurfacePresentModesKHR(demo->device,
661 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600662 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600663 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600664 VkPresentModeKHR *presentModes =
665 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600666 assert(presentModes);
Ian Elliotta0781972015-08-21 15:09:33 -0600667 err = demo->fpGetSurfacePresentModesKHR(demo->device,
668 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600669 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600670 assert(!err);
671
Ian Elliotta0781972015-08-21 15:09:33 -0600672 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600673 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600674 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600675 {
676 // If the surface size is undefined, the size is set to
677 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600678 swapchainExtent.width = demo->width;
679 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600680 }
681 else
682 {
683 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600684 swapchainExtent = surfProperties.currentExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600685 }
686
687 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600688 // tearing mode. If not, try IMMEDIATE which will usually be available,
689 // and is fastest (though it tears). If not, fall back to FIFO which is
690 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600691 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600692 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600693 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
694 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600695 break;
696 }
Ian Elliotta0781972015-08-21 15:09:33 -0600697 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
698 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
699 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600700 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600701 }
702
703 // Determine the number of VkImage's to use in the swap chain (we desire to
704 // own only 1 image at a time, besides the images being displayed and
705 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600706 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600707 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600708 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600709 {
710 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600711 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 }
713
Tony Barbour9fd6d352015-09-16 15:01:32 -0600714 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600715 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
716 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600718 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600719 }
720
Ian Elliotta0781972015-08-21 15:09:33 -0600721 const VkSwapchainCreateInfoKHR swap_chain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600722 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800723 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600724 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
725 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800726 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600727 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800728 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600729 .width = swapchainExtent.width,
730 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600731 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600732 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600733 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800734 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600735 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
736 .queueFamilyCount = 0,
737 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600738 .presentMode = swapchainPresentMode,
739 .oldSwapchain.handle = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600740 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600741 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600742 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743
Ian Elliotta0781972015-08-21 15:09:33 -0600744 err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800745 assert(!err);
746
Ian Elliotta0781972015-08-21 15:09:33 -0600747 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
748 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600749 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800750
Ian Elliotta0781972015-08-21 15:09:33 -0600751 VkImage* swapchainImages =
752 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
753 assert(swapchainImages);
754 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
755 &demo->swapchainImageCount,
756 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600757 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600758
Ian Elliotta0781972015-08-21 15:09:33 -0600759 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600760 assert(demo->buffers);
761
Ian Elliotta0781972015-08-21 15:09:33 -0600762 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600763 VkImageViewCreateInfo color_image_view = {
764 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600765 .pNext = NULL,
766 .format = demo->format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600767 .channels = {
768 .r = VK_CHANNEL_SWIZZLE_R,
769 .g = VK_CHANNEL_SWIZZLE_G,
770 .b = VK_CHANNEL_SWIZZLE_B,
771 .a = VK_CHANNEL_SWIZZLE_A,
772 },
773 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600774 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600775 .baseMipLevel = 0,
776 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600777 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600778 .arraySize = 1
779 },
780 .viewType = VK_IMAGE_VIEW_TYPE_2D,
781 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600782 };
783
Ian Elliotta0781972015-08-21 15:09:33 -0600784 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600785
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600786 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400787 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600788 VK_IMAGE_LAYOUT_UNDEFINED,
789 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600790
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600791 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600792
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600793 err = vkCreateImageView(demo->device,
794 &color_image_view, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600795 assert(!err);
796 }
797}
798
799static void demo_prepare_depth(struct demo *demo)
800{
Tony Barbour72304ef2015-04-16 15:59:00 -0600801 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600802 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600803 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600804 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600805 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600806 .format = depth_format,
807 .extent = { demo->width, demo->height, 1 },
808 .mipLevels = 1,
809 .arraySize = 1,
810 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600811 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600812 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600813 .flags = 0,
814 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200815
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600816 VkImageViewCreateInfo view = {
817 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600818 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600819 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600820 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600821 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600822 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600823 .baseMipLevel = 0,
824 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600825 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600826 .arraySize = 1
827 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600828 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600829 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600831
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500832 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600833 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600834
835 demo->depth.format = depth_format;
836
837 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600838 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600839 &demo->depth.image);
840 assert(!err);
841
Tony Barbour155cce82015-07-03 10:33:54 -0600842 err = vkGetImageMemoryRequirements(demo->device,
843 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600844
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200845 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
846 demo->depth.mem_alloc.pNext = NULL;
847 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
848 demo->depth.mem_alloc.memoryTypeIndex = 0;
849
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600850 err = memory_type_from_properties(demo,
851 mem_reqs.memoryTypeBits,
852 VK_MEMORY_PROPERTY_DEVICE_ONLY,
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200853 &demo->depth.mem_alloc.memoryTypeIndex);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600854 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600855
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500856 /* allocate memory */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200857 err = vkAllocMemory(demo->device, &demo->depth.mem_alloc, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500858 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600859
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500860 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600861 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500862 demo->depth.mem, 0);
863 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600864
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600865 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600866 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600867 VK_IMAGE_LAYOUT_UNDEFINED,
868 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600869
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600870 /* create image view */
871 view.image = demo->depth.image;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600872 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600873 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600874}
875
Tony Barbour1a86d032015-09-21 15:17:33 -0600876/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700877bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600878 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600879 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600880{
Tony Barbour1a86d032015-09-21 15:17:33 -0600881 FILE *fPtr = fopen(filename,"rb");
882 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600883
Tony Barbour1a86d032015-09-21 15:17:33 -0600884 if (!fPtr)
885 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600886
Tony Barbour1a86d032015-09-21 15:17:33 -0600887 cPtr = fgets(header, 256, fPtr); // P6
888 if (cPtr == NULL || strncmp(header, "P6\n", 3))
889 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600890
Tony Barbour1a86d032015-09-21 15:17:33 -0600891 do {
892 cPtr = fgets(header, 256, fPtr);
893 if (cPtr == NULL)
894 return false;
895 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600896
Tony Barbour1a86d032015-09-21 15:17:33 -0600897 sscanf(header, "%u %u", height, width);
898 if (rgba_data == NULL)
899 return true;
900 fgets(header, 256, fPtr); // Format
901 if (cPtr == NULL || strncmp(header, "255\n", 3))
902 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600903
Tony Barbour1a86d032015-09-21 15:17:33 -0600904 for(int y = 0; y < *height; y++)
905 {
906 uint8_t *rowPtr = rgba_data;
907 for(int x = 0; x < *width; x++)
908 {
909 fread(rowPtr, 3, 1, fPtr);
910 rowPtr[3] = 255; /* Alpha of 1 */
911 rowPtr += 4;
912 }
913 rgba_data += layout->rowPitch;
914 }
915 fclose(fPtr);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600916 return true;
917}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600918
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700919static void demo_prepare_texture_image(struct demo *demo,
920 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600921 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600922 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600923 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600924 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600925{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600926 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600927 int32_t tex_width;
928 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600929 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700930
David Pinedo30bd71d2015-04-23 08:16:57 -0600931 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
932 {
933 printf("Failed to load textures\n");
934 fflush(stdout);
935 exit(1);
936 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700937
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600938 tex_obj->tex_width = tex_width;
939 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600941 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600942 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600944 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700945 .format = tex_format,
946 .extent = { tex_width, tex_height, 1 },
947 .mipLevels = 1,
948 .arraySize = 1,
949 .samples = 1,
950 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600951 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952 .flags = 0,
953 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700954
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500955 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600957 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600958 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700959 assert(!err);
960
Tony Barbour155cce82015-07-03 10:33:54 -0600961 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600962 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700963
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200964 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
965 tex_obj->mem_alloc.pNext = NULL;
966 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
967 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700968
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200969 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &tex_obj->mem_alloc.memoryTypeIndex);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600970 assert(!err);
971
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500972 /* allocate memory */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200973 err = vkAllocMemory(demo->device, &tex_obj->mem_alloc,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500974 &(tex_obj->mem));
975 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700976
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500977 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600978 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500979 tex_obj->mem, 0);
980 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981
Tony Barbour72304ef2015-04-16 15:59:00 -0600982 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600983 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600984 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600986 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700987 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600988 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989 void *data;
990
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600991 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
992 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700993
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200994 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700995 assert(!err);
996
997 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
998 fprintf(stderr, "Error loading texture: %s\n", filename);
999 }
1000
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001001 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001003
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001004 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001005 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001006 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001007 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001008 tex_obj->imageLayout);
1009 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001010}
1011
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001012static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001013{
1014 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001015 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001016 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017}
1018
1019static void demo_prepare_textures(struct demo *demo)
1020{
Tony Barbour72304ef2015-04-16 15:59:00 -06001021 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001022 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001023 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001024 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001025
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001026 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001027 assert(!err);
1028
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001029 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030
FslNopper6a7e50e2015-05-06 21:42:01 +02001031 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032 /* Device can texture using linear textures */
1033 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001034 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001035 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001037 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038
1039 memset(&staging_texture, 0, sizeof(staging_texture));
1040 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001041 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001042
1043 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001044 VK_IMAGE_TILING_OPTIMAL,
1045 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1046 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001048 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001049 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001050 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001053 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001054 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001055 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001056 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001058 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001059 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001060 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001061 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001062 .destOffset = { 0, 0, 0 },
1063 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1064 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 vkCmdCopyImage(demo->cmd,
1066 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1067 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001068 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001070 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001071 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001072 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001073 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001074
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001075 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001077 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001079 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1080 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081 }
1082
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001083 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001086 .magFilter = VK_TEX_FILTER_NEAREST,
1087 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001088 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter781da8a2015-09-10 14:08:50 -06001089 .addressModeU = VK_TEX_ADDRESS_MODE_CLAMP,
1090 .addressModeV = VK_TEX_ADDRESS_MODE_CLAMP,
1091 .addressModeW = VK_TEX_ADDRESS_MODE_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001092 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001093 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001094 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001095 .minLod = 0.0f,
1096 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001097 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001098 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001099 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001100
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001101 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001102 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001103 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001104 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001105 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001106 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001107 .channels = { VK_CHANNEL_SWIZZLE_R,
1108 VK_CHANNEL_SWIZZLE_G,
1109 VK_CHANNEL_SWIZZLE_B,
1110 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001111 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001112 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001113 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001114
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001115 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001116 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001117 &demo->textures[i].sampler);
1118 assert(!err);
1119
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001120 /* create image view */
1121 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001122 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001123 &demo->textures[i].view);
1124 assert(!err);
1125 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001126}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001127
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001128void demo_prepare_cube_data_buffer(struct demo *demo)
1129{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001130 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001131 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001132 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001133 int i;
1134 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001135 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001136 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001138 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001139 mat4x4_mul(MVP, VP, demo->model_matrix);
1140 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001141// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001142
1143 for (i=0; i<12*3; i++) {
1144 data.position[i][0] = g_vertex_buffer_data[i*3];
1145 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1146 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1147 data.position[i][3] = 1.0f;
1148 data.attr[i][0] = g_uv_buffer_data[2*i];
1149 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1150 data.attr[i][2] = 0;
1151 data.attr[i][3] = 0;
1152 }
1153
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001154 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001155 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001156 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001157 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001158 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001159 assert(!err);
1160
Tony Barbour155cce82015-07-03 10:33:54 -06001161 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001162 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001163
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001164 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
1165 demo->uniform_data.mem_alloc.pNext = NULL;
1166 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1167 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1168
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001169 err = memory_type_from_properties(demo,
1170 mem_reqs.memoryTypeBits,
1171 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001172 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001173 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001174
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001175 err = vkAllocMemory(demo->device, &demo->uniform_data.mem_alloc, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001176 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001177
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001178 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001179 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001180
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001181 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001182
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001183 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001184
Tony Barbour155cce82015-07-03 10:33:54 -06001185 err = vkBindBufferMemory(demo->device,
1186 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001187 demo->uniform_data.mem, 0);
1188 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001189
Courtney Goeltzenleuchter94415342015-09-14 14:14:21 -06001190 demo->uniform_data.desc.bufferInfo.buffer = demo->uniform_data.buf;
1191 demo->uniform_data.desc.bufferInfo.offset = 0;
1192 demo->uniform_data.desc.bufferInfo.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001193}
1194
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001195static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001196{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001197 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001198 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001199 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001200 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001201 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001202 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001203 },
1204 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001205 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001206 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001207 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001208 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001209 },
1210 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001211 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001212 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001213 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001214 .count = 2,
1215 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001216 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001217 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001218
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001219 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001220 &descriptor_layout, &demo->desc_layout);
1221 assert(!err);
1222
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001223 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1224 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1225 .pNext = NULL,
1226 .descriptorSetCount = 1,
1227 .pSetLayouts = &demo->desc_layout,
1228 };
1229
1230 err = vkCreatePipelineLayout(demo->device,
1231 &pPipelineLayoutCreateInfo,
1232 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001233 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001234}
1235
Chia-I Wuf973e322015-07-08 13:34:24 +08001236static void demo_prepare_render_pass(struct demo *demo)
1237{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001238 const VkAttachmentDescription attachments[2] = {
1239 [0] = {
1240 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1241 .pNext = NULL,
1242 .format = demo->format,
1243 .samples = 1,
1244 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1245 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1246 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1247 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1248 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1249 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1250 },
1251 [1] = {
1252 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1253 .pNext = NULL,
1254 .format = demo->depth.format,
1255 .samples = 1,
1256 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1257 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1258 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1259 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1260 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1261 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1262 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001263 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001264 const VkAttachmentReference color_reference = {
1265 .attachment = 0,
1266 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1267 };
1268 const VkSubpassDescription subpass = {
1269 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1270 .pNext = NULL,
1271 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1272 .flags = 0,
1273 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001274 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001275 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001276 .pColorAttachments = &color_reference,
1277 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001278 .depthStencilAttachment = {
1279 .attachment = 1,
1280 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1281 },
1282 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001283 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001284 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001285 const VkRenderPassCreateInfo rp_info = {
1286 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1287 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001288 .attachmentCount = 2,
1289 .pAttachments = attachments,
1290 .subpassCount = 1,
1291 .pSubpasses = &subpass,
1292 .dependencyCount = 0,
1293 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001294 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001295 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001296
1297 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1298 assert(!err);
1299}
1300
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001301static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001302 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001303 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001304 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001305 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001307 VkShaderModuleCreateInfo moduleCreateInfo;
1308 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001309 VkShader shader;
1310 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001312
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001313 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1314 moduleCreateInfo.pNext = NULL;
1315
1316 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1317 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001318 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001319
Cody Northrop1fedb212015-05-28 11:27:16 -06001320 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001321 moduleCreateInfo.codeSize = size;
1322 moduleCreateInfo.pCode = code;
1323 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001324 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001325 assert(!err);
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001326
1327 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001328 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001329 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001330 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001331 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001332 } else {
1333 // Create fake SPV structure to feed GLSL
1334 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001335 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1336 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1337 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001338
1339 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001340 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1341 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1342 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1343 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001344
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001345 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001346 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001347 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001348 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001349
1350 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001351 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001352 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001353 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001354 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001355 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001356 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001357 return shader;
1358}
1359
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001360char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001361{
1362 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001363 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001364 void *shader_code;
1365
1366 FILE *fp = fopen(filename, "rb");
1367 if (!fp) return NULL;
1368
1369 fseek(fp, 0L, SEEK_END);
1370 size = ftell(fp);
1371
1372 fseek(fp, 0L, SEEK_SET);
1373
1374 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001375 retval = fread(shader_code, size, 1, fp);
1376 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001377
1378 *psize = size;
1379
1380 return shader_code;
1381}
1382
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001383static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384{
Cody Northrop1fedb212015-05-28 11:27:16 -06001385 if (!demo->use_glsl) {
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001386 VkShader shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001387 void *vertShaderCode;
1388 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001389
Cody Northrop1fedb212015-05-28 11:27:16 -06001390 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001391
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001392 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
1393 vertShaderCode, size);
1394 free(vertShaderCode);
1395 return shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001396 } else {
1397 static const char *vertShaderText =
1398 "#version 140\n"
1399 "#extension GL_ARB_separate_shader_objects : enable\n"
1400 "#extension GL_ARB_shading_language_420pack : enable\n"
1401 "\n"
1402 "layout(binding = 0) uniform buf {\n"
1403 " mat4 MVP;\n"
1404 " vec4 position[12*3];\n"
1405 " vec4 attr[12*3];\n"
1406 "} ubuf;\n"
1407 "\n"
1408 "layout (location = 0) out vec4 texcoord;\n"
1409 "\n"
1410 "void main() \n"
1411 "{\n"
1412 " texcoord = ubuf.attr[gl_VertexID];\n"
1413 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1414 "\n"
1415 " // GL->VK conventions\n"
1416 " gl_Position.y = -gl_Position.y;\n"
1417 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1418 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001419
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001420 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001421 (const void *) vertShaderText,
1422 strlen(vertShaderText));
1423 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424}
1425
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001426static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427{
Cody Northrop1fedb212015-05-28 11:27:16 -06001428 if (!demo->use_glsl) {
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001429 VkShader shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001430 void *fragShaderCode;
1431 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001432
Cody Northrop1fedb212015-05-28 11:27:16 -06001433 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001434
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001435 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
1436 fragShaderCode, size);
1437 free(fragShaderCode);
1438 return shader;
Cody Northrop1fedb212015-05-28 11:27:16 -06001439 } else {
1440 static const char *fragShaderText =
1441 "#version 140\n"
1442 "#extension GL_ARB_separate_shader_objects : enable\n"
1443 "#extension GL_ARB_shading_language_420pack : enable\n"
1444 "layout (binding = 1) uniform sampler2D tex;\n"
1445 "\n"
1446 "layout (location = 0) in vec4 texcoord;\n"
1447 "layout (location = 0) out vec4 uFragColor;\n"
1448 "void main() {\n"
1449 " uFragColor = texture(tex, texcoord.xy);\n"
1450 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001451
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001452 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001453 (const void *) fragShaderText,
1454 strlen(fragShaderText));
1455 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456}
1457
1458static void demo_prepare_pipeline(struct demo *demo)
1459{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001460 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001461 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001462 VkPipelineInputAssemblyStateCreateInfo ia;
1463 VkPipelineRasterStateCreateInfo rs;
1464 VkPipelineColorBlendStateCreateInfo cb;
1465 VkPipelineDepthStencilStateCreateInfo ds;
1466 VkPipelineViewportStateCreateInfo vp;
1467 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniellba839d12015-09-29 13:01:09 -06001468 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1469 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001470 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001471
Piers Daniellba839d12015-09-29 13:01:09 -06001472 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1473 memset(&dynamicState, 0, sizeof dynamicState);
1474 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1475 dynamicState.pDynamicStates = dynamicStateEnables;
1476
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001477 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001478 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001479 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001480
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001481 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001482 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001483 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001484
1485 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001486 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001487 rs.fillMode = VK_FILL_MODE_SOLID;
1488 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001489 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001490 rs.depthClipEnable = VK_TRUE;
Cody Northrop709c1742015-08-17 11:10:49 -06001491 rs.rasterizerDiscardEnable = VK_FALSE;
1492 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001493
1494 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001495 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1496 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001497 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001498 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001499 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001500 cb.attachmentCount = 1;
1501 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001502
Tony Barbour29645d02015-01-16 14:27:35 -07001503 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001504 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001505 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001506 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1507 vp.scissorCount = 1;
1508 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001509
1510 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001511 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001512 ds.depthTestEnable = VK_TRUE;
1513 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001514 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001515 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1517 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001518 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001519 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001520 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001521
Tony Barbour29645d02015-01-16 14:27:35 -07001522 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001523 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001524 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001525 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001526
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001527 // Two stages: vs and fs
1528 pipeline.stageCount = 2;
1529 VkPipelineShaderStageCreateInfo shaderStages[2];
1530 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1531
1532 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1533 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1534 shaderStages[0].shader = demo_prepare_vs(demo);
1535
1536 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1537 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1538 shaderStages[1].shader = demo_prepare_fs(demo);
1539
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001540 memset(&pipelineCache, 0, sizeof(pipelineCache));
1541 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001542
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001543 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1544 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001545
1546 pipeline.pVertexInputState = NULL;
1547 pipeline.pInputAssemblyState = &ia;
1548 pipeline.pRasterState = &rs;
1549 pipeline.pColorBlendState = &cb;
1550 pipeline.pMultisampleState = &ms;
1551 pipeline.pViewportState = &vp;
1552 pipeline.pDepthStencilState = &ds;
1553 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001554 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001555 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001556
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001557 pipeline.renderPass = demo->render_pass;
1558
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001559 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560 assert(!err);
1561
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001562 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001563 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001564 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001565 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1566 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567}
1568
Chia-I Wu63ea9262015-03-26 13:14:16 +08001569static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001570{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001571 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001572 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001573 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574 .count = 1,
1575 },
1576 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001577 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001578 .count = DEMO_TEXTURE_COUNT,
1579 },
1580 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001581 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001582 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001583 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001584 .poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT,
1585 .maxSets = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001586 .count = 2,
1587 .pTypeCount = type_counts,
1588 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001589 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001590
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001591 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001592 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001593 assert(!err);
1594}
1595
1596static void demo_prepare_descriptor_set(struct demo *demo)
1597{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001598 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1599 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001600 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001601 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001602
Mike Stroyanebae8322015-04-17 12:36:38 -06001603 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001604 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001605 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001606 &demo->desc_set);
1607 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001608
Chia-I Wuae721ba2015-05-25 16:27:55 +08001609 memset(&tex_descs, 0, sizeof(tex_descs));
1610 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1611 tex_descs[i].sampler = demo->textures[i].sampler;
1612 tex_descs[i].imageView = demo->textures[i].view;
1613 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1614 }
1615
1616 memset(&writes, 0, sizeof(writes));
1617
1618 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1619 writes[0].destSet = demo->desc_set;
1620 writes[0].count = 1;
1621 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1622 writes[0].pDescriptors = &demo->uniform_data.desc;
1623
1624 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1625 writes[1].destSet = demo->desc_set;
1626 writes[1].destBinding = 1;
1627 writes[1].count = DEMO_TEXTURE_COUNT;
1628 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1629 writes[1].pDescriptors = tex_descs;
1630
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001631 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001632}
1633
Chia-I Wuf973e322015-07-08 13:34:24 +08001634static void demo_prepare_framebuffers(struct demo *demo)
1635{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001636 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001637 attachments[1] = demo->depth.view;
1638
Chia-I Wuf973e322015-07-08 13:34:24 +08001639 const VkFramebufferCreateInfo fb_info = {
1640 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1641 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001642 .renderPass = demo->render_pass,
1643 .attachmentCount = 2,
1644 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001645 .width = demo->width,
1646 .height = demo->height,
1647 .layers = 1,
1648 };
1649 VkResult U_ASSERT_ONLY err;
1650 uint32_t i;
1651
Tony Barbourd8e504f2015-10-08 14:26:24 -06001652 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1653 assert(demo->framebuffers);
1654
1655 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001656 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001657 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1658 assert(!err);
1659 }
1660}
1661
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001662static void demo_prepare(struct demo *demo)
1663{
Cody Northrop91e221b2015-07-09 18:08:32 -06001664 VkResult U_ASSERT_ONLY err;
1665
1666 const VkCmdPoolCreateInfo cmd_pool_info = {
1667 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1668 .pNext = NULL,
1669 .queueFamilyIndex = demo->graphics_queue_node_index,
1670 .flags = 0,
1671 };
1672 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1673 assert(!err);
1674
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001675 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001676 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001677 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001678 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001679 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001680 .flags = 0,
1681 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001682
1683 demo_prepare_buffers(demo);
1684 demo_prepare_depth(demo);
1685 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001686 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001688 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001689 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691
Ian Elliotta0781972015-08-21 15:09:33 -06001692 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001693 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001694 assert(!err);
1695 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001696
Chia-I Wu63ea9262015-03-26 13:14:16 +08001697 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001698 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001699
Chia-I Wuf973e322015-07-08 13:34:24 +08001700 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001701
Ian Elliotta0781972015-08-21 15:09:33 -06001702 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001703 demo->current_buffer = i;
1704 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1705 }
1706
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001707 /*
1708 * Prepare functions above may generate pipeline commands
1709 * that need to be flushed before beginning the render loop.
1710 */
1711 demo_flush_init_cmd(demo);
1712
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001713 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001714 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001715}
1716
David Pinedo2bb7c932015-06-18 17:03:14 -06001717static void demo_cleanup(struct demo *demo)
1718{
1719 uint32_t i;
1720
1721 demo->prepared = false;
1722
Tony Barbourd8e504f2015-10-08 14:26:24 -06001723 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001724 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1725 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001726 free(demo->framebuffers);
Tony Barbour155cce82015-07-03 10:33:54 -06001727 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001728
Tony Barbour155cce82015-07-03 10:33:54 -06001729 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001730 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001731 vkDestroyRenderPass(demo->device, demo->render_pass);
1732 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1733 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001734
1735 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001736 vkDestroyImageView(demo->device, demo->textures[i].view);
1737 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001738 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001739 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001740 }
Ian Elliotta0781972015-08-21 15:09:33 -06001741 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001742
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001743 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbour155cce82015-07-03 10:33:54 -06001744 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001745 vkFreeMemory(demo->device, demo->depth.mem);
1746
Tony Barbour155cce82015-07-03 10:33:54 -06001747 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001748 vkFreeMemory(demo->device, demo->uniform_data.mem);
1749
Ian Elliotta0781972015-08-21 15:09:33 -06001750 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001751 vkDestroyImageView(demo->device, demo->buffers[i].view);
Tony Barbour155cce82015-07-03 10:33:54 -06001752 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001753 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001754 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001755
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001756 free(demo->queue_props);
1757
Cody Northrop91e221b2015-07-09 18:08:32 -06001758 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001759 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001760 if (demo->validate) {
1761 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1762 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001763 vkDestroyInstance(demo->inst);
1764
1765#ifndef _WIN32
1766 xcb_destroy_window(demo->connection, demo->window);
1767 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001768 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001769#endif // _WIN32
1770}
1771
1772// On MS-Windows, make this a global, so it's available to WndProc()
1773struct demo demo;
1774
Ian Elliott639ca472015-04-16 15:23:05 -06001775#ifdef _WIN32
1776static void demo_run(struct demo *demo)
1777{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001778 if (!demo->prepared)
1779 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001780 // Wait for work to finish before updating MVP.
1781 vkDeviceWaitIdle(demo->device);
1782 demo_update_data_buffer(demo);
1783
1784 demo_draw(demo);
1785
1786 // Wait for work to finish before updating MVP.
1787 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001788
David Pinedo2bb7c932015-06-18 17:03:14 -06001789 demo->curFrame++;
1790
1791 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1792 {
1793 demo->quit=true;
1794 demo_cleanup(demo);
1795 ExitProcess(0);
1796 }
1797
1798}
Ian Elliott639ca472015-04-16 15:23:05 -06001799
1800// MS-Windows event handling function:
1801LRESULT CALLBACK WndProc(HWND hWnd,
1802 UINT uMsg,
1803 WPARAM wParam,
1804 LPARAM lParam)
1805{
Ian Elliott639ca472015-04-16 15:23:05 -06001806 switch(uMsg)
1807 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001808 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001809 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001810 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001811 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001812 demo_run(&demo);
1813 return 0;
1814 default:
1815 break;
1816 }
1817 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1818}
1819
1820static void demo_create_window(struct demo *demo)
1821{
1822 WNDCLASSEX win_class;
1823
1824 // Initialize the window class structure:
1825 win_class.cbSize = sizeof(WNDCLASSEX);
1826 win_class.style = CS_HREDRAW | CS_VREDRAW;
1827 win_class.lpfnWndProc = WndProc;
1828 win_class.cbClsExtra = 0;
1829 win_class.cbWndExtra = 0;
1830 win_class.hInstance = demo->connection; // hInstance
1831 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1832 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1833 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1834 win_class.lpszMenuName = NULL;
1835 win_class.lpszClassName = demo->name;
1836 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1837 // Register window class:
1838 if (!RegisterClassEx(&win_class)) {
1839 // It didn't work, so try to give a useful error:
1840 printf("Unexpected error trying to start the application!\n");
1841 fflush(stdout);
1842 exit(1);
1843 }
1844 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001845 RECT wr = { 0, 0, demo->width, demo->height };
1846 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001847 demo->window = CreateWindowEx(0,
1848 demo->name, // class name
1849 demo->name, // app name
1850 WS_OVERLAPPEDWINDOW | // window style
1851 WS_VISIBLE |
1852 WS_SYSMENU,
1853 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001854 wr.right-wr.left, // width
1855 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001856 NULL, // handle to parent
1857 NULL, // handle to menu
1858 demo->connection, // hInstance
1859 NULL); // no extra parameters
1860 if (!demo->window) {
1861 // It didn't work, so try to give a useful error:
1862 printf("Cannot create a window in which to draw!\n");
1863 fflush(stdout);
1864 exit(1);
1865 }
1866}
1867#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001868static void demo_handle_event(struct demo *demo,
1869 const xcb_generic_event_t *event)
1870{
Piers Daniell735ee532015-02-23 16:23:13 -07001871 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001872 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001873 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001874 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001875 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001876 case XCB_CLIENT_MESSAGE:
1877 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1878 (*demo->atom_wm_delete_window).atom) {
1879 demo->quit = true;
1880 }
1881 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001882 case XCB_KEY_RELEASE:
1883 {
1884 const xcb_key_release_event_t *key =
1885 (const xcb_key_release_event_t *) event;
1886
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001887 switch (key->detail) {
1888 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001889 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001890 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001891 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001892 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001893 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001894 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001895 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001896 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001897 case 0x41:
1898 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001899 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001900 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001901 }
1902 break;
1903 default:
1904 break;
1905 }
1906}
1907
1908static void demo_run(struct demo *demo)
1909{
1910 xcb_flush(demo->connection);
1911
1912 while (!demo->quit) {
1913 xcb_generic_event_t *event;
1914
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001915 if (demo->pause) {
1916 event = xcb_wait_for_event(demo->connection);
1917 } else {
1918 event = xcb_poll_for_event(demo->connection);
1919 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001920 if (event) {
1921 demo_handle_event(demo, event);
1922 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001923 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001924
1925 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001926 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001927 demo_update_data_buffer(demo);
1928
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001929 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001930
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001931 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001932 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001933 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06001934 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06001935 demo->quit = true;
1936
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001937 }
1938}
1939
1940static void demo_create_window(struct demo *demo)
1941{
1942 uint32_t value_mask, value_list[32];
1943
1944 demo->window = xcb_generate_id(demo->connection);
1945
1946 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1947 value_list[0] = demo->screen->black_pixel;
1948 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1949 XCB_EVENT_MASK_EXPOSURE;
1950
1951 xcb_create_window(demo->connection,
1952 XCB_COPY_FROM_PARENT,
1953 demo->window, demo->screen->root,
1954 0, 0, demo->width, demo->height, 0,
1955 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1956 demo->screen->root_visual,
1957 value_mask, value_list);
1958
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001959 /* Magic code that will send notification when window is destroyed */
1960 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1961 "WM_PROTOCOLS");
1962 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1963
1964 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1965 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1966
1967 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1968 demo->window, (*reply).atom, 4, 32, 1,
1969 &(*demo->atom_wm_delete_window).atom);
1970 free(reply);
1971
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001973
1974 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1975 const uint32_t coords[] = {100, 100};
1976 xcb_configure_window(demo->connection, demo->window,
1977 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001978}
Ian Elliott639ca472015-04-16 15:23:05 -06001979#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001980
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001981/*
1982 * Return 1 (true) if all layer names specified in check_names
1983 * can be found in given layer properties.
1984 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001985static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001986 uint32_t layer_count, VkLayerProperties *layers)
1987{
1988 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001989 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001990 for (uint32_t j = 0; j < layer_count; j++) {
1991 if (!strcmp(check_names[i], layers[j].layerName)) {
1992 found = 1;
1993 }
1994 }
1995 if (!found) {
1996 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1997 return 0;
1998 }
1999 }
2000 return 1;
2001}
2002
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002003static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002004{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002005 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002006 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002007 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002008 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002009 VkLayerProperties *instance_layers;
2010 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002011 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002012 uint32_t instance_layer_count = 0;
2013 uint32_t enabled_extension_count = 0;
2014 uint32_t enabled_layer_count = 0;
2015
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002016 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002017 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002018 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002019 "ObjectTracker",
2020 "DrawState",
2021 "ParamChecker",
2022 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002023 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002024 "DeviceLimits",
2025 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002026 };
2027
2028 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002029 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002030 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002031 "ObjectTracker",
2032 "DrawState",
2033 "ParamChecker",
2034 "ShaderChecker",
Ian Elliottd0c74cf2015-09-22 10:51:24 -06002035 "Swapchain",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002036 "DeviceLimits",
2037 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002038 };
2039
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002040 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002041 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002042 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002043 assert(!err);
2044
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002045 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002046 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002047 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002048
2049 if (demo->validate) {
2050 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2051 instance_layer_count, instance_layers);
2052 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002053 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002054 "required validation layer.\n\n"
2055 "Please look at the Getting Started guide for additional "
2056 "information.\n",
2057 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002058 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002059 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002060 }
2061
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002062 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002063 assert(!err);
2064
Tony Barbourcc69f452015-10-08 13:59:42 -06002065 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002066 memset(extension_names, 0, sizeof(extension_names));
2067 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002068 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002069 assert(!err);
2070 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002071 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002072 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002073 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002074 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002075 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002076 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002077 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002078 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002079 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002080 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002081 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002082 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002083 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002084 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002085 "Vulkan installable client driver (ICD) installed?\nPlease "
2086 "look at the Getting Started guide for additional "
2087 "information.\n",
2088 "vkCreateInstance Failure");
2089 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002090 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002091 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002092 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002093 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002094 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002095 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002096 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002097 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002098 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002099 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002100 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002101 .pNext = NULL,
2102 .pAppInfo = &app,
2103 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002104 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002105 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002106 .extensionCount = enabled_extension_count,
2107 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002108 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002109
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002110 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002111
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002112 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002113 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2114 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002115 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002116 "additional information.\n",
2117 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002118 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002119 ERR_EXIT("Cannot find a specified extension library"
2120 ".\nMake sure your layers path is set appropriately\n",
2121 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002122 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002123 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2124 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002125 "the Getting Started guide for additional information.\n",
2126 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002127 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002128
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002129 free(instance_layers);
2130 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002131
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002132 /* Make initial call to query gpu_count, then second call for gpu info*/
2133 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2134 assert(!err && gpu_count > 0);
2135 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2136 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2137 assert(!err);
2138 /* For cube demo we just grab the first physical device */
2139 demo->gpu = physical_devices[0];
2140 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002141
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002142 /* Look for validation layers */
2143 validation_found = 0;
2144 enabled_layer_count = 0;
2145 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002146 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002147 assert(!err);
2148
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002149 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002150 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002151 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002152
2153 if (demo->validate) {
2154 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2155 device_layer_count, device_layers);
2156 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002157 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002158 "a required validation layer.\n\n"
2159 "Please look at the Getting Started guide for additional "
2160 "information.\n",
2161 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002162 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002163 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002164 }
2165
2166 uint32_t device_extension_count = 0;
2167 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002168 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002169 demo->gpu, NULL, &device_extension_count, NULL);
2170 assert(!err);
2171
Tony Barbourcc69f452015-10-08 13:59:42 -06002172 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002173 enabled_extension_count = 0;
2174 memset(extension_names, 0, sizeof(extension_names));
2175 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002176 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 demo->gpu, NULL, &device_extension_count, device_extensions);
2178 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002179
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002180 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002181 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002182 swapchainExtFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002183 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002184 }
2185 assert(enabled_extension_count < 64);
2186 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002187 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002188 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002189 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002190 "Vulkan installable client driver (ICD) installed?\nPlease "
2191 "look at the Getting Started guide for additional "
2192 "information.\n",
2193 "vkCreateInstance Failure");
2194 }
2195
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002196 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002197 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2198 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002199 if (!demo->dbgCreateMsgCallback) {
2200 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2201 "vkGetProcAddr Failure");
2202 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002203 if (!demo->dbgDestroyMsgCallback) {
2204 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2205 "vkGetProcAddr Failure");
2206 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002207 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2208 if (!demo->dbgBreakCallback) {
2209 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2210 "vkGetProcAddr Failure");
2211 }
2212
2213 PFN_vkDbgMsgCallback callback;
2214
2215 if (!demo->use_break) {
2216 callback = dbgFunc;
2217 } else {
2218 callback = demo->dbgBreakCallback;
2219 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002220 err = demo->dbgCreateMsgCallback(
2221 demo->inst,
2222 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002223 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002224 &demo->msg_callback);
2225 switch (err) {
2226 case VK_SUCCESS:
2227 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002228 case VK_ERROR_OUT_OF_HOST_MEMORY:
2229 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2230 "dbgCreateMsgCallback Failure");
2231 break;
2232 default:
2233 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2234 "dbgCreateMsgCallback Failure");
2235 break;
2236 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002237 }
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002238 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
2239 assert(!err);
2240
2241 /* Call with NULL data to get count */
2242 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
2243 assert(!err);
2244 assert(demo->queue_count >= 1);
2245
2246 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2247 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
2248 assert(!err);
2249 // Find a queue that supports gfx
2250 uint32_t gfx_queue_idx = 0;
2251 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2252 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2253 break;
2254 }
2255 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002256 // Query fine-grained feature support for this device.
2257 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002258 VkPhysicalDeviceFeatures physDevFeatures;
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002259 err = vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
2260 assert(!err);
2261
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002262 const VkDeviceQueueCreateInfo queue = {
2263 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2264 .pNext = NULL,
2265 .queueFamilyIndex = gfx_queue_idx,
2266 .queueCount = 1,
2267 };
2268
2269 VkDeviceCreateInfo device = {
2270 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2271 .pNext = NULL,
2272 .queueRecordCount = 1,
2273 .pRequestedQueues = &queue,
2274 .layerCount = enabled_layer_count,
2275 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
2276 .extensionCount = enabled_extension_count,
2277 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002278 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002279 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002280
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002281 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002282 assert(!err);
2283
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002284 free(device_layers);
2285
Ian Elliotta0781972015-08-21 15:09:33 -06002286 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2287 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2288 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2289 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2290 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002291 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2292 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2293 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2294 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002295}
2296
Tony Barbourcc69f452015-10-08 13:59:42 -06002297static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002298{
2299 VkResult err;
2300 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002301
Tony Barbourcc69f452015-10-08 13:59:42 -06002302 // Construct the surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002303 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002304 demo->surface_description.pNext = NULL;
2305#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002306 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002307 demo->surface_description.pPlatformHandle = demo->connection;
2308 demo->surface_description.pPlatformWindow = demo->window;
2309#else // _WIN32
2310 demo->platform_handle_xcb.connection = demo->connection;
2311 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002312 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002313 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2314 demo->surface_description.pPlatformWindow = &demo->window;
2315#endif // _WIN32
2316
Tony Barbourcc69f452015-10-08 13:59:42 -06002317 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002318 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2319 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002320 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2321 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002322 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002323 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002324
2325 // Search for a graphics and a present queue in the array of queue
2326 // families, try to find one that supports both
2327 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2328 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002329 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002330 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2331 if (graphicsQueueNodeIndex == UINT32_MAX) {
2332 graphicsQueueNodeIndex = i;
2333 }
2334
2335 if (supportsPresent[i] == VK_TRUE) {
2336 graphicsQueueNodeIndex = i;
2337 presentQueueNodeIndex = i;
2338 break;
2339 }
2340 }
2341 }
2342 if (presentQueueNodeIndex == UINT32_MAX) {
2343 // If didn't find a queue that supports both graphics and present, then
2344 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002345 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002346 if (supportsPresent[i] == VK_TRUE) {
2347 presentQueueNodeIndex = i;
2348 break;
2349 }
2350 }
2351 }
2352 free(supportsPresent);
2353
2354 // Generate error if could not find both a graphics and a present queue
2355 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2356 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002357 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002358 }
2359
2360 // TODO: Add support for separate queues, including presentation,
2361 // synchronization, and appropriate tracking for QueueSubmit
2362 // While it is possible for an application to use a separate graphics and a
2363 // present queues, this demo program assumes it is only using one:
2364 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2365 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002366 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002367 }
2368
2369 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002370
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002371 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002372 0, &demo->queue);
2373 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002374
Ian Elliottaaae5352015-07-06 14:27:58 -06002375 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002376 uint32_t formatCount;
Ian Elliotta0781972015-08-21 15:09:33 -06002377 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2378 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002379 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002380 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002381 VkSurfaceFormatKHR *surfFormats =
2382 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2383 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2384 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002385 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002386 assert(!err);
2387 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2388 // the surface has no preferred format. Otherwise, at least one
2389 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002390 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2391 {
2392 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2393 }
2394 else
2395 {
2396 assert(formatCount >= 1);
2397 demo->format = surfFormats[0].format;
2398 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002399 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002400
David Pinedo2bb7c932015-06-18 17:03:14 -06002401 demo->quit = false;
2402 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002403
2404 // Get Memory information and properties
2405 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2406 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002407}
2408
2409static void demo_init_connection(struct demo *demo)
2410{
Ian Elliott639ca472015-04-16 15:23:05 -06002411#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002412 const xcb_setup_t *setup;
2413 xcb_screen_iterator_t iter;
2414 int scr;
2415
2416 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002417 if (demo->connection == NULL) {
2418 printf("Cannot find a compatible Vulkan installable client driver "
2419 "(ICD).\nExiting ...\n");
2420 fflush(stdout);
2421 exit(1);
2422 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002423
2424 setup = xcb_get_setup(demo->connection);
2425 iter = xcb_setup_roots_iterator(setup);
2426 while (scr-- > 0)
2427 xcb_screen_next(&iter);
2428
2429 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002430#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002431}
2432
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002433static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002434{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002435 vec3 eye = {0.0f, 3.0f, 5.0f};
2436 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002437 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002438
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002439 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002440 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002441
Piers Daniell735ee532015-02-23 16:23:13 -07002442 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002443 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002444 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002445 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002446 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002447 if (strcmp(argv[i], "--use_glsl") == 0) {
2448 demo->use_glsl = true;
2449 continue;
2450 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002451 if (strcmp(argv[i], "--break") == 0) {
2452 demo->use_break = true;
2453 continue;
2454 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002455 if (strcmp(argv[i], "--validate") == 0) {
2456 demo->validate = true;
2457 continue;
2458 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002459 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002460 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002461 i < argc-1 &&
2462 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2463 demo->frameCount >= 0)
2464 {
2465 i++;
2466 continue;
2467 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002468
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002469 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002470 fflush(stderr);
2471 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002472 }
2473
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002474 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002475 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002476
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002477 demo->width = 500;
2478 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002479
2480 demo->spin_angle = 0.01f;
2481 demo->spin_increment = 0.01f;
2482 demo->pause = false;
2483
Piers Daniell735ee532015-02-23 16:23:13 -07002484 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002485 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2486 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002487}
2488
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002489
Ian Elliott639ca472015-04-16 15:23:05 -06002490#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002491extern int __getmainargs(
2492 int * _Argc,
2493 char *** _Argv,
2494 char *** _Env,
2495 int _DoWildCard,
2496 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002497
Ian Elliotta748eaf2015-04-28 15:50:36 -06002498int WINAPI WinMain(HINSTANCE hInstance,
2499 HINSTANCE hPrevInstance,
2500 LPSTR pCmdLine,
2501 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002502{
2503 MSG msg; // message
2504 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002505 int argc;
2506 char** argv;
2507 char** env;
2508 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002509
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002510 __getmainargs(&argc,&argv,&env,0,&new_mode);
2511
2512 demo_init(&demo, argc, argv);
2513 demo.connection = hInstance;
2514 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002515 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002516 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002517
2518 demo_prepare(&demo);
2519
2520 done = false; //initialize loop condition variable
2521 /* main message loop*/
2522 while(!done)
2523 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002524 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002525 if (msg.message == WM_QUIT) //check for a quit message
2526 {
2527 done = true; //if found, quit app
2528 }
2529 else
2530 {
2531 /* Translate and dispatch to event queue*/
2532 TranslateMessage(&msg);
2533 DispatchMessage(&msg);
2534 }
2535 }
2536
2537 demo_cleanup(&demo);
2538
Tony Barbourf9921732015-04-22 11:36:22 -06002539 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002540}
2541#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002542int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002543{
2544 struct demo demo;
2545
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002546 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002547 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002548 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002549
2550 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002551 demo_run(&demo);
2552
2553 demo_cleanup(&demo);
2554
2555 return 0;
2556}
Ian Elliott639ca472015-04-16 15:23:05 -06002557#endif // _WIN32