blob: e9341809cca70c623523c53ba8b97226358d0f6b [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_BUFFER_COUNT 2
50#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliott07264132015-04-28 11:35:02 -060062#ifdef _WIN32
63#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
68
Ian Elliott07264132015-04-28 11:35:02 -060069#else // _WIN32
70
71#define ERR_EXIT(err_msg, err_class) \
72 do { \
73 printf(err_msg); \
74 fflush(stdout); \
75 exit(1); \
76 } while (0)
77#endif // _WIN32
78
Ian Elliottaaae5352015-07-06 14:27:58 -060079#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
80{ \
81 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
82 if (demo->fp##entrypoint == NULL) { \
83 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
84 "vkGetInstanceProcAddr Failure"); \
85 } \
86}
87
Ian Elliott673898b2015-06-22 15:07:49 -060088#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
89{ \
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -060090 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060091 if (demo->fp##entrypoint == NULL) { \
92 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
93 "vkGetDeviceProcAddr Failure"); \
94 } \
95}
96
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060097/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070098 * structure to track all objects related to a texture.
99 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600100struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600101 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700102
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600103 VkImage image;
104 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600105
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
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600331 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600333 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600334 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
Ian Elliotta0781972015-08-21 15:09:33 -0600336 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
337 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
338 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
339 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
340 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
341 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
342 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
343 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
344 PFN_vkQueuePresentKHR fpQueuePresentKHR;
345 VkSurfaceDescriptionWindowKHR surface_description;
346 uint32_t swapchainImageCount;
347 VkSwapchainKHR swap_chain;
348 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
Ian Elliottaaae5352015-07-06 14:27:58 -0600350 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
352 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600353 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600355 VkImage image;
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;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500364 VkDeviceMemory mem;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800365 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600366 } uniform_data;
367
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600368 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500369 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600370 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600371 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800372 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600373 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Cody Northrop5e4125d2015-08-26 10:01:32 -0600375 VkDynamicViewportState dynamic_viewport;
376 VkDynamicLineWidthState dynamic_line_width;
377 VkDynamicDepthBiasState dynamic_depth_bias;
378 VkDynamicBlendState dynamic_blend;
379 VkDynamicDepthBoundsState dynamic_depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -0600380 VkDynamicStencilState dynamic_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600382 mat4x4 projection_matrix;
383 mat4x4 view_matrix;
384 mat4x4 model_matrix;
385
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600386 float spin_angle;
387 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600388 bool pause;
389
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600390 VkShaderModule vert_shader_module;
391 VkShaderModule frag_shader_module;
392
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 VkDescriptorPool desc_pool;
394 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800395
Chia-I Wuf973e322015-07-08 13:34:24 +0800396 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
397
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600399 int32_t curFrame;
400 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600401 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600402 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600403 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600404 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600405 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600406 VkDbgMsgCallback msg_callback;
407
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600408 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600409 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600410};
411
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600412static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
413{
414 // Search memtypes to find first index with those properties
415 for (uint32_t i = 0; i < 32; i++) {
416 if ((typeBits & 1) == 1) {
417 // Type is available, does it match user properties?
418 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
419 *typeIndex = i;
420 return VK_SUCCESS;
421 }
422 }
423 typeBits >>= 1;
424 }
425 // No memory types matched, return failure
426 return VK_UNSUPPORTED;
427}
428
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600429static void demo_flush_init_cmd(struct demo *demo)
430{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600431 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600433 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600434 return;
435
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437 assert(!err);
438
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600439 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600440 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600441
Tony Barbour155cce82015-07-03 10:33:54 -0600442 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600443 assert(!err);
444
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600445 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600446 assert(!err);
447
Tony Barbour155cce82015-07-03 10:33:54 -0600448 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600449 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450}
451
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452static void demo_set_image_layout(
453 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600454 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600455 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600456 VkImageLayout old_image_layout,
457 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600459 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600460
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600462 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600464 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600465 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800466 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600467 .flags = 0,
468 };
469
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600470 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471 assert(!err);
472
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600473 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600475 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600476 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600477 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600478 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600479 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600480 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600482 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600483 }
484
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600485 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600486 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487 .pNext = NULL,
488 .outputMask = 0,
489 .inputMask = 0,
490 .oldLayout = old_image_layout,
491 .newLayout = new_image_layout,
492 .image = image,
Cody Northrop0e951672015-09-14 13:48:12 -0600493 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494 };
495
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600496 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600497 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600498 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499 }
500
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600503 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600504 }
505
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600506 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600507
Tony Barbour50bbca42015-06-29 16:20:35 -0600508 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
509 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600511 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600512}
513
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600514static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600515{
Chia-I Wuf973e322015-07-08 13:34:24 +0800516 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600518 .pNext = NULL,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600519 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600520 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600521 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600522 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700523 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800524 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600525 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
526 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800527 };
528 const VkRenderPassBeginInfo rp_begin = {
529 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
530 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800531 .renderPass = demo->render_pass,
532 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800533 .renderArea.offset.x = 0,
534 .renderArea.offset.y = 0,
535 .renderArea.extent.width = demo->width,
536 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600537 .clearValueCount = 2,
538 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700539 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800540 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600542 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543 assert(!err);
544
Chia-I Wua74c5b22015-07-07 11:50:03 +0800545 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
546
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600547 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600549 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600550 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600551
Cody Northrop5e4125d2015-08-26 10:01:32 -0600552 vkCmdBindDynamicViewportState(cmd_buf, demo->dynamic_viewport);
553 vkCmdBindDynamicLineWidthState(cmd_buf, demo->dynamic_line_width);
554 vkCmdBindDynamicDepthBiasState(cmd_buf, demo->dynamic_depth_bias);
555 vkCmdBindDynamicBlendState(cmd_buf, demo->dynamic_blend);
556 vkCmdBindDynamicDepthBoundsState(cmd_buf, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -0600557 vkCmdBindDynamicStencilState(cmd_buf, demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600559 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800560 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600562 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600563 assert(!err);
564}
565
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600566
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600567void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600569 mat4x4 MVP, Model, VP;
570 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600571 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600572 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600574 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600576 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600577 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700578 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600579 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600580
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500581 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600582 assert(!err);
583
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600584 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600585
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600586 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600587}
588
589static void demo_draw(struct demo *demo)
590{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600591 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600592 VkSemaphore presentCompleteSemaphore;
593 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
594 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
595 .pNext = NULL,
596 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
597 };
Tony Barbour155cce82015-07-03 10:33:54 -0600598 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600599
Ian Elliottaaae5352015-07-06 14:27:58 -0600600 err = vkCreateSemaphore(demo->device,
601 &presentCompleteSemaphoreCreateInfo,
602 &presentCompleteSemaphore);
603 assert(!err);
604
605 // Get the index of the next available swapchain image:
Ian Elliotta0781972015-08-21 15:09:33 -0600606 err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600607 UINT64_MAX,
608 presentCompleteSemaphore,
609 &demo->current_buffer);
Ian Elliotta0781972015-08-21 15:09:33 -0600610 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600611 // return codes
612 assert(!err);
613
614 // Wait for the present complete semaphore to be signaled to ensure
615 // that the image won't be rendered to until the presentation
616 // engine has fully released ownership to the application, and it is
617 // okay to render to the image.
618 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
619
Ian Elliotta0781972015-08-21 15:09:33 -0600620// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600621 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600622 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623 assert(!err);
624
Ian Elliotta0781972015-08-21 15:09:33 -0600625 VkPresentInfoKHR present = {
626 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600627 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600628 .swapchainCount = 1,
629 .swapchains = &demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600630 .imageIndices = &demo->current_buffer,
631 };
632
633// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600634 err = demo->fpQueuePresentKHR(demo->queue, &present);
635 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600636 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600637 assert(!err);
638
Chia-I Wucbb564e2015-04-16 22:02:10 +0800639 err = vkQueueWaitIdle(demo->queue);
640 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600641
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600642 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643}
644
645static void demo_prepare_buffers(struct demo *demo)
646{
Ian Elliottaaae5352015-07-06 14:27:58 -0600647 VkResult U_ASSERT_ONLY err;
648
649 // Check the surface properties and formats
Ian Elliotta0781972015-08-21 15:09:33 -0600650 VkSurfacePropertiesKHR surfProperties;
651 err = demo->fpGetSurfacePropertiesKHR(demo->device,
652 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600653 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600654 assert(!err);
655
Ian Elliott8528eff2015-08-07 15:56:59 -0600656 uint32_t presentModeCount;
Ian Elliotta0781972015-08-21 15:09:33 -0600657 err = demo->fpGetSurfacePresentModesKHR(demo->device,
658 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600659 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600660 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600661 VkPresentModeKHR *presentModes =
662 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600663 assert(presentModes);
Ian Elliotta0781972015-08-21 15:09:33 -0600664 err = demo->fpGetSurfacePresentModesKHR(demo->device,
665 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600666 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600667 assert(!err);
668
Ian Elliotta0781972015-08-21 15:09:33 -0600669 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600670 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600671 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600672 {
673 // If the surface size is undefined, the size is set to
674 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600675 swapchainExtent.width = demo->width;
676 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600677 }
678 else
679 {
680 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600681 swapchainExtent = surfProperties.currentExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600682 }
683
684 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600685 // tearing mode. If not, try IMMEDIATE which will usually be available,
686 // and is fastest (though it tears). If not, fall back to FIFO which is
687 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600688 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600689 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600690 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
691 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600692 break;
693 }
Ian Elliotta0781972015-08-21 15:09:33 -0600694 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
695 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
696 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600697 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600698 }
699
Ian Elliott2d47da92015-07-13 12:20:56 -0600700#define WORK_AROUND_CODE
701#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600702 // After the proper code was created, other parts of this demo were
703 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
704 // images, etc. Live with that for now.
705 // TODO: Rework this demo code to live with the number of buffers returned
Ian Elliotta0781972015-08-21 15:09:33 -0600706 // by vkCreateSwapchainKHR().
707 uint32_t desiredNumberOfSwapchainImages = DEMO_BUFFER_COUNT;
Ian Elliott2d47da92015-07-13 12:20:56 -0600708#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600709 // Determine the number of VkImage's to use in the swap chain (we desire to
710 // own only 1 image at a time, besides the images being displayed and
711 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600712 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600713 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600714 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600715 {
716 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600717 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600718 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600719#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600720
Tony Barbour9fd6d352015-09-16 15:01:32 -0600721 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600722 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
723 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600724 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600725 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600726 }
727
Ian Elliotta0781972015-08-21 15:09:33 -0600728 const VkSwapchainCreateInfoKHR swap_chain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600729 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800730 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600731 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
732 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800733 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600734 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800735 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600736 .width = swapchainExtent.width,
737 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600739 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600740 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800741 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600742 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
743 .queueFamilyCount = 0,
744 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600745 .presentMode = swapchainPresentMode,
746 .oldSwapchain.handle = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600747 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600748 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600749 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600750
Ian Elliotta0781972015-08-21 15:09:33 -0600751 err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800752 assert(!err);
753
Ian Elliotta0781972015-08-21 15:09:33 -0600754 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
755 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600756 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800757
Ian Elliotta0781972015-08-21 15:09:33 -0600758 VkImage* swapchainImages =
759 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
760 assert(swapchainImages);
761 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
762 &demo->swapchainImageCount,
763 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600764 assert(!err);
Ian Elliott2d47da92015-07-13 12:20:56 -0600765#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600766 // After the proper code was created, other parts of this demo were
767 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
768 // images, etc. Live with that for now.
769 // TODO: Rework this demo code to live with the number of buffers returned
Ian Elliotta0781972015-08-21 15:09:33 -0600770 // by vkCreateSwapchainKHR().
771 demo->swapchainImageCount = DEMO_BUFFER_COUNT;
Ian Elliott2d47da92015-07-13 12:20:56 -0600772#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600773
Ian Elliotta0781972015-08-21 15:09:33 -0600774 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600775 assert(demo->buffers);
776
Ian Elliotta0781972015-08-21 15:09:33 -0600777 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600778 VkImageViewCreateInfo color_image_view = {
779 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600780 .pNext = NULL,
781 .format = demo->format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600782 .channels = {
783 .r = VK_CHANNEL_SWIZZLE_R,
784 .g = VK_CHANNEL_SWIZZLE_G,
785 .b = VK_CHANNEL_SWIZZLE_B,
786 .a = VK_CHANNEL_SWIZZLE_A,
787 },
788 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600789 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600790 .baseMipLevel = 0,
791 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600792 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600793 .arraySize = 1
794 },
795 .viewType = VK_IMAGE_VIEW_TYPE_2D,
796 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600797 };
798
Ian Elliotta0781972015-08-21 15:09:33 -0600799 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600800
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600801 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400802 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600803 VK_IMAGE_LAYOUT_UNDEFINED,
804 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600805
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600806 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600807
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600808 err = vkCreateImageView(demo->device,
809 &color_image_view, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600810 assert(!err);
811 }
812}
813
814static void demo_prepare_depth(struct demo *demo)
815{
Tony Barbour72304ef2015-04-16 15:59:00 -0600816 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600817 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600818 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600820 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600821 .format = depth_format,
822 .extent = { demo->width, demo->height, 1 },
823 .mipLevels = 1,
824 .arraySize = 1,
825 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600826 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600827 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600828 .flags = 0,
829 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600830 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600831 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500832 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600833 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600834 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835 };
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600836 VkImageViewCreateInfo view = {
837 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600838 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600839 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600840 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600841 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600842 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600843 .baseMipLevel = 0,
844 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600845 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600846 .arraySize = 1
847 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600848 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600849 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600850 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600851
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500852 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600853 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600854
855 demo->depth.format = depth_format;
856
857 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600858 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600859 &demo->depth.image);
860 assert(!err);
861
Tony Barbour155cce82015-07-03 10:33:54 -0600862 err = vkGetImageMemoryRequirements(demo->device,
863 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600864
865 mem_alloc.allocationSize = mem_reqs.size;
866 err = memory_type_from_properties(demo,
867 mem_reqs.memoryTypeBits,
868 VK_MEMORY_PROPERTY_DEVICE_ONLY,
869 &mem_alloc.memoryTypeIndex);
870 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600871
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500872 /* allocate memory */
873 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
874 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600875
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500876 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600877 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500878 demo->depth.mem, 0);
879 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600880
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600881 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600882 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600883 VK_IMAGE_LAYOUT_UNDEFINED,
884 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600885
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600886 /* create image view */
887 view.image = demo->depth.image;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600888 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600889 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600890}
891
Tony Barbour1a86d032015-09-21 15:17:33 -0600892/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700893bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600894 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600895 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600896{
Tony Barbour1a86d032015-09-21 15:17:33 -0600897 FILE *fPtr = fopen(filename,"rb");
898 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600899
Tony Barbour1a86d032015-09-21 15:17:33 -0600900 if (!fPtr)
901 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600902
Tony Barbour1a86d032015-09-21 15:17:33 -0600903 cPtr = fgets(header, 256, fPtr); // P6
904 if (cPtr == NULL || strncmp(header, "P6\n", 3))
905 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600906
Tony Barbour1a86d032015-09-21 15:17:33 -0600907 do {
908 cPtr = fgets(header, 256, fPtr);
909 if (cPtr == NULL)
910 return false;
911 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600912
Tony Barbour1a86d032015-09-21 15:17:33 -0600913 sscanf(header, "%u %u", height, width);
914 if (rgba_data == NULL)
915 return true;
916 fgets(header, 256, fPtr); // Format
917 if (cPtr == NULL || strncmp(header, "255\n", 3))
918 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600919
Tony Barbour1a86d032015-09-21 15:17:33 -0600920 for(int y = 0; y < *height; y++)
921 {
922 uint8_t *rowPtr = rgba_data;
923 for(int x = 0; x < *width; x++)
924 {
925 fread(rowPtr, 3, 1, fPtr);
926 rowPtr[3] = 255; /* Alpha of 1 */
927 rowPtr += 4;
928 }
929 rgba_data += layout->rowPitch;
930 }
931 fclose(fPtr);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600932 return true;
933}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600934
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700935static void demo_prepare_texture_image(struct demo *demo,
936 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600937 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600938 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600939 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600940 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600941{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600942 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600943 int32_t tex_width;
944 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600945 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946
David Pinedo30bd71d2015-04-23 08:16:57 -0600947 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
948 {
949 printf("Failed to load textures\n");
950 fflush(stdout);
951 exit(1);
952 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700953
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600954 tex_obj->tex_width = tex_width;
955 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600957 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600958 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700959 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600960 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700961 .format = tex_format,
962 .extent = { tex_width, tex_height, 1 },
963 .mipLevels = 1,
964 .arraySize = 1,
965 .samples = 1,
966 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600967 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700968 .flags = 0,
969 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600970 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600971 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500972 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700973 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600974 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975 };
976
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500977 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600979 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600980 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981 assert(!err);
982
Tony Barbour155cce82015-07-03 10:33:54 -0600983 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600984 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700985
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500986 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700987
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600988 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
989 assert(!err);
990
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500991 /* allocate memory */
992 err = vkAllocMemory(demo->device, &mem_alloc,
993 &(tex_obj->mem));
994 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700995
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500996 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600997 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500998 tex_obj->mem, 0);
999 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001000
Tony Barbour72304ef2015-04-16 15:59:00 -06001001 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001002 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001005 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001007 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001008 void *data;
1009
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001010 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1011 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001012
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001013 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001014 assert(!err);
1015
1016 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1017 fprintf(stderr, "Error loading texture: %s\n", filename);
1018 }
1019
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001020 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001022
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001023 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001024 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001025 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001026 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001027 tex_obj->imageLayout);
1028 /* 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 -07001029}
1030
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001031static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032{
1033 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001034 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001035 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036}
1037
1038static void demo_prepare_textures(struct demo *demo)
1039{
Tony Barbour72304ef2015-04-16 15:59:00 -06001040 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001041 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001042 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001043 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001045 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046 assert(!err);
1047
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001048 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001049
FslNopper6a7e50e2015-05-06 21:42:01 +02001050 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001051 /* Device can texture using linear textures */
1052 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001053 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001054 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001056 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057
1058 memset(&staging_texture, 0, sizeof(staging_texture));
1059 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001060 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061
1062 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001063 VK_IMAGE_TILING_OPTIMAL,
1064 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1065 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001067 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001068 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001069 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001072 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001073 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001074 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001075 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001077 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001078 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001080 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081 .destOffset = { 0, 0, 0 },
1082 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1083 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 vkCmdCopyImage(demo->cmd,
1085 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1086 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001087 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001088
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001089 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001090 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001091 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001092 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001093
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001094 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001095
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001096 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001097 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001098 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1099 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001100 }
1101
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001102 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001103 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001104 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001105 .magFilter = VK_TEX_FILTER_NEAREST,
1106 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001107 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter781da8a2015-09-10 14:08:50 -06001108 .addressModeU = VK_TEX_ADDRESS_MODE_CLAMP,
1109 .addressModeV = VK_TEX_ADDRESS_MODE_CLAMP,
1110 .addressModeW = VK_TEX_ADDRESS_MODE_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001112 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001113 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114 .minLod = 0.0f,
1115 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001116 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001117 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001118 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001119
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001120 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001121 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001122 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001123 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001124 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001126 .channels = { VK_CHANNEL_SWIZZLE_R,
1127 VK_CHANNEL_SWIZZLE_G,
1128 VK_CHANNEL_SWIZZLE_B,
1129 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001130 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001131 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001133
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001134 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001135 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001136 &demo->textures[i].sampler);
1137 assert(!err);
1138
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001139 /* create image view */
1140 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001141 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001142 &demo->textures[i].view);
1143 assert(!err);
1144 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001145}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001146
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001147void demo_prepare_cube_data_buffer(struct demo *demo)
1148{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001149 VkBufferCreateInfo buf_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001150 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001151 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001152 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001153 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001154 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001155 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001156 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001157 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001158 int i;
1159 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001160 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001161 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001162
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001163 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001164 mat4x4_mul(MVP, VP, demo->model_matrix);
1165 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001166// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001167
1168 for (i=0; i<12*3; i++) {
1169 data.position[i][0] = g_vertex_buffer_data[i*3];
1170 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1171 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1172 data.position[i][3] = 1.0f;
1173 data.attr[i][0] = g_uv_buffer_data[2*i];
1174 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1175 data.attr[i][2] = 0;
1176 data.attr[i][3] = 0;
1177 }
1178
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001179 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001180 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001181 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001182 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001183 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001184 assert(!err);
1185
Tony Barbour155cce82015-07-03 10:33:54 -06001186 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001187 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001188
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001189 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001190 err = memory_type_from_properties(demo,
1191 mem_reqs.memoryTypeBits,
1192 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1193 &alloc_info.memoryTypeIndex);
1194 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001195
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001196 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1197 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001198
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001199 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1200 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001201
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001202 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001203
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001204 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001205
Tony Barbour155cce82015-07-03 10:33:54 -06001206 err = vkBindBufferMemory(demo->device,
1207 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001208 demo->uniform_data.mem, 0);
1209 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001210
Courtney Goeltzenleuchter94415342015-09-14 14:14:21 -06001211 demo->uniform_data.desc.bufferInfo.buffer = demo->uniform_data.buf;
1212 demo->uniform_data.desc.bufferInfo.offset = 0;
1213 demo->uniform_data.desc.bufferInfo.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001214}
1215
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001216static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001217{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001218 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001219 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001220 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001221 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001222 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001223 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001224 },
1225 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001226 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001227 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001228 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001229 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001230 },
1231 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001232 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001233 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001234 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001235 .count = 2,
1236 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001237 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001238 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001240 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001241 &descriptor_layout, &demo->desc_layout);
1242 assert(!err);
1243
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001244 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1245 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1246 .pNext = NULL,
1247 .descriptorSetCount = 1,
1248 .pSetLayouts = &demo->desc_layout,
1249 };
1250
1251 err = vkCreatePipelineLayout(demo->device,
1252 &pPipelineLayoutCreateInfo,
1253 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001254 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001255}
1256
Chia-I Wuf973e322015-07-08 13:34:24 +08001257static void demo_prepare_render_pass(struct demo *demo)
1258{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001259 const VkAttachmentDescription attachments[2] = {
1260 [0] = {
1261 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1262 .pNext = NULL,
1263 .format = demo->format,
1264 .samples = 1,
1265 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1266 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1267 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1268 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1269 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1270 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1271 },
1272 [1] = {
1273 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1274 .pNext = NULL,
1275 .format = demo->depth.format,
1276 .samples = 1,
1277 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1278 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1279 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1280 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1281 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1282 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1283 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001284 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001285 const VkAttachmentReference color_reference = {
1286 .attachment = 0,
1287 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1288 };
1289 const VkSubpassDescription subpass = {
1290 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1291 .pNext = NULL,
1292 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1293 .flags = 0,
1294 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001295 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001296 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001297 .pColorAttachments = &color_reference,
1298 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001299 .depthStencilAttachment = {
1300 .attachment = 1,
1301 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1302 },
1303 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001304 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001305 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001306 const VkRenderPassCreateInfo rp_info = {
1307 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1308 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001309 .attachmentCount = 2,
1310 .pAttachments = attachments,
1311 .subpassCount = 1,
1312 .pSubpasses = &subpass,
1313 .dependencyCount = 0,
1314 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001315 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001316 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001317
1318 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1319 assert(!err);
1320}
1321
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001322static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001323 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001324 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001325 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001326 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001327{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001328 VkShaderModuleCreateInfo moduleCreateInfo;
1329 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001330 VkShader shader;
1331 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001332
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001333
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001334 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1335 moduleCreateInfo.pNext = NULL;
1336
1337 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1338 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001339 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001340
Cody Northrop1fedb212015-05-28 11:27:16 -06001341 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001342 moduleCreateInfo.codeSize = size;
1343 moduleCreateInfo.pCode = code;
1344 moduleCreateInfo.flags = 0;
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);
Cody Northrop1fedb212015-05-28 11:27:16 -06001355 } else {
1356 // Create fake SPV structure to feed GLSL
1357 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001358 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1359 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1360 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001361
1362 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001363 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1364 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1365 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1366 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001367
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001368 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001369 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001370 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001371 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001372
1373 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001374 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001375 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001376 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001377 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001378 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379 return shader;
1380}
1381
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001382char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001383{
1384 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001385 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001386 void *shader_code;
1387
1388 FILE *fp = fopen(filename, "rb");
1389 if (!fp) return NULL;
1390
1391 fseek(fp, 0L, SEEK_END);
1392 size = ftell(fp);
1393
1394 fseek(fp, 0L, SEEK_SET);
1395
1396 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001397 retval = fread(shader_code, size, 1, fp);
1398 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001399
1400 *psize = size;
1401
1402 return shader_code;
1403}
1404
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001405static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406{
Cody Northrop1fedb212015-05-28 11:27:16 -06001407 if (!demo->use_glsl) {
1408 void *vertShaderCode;
1409 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001410
Cody Northrop1fedb212015-05-28 11:27:16 -06001411 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001412
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001413 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001414 vertShaderCode, size);
1415 } else {
1416 static const char *vertShaderText =
1417 "#version 140\n"
1418 "#extension GL_ARB_separate_shader_objects : enable\n"
1419 "#extension GL_ARB_shading_language_420pack : enable\n"
1420 "\n"
1421 "layout(binding = 0) uniform buf {\n"
1422 " mat4 MVP;\n"
1423 " vec4 position[12*3];\n"
1424 " vec4 attr[12*3];\n"
1425 "} ubuf;\n"
1426 "\n"
1427 "layout (location = 0) out vec4 texcoord;\n"
1428 "\n"
1429 "void main() \n"
1430 "{\n"
1431 " texcoord = ubuf.attr[gl_VertexID];\n"
1432 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1433 "\n"
1434 " // GL->VK conventions\n"
1435 " gl_Position.y = -gl_Position.y;\n"
1436 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1437 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001438
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001439 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001440 (const void *) vertShaderText,
1441 strlen(vertShaderText));
1442 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443}
1444
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001445static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001446{
Cody Northrop1fedb212015-05-28 11:27:16 -06001447 if (!demo->use_glsl) {
1448 void *fragShaderCode;
1449 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001450
Cody Northrop1fedb212015-05-28 11:27:16 -06001451 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001452
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001453 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001454 fragShaderCode, size);
1455 } else {
1456 static const char *fragShaderText =
1457 "#version 140\n"
1458 "#extension GL_ARB_separate_shader_objects : enable\n"
1459 "#extension GL_ARB_shading_language_420pack : enable\n"
1460 "layout (binding = 1) uniform sampler2D tex;\n"
1461 "\n"
1462 "layout (location = 0) in vec4 texcoord;\n"
1463 "layout (location = 0) out vec4 uFragColor;\n"
1464 "void main() {\n"
1465 " uFragColor = texture(tex, texcoord.xy);\n"
1466 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001467
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001468 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001469 (const void *) fragShaderText,
1470 strlen(fragShaderText));
1471 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472}
1473
1474static void demo_prepare_pipeline(struct demo *demo)
1475{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001476 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001477 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001478 VkPipelineInputAssemblyStateCreateInfo ia;
1479 VkPipelineRasterStateCreateInfo rs;
1480 VkPipelineColorBlendStateCreateInfo cb;
1481 VkPipelineDepthStencilStateCreateInfo ds;
1482 VkPipelineViewportStateCreateInfo vp;
1483 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001484 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485
1486 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001487 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001488 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001489
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001491 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001492 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001493
1494 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001495 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001496 rs.fillMode = VK_FILL_MODE_SOLID;
1497 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001498 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001499 rs.depthClipEnable = VK_TRUE;
Cody Northrop709c1742015-08-17 11:10:49 -06001500 rs.rasterizerDiscardEnable = VK_FALSE;
1501 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001502
1503 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001504 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1505 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001506 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001507 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001508 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001509 cb.attachmentCount = 1;
1510 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511
Tony Barbour29645d02015-01-16 14:27:35 -07001512 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001513 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001514 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001515
1516 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001517 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001518 ds.depthTestEnable = VK_TRUE;
1519 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001520 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001521 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001522 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1523 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001524 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001525 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001526 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001527
Tony Barbour29645d02015-01-16 14:27:35 -07001528 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001529 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001530 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001531 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001533 // Two stages: vs and fs
1534 pipeline.stageCount = 2;
1535 VkPipelineShaderStageCreateInfo shaderStages[2];
1536 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1537
1538 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1539 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1540 shaderStages[0].shader = demo_prepare_vs(demo);
1541
1542 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1543 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1544 shaderStages[1].shader = demo_prepare_fs(demo);
1545
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001546 memset(&pipelineCache, 0, sizeof(pipelineCache));
1547 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001549 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1550 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001551
1552 pipeline.pVertexInputState = NULL;
1553 pipeline.pInputAssemblyState = &ia;
1554 pipeline.pRasterState = &rs;
1555 pipeline.pColorBlendState = &cb;
1556 pipeline.pMultisampleState = &ms;
1557 pipeline.pViewportState = &vp;
1558 pipeline.pDepthStencilState = &ds;
1559 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001560 pipeline.renderPass = demo->render_pass;
Tony Barbour194bf282015-07-10 15:29:03 -06001561
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001562 pipeline.renderPass = demo->render_pass;
1563
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001564 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565 assert(!err);
1566
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001567 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001568 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001569 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001570 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1571 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001572}
1573
1574static void demo_prepare_dynamic_states(struct demo *demo)
1575{
Tony Barbour155cce82015-07-03 10:33:54 -06001576 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001577 VkDynamicLineWidthStateCreateInfo line_width;
1578 VkDynamicDepthBiasStateCreateInfo depth_bias;
1579 VkDynamicBlendStateCreateInfo blend;
1580 VkDynamicDepthBoundsStateCreateInfo depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -06001581 VkDynamicStencilStateCreateInfo stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001582 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583
Tony Barbour29645d02015-01-16 14:27:35 -07001584 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001585 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001586 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001587 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001588 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001589 viewport.height = (float) demo->height;
1590 viewport.width = (float) demo->width;
1591 viewport.minDepth = (float) 0.0f;
1592 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001593 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001594 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001595 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001596 scissor.extent.width = demo->width;
1597 scissor.extent.height = demo->height;
1598 scissor.offset.x = 0;
1599 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001600 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601
Cody Northrop5e4125d2015-08-26 10:01:32 -06001602 memset(&line_width, 0, sizeof(line_width));
1603 line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
1604 line_width.lineWidth = 1.0;
Cody Northrop709c1742015-08-17 11:10:49 -06001605
Cody Northrop5e4125d2015-08-26 10:01:32 -06001606 memset(&depth_bias, 0, sizeof(depth_bias));
1607 depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
1608 depth_bias.depthBias = 0.0f;
1609 depth_bias.depthBiasClamp = 0.0f;
1610 depth_bias.slopeScaledDepthBias = 0.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001611
Cody Northrop5e4125d2015-08-26 10:01:32 -06001612 memset(&blend, 0, sizeof(blend));
1613 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
1614 blend.blendConst[0] = 1.0f;
1615 blend.blendConst[1] = 1.0f;
1616 blend.blendConst[2] = 1.0f;
1617 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001618
Cody Northrop5e4125d2015-08-26 10:01:32 -06001619 memset(&depth_bounds, 0, sizeof(depth_bounds));
1620 depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
1621 depth_bounds.minDepthBounds = 0.0f;
1622 depth_bounds.maxDepthBounds = 1.0f;
Cody Northrop36a642f2015-08-18 15:21:16 -06001623
1624 memset(&stencil, 0, sizeof(stencil));
1625 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
1626 stencil.stencilReference = 0;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001627 stencil.stencilCompareMask = 0xff;
Cody Northrop36a642f2015-08-18 15:21:16 -06001628 stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001629
Cody Northrop5e4125d2015-08-26 10:01:32 -06001630 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631 assert(!err);
1632
Cody Northrop5e4125d2015-08-26 10:01:32 -06001633 err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width);
Cody Northrop709c1742015-08-17 11:10:49 -06001634 assert(!err);
1635
Cody Northrop5e4125d2015-08-26 10:01:32 -06001636 err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001637 assert(!err);
1638
Cody Northrop5e4125d2015-08-26 10:01:32 -06001639 err = vkCreateDynamicBlendState(demo->device,
1640 &blend, &demo->dynamic_blend);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001641 assert(!err);
1642
Cody Northrop5e4125d2015-08-26 10:01:32 -06001643 err = vkCreateDynamicDepthBoundsState(demo->device,
1644 &depth_bounds, &demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001645 assert(!err);
1646
1647 err = vkCreateDynamicStencilState(demo->device,
1648 &stencil, &stencil, &demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001649 assert(!err);
1650}
1651
Chia-I Wu63ea9262015-03-26 13:14:16 +08001652static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001653{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001654 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001655 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001656 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001657 .count = 1,
1658 },
1659 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001660 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001661 .count = DEMO_TEXTURE_COUNT,
1662 },
1663 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001664 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001665 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001666 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001667 .poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT,
1668 .maxSets = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001669 .count = 2,
1670 .pTypeCount = type_counts,
1671 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001672 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001673
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001674 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001675 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001676 assert(!err);
1677}
1678
1679static void demo_prepare_descriptor_set(struct demo *demo)
1680{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001681 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1682 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001683 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001684 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001685
Mike Stroyanebae8322015-04-17 12:36:38 -06001686 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001687 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001688 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001689 &demo->desc_set);
1690 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001691
Chia-I Wuae721ba2015-05-25 16:27:55 +08001692 memset(&tex_descs, 0, sizeof(tex_descs));
1693 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1694 tex_descs[i].sampler = demo->textures[i].sampler;
1695 tex_descs[i].imageView = demo->textures[i].view;
1696 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1697 }
1698
1699 memset(&writes, 0, sizeof(writes));
1700
1701 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1702 writes[0].destSet = demo->desc_set;
1703 writes[0].count = 1;
1704 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1705 writes[0].pDescriptors = &demo->uniform_data.desc;
1706
1707 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1708 writes[1].destSet = demo->desc_set;
1709 writes[1].destBinding = 1;
1710 writes[1].count = DEMO_TEXTURE_COUNT;
1711 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1712 writes[1].pDescriptors = tex_descs;
1713
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001714 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001715}
1716
Chia-I Wuf973e322015-07-08 13:34:24 +08001717static void demo_prepare_framebuffers(struct demo *demo)
1718{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001719 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001720 attachments[1] = demo->depth.view;
1721
Chia-I Wuf973e322015-07-08 13:34:24 +08001722 const VkFramebufferCreateInfo fb_info = {
1723 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1724 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001725 .renderPass = demo->render_pass,
1726 .attachmentCount = 2,
1727 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001728 .width = demo->width,
1729 .height = demo->height,
1730 .layers = 1,
1731 };
1732 VkResult U_ASSERT_ONLY err;
1733 uint32_t i;
1734
1735 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001736 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001737 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1738 assert(!err);
1739 }
1740}
1741
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742static void demo_prepare(struct demo *demo)
1743{
Cody Northrop91e221b2015-07-09 18:08:32 -06001744 VkResult U_ASSERT_ONLY err;
1745
1746 const VkCmdPoolCreateInfo cmd_pool_info = {
1747 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1748 .pNext = NULL,
1749 .queueFamilyIndex = demo->graphics_queue_node_index,
1750 .flags = 0,
1751 };
1752 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1753 assert(!err);
1754
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001755 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001756 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001758 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001759 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001760 .flags = 0,
1761 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762
1763 demo_prepare_buffers(demo);
1764 demo_prepare_depth(demo);
1765 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001766 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001767
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001768 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001769 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001770 demo_prepare_pipeline(demo);
1771 demo_prepare_dynamic_states(demo);
1772
Ian Elliotta0781972015-08-21 15:09:33 -06001773 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001774 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001775 assert(!err);
1776 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001777
Chia-I Wu63ea9262015-03-26 13:14:16 +08001778 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001779 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001780
Chia-I Wuf973e322015-07-08 13:34:24 +08001781 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001782
Ian Elliotta0781972015-08-21 15:09:33 -06001783 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001784 demo->current_buffer = i;
1785 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1786 }
1787
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001788 /*
1789 * Prepare functions above may generate pipeline commands
1790 * that need to be flushed before beginning the render loop.
1791 */
1792 demo_flush_init_cmd(demo);
1793
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001794 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001795 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796}
1797
David Pinedo2bb7c932015-06-18 17:03:14 -06001798static void demo_cleanup(struct demo *demo)
1799{
1800 uint32_t i;
1801
1802 demo->prepared = false;
1803
Tony Barbour155cce82015-07-03 10:33:54 -06001804 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1805 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1806 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001807 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001808 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001809
Cody Northrop5e4125d2015-08-26 10:01:32 -06001810 vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport);
1811 vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width);
1812 vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias);
1813 vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend);
1814 vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001815 vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001816
Tony Barbour155cce82015-07-03 10:33:54 -06001817 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001818 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001819 vkDestroyRenderPass(demo->device, demo->render_pass);
1820 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1821 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001822
1823 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001824 vkDestroyImageView(demo->device, demo->textures[i].view);
1825 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001826 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001827 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001828 }
Ian Elliotta0781972015-08-21 15:09:33 -06001829 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001830
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001831 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbour155cce82015-07-03 10:33:54 -06001832 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001833 vkFreeMemory(demo->device, demo->depth.mem);
1834
Tony Barbour155cce82015-07-03 10:33:54 -06001835 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001836 vkFreeMemory(demo->device, demo->uniform_data.mem);
1837
Ian Elliotta0781972015-08-21 15:09:33 -06001838 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001839 vkDestroyImageView(demo->device, demo->buffers[i].view);
Tony Barbour155cce82015-07-03 10:33:54 -06001840 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001841 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001842 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001843
Cody Northrop91e221b2015-07-09 18:08:32 -06001844 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001845 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001846 if (demo->validate) {
1847 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1848 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001849 vkDestroyInstance(demo->inst);
1850
1851#ifndef _WIN32
1852 xcb_destroy_window(demo->connection, demo->window);
1853 xcb_disconnect(demo->connection);
1854#endif // _WIN32
1855}
1856
1857// On MS-Windows, make this a global, so it's available to WndProc()
1858struct demo demo;
1859
Ian Elliott639ca472015-04-16 15:23:05 -06001860#ifdef _WIN32
1861static void demo_run(struct demo *demo)
1862{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001863 if (!demo->prepared)
1864 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001865 // Wait for work to finish before updating MVP.
1866 vkDeviceWaitIdle(demo->device);
1867 demo_update_data_buffer(demo);
1868
1869 demo_draw(demo);
1870
1871 // Wait for work to finish before updating MVP.
1872 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001873
David Pinedo2bb7c932015-06-18 17:03:14 -06001874 demo->curFrame++;
1875
1876 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1877 {
1878 demo->quit=true;
1879 demo_cleanup(demo);
1880 ExitProcess(0);
1881 }
1882
1883}
Ian Elliott639ca472015-04-16 15:23:05 -06001884
1885// MS-Windows event handling function:
1886LRESULT CALLBACK WndProc(HWND hWnd,
1887 UINT uMsg,
1888 WPARAM wParam,
1889 LPARAM lParam)
1890{
Ian Elliott639ca472015-04-16 15:23:05 -06001891 switch(uMsg)
1892 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001893 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001894 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001895 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001896 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001897 demo_run(&demo);
1898 return 0;
1899 default:
1900 break;
1901 }
1902 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1903}
1904
1905static void demo_create_window(struct demo *demo)
1906{
1907 WNDCLASSEX win_class;
1908
1909 // Initialize the window class structure:
1910 win_class.cbSize = sizeof(WNDCLASSEX);
1911 win_class.style = CS_HREDRAW | CS_VREDRAW;
1912 win_class.lpfnWndProc = WndProc;
1913 win_class.cbClsExtra = 0;
1914 win_class.cbWndExtra = 0;
1915 win_class.hInstance = demo->connection; // hInstance
1916 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1917 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1918 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1919 win_class.lpszMenuName = NULL;
1920 win_class.lpszClassName = demo->name;
1921 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1922 // Register window class:
1923 if (!RegisterClassEx(&win_class)) {
1924 // It didn't work, so try to give a useful error:
1925 printf("Unexpected error trying to start the application!\n");
1926 fflush(stdout);
1927 exit(1);
1928 }
1929 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001930 RECT wr = { 0, 0, demo->width, demo->height };
1931 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001932 demo->window = CreateWindowEx(0,
1933 demo->name, // class name
1934 demo->name, // app name
1935 WS_OVERLAPPEDWINDOW | // window style
1936 WS_VISIBLE |
1937 WS_SYSMENU,
1938 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001939 wr.right-wr.left, // width
1940 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001941 NULL, // handle to parent
1942 NULL, // handle to menu
1943 demo->connection, // hInstance
1944 NULL); // no extra parameters
1945 if (!demo->window) {
1946 // It didn't work, so try to give a useful error:
1947 printf("Cannot create a window in which to draw!\n");
1948 fflush(stdout);
1949 exit(1);
1950 }
1951}
1952#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953static void demo_handle_event(struct demo *demo,
1954 const xcb_generic_event_t *event)
1955{
Piers Daniell735ee532015-02-23 16:23:13 -07001956 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001957 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001958 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001959 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001960 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001961 case XCB_CLIENT_MESSAGE:
1962 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1963 (*demo->atom_wm_delete_window).atom) {
1964 demo->quit = true;
1965 }
1966 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001967 case XCB_KEY_RELEASE:
1968 {
1969 const xcb_key_release_event_t *key =
1970 (const xcb_key_release_event_t *) event;
1971
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001972 switch (key->detail) {
1973 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001974 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001975 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001976 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001977 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001978 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001979 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001980 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001981 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001982 case 0x41:
1983 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001984 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001985 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001986 }
1987 break;
1988 default:
1989 break;
1990 }
1991}
1992
1993static void demo_run(struct demo *demo)
1994{
1995 xcb_flush(demo->connection);
1996
1997 while (!demo->quit) {
1998 xcb_generic_event_t *event;
1999
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002000 if (demo->pause) {
2001 event = xcb_wait_for_event(demo->connection);
2002 } else {
2003 event = xcb_poll_for_event(demo->connection);
2004 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002005 if (event) {
2006 demo_handle_event(demo, event);
2007 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002008 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002009
2010 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002011 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002012 demo_update_data_buffer(demo);
2013
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002014 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002015
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002016 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002017 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002018 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002019 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002020 demo->quit = true;
2021
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002022 }
2023}
2024
2025static void demo_create_window(struct demo *demo)
2026{
2027 uint32_t value_mask, value_list[32];
2028
2029 demo->window = xcb_generate_id(demo->connection);
2030
2031 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2032 value_list[0] = demo->screen->black_pixel;
2033 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2034 XCB_EVENT_MASK_EXPOSURE;
2035
2036 xcb_create_window(demo->connection,
2037 XCB_COPY_FROM_PARENT,
2038 demo->window, demo->screen->root,
2039 0, 0, demo->width, demo->height, 0,
2040 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2041 demo->screen->root_visual,
2042 value_mask, value_list);
2043
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002044 /* Magic code that will send notification when window is destroyed */
2045 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2046 "WM_PROTOCOLS");
2047 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2048
2049 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2050 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2051
2052 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2053 demo->window, (*reply).atom, 4, 32, 1,
2054 &(*demo->atom_wm_delete_window).atom);
2055 free(reply);
2056
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002058
2059 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2060 const uint32_t coords[] = {100, 100};
2061 xcb_configure_window(demo->connection, demo->window,
2062 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002063}
Ian Elliott639ca472015-04-16 15:23:05 -06002064#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002065
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002066/*
2067 * Return 1 (true) if all layer names specified in check_names
2068 * can be found in given layer properties.
2069 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002070static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002071 uint32_t layer_count, VkLayerProperties *layers)
2072{
2073 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002074 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002075 for (uint32_t j = 0; j < layer_count; j++) {
2076 if (!strcmp(check_names[i], layers[j].layerName)) {
2077 found = 1;
2078 }
2079 }
2080 if (!found) {
2081 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2082 return 0;
2083 }
2084 }
2085 return 1;
2086}
2087
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002088static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002090 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002091 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002092 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002093 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002094 VkLayerProperties *instance_layers;
2095 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002096 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002097 uint32_t instance_layer_count = 0;
2098 uint32_t enabled_extension_count = 0;
2099 uint32_t enabled_layer_count = 0;
2100
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002101 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002102 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002103 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002104 "ObjectTracker",
2105 "DrawState",
2106 "ParamChecker",
2107 "ShaderChecker",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002108 "DeviceLimits",
2109 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002110 };
2111
2112 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002113 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002114 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002115 "ObjectTracker",
2116 "DrawState",
2117 "ParamChecker",
2118 "ShaderChecker",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002119 "DeviceLimits",
2120 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002121 };
2122
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002123 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002124 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002125 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002126 assert(!err);
2127
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002128 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002129 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002130 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002131
2132 if (demo->validate) {
2133 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2134 instance_layer_count, instance_layers);
2135 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002136 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002137 "required validation layer.\n\n"
2138 "Please look at the Getting Started guide for additional "
2139 "information.\n",
2140 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002141 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002142 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002143 }
2144
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002145 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002146 assert(!err);
2147
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002148 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002149 memset(extension_names, 0, sizeof(extension_names));
2150 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002151 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002152 assert(!err);
2153 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002154 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002155 WSIextFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002156 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002157 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002158 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002159 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002160 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002161 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002162 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002163 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002164 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002165 if (!WSIextFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002166 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002167 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002168 "Vulkan installable client driver (ICD) installed?\nPlease "
2169 "look at the Getting Started guide for additional "
2170 "information.\n",
2171 "vkCreateInstance Failure");
2172 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002173 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002174 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002175 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002176 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002177 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002178 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002179 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002180 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002181 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002182 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002183 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002184 .pNext = NULL,
2185 .pAppInfo = &app,
2186 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002187 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002188 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002189 .extensionCount = enabled_extension_count,
2190 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002191 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002192
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002193 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002194
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002195 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002196 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2197 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002198 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002199 "additional information.\n",
2200 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002201 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002202 ERR_EXIT("Cannot find a specified extension library"
2203 ".\nMake sure your layers path is set appropriately\n",
2204 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002205 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002206 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2207 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002208 "the Getting Started guide for additional information.\n",
2209 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002210 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002211
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002212 free(instance_layers);
2213 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002214
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002215 /* Make initial call to query gpu_count, then second call for gpu info*/
2216 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2217 assert(!err && gpu_count > 0);
2218 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2219 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2220 assert(!err);
2221 /* For cube demo we just grab the first physical device */
2222 demo->gpu = physical_devices[0];
2223 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002224
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002225 /* Look for validation layers */
2226 validation_found = 0;
2227 enabled_layer_count = 0;
2228 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002229 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002230 assert(!err);
2231
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002232 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002233 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002234 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002235
2236 if (demo->validate) {
2237 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2238 device_layer_count, device_layers);
2239 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002240 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002241 "a required validation layer.\n\n"
2242 "Please look at the Getting Started guide for additional "
2243 "information.\n",
2244 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002245 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002246 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002247 }
2248
2249 uint32_t device_extension_count = 0;
2250 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002251 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002252 demo->gpu, NULL, &device_extension_count, NULL);
2253 assert(!err);
2254
2255 WSIextFound = 0;
2256 enabled_extension_count = 0;
2257 memset(extension_names, 0, sizeof(extension_names));
2258 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002259 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002260 demo->gpu, NULL, &device_extension_count, device_extensions);
2261 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002262
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002263 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002264 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002265 WSIextFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002266 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002267 }
2268 assert(enabled_extension_count < 64);
2269 }
2270 if (!WSIextFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002271 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002272 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002273 "Vulkan installable client driver (ICD) installed?\nPlease "
2274 "look at the Getting Started guide for additional "
2275 "information.\n",
2276 "vkCreateInstance Failure");
2277 }
2278
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002279 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002280 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2281 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002282 if (!demo->dbgCreateMsgCallback) {
2283 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2284 "vkGetProcAddr Failure");
2285 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002286 if (!demo->dbgDestroyMsgCallback) {
2287 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2288 "vkGetProcAddr Failure");
2289 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002290 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2291 if (!demo->dbgBreakCallback) {
2292 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2293 "vkGetProcAddr Failure");
2294 }
2295
2296 PFN_vkDbgMsgCallback callback;
2297
2298 if (!demo->use_break) {
2299 callback = dbgFunc;
2300 } else {
2301 callback = demo->dbgBreakCallback;
2302 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002303 err = demo->dbgCreateMsgCallback(
2304 demo->inst,
2305 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002306 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002307 &demo->msg_callback);
2308 switch (err) {
2309 case VK_SUCCESS:
2310 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002311 case VK_ERROR_OUT_OF_HOST_MEMORY:
2312 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2313 "dbgCreateMsgCallback Failure");
2314 break;
2315 default:
2316 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2317 "dbgCreateMsgCallback Failure");
2318 break;
2319 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002320 }
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002321 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
2322 assert(!err);
2323
2324 /* Call with NULL data to get count */
2325 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
2326 assert(!err);
2327 assert(demo->queue_count >= 1);
2328
2329 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2330 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
2331 assert(!err);
2332 // Find a queue that supports gfx
2333 uint32_t gfx_queue_idx = 0;
2334 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2335 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2336 break;
2337 }
2338 assert(gfx_queue_idx < demo->queue_count);
2339 const VkDeviceQueueCreateInfo queue = {
2340 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2341 .pNext = NULL,
2342 .queueFamilyIndex = gfx_queue_idx,
2343 .queueCount = 1,
2344 };
2345
2346 VkDeviceCreateInfo device = {
2347 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2348 .pNext = NULL,
2349 .queueRecordCount = 1,
2350 .pRequestedQueues = &queue,
2351 .layerCount = enabled_layer_count,
2352 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
2353 .extensionCount = enabled_extension_count,
2354 .ppEnabledExtensionNames = (const char *const*) extension_names,
2355 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002356
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002357 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002358 assert(!err);
2359
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002360 free(device_layers);
2361
Ian Elliotta0781972015-08-21 15:09:33 -06002362 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2363 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2364 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2365 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2366 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002367 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2368 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2369 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2370 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002371}
2372
2373static void demo_init_vk_wsi(struct demo *demo)
2374{
2375 VkResult err;
2376 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002377
Ian Elliottaaae5352015-07-06 14:27:58 -06002378 // Construct the WSI surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002379 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002380 demo->surface_description.pNext = NULL;
2381#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002382 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002383 demo->surface_description.pPlatformHandle = demo->connection;
2384 demo->surface_description.pPlatformWindow = demo->window;
2385#else // _WIN32
2386 demo->platform_handle_xcb.connection = demo->connection;
2387 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002388 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002389 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2390 demo->surface_description.pPlatformWindow = &demo->window;
2391#endif // _WIN32
2392
2393 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002394 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2395 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002396 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2397 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002398 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002399 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002400
2401 // Search for a graphics and a present queue in the array of queue
2402 // families, try to find one that supports both
2403 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2404 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002405 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002406 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2407 if (graphicsQueueNodeIndex == UINT32_MAX) {
2408 graphicsQueueNodeIndex = i;
2409 }
2410
2411 if (supportsPresent[i] == VK_TRUE) {
2412 graphicsQueueNodeIndex = i;
2413 presentQueueNodeIndex = i;
2414 break;
2415 }
2416 }
2417 }
2418 if (presentQueueNodeIndex == UINT32_MAX) {
2419 // If didn't find a queue that supports both graphics and present, then
2420 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002421 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002422 if (supportsPresent[i] == VK_TRUE) {
2423 presentQueueNodeIndex = i;
2424 break;
2425 }
2426 }
2427 }
2428 free(supportsPresent);
2429
2430 // Generate error if could not find both a graphics and a present queue
2431 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2432 ERR_EXIT("Could not find a graphics and a present queue\n",
2433 "WSI Initialization Failure");
2434 }
2435
2436 // TODO: Add support for separate queues, including presentation,
2437 // synchronization, and appropriate tracking for QueueSubmit
2438 // While it is possible for an application to use a separate graphics and a
2439 // present queues, this demo program assumes it is only using one:
2440 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2441 ERR_EXIT("Could not find a common graphics and a present queue\n",
2442 "WSI Initialization Failure");
2443 }
2444
2445 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002446
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002447 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002448 0, &demo->queue);
2449 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002450
Ian Elliottaaae5352015-07-06 14:27:58 -06002451 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002452 uint32_t formatCount;
Ian Elliotta0781972015-08-21 15:09:33 -06002453 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2454 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002455 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002456 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002457 VkSurfaceFormatKHR *surfFormats =
2458 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2459 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2460 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002461 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002462 assert(!err);
2463 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2464 // the surface has no preferred format. Otherwise, at least one
2465 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002466 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2467 {
2468 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2469 }
2470 else
2471 {
2472 assert(formatCount >= 1);
2473 demo->format = surfFormats[0].format;
2474 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002475 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002476
David Pinedo2bb7c932015-06-18 17:03:14 -06002477 demo->quit = false;
2478 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002479
2480 // Get Memory information and properties
2481 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2482 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002483}
2484
2485static void demo_init_connection(struct demo *demo)
2486{
Ian Elliott639ca472015-04-16 15:23:05 -06002487#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002488 const xcb_setup_t *setup;
2489 xcb_screen_iterator_t iter;
2490 int scr;
2491
2492 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002493 if (demo->connection == NULL) {
2494 printf("Cannot find a compatible Vulkan installable client driver "
2495 "(ICD).\nExiting ...\n");
2496 fflush(stdout);
2497 exit(1);
2498 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002499
2500 setup = xcb_get_setup(demo->connection);
2501 iter = xcb_setup_roots_iterator(setup);
2502 while (scr-- > 0)
2503 xcb_screen_next(&iter);
2504
2505 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002506#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002507}
2508
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002509static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002510{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002511 vec3 eye = {0.0f, 3.0f, 5.0f};
2512 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002513 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002514
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002515 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002516 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002517
Piers Daniell735ee532015-02-23 16:23:13 -07002518 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002519 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002520 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002521 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002522 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002523 if (strcmp(argv[i], "--use_glsl") == 0) {
2524 demo->use_glsl = true;
2525 continue;
2526 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002527 if (strcmp(argv[i], "--break") == 0) {
2528 demo->use_break = true;
2529 continue;
2530 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002531 if (strcmp(argv[i], "--validate") == 0) {
2532 demo->validate = true;
2533 continue;
2534 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002535 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002536 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002537 i < argc-1 &&
2538 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2539 demo->frameCount >= 0)
2540 {
2541 i++;
2542 continue;
2543 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002544
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002545 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002546 fflush(stderr);
2547 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002548 }
2549
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002550 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002551 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002552
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002553 demo->width = 500;
2554 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002555
2556 demo->spin_angle = 0.01f;
2557 demo->spin_increment = 0.01f;
2558 demo->pause = false;
2559
Piers Daniell735ee532015-02-23 16:23:13 -07002560 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002561 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2562 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002563}
2564
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002565
Ian Elliott639ca472015-04-16 15:23:05 -06002566#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002567extern int __getmainargs(
2568 int * _Argc,
2569 char *** _Argv,
2570 char *** _Env,
2571 int _DoWildCard,
2572 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002573
Ian Elliotta748eaf2015-04-28 15:50:36 -06002574int WINAPI WinMain(HINSTANCE hInstance,
2575 HINSTANCE hPrevInstance,
2576 LPSTR pCmdLine,
2577 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002578{
2579 MSG msg; // message
2580 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002581 int argc;
2582 char** argv;
2583 char** env;
2584 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002585
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002586 __getmainargs(&argc,&argv,&env,0,&new_mode);
2587
2588 demo_init(&demo, argc, argv);
2589 demo.connection = hInstance;
2590 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002591 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002592 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002593
2594 demo_prepare(&demo);
2595
2596 done = false; //initialize loop condition variable
2597 /* main message loop*/
2598 while(!done)
2599 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002600 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002601 if (msg.message == WM_QUIT) //check for a quit message
2602 {
2603 done = true; //if found, quit app
2604 }
2605 else
2606 {
2607 /* Translate and dispatch to event queue*/
2608 TranslateMessage(&msg);
2609 DispatchMessage(&msg);
2610 }
2611 }
2612
2613 demo_cleanup(&demo);
2614
Tony Barbourf9921732015-04-22 11:36:22 -06002615 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002616}
2617#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002618int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002619{
2620 struct demo demo;
2621
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002622 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002623 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002624 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002625
2626 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002627 demo_run(&demo);
2628
2629 demo_cleanup(&demo);
2630
2631 return 0;
2632}
Ian Elliott639ca472015-04-16 15:23:05 -06002633#endif // _WIN32