blob: 5778df310a7d3b76339106c77bbad6f7b2ce047e [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 Elliottaaae5352015-07-06 14:27:58 -060040#include <vk_wsi_swapchain.h>
41#include <vk_wsi_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
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060046#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060047#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048
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[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600112 "lunarg-logo-256x256-solid.png"
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 Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600255void 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")){
274 return;
275 }
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 {
278 return;
279 }
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);
288}
289
Ian Elliottaaae5352015-07-06 14:27:58 -0600290typedef struct _SwapChainBuffers {
291 VkImage image;
292 VkCmdBuffer cmd;
293 VkAttachmentView view;
294} SwapChainBuffers;
295
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600296struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600297#ifdef _WIN32
298#define APP_NAME_STR_LEN 80
299 HINSTANCE connection; // hInstance - Windows Instance
300 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
301 HWND window; // hWnd - window handle
302#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600303 xcb_connection_t *connection;
304 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800305 xcb_window_t window;
306 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliottaaae5352015-07-06 14:27:58 -0600307 VkPlatformHandleXcbWSI platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600308#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600309 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700310 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600311 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600313 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600314 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkDevice device;
316 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700317 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600318 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600319 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600320 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkFormat format;
Ian Elliott8528eff2015-08-07 15:56:59 -0600325 VkColorSpaceWSI color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Ian Elliottaaae5352015-07-06 14:27:58 -0600327 PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI;
Ian Elliott8528eff2015-08-07 15:56:59 -0600328 PFN_vkGetSurfacePropertiesWSI fpGetSurfacePropertiesWSI;
329 PFN_vkGetSurfaceFormatsWSI fpGetSurfaceFormatsWSI;
330 PFN_vkGetSurfacePresentModesWSI fpGetSurfacePresentModesWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600331 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
332 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
Ian Elliott8528eff2015-08-07 15:56:59 -0600333 PFN_vkGetSwapChainImagesWSI fpGetSwapChainImagesWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600334 PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600335 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600336 VkSurfaceDescriptionWindowWSI surface_description;
Ian Elliott8528eff2015-08-07 15:56:59 -0600337 uint32_t swapChainImageCount;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800338 VkSwapChainWSI swap_chain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600339 SwapChainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600340
Ian Elliottaaae5352015-07-06 14:27:58 -0600341 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600342
343 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600344 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600345
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600346 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500347 VkDeviceMemory mem;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800348 VkAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349 } depth;
350
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600351 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352
353 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600354 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500355 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800357 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600358 } uniform_data;
359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500361 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600363 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800364 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600365 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366
Cody Northrop5e4125d2015-08-26 10:01:32 -0600367 VkDynamicViewportState dynamic_viewport;
368 VkDynamicLineWidthState dynamic_line_width;
369 VkDynamicDepthBiasState dynamic_depth_bias;
370 VkDynamicBlendState dynamic_blend;
371 VkDynamicDepthBoundsState dynamic_depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -0600372 VkDynamicStencilState dynamic_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600374 mat4x4 projection_matrix;
375 mat4x4 view_matrix;
376 mat4x4 model_matrix;
377
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600378 float spin_angle;
379 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600380 bool pause;
381
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600382 VkShaderModule vert_shader_module;
383 VkShaderModule frag_shader_module;
384
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 VkDescriptorPool desc_pool;
386 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800387
Chia-I Wuf973e322015-07-08 13:34:24 +0800388 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
389
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600391 int32_t curFrame;
392 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600393 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600394 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600395 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600396 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600397 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600398 VkDbgMsgCallback msg_callback;
399
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600400 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600401 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600402};
403
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600404static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
405{
406 // Search memtypes to find first index with those properties
407 for (uint32_t i = 0; i < 32; i++) {
408 if ((typeBits & 1) == 1) {
409 // Type is available, does it match user properties?
410 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
411 *typeIndex = i;
412 return VK_SUCCESS;
413 }
414 }
415 typeBits >>= 1;
416 }
417 // No memory types matched, return failure
418 return VK_UNSUPPORTED;
419}
420
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421static void demo_flush_init_cmd(struct demo *demo)
422{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600423 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600425 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600426 return;
427
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600428 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600429 assert(!err);
430
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600431 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600432 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433
Tony Barbour155cce82015-07-03 10:33:54 -0600434 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435 assert(!err);
436
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 assert(!err);
439
Tony Barbour155cce82015-07-03 10:33:54 -0600440 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600441 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600442}
443
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444static void demo_set_image_layout(
445 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600446 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400447 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600448 VkImageLayout old_image_layout,
449 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600451 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600454 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600456 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600457 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800458 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459 .flags = 0,
460 };
461
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600462 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600463 assert(!err);
464
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600465 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600466 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600467 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600468 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600470 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600471 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600472 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600475 }
476
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600477 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479 .pNext = NULL,
480 .outputMask = 0,
481 .inputMask = 0,
482 .oldLayout = old_image_layout,
483 .newLayout = new_image_layout,
484 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400485 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600486 };
487
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600488 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600489 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600490 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491 }
492
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600493 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600495 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 }
497
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600498 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499
Tony Barbour50bbca42015-06-29 16:20:35 -0600500 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
501 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600503 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600504}
505
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600506static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507{
Chia-I Wuf973e322015-07-08 13:34:24 +0800508 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600509 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600510 .pNext = NULL,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600511 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600512 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600513 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600514 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700515 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800516 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600517 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
518 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800519 };
520 const VkRenderPassBeginInfo rp_begin = {
521 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
522 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800523 .renderPass = demo->render_pass,
524 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800525 .renderArea.offset.x = 0,
526 .renderArea.offset.y = 0,
527 .renderArea.extent.width = demo->width,
528 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600529 .clearValueCount = 2,
530 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700531 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800532 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600534 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535 assert(!err);
536
Chia-I Wua74c5b22015-07-07 11:50:03 +0800537 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
538
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600539 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600540 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600541 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600542 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543
Cody Northrop5e4125d2015-08-26 10:01:32 -0600544 vkCmdBindDynamicViewportState(cmd_buf, demo->dynamic_viewport);
545 vkCmdBindDynamicLineWidthState(cmd_buf, demo->dynamic_line_width);
546 vkCmdBindDynamicDepthBiasState(cmd_buf, demo->dynamic_depth_bias);
547 vkCmdBindDynamicBlendState(cmd_buf, demo->dynamic_blend);
548 vkCmdBindDynamicDepthBoundsState(cmd_buf, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -0600549 vkCmdBindDynamicStencilState(cmd_buf, demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600551 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800552 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600554 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555 assert(!err);
556}
557
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600558
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600559void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600560{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600561 mat4x4 MVP, Model, VP;
562 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600563 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600564 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600566 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600568 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600569 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700570 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600571 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600572
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500573 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600574 assert(!err);
575
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600576 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600577
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500578 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579 assert(!err);
580}
581
582static void demo_draw(struct demo *demo)
583{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600584 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600585 VkSemaphore presentCompleteSemaphore;
586 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
587 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
588 .pNext = NULL,
589 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
590 };
Tony Barbour155cce82015-07-03 10:33:54 -0600591 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592
Ian Elliottaaae5352015-07-06 14:27:58 -0600593 err = vkCreateSemaphore(demo->device,
594 &presentCompleteSemaphoreCreateInfo,
595 &presentCompleteSemaphore);
596 assert(!err);
597
598 // Get the index of the next available swapchain image:
599 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
600 UINT64_MAX,
601 presentCompleteSemaphore,
602 &demo->current_buffer);
603 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
604 // return codes
605 assert(!err);
606
607 // Wait for the present complete semaphore to be signaled to ensure
608 // that the image won't be rendered to until the presentation
609 // engine has fully released ownership to the application, and it is
610 // okay to render to the image.
611 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
612
613// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600614 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600615 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 assert(!err);
617
Ian Elliottaaae5352015-07-06 14:27:58 -0600618 VkPresentInfoWSI present = {
Ian Elliott8528eff2015-08-07 15:56:59 -0600619 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
Ian Elliottaaae5352015-07-06 14:27:58 -0600620 .pNext = NULL,
621 .swapChainCount = 1,
622 .swapChains = &demo->swap_chain,
623 .imageIndices = &demo->current_buffer,
624 };
625
626// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600627 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600628 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
629 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600630 assert(!err);
631
Chia-I Wucbb564e2015-04-16 22:02:10 +0800632 err = vkQueueWaitIdle(demo->queue);
633 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600634
635 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
636 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600637}
638
639static void demo_prepare_buffers(struct demo *demo)
640{
Ian Elliottaaae5352015-07-06 14:27:58 -0600641 VkResult U_ASSERT_ONLY err;
642
643 // Check the surface properties and formats
Ian Elliott8528eff2015-08-07 15:56:59 -0600644 VkSurfacePropertiesWSI surfProperties;
645 err = demo->fpGetSurfacePropertiesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600646 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600647 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600648 assert(!err);
649
Ian Elliott8528eff2015-08-07 15:56:59 -0600650 uint32_t presentModeCount;
651 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600652 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600653 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600654 assert(!err);
Ian Elliott8528eff2015-08-07 15:56:59 -0600655 VkPresentModeWSI *presentModes =
656 (VkPresentModeWSI *)malloc(presentModeCount * sizeof(VkPresentModeWSI));
657 assert(presentModes);
658 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600659 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600660 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600661 assert(!err);
662
663 VkExtent2D swapChainExtent;
664 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600665 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600666 {
667 // If the surface size is undefined, the size is set to
668 // the size of the images requested.
669 swapChainExtent.width = demo->width;
670 swapChainExtent.height = demo->height;
671 }
672 else
673 {
674 // If the surface size is defined, the swap chain size must match
Ian Elliott8528eff2015-08-07 15:56:59 -0600675 swapChainExtent = surfProperties.currentExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600676 }
677
678 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600679 // tearing mode. If not, try IMMEDIATE which will usually be available,
680 // and is fastest (though it tears). If not, fall back to FIFO which is
681 // always available.
682 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600683 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott8528eff2015-08-07 15:56:59 -0600684 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_WSI) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600685 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
686 break;
687 }
Ian Elliott3ebce342015-07-27 13:53:11 -0600688 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
Ian Elliott8528eff2015-08-07 15:56:59 -0600689 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
Ian Elliott3ebce342015-07-27 13:53:11 -0600690 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
691 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600692 }
693
Ian Elliott2d47da92015-07-13 12:20:56 -0600694#define WORK_AROUND_CODE
695#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600696 // After the proper code was created, other parts of this demo were
697 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
698 // images, etc. Live with that for now.
699 // TODO: Rework this demo code to live with the number of buffers returned
700 // by vkCreateSwapChainWSI().
Ian Elliott2d47da92015-07-13 12:20:56 -0600701 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
702#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600703 // Determine the number of VkImage's to use in the swap chain (we desire to
704 // own only 1 image at a time, besides the images being displayed and
705 // queued for display):
Ian Elliott8528eff2015-08-07 15:56:59 -0600706 uint32_t desiredNumberOfSwapChainImages = surfProperties.minImageCount + 1;
707 if ((surfProperties.maxImageCount > 0) &&
708 (desiredNumberOfSwapChainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600709 {
710 // Application must settle for fewer images than desired:
Ian Elliott8528eff2015-08-07 15:56:59 -0600711 desiredNumberOfSwapChainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600713#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600714
715 VkSurfaceTransformFlagBitsWSI preTransform;
Ian Elliott8528eff2015-08-07 15:56:59 -0600716 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
718 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600719 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600720 }
721
Chia-I Wucbb564e2015-04-16 22:02:10 +0800722 const VkSwapChainCreateInfoWSI swap_chain = {
723 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
724 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600725 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
726 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800727 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600728 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800729 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600730 .width = swapChainExtent.width,
731 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600732 },
Ian Elliottaaae5352015-07-06 14:27:58 -0600733 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800734 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600735 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
736 .queueFamilyCount = 0,
737 .pQueueFamilyIndices = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600738 .presentMode = swapChainPresentMode,
739 .oldSwapChain.handle = 0,
740 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600741 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600742 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743
Jon Ashburn0b85d052015-05-21 18:13:33 -0600744 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800745 assert(!err);
746
Ian Elliott8528eff2015-08-07 15:56:59 -0600747 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
748 &demo->swapChainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600749 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800750
Ian Elliott8528eff2015-08-07 15:56:59 -0600751 VkImage* swapChainImages =
752 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliottaaae5352015-07-06 14:27:58 -0600753 assert(swapChainImages);
Ian Elliott8528eff2015-08-07 15:56:59 -0600754 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
755 &demo->swapChainImageCount,
756 swapChainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600757 assert(!err);
Ian Elliott2d47da92015-07-13 12:20:56 -0600758#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600759 // After the proper code was created, other parts of this demo were
760 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
761 // images, etc. Live with that for now.
762 // TODO: Rework this demo code to live with the number of buffers returned
763 // by vkCreateSwapChainWSI().
Ian Elliott2d47da92015-07-13 12:20:56 -0600764 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
Ian Elliott2d47da92015-07-13 12:20:56 -0600765#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600766
767 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
768 assert(demo->buffers);
769
770 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800771 VkAttachmentViewCreateInfo color_attachment_view = {
772 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600773 .pNext = NULL,
774 .format = demo->format,
775 .mipLevel = 0,
776 .baseArraySlice = 0,
777 .arraySize = 1,
778 };
779
Ian Elliott8528eff2015-08-07 15:56:59 -0600780 demo->buffers[i].image = swapChainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600782 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400783 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600784 VK_IMAGE_LAYOUT_UNDEFINED,
785 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600786
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600787 color_attachment_view.image = demo->buffers[i].image;
788
Chia-I Wua74c5b22015-07-07 11:50:03 +0800789 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600790 &color_attachment_view, &demo->buffers[i].view);
791 assert(!err);
792 }
793}
794
795static void demo_prepare_depth(struct demo *demo)
796{
Tony Barbour72304ef2015-04-16 15:59:00 -0600797 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600798 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600799 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600800 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600801 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600802 .format = depth_format,
803 .extent = { demo->width, demo->height, 1 },
804 .mipLevels = 1,
805 .arraySize = 1,
806 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600807 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600808 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600809 .flags = 0,
810 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600811 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600812 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500813 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600814 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600815 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600816 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800817 VkAttachmentViewCreateInfo view = {
818 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600820 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600821 .format = depth_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600822 .mipLevel = 0,
823 .baseArraySlice = 0,
824 .arraySize = 1,
825 .flags = 0,
826 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600827
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500828 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600829 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830
831 demo->depth.format = depth_format;
832
833 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600834 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835 &demo->depth.image);
836 assert(!err);
837
Tony Barbour155cce82015-07-03 10:33:54 -0600838 err = vkGetImageMemoryRequirements(demo->device,
839 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600840
841 mem_alloc.allocationSize = mem_reqs.size;
842 err = memory_type_from_properties(demo,
843 mem_reqs.memoryTypeBits,
844 VK_MEMORY_PROPERTY_DEVICE_ONLY,
845 &mem_alloc.memoryTypeIndex);
846 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500848 /* allocate memory */
849 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
850 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500852 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600853 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500854 demo->depth.mem, 0);
855 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600856
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600857 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400858 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600859 VK_IMAGE_LAYOUT_UNDEFINED,
860 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600861
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600862 /* create image view */
863 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800864 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600865 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600866}
867
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600868/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600869 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600870 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600871 * \param demo : Needed to access VK calls
872 * \param filename : the png file to be loaded
873 * \param width : width of png, to be updated as a side effect of this function
874 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600875 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600876 * \return bool : an opengl texture id. true if successful?,
877 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600878 *
879 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
880 * Modified to copy image to memory
881 *
882 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700883bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600884 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600885 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600886{
887 //header for testing if it is a png
888 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600889 int is_png, bit_depth, color_type, rowbytes;
890 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700891 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600892 png_structp png_ptr;
893 png_infop info_ptr, end_info;
894 png_byte *image_data;
895 png_bytep *row_pointers;
896
897 //open file as binary
898 FILE *fp = fopen(filename, "rb");
899 if (!fp) {
900 return false;
901 }
902
903 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600904 retval = fread(header, 1, 8, fp);
905 if (retval != 8) {
906 fclose(fp);
907 return false;
908 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600909
910 //test if png
911 is_png = !png_sig_cmp(header, 0, 8);
912 if (!is_png) {
913 fclose(fp);
914 return false;
915 }
916
917 //create png struct
918 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
919 NULL, NULL);
920 if (!png_ptr) {
921 fclose(fp);
922 return (false);
923 }
924
925 //create png info struct
926 info_ptr = png_create_info_struct(png_ptr);
927 if (!info_ptr) {
928 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
929 fclose(fp);
930 return (false);
931 }
932
933 //create png info struct
934 end_info = png_create_info_struct(png_ptr);
935 if (!end_info) {
936 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
937 fclose(fp);
938 return (false);
939 }
940
941 //png error stuff, not sure libpng man suggests this.
942 if (setjmp(png_jmpbuf(png_ptr))) {
943 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
944 fclose(fp);
945 return (false);
946 }
947
948 //init png reading
949 png_init_io(png_ptr, fp);
950
951 //let libpng know you already read the first 8 bytes
952 png_set_sig_bytes(png_ptr, 8);
953
954 // read all the info up to the image data
955 png_read_info(png_ptr, info_ptr);
956
957 // get info about png
958 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
959 NULL, NULL, NULL);
960
961 //update width and height based on png info
962 *width = twidth;
963 *height = theight;
964
965 // Require that incoming texture be 8bits per color component
966 // and 4 components (RGBA).
967 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
968 png_get_channels(png_ptr, info_ptr) != 4) {
969 return false;
970 }
971
972 if (rgba_data == NULL) {
973 // If data pointer is null, we just want the width & height
974 // clean up memory and close stuff
975 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
976 fclose(fp);
977
978 return true;
979 }
980
981 // Update the png info struct.
982 png_read_update_info(png_ptr, info_ptr);
983
984 // Row size in bytes.
985 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
986
987 // Allocate the image_data as a big block, to be given to opengl
988 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
989 if (!image_data) {
990 //clean up memory and close stuff
991 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
992 fclose(fp);
993 return false;
994 }
995
996 // row_pointers is for pointing to image_data for reading the png with libpng
997 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
998 if (!row_pointers) {
999 //clean up memory and close stuff
1000 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1001 // delete[] image_data;
1002 fclose(fp);
1003 return false;
1004 }
1005 // set the individual row_pointers to point at the correct offsets of image_data
1006 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001008
1009 // read the png into image_data through row_pointers
1010 png_read_image(png_ptr, row_pointers);
1011
1012 // clean up memory and close stuff
1013 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1014 free(row_pointers);
1015 free(image_data);
1016 fclose(fp);
1017
1018 return true;
1019}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001020
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021static void demo_prepare_texture_image(struct demo *demo,
1022 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001023 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001024 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001025 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001026 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001027{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001028 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001029 int32_t tex_width;
1030 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001031 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032
David Pinedo30bd71d2015-04-23 08:16:57 -06001033 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1034 {
1035 printf("Failed to load textures\n");
1036 fflush(stdout);
1037 exit(1);
1038 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001039
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001040 tex_obj->tex_width = tex_width;
1041 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001042
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001043 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001044 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001045 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001046 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047 .format = tex_format,
1048 .extent = { tex_width, tex_height, 1 },
1049 .mipLevels = 1,
1050 .arraySize = 1,
1051 .samples = 1,
1052 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001053 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001054 .flags = 0,
1055 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001056 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001057 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001058 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001059 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001060 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061 };
1062
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001063 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001066 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067 assert(!err);
1068
Tony Barbour155cce82015-07-03 10:33:54 -06001069 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001070 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001071
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001072 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001074 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1075 assert(!err);
1076
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001077 /* allocate memory */
1078 err = vkAllocMemory(demo->device, &mem_alloc,
1079 &(tex_obj->mem));
1080 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001082 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001083 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001084 tex_obj->mem, 0);
1085 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086
Tony Barbour72304ef2015-04-16 15:59:00 -06001087 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001088 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001089 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001090 .mipLevel = 0,
1091 .arraySlice = 0,
1092 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001093 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001094 void *data;
1095
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001096 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1097 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001098
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001099 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001100 assert(!err);
1101
1102 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1103 fprintf(stderr, "Error loading texture: %s\n", filename);
1104 }
1105
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001106 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107 assert(!err);
1108 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001109
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001110 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001111 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001112 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001113 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001114 tex_obj->imageLayout);
1115 /* 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 -07001116}
1117
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001118static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119{
1120 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001121 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001122 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123}
1124
1125static void demo_prepare_textures(struct demo *demo)
1126{
Tony Barbour72304ef2015-04-16 15:59:00 -06001127 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001128 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001129 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001130 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001131
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001132 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133 assert(!err);
1134
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001135 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001136
FslNopper6a7e50e2015-05-06 21:42:01 +02001137 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138 /* Device can texture using linear textures */
1139 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001140 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001141 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001143 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144
1145 memset(&staging_texture, 0, sizeof(staging_texture));
1146 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001147 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148
1149 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001150 VK_IMAGE_TILING_OPTIMAL,
1151 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1152 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001153
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001154 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001155 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001156 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001157 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001158
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001159 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001160 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001161 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001162 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001164 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001165 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001167 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168 .destOffset = { 0, 0, 0 },
1169 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1170 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001171 vkCmdCopyImage(demo->cmd,
1172 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1173 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001174 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001175
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001176 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001177 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001178 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001179 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001180
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001181 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001183 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001185 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1186 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001187 }
1188
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001189 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001190 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001191 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001192 .magFilter = VK_TEX_FILTER_NEAREST,
1193 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001194 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001195 .addressU = VK_TEX_ADDRESS_CLAMP,
1196 .addressV = VK_TEX_ADDRESS_CLAMP,
1197 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001200 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201 .minLod = 0.0f,
1202 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001203 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Cody Northrop25439e42015-08-11 15:50:55 -06001204 .texelCoords = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001205 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001206
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001207 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001208 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001210 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001211 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001212 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001213 .channels = { VK_CHANNEL_SWIZZLE_R,
1214 VK_CHANNEL_SWIZZLE_G,
1215 VK_CHANNEL_SWIZZLE_B,
1216 VK_CHANNEL_SWIZZLE_A, },
1217 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001218 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001219
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001221 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001222 &demo->textures[i].sampler);
1223 assert(!err);
1224
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 /* create image view */
1226 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001227 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228 &demo->textures[i].view);
1229 assert(!err);
1230 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001231}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001233void demo_prepare_cube_data_buffer(struct demo *demo)
1234{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001235 VkBufferCreateInfo buf_info;
1236 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001237 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001238 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001239 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001240 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001241 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001242 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001243 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001244 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001245 int i;
1246 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001247 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001248 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001249
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001250 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001251 mat4x4_mul(MVP, VP, demo->model_matrix);
1252 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001253// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001254
1255 for (i=0; i<12*3; i++) {
1256 data.position[i][0] = g_vertex_buffer_data[i*3];
1257 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1258 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1259 data.position[i][3] = 1.0f;
1260 data.attr[i][0] = g_uv_buffer_data[2*i];
1261 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1262 data.attr[i][2] = 0;
1263 data.attr[i][3] = 0;
1264 }
1265
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001266 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001267 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001268 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001269 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001270 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001271 assert(!err);
1272
Tony Barbour155cce82015-07-03 10:33:54 -06001273 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001274 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001275
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001276 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001277 err = memory_type_from_properties(demo,
1278 mem_reqs.memoryTypeBits,
1279 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1280 &alloc_info.memoryTypeIndex);
1281 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001282
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001283 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1284 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001285
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001286 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1287 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001288
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001289 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001290
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001291 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1292 assert(!err);
1293
Tony Barbour155cce82015-07-03 10:33:54 -06001294 err = vkBindBufferMemory(demo->device,
1295 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001296 demo->uniform_data.mem, 0);
1297 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001298
1299 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001300 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001301 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001302 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001303 view_info.offset = 0;
1304 view_info.range = sizeof(data);
1305
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001306 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001307 assert(!err);
1308
Chia-I Wuae721ba2015-05-25 16:27:55 +08001309 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001310}
1311
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001312static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001314 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001315 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001316 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001317 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001318 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001319 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001320 },
1321 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001322 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001323 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001324 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001325 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001326 },
1327 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001328 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001329 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001330 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001331 .count = 2,
1332 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001333 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001334 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001336 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001337 &descriptor_layout, &demo->desc_layout);
1338 assert(!err);
1339
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001340 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1341 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1342 .pNext = NULL,
1343 .descriptorSetCount = 1,
1344 .pSetLayouts = &demo->desc_layout,
1345 };
1346
1347 err = vkCreatePipelineLayout(demo->device,
1348 &pPipelineLayoutCreateInfo,
1349 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001351}
1352
Chia-I Wuf973e322015-07-08 13:34:24 +08001353static void demo_prepare_render_pass(struct demo *demo)
1354{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001355 const VkAttachmentDescription attachments[2] = {
1356 [0] = {
1357 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1358 .pNext = NULL,
1359 .format = demo->format,
1360 .samples = 1,
1361 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1362 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1363 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1364 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1365 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1366 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1367 },
1368 [1] = {
1369 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1370 .pNext = NULL,
1371 .format = demo->depth.format,
1372 .samples = 1,
1373 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1374 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1375 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1376 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1377 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1378 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1379 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001380 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001381 const VkAttachmentReference color_reference = {
1382 .attachment = 0,
1383 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1384 };
1385 const VkSubpassDescription subpass = {
1386 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1387 .pNext = NULL,
1388 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1389 .flags = 0,
1390 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001391 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001392 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001393 .pColorAttachments = &color_reference,
1394 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001395 .depthStencilAttachment = {
1396 .attachment = 1,
1397 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1398 },
1399 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001400 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001401 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001402 const VkRenderPassCreateInfo rp_info = {
1403 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1404 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001405 .attachmentCount = 2,
1406 .pAttachments = attachments,
1407 .subpassCount = 1,
1408 .pSubpasses = &subpass,
1409 .dependencyCount = 0,
1410 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001411 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001412 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001413
1414 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1415 assert(!err);
1416}
1417
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001418static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001419 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001420 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001421 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001422 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001424 VkShaderModuleCreateInfo moduleCreateInfo;
1425 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001426 VkShader shader;
1427 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001429
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001430 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1431 moduleCreateInfo.pNext = NULL;
1432
1433 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1434 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001435 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001436
Cody Northrop1fedb212015-05-28 11:27:16 -06001437 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001438 moduleCreateInfo.codeSize = size;
1439 moduleCreateInfo.pCode = code;
1440 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001441 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001442 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001443 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001444 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001445
1446 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001447 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001448 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001449 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001450 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001451 } else {
1452 // Create fake SPV structure to feed GLSL
1453 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001454 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1455 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1456 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001457
1458 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001459 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1460 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1461 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1462 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001463
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001464 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001465 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001466 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001467 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001468
1469 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001470 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001471 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001472 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001473 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001474 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475 return shader;
1476}
1477
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001478char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001479{
1480 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001481 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001482 void *shader_code;
1483
1484 FILE *fp = fopen(filename, "rb");
1485 if (!fp) return NULL;
1486
1487 fseek(fp, 0L, SEEK_END);
1488 size = ftell(fp);
1489
1490 fseek(fp, 0L, SEEK_SET);
1491
1492 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001493 retval = fread(shader_code, size, 1, fp);
1494 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001495
1496 *psize = size;
1497
1498 return shader_code;
1499}
1500
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001501static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001502{
Cody Northrop1fedb212015-05-28 11:27:16 -06001503 if (!demo->use_glsl) {
1504 void *vertShaderCode;
1505 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001506
Cody Northrop1fedb212015-05-28 11:27:16 -06001507 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001508
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001509 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001510 vertShaderCode, size);
1511 } else {
1512 static const char *vertShaderText =
1513 "#version 140\n"
1514 "#extension GL_ARB_separate_shader_objects : enable\n"
1515 "#extension GL_ARB_shading_language_420pack : enable\n"
1516 "\n"
1517 "layout(binding = 0) uniform buf {\n"
1518 " mat4 MVP;\n"
1519 " vec4 position[12*3];\n"
1520 " vec4 attr[12*3];\n"
1521 "} ubuf;\n"
1522 "\n"
1523 "layout (location = 0) out vec4 texcoord;\n"
1524 "\n"
1525 "void main() \n"
1526 "{\n"
1527 " texcoord = ubuf.attr[gl_VertexID];\n"
1528 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1529 "\n"
1530 " // GL->VK conventions\n"
1531 " gl_Position.y = -gl_Position.y;\n"
1532 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1533 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001534
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001535 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001536 (const void *) vertShaderText,
1537 strlen(vertShaderText));
1538 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001539}
1540
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001541static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001542{
Cody Northrop1fedb212015-05-28 11:27:16 -06001543 if (!demo->use_glsl) {
1544 void *fragShaderCode;
1545 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001546
Cody Northrop1fedb212015-05-28 11:27:16 -06001547 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001548
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001549 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001550 fragShaderCode, size);
1551 } else {
1552 static const char *fragShaderText =
1553 "#version 140\n"
1554 "#extension GL_ARB_separate_shader_objects : enable\n"
1555 "#extension GL_ARB_shading_language_420pack : enable\n"
1556 "layout (binding = 1) uniform sampler2D tex;\n"
1557 "\n"
1558 "layout (location = 0) in vec4 texcoord;\n"
1559 "layout (location = 0) out vec4 uFragColor;\n"
1560 "void main() {\n"
1561 " uFragColor = texture(tex, texcoord.xy);\n"
1562 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001563
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001564 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001565 (const void *) fragShaderText,
1566 strlen(fragShaderText));
1567 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568}
1569
1570static void demo_prepare_pipeline(struct demo *demo)
1571{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001572 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001573 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001574 VkPipelineInputAssemblyStateCreateInfo ia;
1575 VkPipelineRasterStateCreateInfo rs;
1576 VkPipelineColorBlendStateCreateInfo cb;
1577 VkPipelineDepthStencilStateCreateInfo ds;
1578 VkPipelineViewportStateCreateInfo vp;
1579 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001580 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001581
1582 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001583 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001584 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001585
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001586 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001587 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001588 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001589
1590 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001591 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001592 rs.fillMode = VK_FILL_MODE_SOLID;
1593 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001594 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001595 rs.depthClipEnable = VK_TRUE;
Cody Northrop709c1742015-08-17 11:10:49 -06001596 rs.rasterizerDiscardEnable = VK_FALSE;
1597 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001598
1599 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001600 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1601 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001602 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001603 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001604 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001605 cb.attachmentCount = 1;
1606 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001607
Tony Barbour29645d02015-01-16 14:27:35 -07001608 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001609 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001610 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001611
1612 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001613 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001614 ds.depthTestEnable = VK_TRUE;
1615 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001616 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001617 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001618 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1619 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001620 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001621 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001622 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001623
Tony Barbour29645d02015-01-16 14:27:35 -07001624 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001625 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001626 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001627 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001628
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001629 // Two stages: vs and fs
1630 pipeline.stageCount = 2;
1631 VkPipelineShaderStageCreateInfo shaderStages[2];
1632 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1633
1634 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1635 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1636 shaderStages[0].shader = demo_prepare_vs(demo);
1637
1638 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1639 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1640 shaderStages[1].shader = demo_prepare_fs(demo);
1641
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001642 memset(&pipelineCache, 0, sizeof(pipelineCache));
1643 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001644
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001645 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1646 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001647
1648 pipeline.pVertexInputState = NULL;
1649 pipeline.pInputAssemblyState = &ia;
1650 pipeline.pRasterState = &rs;
1651 pipeline.pColorBlendState = &cb;
1652 pipeline.pMultisampleState = &ms;
1653 pipeline.pViewportState = &vp;
1654 pipeline.pDepthStencilState = &ds;
1655 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001656 pipeline.renderPass = demo->render_pass;
Tony Barbour194bf282015-07-10 15:29:03 -06001657
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001658 pipeline.renderPass = demo->render_pass;
1659
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001660 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001661 assert(!err);
1662
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001663 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001664 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001665 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001666 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1667 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001668}
1669
1670static void demo_prepare_dynamic_states(struct demo *demo)
1671{
Tony Barbour155cce82015-07-03 10:33:54 -06001672 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001673 VkDynamicLineWidthStateCreateInfo line_width;
1674 VkDynamicDepthBiasStateCreateInfo depth_bias;
1675 VkDynamicBlendStateCreateInfo blend;
1676 VkDynamicDepthBoundsStateCreateInfo depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -06001677 VkDynamicStencilStateCreateInfo stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001678 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001679
Tony Barbour29645d02015-01-16 14:27:35 -07001680 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001681 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001682 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001683 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001684 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001685 viewport.height = (float) demo->height;
1686 viewport.width = (float) demo->width;
1687 viewport.minDepth = (float) 0.0f;
1688 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001689 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001690 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001691 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001692 scissor.extent.width = demo->width;
1693 scissor.extent.height = demo->height;
1694 scissor.offset.x = 0;
1695 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001696 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001697
Cody Northrop5e4125d2015-08-26 10:01:32 -06001698 memset(&line_width, 0, sizeof(line_width));
1699 line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
1700 line_width.lineWidth = 1.0;
Cody Northrop709c1742015-08-17 11:10:49 -06001701
Cody Northrop5e4125d2015-08-26 10:01:32 -06001702 memset(&depth_bias, 0, sizeof(depth_bias));
1703 depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
1704 depth_bias.depthBias = 0.0f;
1705 depth_bias.depthBiasClamp = 0.0f;
1706 depth_bias.slopeScaledDepthBias = 0.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707
Cody Northrop5e4125d2015-08-26 10:01:32 -06001708 memset(&blend, 0, sizeof(blend));
1709 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
1710 blend.blendConst[0] = 1.0f;
1711 blend.blendConst[1] = 1.0f;
1712 blend.blendConst[2] = 1.0f;
1713 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714
Cody Northrop5e4125d2015-08-26 10:01:32 -06001715 memset(&depth_bounds, 0, sizeof(depth_bounds));
1716 depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
1717 depth_bounds.minDepthBounds = 0.0f;
1718 depth_bounds.maxDepthBounds = 1.0f;
Cody Northrop36a642f2015-08-18 15:21:16 -06001719
1720 memset(&stencil, 0, sizeof(stencil));
1721 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
1722 stencil.stencilReference = 0;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001723 stencil.stencilCompareMask = 0xff;
Cody Northrop36a642f2015-08-18 15:21:16 -06001724 stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001725
Cody Northrop5e4125d2015-08-26 10:01:32 -06001726 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727 assert(!err);
1728
Cody Northrop5e4125d2015-08-26 10:01:32 -06001729 err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width);
Cody Northrop709c1742015-08-17 11:10:49 -06001730 assert(!err);
1731
Cody Northrop5e4125d2015-08-26 10:01:32 -06001732 err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733 assert(!err);
1734
Cody Northrop5e4125d2015-08-26 10:01:32 -06001735 err = vkCreateDynamicBlendState(demo->device,
1736 &blend, &demo->dynamic_blend);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001737 assert(!err);
1738
Cody Northrop5e4125d2015-08-26 10:01:32 -06001739 err = vkCreateDynamicDepthBoundsState(demo->device,
1740 &depth_bounds, &demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001741 assert(!err);
1742
1743 err = vkCreateDynamicStencilState(demo->device,
1744 &stencil, &stencil, &demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 assert(!err);
1746}
1747
Chia-I Wu63ea9262015-03-26 13:14:16 +08001748static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001749{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001750 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001751 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001752 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001753 .count = 1,
1754 },
1755 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001756 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001757 .count = DEMO_TEXTURE_COUNT,
1758 },
1759 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001760 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001761 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001762 .pNext = NULL,
1763 .count = 2,
1764 .pTypeCount = type_counts,
1765 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001766 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001767
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001768 err = vkCreateDescriptorPool(demo->device,
1769 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001770 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001771 assert(!err);
1772}
1773
1774static void demo_prepare_descriptor_set(struct demo *demo)
1775{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001776 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1777 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001778 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001779 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001780
Mike Stroyanebae8322015-04-17 12:36:38 -06001781 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001782 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001783 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001784 &demo->desc_set);
1785 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001786
Chia-I Wuae721ba2015-05-25 16:27:55 +08001787 memset(&tex_descs, 0, sizeof(tex_descs));
1788 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1789 tex_descs[i].sampler = demo->textures[i].sampler;
1790 tex_descs[i].imageView = demo->textures[i].view;
1791 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1792 }
1793
1794 memset(&writes, 0, sizeof(writes));
1795
1796 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1797 writes[0].destSet = demo->desc_set;
1798 writes[0].count = 1;
1799 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1800 writes[0].pDescriptors = &demo->uniform_data.desc;
1801
1802 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1803 writes[1].destSet = demo->desc_set;
1804 writes[1].destBinding = 1;
1805 writes[1].count = DEMO_TEXTURE_COUNT;
1806 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1807 writes[1].pDescriptors = tex_descs;
1808
1809 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1810 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001811}
1812
Chia-I Wuf973e322015-07-08 13:34:24 +08001813static void demo_prepare_framebuffers(struct demo *demo)
1814{
Cody Northrop2e11cad2015-08-04 10:47:08 -06001815 VkAttachmentView attachments[2];
1816 attachments[1] = demo->depth.view;
1817
Chia-I Wuf973e322015-07-08 13:34:24 +08001818 const VkFramebufferCreateInfo fb_info = {
1819 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1820 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001821 .renderPass = demo->render_pass,
1822 .attachmentCount = 2,
1823 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001824 .width = demo->width,
1825 .height = demo->height,
1826 .layers = 1,
1827 };
1828 VkResult U_ASSERT_ONLY err;
1829 uint32_t i;
1830
1831 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001832 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001833 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1834 assert(!err);
1835 }
1836}
1837
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838static void demo_prepare(struct demo *demo)
1839{
Cody Northrop91e221b2015-07-09 18:08:32 -06001840 VkResult U_ASSERT_ONLY err;
1841
1842 const VkCmdPoolCreateInfo cmd_pool_info = {
1843 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1844 .pNext = NULL,
1845 .queueFamilyIndex = demo->graphics_queue_node_index,
1846 .flags = 0,
1847 };
1848 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1849 assert(!err);
1850
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001851 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001852 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001854 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001855 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856 .flags = 0,
1857 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858
1859 demo_prepare_buffers(demo);
1860 demo_prepare_depth(demo);
1861 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001862 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001863
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001864 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001865 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001866 demo_prepare_pipeline(demo);
1867 demo_prepare_dynamic_states(demo);
1868
Ian Elliottaaae5352015-07-06 14:27:58 -06001869 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001870 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001871 assert(!err);
1872 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001873
Chia-I Wu63ea9262015-03-26 13:14:16 +08001874 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001875 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001876
Chia-I Wuf973e322015-07-08 13:34:24 +08001877 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001878
Ian Elliottaaae5352015-07-06 14:27:58 -06001879 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001880 demo->current_buffer = i;
1881 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1882 }
1883
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001884 /*
1885 * Prepare functions above may generate pipeline commands
1886 * that need to be flushed before beginning the render loop.
1887 */
1888 demo_flush_init_cmd(demo);
1889
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001890 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001891 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001892}
1893
David Pinedo2bb7c932015-06-18 17:03:14 -06001894static void demo_cleanup(struct demo *demo)
1895{
1896 uint32_t i;
1897
1898 demo->prepared = false;
1899
Tony Barbour155cce82015-07-03 10:33:54 -06001900 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1901 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1902 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001903 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001904 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001905
Cody Northrop5e4125d2015-08-26 10:01:32 -06001906 vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport);
1907 vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width);
1908 vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias);
1909 vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend);
1910 vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001911 vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001912
Tony Barbour155cce82015-07-03 10:33:54 -06001913 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001914 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001915 vkDestroyRenderPass(demo->device, demo->render_pass);
1916 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1917 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001918
1919 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001920 vkDestroyImageView(demo->device, demo->textures[i].view);
1921 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001922 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001923 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001924 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001925 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001926
Tony Barbour155cce82015-07-03 10:33:54 -06001927 vkDestroyAttachmentView(demo->device, demo->depth.view);
1928 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001929 vkFreeMemory(demo->device, demo->depth.mem);
1930
Tony Barbour155cce82015-07-03 10:33:54 -06001931 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1932 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001933 vkFreeMemory(demo->device, demo->uniform_data.mem);
1934
Ian Elliottaaae5352015-07-06 14:27:58 -06001935 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001936 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1937 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001938 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001939 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001940
Cody Northrop91e221b2015-07-09 18:08:32 -06001941 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001942 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001943 if (demo->validate) {
1944 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1945 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001946 vkDestroyInstance(demo->inst);
1947
1948#ifndef _WIN32
1949 xcb_destroy_window(demo->connection, demo->window);
1950 xcb_disconnect(demo->connection);
1951#endif // _WIN32
1952}
1953
1954// On MS-Windows, make this a global, so it's available to WndProc()
1955struct demo demo;
1956
Ian Elliott639ca472015-04-16 15:23:05 -06001957#ifdef _WIN32
1958static void demo_run(struct demo *demo)
1959{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001960 if (!demo->prepared)
1961 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001962 // Wait for work to finish before updating MVP.
1963 vkDeviceWaitIdle(demo->device);
1964 demo_update_data_buffer(demo);
1965
1966 demo_draw(demo);
1967
1968 // Wait for work to finish before updating MVP.
1969 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001970
David Pinedo2bb7c932015-06-18 17:03:14 -06001971 demo->curFrame++;
1972
1973 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1974 {
1975 demo->quit=true;
1976 demo_cleanup(demo);
1977 ExitProcess(0);
1978 }
1979
1980}
Ian Elliott639ca472015-04-16 15:23:05 -06001981
1982// MS-Windows event handling function:
1983LRESULT CALLBACK WndProc(HWND hWnd,
1984 UINT uMsg,
1985 WPARAM wParam,
1986 LPARAM lParam)
1987{
Ian Elliott639ca472015-04-16 15:23:05 -06001988 switch(uMsg)
1989 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001990 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001991 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001992 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001993 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001994 demo_run(&demo);
1995 return 0;
1996 default:
1997 break;
1998 }
1999 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2000}
2001
2002static void demo_create_window(struct demo *demo)
2003{
2004 WNDCLASSEX win_class;
2005
2006 // Initialize the window class structure:
2007 win_class.cbSize = sizeof(WNDCLASSEX);
2008 win_class.style = CS_HREDRAW | CS_VREDRAW;
2009 win_class.lpfnWndProc = WndProc;
2010 win_class.cbClsExtra = 0;
2011 win_class.cbWndExtra = 0;
2012 win_class.hInstance = demo->connection; // hInstance
2013 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2014 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2015 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2016 win_class.lpszMenuName = NULL;
2017 win_class.lpszClassName = demo->name;
2018 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2019 // Register window class:
2020 if (!RegisterClassEx(&win_class)) {
2021 // It didn't work, so try to give a useful error:
2022 printf("Unexpected error trying to start the application!\n");
2023 fflush(stdout);
2024 exit(1);
2025 }
2026 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06002027 RECT wr = { 0, 0, demo->width, demo->height };
2028 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002029 demo->window = CreateWindowEx(0,
2030 demo->name, // class name
2031 demo->name, // app name
2032 WS_OVERLAPPEDWINDOW | // window style
2033 WS_VISIBLE |
2034 WS_SYSMENU,
2035 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06002036 wr.right-wr.left, // width
2037 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06002038 NULL, // handle to parent
2039 NULL, // handle to menu
2040 demo->connection, // hInstance
2041 NULL); // no extra parameters
2042 if (!demo->window) {
2043 // It didn't work, so try to give a useful error:
2044 printf("Cannot create a window in which to draw!\n");
2045 fflush(stdout);
2046 exit(1);
2047 }
2048}
2049#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050static void demo_handle_event(struct demo *demo,
2051 const xcb_generic_event_t *event)
2052{
Piers Daniell735ee532015-02-23 16:23:13 -07002053 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002054 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002055 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002056 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002058 case XCB_CLIENT_MESSAGE:
2059 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2060 (*demo->atom_wm_delete_window).atom) {
2061 demo->quit = true;
2062 }
2063 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064 case XCB_KEY_RELEASE:
2065 {
2066 const xcb_key_release_event_t *key =
2067 (const xcb_key_release_event_t *) event;
2068
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002069 switch (key->detail) {
2070 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002071 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002072 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002073 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002074 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002075 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002076 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002077 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002078 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002079 case 0x41:
2080 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002081 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002082 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002083 }
2084 break;
2085 default:
2086 break;
2087 }
2088}
2089
2090static void demo_run(struct demo *demo)
2091{
2092 xcb_flush(demo->connection);
2093
2094 while (!demo->quit) {
2095 xcb_generic_event_t *event;
2096
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002097 if (demo->pause) {
2098 event = xcb_wait_for_event(demo->connection);
2099 } else {
2100 event = xcb_poll_for_event(demo->connection);
2101 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102 if (event) {
2103 demo_handle_event(demo, event);
2104 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002105 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002106
2107 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002108 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002109 demo_update_data_buffer(demo);
2110
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002111 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002112
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002113 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002114 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002115 demo->curFrame++;
2116 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2117 demo->quit = true;
2118
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002119 }
2120}
2121
2122static void demo_create_window(struct demo *demo)
2123{
2124 uint32_t value_mask, value_list[32];
2125
2126 demo->window = xcb_generate_id(demo->connection);
2127
2128 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2129 value_list[0] = demo->screen->black_pixel;
2130 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2131 XCB_EVENT_MASK_EXPOSURE;
2132
2133 xcb_create_window(demo->connection,
2134 XCB_COPY_FROM_PARENT,
2135 demo->window, demo->screen->root,
2136 0, 0, demo->width, demo->height, 0,
2137 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2138 demo->screen->root_visual,
2139 value_mask, value_list);
2140
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002141 /* Magic code that will send notification when window is destroyed */
2142 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2143 "WM_PROTOCOLS");
2144 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2145
2146 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2147 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2148
2149 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2150 demo->window, (*reply).atom, 4, 32, 1,
2151 &(*demo->atom_wm_delete_window).atom);
2152 free(reply);
2153
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002154 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002155
2156 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2157 const uint32_t coords[] = {100, 100};
2158 xcb_configure_window(demo->connection, demo->window,
2159 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002160}
Ian Elliott639ca472015-04-16 15:23:05 -06002161#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002162
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002163/*
2164 * Return 1 (true) if all layer names specified in check_names
2165 * can be found in given layer properties.
2166 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002167static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002168 uint32_t layer_count, VkLayerProperties *layers)
2169{
2170 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002171 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002172 for (uint32_t j = 0; j < layer_count; j++) {
2173 if (!strcmp(check_names[i], layers[j].layerName)) {
2174 found = 1;
2175 }
2176 }
2177 if (!found) {
2178 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2179 return 0;
2180 }
2181 }
2182 return 1;
2183}
2184
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002185static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002186{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002187 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002188 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002189 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002190 VkLayerProperties *instance_layers;
2191 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002192 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002193 uint32_t instance_layer_count = 0;
2194 uint32_t enabled_extension_count = 0;
2195 uint32_t enabled_layer_count = 0;
2196
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002197 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002198 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002199 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002200 "ObjectTracker",
2201 "DrawState",
2202 "ParamChecker",
2203 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002204 };
2205
2206 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002207 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002208 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002209 "ObjectTracker",
2210 "DrawState",
2211 "ParamChecker",
2212 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002213 };
2214
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002215 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002216 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002217 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002218 assert(!err);
2219
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002220 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2221 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2222 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002223
2224 if (demo->validate) {
2225 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2226 instance_layer_count, instance_layers);
2227 if (!validation_found) {
2228 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2229 "required validation layer.\n\n"
2230 "Please look at the Getting Started guide for additional "
2231 "information.\n",
2232 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002233 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002234 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002235 }
2236
2237 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2238 assert(!err);
2239
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002240 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002241 memset(extension_names, 0, sizeof(extension_names));
2242 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2243 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2244 assert(!err);
2245 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002246 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002247 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002248 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002249 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002250 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002251 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002252 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002253 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002254 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002255 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002256 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002257 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002258 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002259 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002260 "Vulkan installable client driver (ICD) installed?\nPlease "
2261 "look at the Getting Started guide for additional "
2262 "information.\n",
2263 "vkCreateInstance Failure");
2264 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002265 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002266 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002267 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002268 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002269 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002270 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002271 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002272 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002273 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002274 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002275 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002276 .pNext = NULL,
2277 .pAppInfo = &app,
2278 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002279 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002280 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002281 .extensionCount = enabled_extension_count,
2282 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002283 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002284 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002285 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286 .queueCount = 1,
2287 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002288
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002289 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002290
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002291 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002292 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2293 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002294 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002295 "additional information.\n",
2296 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002297 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2298 ERR_EXIT("Cannot find a specified extension library"
2299 ".\nMake sure your layers path is set appropriately\n",
2300 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002301 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002302 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2303 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002304 "the Getting Started guide for additional information.\n",
2305 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002306 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002307
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002308 free(instance_layers);
2309 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002310
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002311 gpu_count = 1;
2312 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002313 assert(!err && gpu_count == 1);
2314
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002315 /* Look for validation layers */
2316 validation_found = 0;
2317 enabled_layer_count = 0;
2318 uint32_t device_layer_count = 0;
2319 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2320 assert(!err);
2321
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002322 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2323 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2324 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002325
2326 if (demo->validate) {
2327 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2328 device_layer_count, device_layers);
2329 if (!validation_found) {
2330 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2331 "a required validation layer.\n\n"
2332 "Please look at the Getting Started guide for additional "
2333 "information.\n",
2334 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002335 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002336 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002337 }
2338
2339 uint32_t device_extension_count = 0;
2340 VkExtensionProperties *device_extensions = NULL;
2341 err = vkGetPhysicalDeviceExtensionProperties(
2342 demo->gpu, NULL, &device_extension_count, NULL);
2343 assert(!err);
2344
2345 WSIextFound = 0;
2346 enabled_extension_count = 0;
2347 memset(extension_names, 0, sizeof(extension_names));
2348 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2349 err = vkGetPhysicalDeviceExtensionProperties(
2350 demo->gpu, NULL, &device_extension_count, device_extensions);
2351 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002352
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002353 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002354 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002355 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002356 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002357 }
2358 assert(enabled_extension_count < 64);
2359 }
2360 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002361 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2362 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002363 "Vulkan installable client driver (ICD) installed?\nPlease "
2364 "look at the Getting Started guide for additional "
2365 "information.\n",
2366 "vkCreateInstance Failure");
2367 }
2368
2369 VkDeviceCreateInfo device = {
2370 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2371 .pNext = NULL,
2372 .queueRecordCount = 1,
2373 .pRequestedQueues = &queue,
2374 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002375 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002376 .extensionCount = enabled_extension_count,
2377 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002378 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002379
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002380 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002381 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2382 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002383 if (!demo->dbgCreateMsgCallback) {
2384 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2385 "vkGetProcAddr Failure");
2386 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002387 if (!demo->dbgDestroyMsgCallback) {
2388 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2389 "vkGetProcAddr Failure");
2390 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002391 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2392 if (!demo->dbgBreakCallback) {
2393 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2394 "vkGetProcAddr Failure");
2395 }
2396
2397 PFN_vkDbgMsgCallback callback;
2398
2399 if (!demo->use_break) {
2400 callback = dbgFunc;
2401 } else {
2402 callback = demo->dbgBreakCallback;
2403 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002404 err = demo->dbgCreateMsgCallback(
2405 demo->inst,
2406 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002407 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002408 &demo->msg_callback);
2409 switch (err) {
2410 case VK_SUCCESS:
2411 break;
2412 case VK_ERROR_INVALID_POINTER:
2413 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2414 "dbgCreateMsgCallback Failure");
2415 break;
2416 case VK_ERROR_OUT_OF_HOST_MEMORY:
2417 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2418 "dbgCreateMsgCallback Failure");
2419 break;
2420 default:
2421 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2422 "dbgCreateMsgCallback Failure");
2423 break;
2424 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002425 }
2426
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002427 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002428 assert(!err);
2429
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002430 free(device_layers);
2431
Ian Elliottaaae5352015-07-06 14:27:58 -06002432 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott8528eff2015-08-07 15:56:59 -06002433 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
2434 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
2435 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002436 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2437 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2438 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott8528eff2015-08-07 15:56:59 -06002439 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002440 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002441 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002442
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002443 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002444 assert(!err);
2445
Cody Northrop4d3c11d2015-08-03 17:04:53 -06002446 /* Call with NULL data to get count */
2447 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002448 assert(!err);
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002449 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002450
Cody Northrop4d3c11d2015-08-03 17:04:53 -06002451 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2452 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002453 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002454 assert(demo->queue_count >= 1);
2455}
2456
2457static void demo_init_vk_wsi(struct demo *demo)
2458{
2459 VkResult err;
2460 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002461
Ian Elliottaaae5352015-07-06 14:27:58 -06002462 // Construct the WSI surface description:
2463 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2464 demo->surface_description.pNext = NULL;
2465#ifdef _WIN32
2466 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2467 demo->surface_description.pPlatformHandle = demo->connection;
2468 demo->surface_description.pPlatformWindow = demo->window;
2469#else // _WIN32
2470 demo->platform_handle_xcb.connection = demo->connection;
2471 demo->platform_handle_xcb.root = demo->screen->root;
2472 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2473 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2474 demo->surface_description.pPlatformWindow = &demo->window;
2475#endif // _WIN32
2476
2477 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002478 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2479 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002480 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2481 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2482 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002483 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002484
2485 // Search for a graphics and a present queue in the array of queue
2486 // families, try to find one that supports both
2487 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2488 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002489 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002490 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2491 if (graphicsQueueNodeIndex == UINT32_MAX) {
2492 graphicsQueueNodeIndex = i;
2493 }
2494
2495 if (supportsPresent[i] == VK_TRUE) {
2496 graphicsQueueNodeIndex = i;
2497 presentQueueNodeIndex = i;
2498 break;
2499 }
2500 }
2501 }
2502 if (presentQueueNodeIndex == UINT32_MAX) {
2503 // If didn't find a queue that supports both graphics and present, then
2504 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002505 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002506 if (supportsPresent[i] == VK_TRUE) {
2507 presentQueueNodeIndex = i;
2508 break;
2509 }
2510 }
2511 }
2512 free(supportsPresent);
2513
2514 // Generate error if could not find both a graphics and a present queue
2515 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2516 ERR_EXIT("Could not find a graphics and a present queue\n",
2517 "WSI Initialization Failure");
2518 }
2519
2520 // TODO: Add support for separate queues, including presentation,
2521 // synchronization, and appropriate tracking for QueueSubmit
2522 // While it is possible for an application to use a separate graphics and a
2523 // present queues, this demo program assumes it is only using one:
2524 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2525 ERR_EXIT("Could not find a common graphics and a present queue\n",
2526 "WSI Initialization Failure");
2527 }
2528
2529 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002530
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002531 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002532 0, &demo->queue);
2533 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002534
Ian Elliottaaae5352015-07-06 14:27:58 -06002535 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002536 uint32_t formatCount;
2537 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -06002538 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002539 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002540 assert(!err);
Ian Elliott8528eff2015-08-07 15:56:59 -06002541 VkSurfaceFormatWSI *surfFormats =
2542 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
2543 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -06002544 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002545 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002546 assert(!err);
2547 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2548 // the surface has no preferred format. Otherwise, at least one
2549 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002550 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2551 {
2552 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2553 }
2554 else
2555 {
2556 assert(formatCount >= 1);
2557 demo->format = surfFormats[0].format;
2558 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002559 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002560
David Pinedo2bb7c932015-06-18 17:03:14 -06002561 demo->quit = false;
2562 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002563
2564 // Get Memory information and properties
2565 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2566 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002567}
2568
2569static void demo_init_connection(struct demo *demo)
2570{
Ian Elliott639ca472015-04-16 15:23:05 -06002571#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002572 const xcb_setup_t *setup;
2573 xcb_screen_iterator_t iter;
2574 int scr;
2575
2576 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002577 if (demo->connection == NULL) {
2578 printf("Cannot find a compatible Vulkan installable client driver "
2579 "(ICD).\nExiting ...\n");
2580 fflush(stdout);
2581 exit(1);
2582 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002583
2584 setup = xcb_get_setup(demo->connection);
2585 iter = xcb_setup_roots_iterator(setup);
2586 while (scr-- > 0)
2587 xcb_screen_next(&iter);
2588
2589 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002590#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002591}
2592
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002593static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002594{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002595 vec3 eye = {0.0f, 3.0f, 5.0f};
2596 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002597 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002598
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002599 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002600 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002601
Piers Daniell735ee532015-02-23 16:23:13 -07002602 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002603 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002604 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002605 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002606 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002607 if (strcmp(argv[i], "--use_glsl") == 0) {
2608 demo->use_glsl = true;
2609 continue;
2610 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002611 if (strcmp(argv[i], "--break") == 0) {
2612 demo->use_break = true;
2613 continue;
2614 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002615 if (strcmp(argv[i], "--validate") == 0) {
2616 demo->validate = true;
2617 continue;
2618 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002619 if (strcmp(argv[i], "--c") == 0 &&
2620 demo->frameCount == INT_MAX &&
2621 i < argc-1 &&
2622 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2623 demo->frameCount >= 0)
2624 {
2625 i++;
2626 continue;
2627 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002628
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002629 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002630 fflush(stderr);
2631 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002632 }
2633
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002634 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002635 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002636
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002637 demo->width = 500;
2638 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002639
2640 demo->spin_angle = 0.01f;
2641 demo->spin_increment = 0.01f;
2642 demo->pause = false;
2643
Piers Daniell735ee532015-02-23 16:23:13 -07002644 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002645 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2646 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002647}
2648
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002649
Ian Elliott639ca472015-04-16 15:23:05 -06002650#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002651extern int __getmainargs(
2652 int * _Argc,
2653 char *** _Argv,
2654 char *** _Env,
2655 int _DoWildCard,
2656 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002657
Ian Elliotta748eaf2015-04-28 15:50:36 -06002658int WINAPI WinMain(HINSTANCE hInstance,
2659 HINSTANCE hPrevInstance,
2660 LPSTR pCmdLine,
2661 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002662{
2663 MSG msg; // message
2664 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002665 int argc;
2666 char** argv;
2667 char** env;
2668 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002669
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002670 __getmainargs(&argc,&argv,&env,0,&new_mode);
2671
2672 demo_init(&demo, argc, argv);
2673 demo.connection = hInstance;
2674 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002675 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002676 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002677
2678 demo_prepare(&demo);
2679
2680 done = false; //initialize loop condition variable
2681 /* main message loop*/
2682 while(!done)
2683 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002684 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002685 if (msg.message == WM_QUIT) //check for a quit message
2686 {
2687 done = true; //if found, quit app
2688 }
2689 else
2690 {
2691 /* Translate and dispatch to event queue*/
2692 TranslateMessage(&msg);
2693 DispatchMessage(&msg);
2694 }
2695 }
2696
2697 demo_cleanup(&demo);
2698
Tony Barbourf9921732015-04-22 11:36:22 -06002699 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002700}
2701#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002702int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002703{
2704 struct demo demo;
2705
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002706 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002707 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002708 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002709
2710 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002711 demo_run(&demo);
2712
2713 demo_cleanup(&demo);
2714
2715 return 0;
2716}
Ian Elliott639ca472015-04-16 15:23:05 -06002717#endif // _WIN32