blob: 662f31c848f36d4a38029314134a4c5f19d78506 [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
Tony Barbour155cce82015-07-03 10:33:54 -0600367 VkDynamicViewportState viewport;
368 VkDynamicRasterState raster;
369 VkDynamicColorBlendState color_blend;
370 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600372 mat4x4 projection_matrix;
373 mat4x4 view_matrix;
374 mat4x4 model_matrix;
375
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600376 float spin_angle;
377 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600378 bool pause;
379
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600380 VkShaderModule vert_shader_module;
381 VkShaderModule frag_shader_module;
382
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600383 VkDescriptorPool desc_pool;
384 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800385
Chia-I Wuf973e322015-07-08 13:34:24 +0800386 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
387
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600389 int32_t curFrame;
390 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600391 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600392 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600393 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600394 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600395 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600396 VkDbgMsgCallback msg_callback;
397
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600398 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600399 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400};
401
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600402static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
403{
404 // Search memtypes to find first index with those properties
405 for (uint32_t i = 0; i < 32; i++) {
406 if ((typeBits & 1) == 1) {
407 // Type is available, does it match user properties?
408 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
409 *typeIndex = i;
410 return VK_SUCCESS;
411 }
412 }
413 typeBits >>= 1;
414 }
415 // No memory types matched, return failure
416 return VK_UNSUPPORTED;
417}
418
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419static void demo_flush_init_cmd(struct demo *demo)
420{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600421 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600422
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600423 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424 return;
425
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600426 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427 assert(!err);
428
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600430 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431
Tony Barbour155cce82015-07-03 10:33:54 -0600432 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433 assert(!err);
434
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600435 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436 assert(!err);
437
Tony Barbour155cce82015-07-03 10:33:54 -0600438 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600439 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600440}
441
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600442static void demo_set_image_layout(
443 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600444 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400445 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600446 VkImageLayout old_image_layout,
447 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600448{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600449 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600451 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600452 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600455 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800456 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600457 .flags = 0,
458 };
459
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461 assert(!err);
462
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600463 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600466 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600467 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Cody Northropa0a178c2015-08-11 11:35:58 -0600468 .renderPass = VK_NULL_HANDLE,
469 .subpass = 0,
470 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473 }
474
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600475 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 .pNext = NULL,
478 .outputMask = 0,
479 .inputMask = 0,
480 .oldLayout = old_image_layout,
481 .newLayout = new_image_layout,
482 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400483 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600484 };
485
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600486 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600488 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600489 }
490
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600491 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600493 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494 }
495
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600496 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600497
Tony Barbour50bbca42015-06-29 16:20:35 -0600498 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
499 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600500
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600501 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502}
503
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600504static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505{
Chia-I Wuf973e322015-07-08 13:34:24 +0800506 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600507 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600508 .pNext = NULL,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600509 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Cody Northropa0a178c2015-08-11 11:35:58 -0600510 .renderPass = VK_NULL_HANDLE,
511 .subpass = 0,
512 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700513 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800514 const VkClearValue clear_values[2] = {
515 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
516 [1] = { .ds = { 1.0f, 0 } },
517 };
518 const VkRenderPassBeginInfo rp_begin = {
519 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
520 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800521 .renderPass = demo->render_pass,
522 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800523 .renderArea.offset.x = 0,
524 .renderArea.offset.y = 0,
525 .renderArea.extent.width = demo->width,
526 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600527 .clearValueCount = 2,
528 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700529 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800530 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600531
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600532 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533 assert(!err);
534
Chia-I Wua74c5b22015-07-07 11:50:03 +0800535 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
536
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600539 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600540 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541
Tony Barbour155cce82015-07-03 10:33:54 -0600542 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
543 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
544 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
545 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600547 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800548 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600550 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600551 assert(!err);
552}
553
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600555void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600556{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600557 mat4x4 MVP, Model, VP;
558 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600559 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600560 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600562 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600563
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600564 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600565 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700566 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600567 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600568
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500569 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570 assert(!err);
571
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600572 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500574 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575 assert(!err);
576}
577
578static void demo_draw(struct demo *demo)
579{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600580 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600581 VkSemaphore presentCompleteSemaphore;
582 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
583 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
584 .pNext = NULL,
585 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
586 };
Tony Barbour155cce82015-07-03 10:33:54 -0600587 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600588
Ian Elliottaaae5352015-07-06 14:27:58 -0600589 err = vkCreateSemaphore(demo->device,
590 &presentCompleteSemaphoreCreateInfo,
591 &presentCompleteSemaphore);
592 assert(!err);
593
594 // Get the index of the next available swapchain image:
595 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
596 UINT64_MAX,
597 presentCompleteSemaphore,
598 &demo->current_buffer);
599 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
600 // return codes
601 assert(!err);
602
603 // Wait for the present complete semaphore to be signaled to ensure
604 // that the image won't be rendered to until the presentation
605 // engine has fully released ownership to the application, and it is
606 // okay to render to the image.
607 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
608
609// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600610 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600611 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612 assert(!err);
613
Ian Elliottaaae5352015-07-06 14:27:58 -0600614 VkPresentInfoWSI present = {
Ian Elliott8528eff2015-08-07 15:56:59 -0600615 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
Ian Elliottaaae5352015-07-06 14:27:58 -0600616 .pNext = NULL,
617 .swapChainCount = 1,
618 .swapChains = &demo->swap_chain,
619 .imageIndices = &demo->current_buffer,
620 };
621
622// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600623 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600624 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
625 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626 assert(!err);
627
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600628 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600629 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800630
631 err = vkQueueWaitIdle(demo->queue);
632 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600633}
634
635static void demo_prepare_buffers(struct demo *demo)
636{
Ian Elliottaaae5352015-07-06 14:27:58 -0600637 VkResult U_ASSERT_ONLY err;
638
639 // Check the surface properties and formats
Ian Elliott8528eff2015-08-07 15:56:59 -0600640 VkSurfacePropertiesWSI surfProperties;
641 err = demo->fpGetSurfacePropertiesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600642 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600643 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600644 assert(!err);
645
Ian Elliott8528eff2015-08-07 15:56:59 -0600646 uint32_t presentModeCount;
647 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600648 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600649 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600650 assert(!err);
Ian Elliott8528eff2015-08-07 15:56:59 -0600651 VkPresentModeWSI *presentModes =
652 (VkPresentModeWSI *)malloc(presentModeCount * sizeof(VkPresentModeWSI));
653 assert(presentModes);
654 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600655 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600656 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600657 assert(!err);
658
659 VkExtent2D swapChainExtent;
660 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600661 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600662 {
663 // If the surface size is undefined, the size is set to
664 // the size of the images requested.
665 swapChainExtent.width = demo->width;
666 swapChainExtent.height = demo->height;
667 }
668 else
669 {
670 // If the surface size is defined, the swap chain size must match
Ian Elliott8528eff2015-08-07 15:56:59 -0600671 swapChainExtent = surfProperties.currentExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600672 }
673
674 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600675 // tearing mode. If not, try IMMEDIATE which will usually be available,
676 // and is fastest (though it tears). If not, fall back to FIFO which is
677 // always available.
678 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600679 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott8528eff2015-08-07 15:56:59 -0600680 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_WSI) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600681 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
682 break;
683 }
Ian Elliott3ebce342015-07-27 13:53:11 -0600684 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
Ian Elliott8528eff2015-08-07 15:56:59 -0600685 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
Ian Elliott3ebce342015-07-27 13:53:11 -0600686 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
687 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600688 }
689
Ian Elliott2d47da92015-07-13 12:20:56 -0600690#define WORK_AROUND_CODE
691#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600692 // After the proper code was created, other parts of this demo were
693 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
694 // images, etc. Live with that for now.
695 // TODO: Rework this demo code to live with the number of buffers returned
696 // by vkCreateSwapChainWSI().
Ian Elliott2d47da92015-07-13 12:20:56 -0600697 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
698#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600699 // Determine the number of VkImage's to use in the swap chain (we desire to
700 // own only 1 image at a time, besides the images being displayed and
701 // queued for display):
Ian Elliott8528eff2015-08-07 15:56:59 -0600702 uint32_t desiredNumberOfSwapChainImages = surfProperties.minImageCount + 1;
703 if ((surfProperties.maxImageCount > 0) &&
704 (desiredNumberOfSwapChainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600705 {
706 // Application must settle for fewer images than desired:
Ian Elliott8528eff2015-08-07 15:56:59 -0600707 desiredNumberOfSwapChainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600708 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600709#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600710
711 VkSurfaceTransformFlagBitsWSI preTransform;
Ian Elliott8528eff2015-08-07 15:56:59 -0600712 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600713 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
714 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600715 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600716 }
717
Chia-I Wucbb564e2015-04-16 22:02:10 +0800718 const VkSwapChainCreateInfoWSI swap_chain = {
719 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
720 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
722 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800723 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600724 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800725 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600726 .width = swapChainExtent.width,
727 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600728 },
Ian Elliottaaae5352015-07-06 14:27:58 -0600729 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800730 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600731 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
732 .queueFamilyCount = 0,
733 .pQueueFamilyIndices = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600734 .presentMode = swapChainPresentMode,
735 .oldSwapChain.handle = 0,
736 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600737 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600738 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600739
Jon Ashburn0b85d052015-05-21 18:13:33 -0600740 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800741 assert(!err);
742
Ian Elliott8528eff2015-08-07 15:56:59 -0600743 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
744 &demo->swapChainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600745 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800746
Ian Elliott8528eff2015-08-07 15:56:59 -0600747 VkImage* swapChainImages =
748 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliottaaae5352015-07-06 14:27:58 -0600749 assert(swapChainImages);
Ian Elliott8528eff2015-08-07 15:56:59 -0600750 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
751 &demo->swapChainImageCount,
752 swapChainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600753 assert(!err);
Ian Elliott2d47da92015-07-13 12:20:56 -0600754#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600755 // After the proper code was created, other parts of this demo were
756 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
757 // images, etc. Live with that for now.
758 // TODO: Rework this demo code to live with the number of buffers returned
759 // by vkCreateSwapChainWSI().
Ian Elliott2d47da92015-07-13 12:20:56 -0600760 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
Ian Elliott2d47da92015-07-13 12:20:56 -0600761#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600762
763 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
764 assert(demo->buffers);
765
766 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800767 VkAttachmentViewCreateInfo color_attachment_view = {
768 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600769 .pNext = NULL,
770 .format = demo->format,
771 .mipLevel = 0,
772 .baseArraySlice = 0,
773 .arraySize = 1,
774 };
775
Ian Elliott8528eff2015-08-07 15:56:59 -0600776 demo->buffers[i].image = swapChainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600777
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600778 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400779 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600780 VK_IMAGE_LAYOUT_UNDEFINED,
781 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600782
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600783 color_attachment_view.image = demo->buffers[i].image;
784
Chia-I Wua74c5b22015-07-07 11:50:03 +0800785 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600786 &color_attachment_view, &demo->buffers[i].view);
787 assert(!err);
788 }
789}
790
791static void demo_prepare_depth(struct demo *demo)
792{
Tony Barbour72304ef2015-04-16 15:59:00 -0600793 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600794 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600795 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600796 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600797 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600798 .format = depth_format,
799 .extent = { demo->width, demo->height, 1 },
800 .mipLevels = 1,
801 .arraySize = 1,
802 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600803 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600804 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600805 .flags = 0,
806 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600807 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600808 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500809 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600810 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600811 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600812 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800813 VkAttachmentViewCreateInfo view = {
814 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600815 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600816 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600817 .format = depth_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600818 .mipLevel = 0,
819 .baseArraySlice = 0,
820 .arraySize = 1,
821 .flags = 0,
822 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600823
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500824 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600825 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600826
827 demo->depth.format = depth_format;
828
829 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600830 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600831 &demo->depth.image);
832 assert(!err);
833
Tony Barbour155cce82015-07-03 10:33:54 -0600834 err = vkGetImageMemoryRequirements(demo->device,
835 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600836
837 mem_alloc.allocationSize = mem_reqs.size;
838 err = memory_type_from_properties(demo,
839 mem_reqs.memoryTypeBits,
840 VK_MEMORY_PROPERTY_DEVICE_ONLY,
841 &mem_alloc.memoryTypeIndex);
842 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600843
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500844 /* allocate memory */
845 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
846 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500848 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600849 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500850 demo->depth.mem, 0);
851 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600852
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600853 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400854 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600855 VK_IMAGE_LAYOUT_UNDEFINED,
856 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600857
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600858 /* create image view */
859 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800860 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600861 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600862}
863
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600864/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600865 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600866 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600867 * \param demo : Needed to access VK calls
868 * \param filename : the png file to be loaded
869 * \param width : width of png, to be updated as a side effect of this function
870 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600871 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600872 * \return bool : an opengl texture id. true if successful?,
873 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600874 *
875 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
876 * Modified to copy image to memory
877 *
878 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700879bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600880 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600881 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600882{
883 //header for testing if it is a png
884 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600885 int is_png, bit_depth, color_type, rowbytes;
886 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700887 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600888 png_structp png_ptr;
889 png_infop info_ptr, end_info;
890 png_byte *image_data;
891 png_bytep *row_pointers;
892
893 //open file as binary
894 FILE *fp = fopen(filename, "rb");
895 if (!fp) {
896 return false;
897 }
898
899 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600900 retval = fread(header, 1, 8, fp);
901 if (retval != 8) {
902 fclose(fp);
903 return false;
904 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600905
906 //test if png
907 is_png = !png_sig_cmp(header, 0, 8);
908 if (!is_png) {
909 fclose(fp);
910 return false;
911 }
912
913 //create png struct
914 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
915 NULL, NULL);
916 if (!png_ptr) {
917 fclose(fp);
918 return (false);
919 }
920
921 //create png info struct
922 info_ptr = png_create_info_struct(png_ptr);
923 if (!info_ptr) {
924 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
925 fclose(fp);
926 return (false);
927 }
928
929 //create png info struct
930 end_info = png_create_info_struct(png_ptr);
931 if (!end_info) {
932 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
933 fclose(fp);
934 return (false);
935 }
936
937 //png error stuff, not sure libpng man suggests this.
938 if (setjmp(png_jmpbuf(png_ptr))) {
939 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
940 fclose(fp);
941 return (false);
942 }
943
944 //init png reading
945 png_init_io(png_ptr, fp);
946
947 //let libpng know you already read the first 8 bytes
948 png_set_sig_bytes(png_ptr, 8);
949
950 // read all the info up to the image data
951 png_read_info(png_ptr, info_ptr);
952
953 // get info about png
954 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
955 NULL, NULL, NULL);
956
957 //update width and height based on png info
958 *width = twidth;
959 *height = theight;
960
961 // Require that incoming texture be 8bits per color component
962 // and 4 components (RGBA).
963 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
964 png_get_channels(png_ptr, info_ptr) != 4) {
965 return false;
966 }
967
968 if (rgba_data == NULL) {
969 // If data pointer is null, we just want the width & height
970 // clean up memory and close stuff
971 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
972 fclose(fp);
973
974 return true;
975 }
976
977 // Update the png info struct.
978 png_read_update_info(png_ptr, info_ptr);
979
980 // Row size in bytes.
981 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
982
983 // Allocate the image_data as a big block, to be given to opengl
984 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
985 if (!image_data) {
986 //clean up memory and close stuff
987 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
988 fclose(fp);
989 return false;
990 }
991
992 // row_pointers is for pointing to image_data for reading the png with libpng
993 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
994 if (!row_pointers) {
995 //clean up memory and close stuff
996 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
997 // delete[] image_data;
998 fclose(fp);
999 return false;
1000 }
1001 // set the individual row_pointers to point at the correct offsets of image_data
1002 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001003 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001004
1005 // read the png into image_data through row_pointers
1006 png_read_image(png_ptr, row_pointers);
1007
1008 // clean up memory and close stuff
1009 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1010 free(row_pointers);
1011 free(image_data);
1012 fclose(fp);
1013
1014 return true;
1015}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001016
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017static void demo_prepare_texture_image(struct demo *demo,
1018 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001019 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001020 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001021 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001022 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001023{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001024 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001025 int32_t tex_width;
1026 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001027 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028
David Pinedo30bd71d2015-04-23 08:16:57 -06001029 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1030 {
1031 printf("Failed to load textures\n");
1032 fflush(stdout);
1033 exit(1);
1034 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001036 tex_obj->tex_width = tex_width;
1037 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001039 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001040 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001042 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043 .format = tex_format,
1044 .extent = { tex_width, tex_height, 1 },
1045 .mipLevels = 1,
1046 .arraySize = 1,
1047 .samples = 1,
1048 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001049 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001050 .flags = 0,
1051 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001052 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001053 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001054 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001056 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057 };
1058
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001059 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001060
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001061 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001062 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063 assert(!err);
1064
Tony Barbour155cce82015-07-03 10:33:54 -06001065 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001066 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001067
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001068 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001070 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1071 assert(!err);
1072
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001073 /* allocate memory */
1074 err = vkAllocMemory(demo->device, &mem_alloc,
1075 &(tex_obj->mem));
1076 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001077
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001078 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001079 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001080 tex_obj->mem, 0);
1081 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082
Tony Barbour72304ef2015-04-16 15:59:00 -06001083 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001084 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001085 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086 .mipLevel = 0,
1087 .arraySlice = 0,
1088 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001089 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001090 void *data;
1091
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001092 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1093 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001094
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001095 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001096 assert(!err);
1097
1098 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1099 fprintf(stderr, "Error loading texture: %s\n", filename);
1100 }
1101
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103 assert(!err);
1104 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001105
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001106 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001107 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001108 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001109 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001110 tex_obj->imageLayout);
1111 /* 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 -07001112}
1113
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001114static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115{
1116 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001117 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001118 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119}
1120
1121static void demo_prepare_textures(struct demo *demo)
1122{
Tony Barbour72304ef2015-04-16 15:59:00 -06001123 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001124 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001125 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001126 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001127
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001128 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129 assert(!err);
1130
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001131 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132
FslNopper6a7e50e2015-05-06 21:42:01 +02001133 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134 /* Device can texture using linear textures */
1135 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001136 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001137 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001139 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001140
1141 memset(&staging_texture, 0, sizeof(staging_texture));
1142 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001143 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144
1145 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001146 VK_IMAGE_TILING_OPTIMAL,
1147 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1148 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001149
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001150 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001151 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001152 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001153 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001154
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001155 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001156 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001157 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001158 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001160 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001161 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001163 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164 .destOffset = { 0, 0, 0 },
1165 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1166 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001167 vkCmdCopyImage(demo->cmd,
1168 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1169 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001170 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001171
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001172 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001173 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001174 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001175 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001176
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001177 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001179 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001180 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001181 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1182 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001183 }
1184
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001185 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001187 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001188 .magFilter = VK_TEX_FILTER_NEAREST,
1189 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001190 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 .addressU = VK_TEX_ADDRESS_CLAMP,
1192 .addressV = VK_TEX_ADDRESS_CLAMP,
1193 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001194 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001196 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001197 .minLod = 0.0f,
1198 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001199 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Cody Northrop25439e42015-08-11 15:50:55 -06001200 .texelCoords = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001202
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001203 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001204 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001205 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001206 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001207 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001209 .channels = { VK_CHANNEL_SWIZZLE_R,
1210 VK_CHANNEL_SWIZZLE_G,
1211 VK_CHANNEL_SWIZZLE_B,
1212 VK_CHANNEL_SWIZZLE_A, },
1213 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001214 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001215
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001216 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001217 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001218 &demo->textures[i].sampler);
1219 assert(!err);
1220
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001221 /* create image view */
1222 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001223 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 &demo->textures[i].view);
1225 assert(!err);
1226 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001227}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001229void demo_prepare_cube_data_buffer(struct demo *demo)
1230{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001231 VkBufferCreateInfo buf_info;
1232 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001233 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001234 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001235 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001236 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001237 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001238 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001239 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001240 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001241 int i;
1242 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001243 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001244 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001245
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001246 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001247 mat4x4_mul(MVP, VP, demo->model_matrix);
1248 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001249// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001250
1251 for (i=0; i<12*3; i++) {
1252 data.position[i][0] = g_vertex_buffer_data[i*3];
1253 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1254 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1255 data.position[i][3] = 1.0f;
1256 data.attr[i][0] = g_uv_buffer_data[2*i];
1257 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1258 data.attr[i][2] = 0;
1259 data.attr[i][3] = 0;
1260 }
1261
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001262 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001263 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001264 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001265 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001266 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001267 assert(!err);
1268
Tony Barbour155cce82015-07-03 10:33:54 -06001269 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001270 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001271
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001272 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001273 err = memory_type_from_properties(demo,
1274 mem_reqs.memoryTypeBits,
1275 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1276 &alloc_info.memoryTypeIndex);
1277 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001278
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001279 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1280 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001281
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001282 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1283 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001284
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001285 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001286
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001287 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1288 assert(!err);
1289
Tony Barbour155cce82015-07-03 10:33:54 -06001290 err = vkBindBufferMemory(demo->device,
1291 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001292 demo->uniform_data.mem, 0);
1293 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001294
1295 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001296 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001297 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001298 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001299 view_info.offset = 0;
1300 view_info.range = sizeof(data);
1301
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001302 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001303 assert(!err);
1304
Chia-I Wuae721ba2015-05-25 16:27:55 +08001305 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001306}
1307
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001308static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001309{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001310 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001311 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001312 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001313 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001314 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001315 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001316 },
1317 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001318 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001319 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001320 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001321 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001322 },
1323 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001324 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001325 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001326 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001327 .count = 2,
1328 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001329 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001330 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001331
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001332 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001333 &descriptor_layout, &demo->desc_layout);
1334 assert(!err);
1335
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001336 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1337 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1338 .pNext = NULL,
1339 .descriptorSetCount = 1,
1340 .pSetLayouts = &demo->desc_layout,
1341 };
1342
1343 err = vkCreatePipelineLayout(demo->device,
1344 &pPipelineLayoutCreateInfo,
1345 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347}
1348
Chia-I Wuf973e322015-07-08 13:34:24 +08001349static void demo_prepare_render_pass(struct demo *demo)
1350{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001351 const VkAttachmentDescription attachments[2] = {
1352 [0] = {
1353 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1354 .pNext = NULL,
1355 .format = demo->format,
1356 .samples = 1,
1357 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1358 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1359 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1360 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1361 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1362 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1363 },
1364 [1] = {
1365 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1366 .pNext = NULL,
1367 .format = demo->depth.format,
1368 .samples = 1,
1369 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1370 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1371 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1372 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1373 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1374 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1375 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001376 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001377 const VkAttachmentReference color_reference = {
1378 .attachment = 0,
1379 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1380 };
1381 const VkSubpassDescription subpass = {
1382 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1383 .pNext = NULL,
1384 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1385 .flags = 0,
1386 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001387 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001388 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001389 .pColorAttachments = &color_reference,
1390 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001391 .depthStencilAttachment = {
1392 .attachment = 1,
1393 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1394 },
1395 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001396 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001397 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001398 const VkRenderPassCreateInfo rp_info = {
1399 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1400 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001401 .attachmentCount = 2,
1402 .pAttachments = attachments,
1403 .subpassCount = 1,
1404 .pSubpasses = &subpass,
1405 .dependencyCount = 0,
1406 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001407 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001408 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001409
1410 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1411 assert(!err);
1412}
1413
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001414static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001415 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001416 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001417 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001418 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001420 VkShaderModuleCreateInfo moduleCreateInfo;
1421 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001422 VkShader shader;
1423 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001425
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001426 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1427 moduleCreateInfo.pNext = NULL;
1428
1429 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1430 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001431 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432
Cody Northrop1fedb212015-05-28 11:27:16 -06001433 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001434 moduleCreateInfo.codeSize = size;
1435 moduleCreateInfo.pCode = code;
1436 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001437 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001438 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001439 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001440 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001441
1442 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001443 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001444 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001445 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001446 } else {
1447 // Create fake SPV structure to feed GLSL
1448 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001449 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1450 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1451 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001452
1453 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001454 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1455 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1456 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1457 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001458
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001459 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001460 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001461 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001462 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001463
1464 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001465 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001466 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001467 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001468 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001469 return shader;
1470}
1471
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001472char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001473{
1474 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001475 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001476 void *shader_code;
1477
1478 FILE *fp = fopen(filename, "rb");
1479 if (!fp) return NULL;
1480
1481 fseek(fp, 0L, SEEK_END);
1482 size = ftell(fp);
1483
1484 fseek(fp, 0L, SEEK_SET);
1485
1486 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001487 retval = fread(shader_code, size, 1, fp);
1488 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001489
1490 *psize = size;
1491
1492 return shader_code;
1493}
1494
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001495static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001496{
Cody Northrop1fedb212015-05-28 11:27:16 -06001497 if (!demo->use_glsl) {
1498 void *vertShaderCode;
1499 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001500
Cody Northrop1fedb212015-05-28 11:27:16 -06001501 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001502
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001503 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001504 vertShaderCode, size);
1505 } else {
1506 static const char *vertShaderText =
1507 "#version 140\n"
1508 "#extension GL_ARB_separate_shader_objects : enable\n"
1509 "#extension GL_ARB_shading_language_420pack : enable\n"
1510 "\n"
1511 "layout(binding = 0) uniform buf {\n"
1512 " mat4 MVP;\n"
1513 " vec4 position[12*3];\n"
1514 " vec4 attr[12*3];\n"
1515 "} ubuf;\n"
1516 "\n"
1517 "layout (location = 0) out vec4 texcoord;\n"
1518 "\n"
1519 "void main() \n"
1520 "{\n"
1521 " texcoord = ubuf.attr[gl_VertexID];\n"
1522 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1523 "\n"
1524 " // GL->VK conventions\n"
1525 " gl_Position.y = -gl_Position.y;\n"
1526 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1527 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001528
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001529 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001530 (const void *) vertShaderText,
1531 strlen(vertShaderText));
1532 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001533}
1534
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001535static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001536{
Cody Northrop1fedb212015-05-28 11:27:16 -06001537 if (!demo->use_glsl) {
1538 void *fragShaderCode;
1539 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001540
Cody Northrop1fedb212015-05-28 11:27:16 -06001541 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001542
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001543 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001544 fragShaderCode, size);
1545 } else {
1546 static const char *fragShaderText =
1547 "#version 140\n"
1548 "#extension GL_ARB_separate_shader_objects : enable\n"
1549 "#extension GL_ARB_shading_language_420pack : enable\n"
1550 "layout (binding = 1) uniform sampler2D tex;\n"
1551 "\n"
1552 "layout (location = 0) in vec4 texcoord;\n"
1553 "layout (location = 0) out vec4 uFragColor;\n"
1554 "void main() {\n"
1555 " uFragColor = texture(tex, texcoord.xy);\n"
1556 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001557
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001558 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001559 (const void *) fragShaderText,
1560 strlen(fragShaderText));
1561 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562}
1563
1564static void demo_prepare_pipeline(struct demo *demo)
1565{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001566 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001567 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001568 VkPipelineInputAssemblyStateCreateInfo ia;
1569 VkPipelineRasterStateCreateInfo rs;
1570 VkPipelineColorBlendStateCreateInfo cb;
1571 VkPipelineDepthStencilStateCreateInfo ds;
1572 VkPipelineViewportStateCreateInfo vp;
1573 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001574 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001575
1576 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001577 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001578 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001579
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001580 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001581 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001582 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583
1584 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001585 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001586 rs.fillMode = VK_FILL_MODE_SOLID;
1587 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001588 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001589 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001590
1591 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001592 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1593 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001594 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001595 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001596 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001597 cb.attachmentCount = 1;
1598 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001599
Tony Barbour29645d02015-01-16 14:27:35 -07001600 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001601 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001602 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001603
1604 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001605 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001606 ds.depthTestEnable = VK_TRUE;
1607 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001608 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001609 ds.depthBoundsEnable = VK_FALSE;
1610 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1611 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001612 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001613 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001614 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001615
Tony Barbour29645d02015-01-16 14:27:35 -07001616 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001617 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001618 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001619 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001620
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001621 // Two stages: vs and fs
1622 pipeline.stageCount = 2;
1623 VkPipelineShaderStageCreateInfo shaderStages[2];
1624 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1625
1626 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1627 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1628 shaderStages[0].shader = demo_prepare_vs(demo);
1629
1630 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1631 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1632 shaderStages[1].shader = demo_prepare_fs(demo);
1633
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001634 memset(&pipelineCache, 0, sizeof(pipelineCache));
1635 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001637 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1638 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001639
1640 pipeline.pVertexInputState = NULL;
1641 pipeline.pInputAssemblyState = &ia;
1642 pipeline.pRasterState = &rs;
1643 pipeline.pColorBlendState = &cb;
1644 pipeline.pMultisampleState = &ms;
1645 pipeline.pViewportState = &vp;
1646 pipeline.pDepthStencilState = &ds;
1647 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001648 pipeline.renderPass = demo->render_pass;
Tony Barbour194bf282015-07-10 15:29:03 -06001649
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001650 pipeline.renderPass = demo->render_pass;
1651
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001652 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001653 assert(!err);
1654
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001655 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001656 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001657 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001658 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1659 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001660}
1661
1662static void demo_prepare_dynamic_states(struct demo *demo)
1663{
Tony Barbour155cce82015-07-03 10:33:54 -06001664 VkDynamicViewportStateCreateInfo viewport_create;
1665 VkDynamicRasterStateCreateInfo raster;
1666 VkDynamicColorBlendStateCreateInfo color_blend;
1667 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001668 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669
Tony Barbour29645d02015-01-16 14:27:35 -07001670 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001671 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001672 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001673 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001674 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001675 viewport.height = (float) demo->height;
1676 viewport.width = (float) demo->width;
1677 viewport.minDepth = (float) 0.0f;
1678 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001679 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001680 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001681 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001682 scissor.extent.width = demo->width;
1683 scissor.extent.height = demo->height;
1684 scissor.offset.x = 0;
1685 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001686 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687
1688 memset(&raster, 0, sizeof(raster));
Tony Barbour155cce82015-07-03 10:33:54 -06001689 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001690 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691
1692 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour155cce82015-07-03 10:33:54 -06001693 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001694 color_blend.blendConst[0] = 1.0f;
1695 color_blend.blendConst[1] = 1.0f;
1696 color_blend.blendConst[2] = 1.0f;
1697 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001698
1699 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour155cce82015-07-03 10:33:54 -06001700 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001701 depth_stencil.minDepthBounds = 0.0f;
1702 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001703 depth_stencil.stencilBackRef = 0;
1704 depth_stencil.stencilFrontRef = 0;
1705 depth_stencil.stencilReadMask = 0xff;
1706 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001708 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709 assert(!err);
1710
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001711 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001712 assert(!err);
1713
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001714 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001715 &color_blend, &demo->color_blend);
1716 assert(!err);
1717
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001718 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 &depth_stencil, &demo->depth_stencil);
1720 assert(!err);
1721}
1722
Chia-I Wu63ea9262015-03-26 13:14:16 +08001723static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001724{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001725 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001726 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001727 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001728 .count = 1,
1729 },
1730 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001731 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001732 .count = DEMO_TEXTURE_COUNT,
1733 },
1734 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001735 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001736 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001737 .pNext = NULL,
1738 .count = 2,
1739 .pTypeCount = type_counts,
1740 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001741 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001742
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001743 err = vkCreateDescriptorPool(demo->device,
1744 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001745 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001746 assert(!err);
1747}
1748
1749static void demo_prepare_descriptor_set(struct demo *demo)
1750{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001751 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1752 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001753 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001754 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001755
Mike Stroyanebae8322015-04-17 12:36:38 -06001756 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001757 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001758 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001759 &demo->desc_set);
1760 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001761
Chia-I Wuae721ba2015-05-25 16:27:55 +08001762 memset(&tex_descs, 0, sizeof(tex_descs));
1763 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1764 tex_descs[i].sampler = demo->textures[i].sampler;
1765 tex_descs[i].imageView = demo->textures[i].view;
1766 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1767 }
1768
1769 memset(&writes, 0, sizeof(writes));
1770
1771 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1772 writes[0].destSet = demo->desc_set;
1773 writes[0].count = 1;
1774 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1775 writes[0].pDescriptors = &demo->uniform_data.desc;
1776
1777 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1778 writes[1].destSet = demo->desc_set;
1779 writes[1].destBinding = 1;
1780 writes[1].count = DEMO_TEXTURE_COUNT;
1781 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1782 writes[1].pDescriptors = tex_descs;
1783
1784 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1785 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001786}
1787
Chia-I Wuf973e322015-07-08 13:34:24 +08001788static void demo_prepare_framebuffers(struct demo *demo)
1789{
Cody Northrop2e11cad2015-08-04 10:47:08 -06001790 VkAttachmentView attachments[2];
1791 attachments[1] = demo->depth.view;
1792
Chia-I Wuf973e322015-07-08 13:34:24 +08001793 const VkFramebufferCreateInfo fb_info = {
1794 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1795 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001796 .renderPass = demo->render_pass,
1797 .attachmentCount = 2,
1798 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001799 .width = demo->width,
1800 .height = demo->height,
1801 .layers = 1,
1802 };
1803 VkResult U_ASSERT_ONLY err;
1804 uint32_t i;
1805
1806 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001807 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001808 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1809 assert(!err);
1810 }
1811}
1812
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001813static void demo_prepare(struct demo *demo)
1814{
Cody Northrop91e221b2015-07-09 18:08:32 -06001815 VkResult U_ASSERT_ONLY err;
1816
1817 const VkCmdPoolCreateInfo cmd_pool_info = {
1818 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1819 .pNext = NULL,
1820 .queueFamilyIndex = demo->graphics_queue_node_index,
1821 .flags = 0,
1822 };
1823 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1824 assert(!err);
1825
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001826 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001827 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001828 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001829 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001830 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001831 .flags = 0,
1832 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001833
1834 demo_prepare_buffers(demo);
1835 demo_prepare_depth(demo);
1836 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001837 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001839 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001840 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001841 demo_prepare_pipeline(demo);
1842 demo_prepare_dynamic_states(demo);
1843
Ian Elliottaaae5352015-07-06 14:27:58 -06001844 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001845 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001846 assert(!err);
1847 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001848
Chia-I Wu63ea9262015-03-26 13:14:16 +08001849 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001850 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001851
Chia-I Wuf973e322015-07-08 13:34:24 +08001852 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001853
Ian Elliottaaae5352015-07-06 14:27:58 -06001854 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001855 demo->current_buffer = i;
1856 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1857 }
1858
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001859 /*
1860 * Prepare functions above may generate pipeline commands
1861 * that need to be flushed before beginning the render loop.
1862 */
1863 demo_flush_init_cmd(demo);
1864
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001865 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001866 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001867}
1868
David Pinedo2bb7c932015-06-18 17:03:14 -06001869static void demo_cleanup(struct demo *demo)
1870{
1871 uint32_t i;
1872
1873 demo->prepared = false;
1874
Tony Barbour155cce82015-07-03 10:33:54 -06001875 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1876 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1877 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001878 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001879 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001880
Tony Barbour155cce82015-07-03 10:33:54 -06001881 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1882 vkDestroyDynamicRasterState(demo->device, demo->raster);
1883 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1884 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001885
Tony Barbour155cce82015-07-03 10:33:54 -06001886 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001887 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001888 vkDestroyRenderPass(demo->device, demo->render_pass);
1889 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1890 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001891
1892 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001893 vkDestroyImageView(demo->device, demo->textures[i].view);
1894 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001895 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001896 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001897 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001898 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001899
Tony Barbour155cce82015-07-03 10:33:54 -06001900 vkDestroyAttachmentView(demo->device, demo->depth.view);
1901 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001902 vkFreeMemory(demo->device, demo->depth.mem);
1903
Tony Barbour155cce82015-07-03 10:33:54 -06001904 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1905 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001906 vkFreeMemory(demo->device, demo->uniform_data.mem);
1907
Ian Elliottaaae5352015-07-06 14:27:58 -06001908 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001909 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1910 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001911 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001912 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001913
Cody Northrop91e221b2015-07-09 18:08:32 -06001914 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001915 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001916 if (demo->validate) {
1917 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1918 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001919 vkDestroyInstance(demo->inst);
1920
1921#ifndef _WIN32
1922 xcb_destroy_window(demo->connection, demo->window);
1923 xcb_disconnect(demo->connection);
1924#endif // _WIN32
1925}
1926
1927// On MS-Windows, make this a global, so it's available to WndProc()
1928struct demo demo;
1929
Ian Elliott639ca472015-04-16 15:23:05 -06001930#ifdef _WIN32
1931static void demo_run(struct demo *demo)
1932{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001933 if (!demo->prepared)
1934 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001935 // Wait for work to finish before updating MVP.
1936 vkDeviceWaitIdle(demo->device);
1937 demo_update_data_buffer(demo);
1938
1939 demo_draw(demo);
1940
1941 // Wait for work to finish before updating MVP.
1942 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001943
David Pinedo2bb7c932015-06-18 17:03:14 -06001944 demo->curFrame++;
1945
1946 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1947 {
1948 demo->quit=true;
1949 demo_cleanup(demo);
1950 ExitProcess(0);
1951 }
1952
1953}
Ian Elliott639ca472015-04-16 15:23:05 -06001954
1955// MS-Windows event handling function:
1956LRESULT CALLBACK WndProc(HWND hWnd,
1957 UINT uMsg,
1958 WPARAM wParam,
1959 LPARAM lParam)
1960{
Ian Elliott639ca472015-04-16 15:23:05 -06001961 switch(uMsg)
1962 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001963 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001964 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001965 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001966 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001967 demo_run(&demo);
1968 return 0;
1969 default:
1970 break;
1971 }
1972 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1973}
1974
1975static void demo_create_window(struct demo *demo)
1976{
1977 WNDCLASSEX win_class;
1978
1979 // Initialize the window class structure:
1980 win_class.cbSize = sizeof(WNDCLASSEX);
1981 win_class.style = CS_HREDRAW | CS_VREDRAW;
1982 win_class.lpfnWndProc = WndProc;
1983 win_class.cbClsExtra = 0;
1984 win_class.cbWndExtra = 0;
1985 win_class.hInstance = demo->connection; // hInstance
1986 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1987 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1988 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1989 win_class.lpszMenuName = NULL;
1990 win_class.lpszClassName = demo->name;
1991 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1992 // Register window class:
1993 if (!RegisterClassEx(&win_class)) {
1994 // It didn't work, so try to give a useful error:
1995 printf("Unexpected error trying to start the application!\n");
1996 fflush(stdout);
1997 exit(1);
1998 }
1999 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06002000 RECT wr = { 0, 0, demo->width, demo->height };
2001 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002002 demo->window = CreateWindowEx(0,
2003 demo->name, // class name
2004 demo->name, // app name
2005 WS_OVERLAPPEDWINDOW | // window style
2006 WS_VISIBLE |
2007 WS_SYSMENU,
2008 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06002009 wr.right-wr.left, // width
2010 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06002011 NULL, // handle to parent
2012 NULL, // handle to menu
2013 demo->connection, // hInstance
2014 NULL); // no extra parameters
2015 if (!demo->window) {
2016 // It didn't work, so try to give a useful error:
2017 printf("Cannot create a window in which to draw!\n");
2018 fflush(stdout);
2019 exit(1);
2020 }
2021}
2022#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002023static void demo_handle_event(struct demo *demo,
2024 const xcb_generic_event_t *event)
2025{
Piers Daniell735ee532015-02-23 16:23:13 -07002026 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002027 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002028 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002029 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002031 case XCB_CLIENT_MESSAGE:
2032 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2033 (*demo->atom_wm_delete_window).atom) {
2034 demo->quit = true;
2035 }
2036 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002037 case XCB_KEY_RELEASE:
2038 {
2039 const xcb_key_release_event_t *key =
2040 (const xcb_key_release_event_t *) event;
2041
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002042 switch (key->detail) {
2043 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002045 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002046 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002047 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002048 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002049 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002050 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002051 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002052 case 0x41:
2053 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002054 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002055 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056 }
2057 break;
2058 default:
2059 break;
2060 }
2061}
2062
2063static void demo_run(struct demo *demo)
2064{
2065 xcb_flush(demo->connection);
2066
2067 while (!demo->quit) {
2068 xcb_generic_event_t *event;
2069
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002070 if (demo->pause) {
2071 event = xcb_wait_for_event(demo->connection);
2072 } else {
2073 event = xcb_poll_for_event(demo->connection);
2074 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002075 if (event) {
2076 demo_handle_event(demo, event);
2077 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002078 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002079
2080 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002081 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002082 demo_update_data_buffer(demo);
2083
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002084 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002085
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002086 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002087 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002088 demo->curFrame++;
2089 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2090 demo->quit = true;
2091
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002092 }
2093}
2094
2095static void demo_create_window(struct demo *demo)
2096{
2097 uint32_t value_mask, value_list[32];
2098
2099 demo->window = xcb_generate_id(demo->connection);
2100
2101 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2102 value_list[0] = demo->screen->black_pixel;
2103 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2104 XCB_EVENT_MASK_EXPOSURE;
2105
2106 xcb_create_window(demo->connection,
2107 XCB_COPY_FROM_PARENT,
2108 demo->window, demo->screen->root,
2109 0, 0, demo->width, demo->height, 0,
2110 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2111 demo->screen->root_visual,
2112 value_mask, value_list);
2113
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002114 /* Magic code that will send notification when window is destroyed */
2115 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2116 "WM_PROTOCOLS");
2117 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2118
2119 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2120 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2121
2122 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2123 demo->window, (*reply).atom, 4, 32, 1,
2124 &(*demo->atom_wm_delete_window).atom);
2125 free(reply);
2126
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002127 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002128
2129 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2130 const uint32_t coords[] = {100, 100};
2131 xcb_configure_window(demo->connection, demo->window,
2132 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002133}
Ian Elliott639ca472015-04-16 15:23:05 -06002134#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002135
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002136/*
2137 * Return 1 (true) if all layer names specified in check_names
2138 * can be found in given layer properties.
2139 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002140static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002141 uint32_t layer_count, VkLayerProperties *layers)
2142{
2143 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002144 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002145 for (uint32_t j = 0; j < layer_count; j++) {
2146 if (!strcmp(check_names[i], layers[j].layerName)) {
2147 found = 1;
2148 }
2149 }
2150 if (!found) {
2151 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2152 return 0;
2153 }
2154 }
2155 return 1;
2156}
2157
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002158static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002159{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002160 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002161 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002162 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002163 VkLayerProperties *instance_layers;
2164 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002165 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002166 uint32_t instance_layer_count = 0;
2167 uint32_t enabled_extension_count = 0;
2168 uint32_t enabled_layer_count = 0;
2169
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002170 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002171 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002172 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002173 "ObjectTracker",
2174 "DrawState",
2175 "ParamChecker",
2176 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002177 };
2178
2179 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002180 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002181 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002182 "ObjectTracker",
2183 "DrawState",
2184 "ParamChecker",
2185 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002186 };
2187
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002188 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002189 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002190 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002191 assert(!err);
2192
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002193 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2194 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2195 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002196
2197 if (demo->validate) {
2198 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2199 instance_layer_count, instance_layers);
2200 if (!validation_found) {
2201 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2202 "required validation layer.\n\n"
2203 "Please look at the Getting Started guide for additional "
2204 "information.\n",
2205 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002206 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002207 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002208 }
2209
2210 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2211 assert(!err);
2212
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002213 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002214 memset(extension_names, 0, sizeof(extension_names));
2215 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2216 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2217 assert(!err);
2218 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002219 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002220 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002221 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002222 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002223 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002224 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002225 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002226 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002227 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002228 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002229 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002230 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002231 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002232 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002233 "Vulkan installable client driver (ICD) installed?\nPlease "
2234 "look at the Getting Started guide for additional "
2235 "information.\n",
2236 "vkCreateInstance Failure");
2237 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002238 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002239 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002240 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002241 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002242 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002243 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002244 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002245 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002246 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002247 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002248 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002249 .pNext = NULL,
2250 .pAppInfo = &app,
2251 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002252 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002253 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002254 .extensionCount = enabled_extension_count,
2255 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002256 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002257 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002258 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002259 .queueCount = 1,
2260 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002261
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002262 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002263
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002264 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002265 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2266 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002267 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002268 "additional information.\n",
2269 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002270 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2271 ERR_EXIT("Cannot find a specified extension library"
2272 ".\nMake sure your layers path is set appropriately\n",
2273 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002274 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002275 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2276 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002277 "the Getting Started guide for additional information.\n",
2278 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002279 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002280
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002281 free(instance_layers);
2282 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002283
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002284 gpu_count = 1;
2285 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286 assert(!err && gpu_count == 1);
2287
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002288 /* Look for validation layers */
2289 validation_found = 0;
2290 enabled_layer_count = 0;
2291 uint32_t device_layer_count = 0;
2292 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2293 assert(!err);
2294
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002295 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2296 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2297 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002298
2299 if (demo->validate) {
2300 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2301 device_layer_count, device_layers);
2302 if (!validation_found) {
2303 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2304 "a required validation layer.\n\n"
2305 "Please look at the Getting Started guide for additional "
2306 "information.\n",
2307 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002308 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002309 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002310 }
2311
2312 uint32_t device_extension_count = 0;
2313 VkExtensionProperties *device_extensions = NULL;
2314 err = vkGetPhysicalDeviceExtensionProperties(
2315 demo->gpu, NULL, &device_extension_count, NULL);
2316 assert(!err);
2317
2318 WSIextFound = 0;
2319 enabled_extension_count = 0;
2320 memset(extension_names, 0, sizeof(extension_names));
2321 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2322 err = vkGetPhysicalDeviceExtensionProperties(
2323 demo->gpu, NULL, &device_extension_count, device_extensions);
2324 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002325
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002326 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002327 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002328 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002329 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002330 }
2331 assert(enabled_extension_count < 64);
2332 }
2333 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002334 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2335 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002336 "Vulkan installable client driver (ICD) installed?\nPlease "
2337 "look at the Getting Started guide for additional "
2338 "information.\n",
2339 "vkCreateInstance Failure");
2340 }
2341
2342 VkDeviceCreateInfo device = {
2343 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2344 .pNext = NULL,
2345 .queueRecordCount = 1,
2346 .pRequestedQueues = &queue,
2347 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002348 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002349 .extensionCount = enabled_extension_count,
2350 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002351 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002352
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002353 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002354 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2355 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002356 if (!demo->dbgCreateMsgCallback) {
2357 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2358 "vkGetProcAddr Failure");
2359 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002360 if (!demo->dbgDestroyMsgCallback) {
2361 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2362 "vkGetProcAddr Failure");
2363 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002364 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2365 if (!demo->dbgBreakCallback) {
2366 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2367 "vkGetProcAddr Failure");
2368 }
2369
2370 PFN_vkDbgMsgCallback callback;
2371
2372 if (!demo->use_break) {
2373 callback = dbgFunc;
2374 } else {
2375 callback = demo->dbgBreakCallback;
2376 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002377 err = demo->dbgCreateMsgCallback(
2378 demo->inst,
2379 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002380 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002381 &demo->msg_callback);
2382 switch (err) {
2383 case VK_SUCCESS:
2384 break;
2385 case VK_ERROR_INVALID_POINTER:
2386 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2387 "dbgCreateMsgCallback Failure");
2388 break;
2389 case VK_ERROR_OUT_OF_HOST_MEMORY:
2390 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2391 "dbgCreateMsgCallback Failure");
2392 break;
2393 default:
2394 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2395 "dbgCreateMsgCallback Failure");
2396 break;
2397 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002398 }
2399
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002400 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002401 assert(!err);
2402
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002403 free(device_layers);
2404
Ian Elliottaaae5352015-07-06 14:27:58 -06002405 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott8528eff2015-08-07 15:56:59 -06002406 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
2407 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
2408 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002409 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2410 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2411 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott8528eff2015-08-07 15:56:59 -06002412 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002413 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002414 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002415
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002416 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002417 assert(!err);
2418
Cody Northrop4d3c11d2015-08-03 17:04:53 -06002419 /* Call with NULL data to get count */
2420 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002421 assert(!err);
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002422 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002423
Cody Northrop4d3c11d2015-08-03 17:04:53 -06002424 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2425 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002426 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002427 assert(demo->queue_count >= 1);
2428}
2429
2430static void demo_init_vk_wsi(struct demo *demo)
2431{
2432 VkResult err;
2433 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002434
Ian Elliottaaae5352015-07-06 14:27:58 -06002435 // Construct the WSI surface description:
2436 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2437 demo->surface_description.pNext = NULL;
2438#ifdef _WIN32
2439 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2440 demo->surface_description.pPlatformHandle = demo->connection;
2441 demo->surface_description.pPlatformWindow = demo->window;
2442#else // _WIN32
2443 demo->platform_handle_xcb.connection = demo->connection;
2444 demo->platform_handle_xcb.root = demo->screen->root;
2445 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2446 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2447 demo->surface_description.pPlatformWindow = &demo->window;
2448#endif // _WIN32
2449
2450 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002451 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2452 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002453 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2454 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2455 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002456 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002457
2458 // Search for a graphics and a present queue in the array of queue
2459 // families, try to find one that supports both
2460 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2461 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002462 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002463 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2464 if (graphicsQueueNodeIndex == UINT32_MAX) {
2465 graphicsQueueNodeIndex = i;
2466 }
2467
2468 if (supportsPresent[i] == VK_TRUE) {
2469 graphicsQueueNodeIndex = i;
2470 presentQueueNodeIndex = i;
2471 break;
2472 }
2473 }
2474 }
2475 if (presentQueueNodeIndex == UINT32_MAX) {
2476 // If didn't find a queue that supports both graphics and present, then
2477 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002478 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002479 if (supportsPresent[i] == VK_TRUE) {
2480 presentQueueNodeIndex = i;
2481 break;
2482 }
2483 }
2484 }
2485 free(supportsPresent);
2486
2487 // Generate error if could not find both a graphics and a present queue
2488 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2489 ERR_EXIT("Could not find a graphics and a present queue\n",
2490 "WSI Initialization Failure");
2491 }
2492
2493 // TODO: Add support for separate queues, including presentation,
2494 // synchronization, and appropriate tracking for QueueSubmit
2495 // While it is possible for an application to use a separate graphics and a
2496 // present queues, this demo program assumes it is only using one:
2497 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2498 ERR_EXIT("Could not find a common graphics and a present queue\n",
2499 "WSI Initialization Failure");
2500 }
2501
2502 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002503
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002504 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002505 0, &demo->queue);
2506 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002507
Ian Elliottaaae5352015-07-06 14:27:58 -06002508 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002509 uint32_t formatCount;
2510 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -06002511 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002512 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002513 assert(!err);
Ian Elliott8528eff2015-08-07 15:56:59 -06002514 VkSurfaceFormatWSI *surfFormats =
2515 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
2516 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -06002517 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002518 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002519 assert(!err);
2520 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2521 // the surface has no preferred format. Otherwise, at least one
2522 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002523 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2524 {
2525 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2526 }
2527 else
2528 {
2529 assert(formatCount >= 1);
2530 demo->format = surfFormats[0].format;
2531 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002532 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002533
David Pinedo2bb7c932015-06-18 17:03:14 -06002534 demo->quit = false;
2535 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002536
2537 // Get Memory information and properties
2538 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2539 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002540}
2541
2542static void demo_init_connection(struct demo *demo)
2543{
Ian Elliott639ca472015-04-16 15:23:05 -06002544#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002545 const xcb_setup_t *setup;
2546 xcb_screen_iterator_t iter;
2547 int scr;
2548
2549 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002550 if (demo->connection == NULL) {
2551 printf("Cannot find a compatible Vulkan installable client driver "
2552 "(ICD).\nExiting ...\n");
2553 fflush(stdout);
2554 exit(1);
2555 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002556
2557 setup = xcb_get_setup(demo->connection);
2558 iter = xcb_setup_roots_iterator(setup);
2559 while (scr-- > 0)
2560 xcb_screen_next(&iter);
2561
2562 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002563#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002564}
2565
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002566static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002567{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002568 vec3 eye = {0.0f, 3.0f, 5.0f};
2569 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002570 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002571
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002572 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002573 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002574
Piers Daniell735ee532015-02-23 16:23:13 -07002575 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002576 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002577 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002578 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002579 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002580 if (strcmp(argv[i], "--use_glsl") == 0) {
2581 demo->use_glsl = true;
2582 continue;
2583 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002584 if (strcmp(argv[i], "--break") == 0) {
2585 demo->use_break = true;
2586 continue;
2587 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002588 if (strcmp(argv[i], "--validate") == 0) {
2589 demo->validate = true;
2590 continue;
2591 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002592 if (strcmp(argv[i], "--c") == 0 &&
2593 demo->frameCount == INT_MAX &&
2594 i < argc-1 &&
2595 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2596 demo->frameCount >= 0)
2597 {
2598 i++;
2599 continue;
2600 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002601
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002602 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002603 fflush(stderr);
2604 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002605 }
2606
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002607 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002608 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002609
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002610 demo->width = 500;
2611 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002612
2613 demo->spin_angle = 0.01f;
2614 demo->spin_increment = 0.01f;
2615 demo->pause = false;
2616
Piers Daniell735ee532015-02-23 16:23:13 -07002617 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002618 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2619 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002620}
2621
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002622
Ian Elliott639ca472015-04-16 15:23:05 -06002623#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002624extern int __getmainargs(
2625 int * _Argc,
2626 char *** _Argv,
2627 char *** _Env,
2628 int _DoWildCard,
2629 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002630
Ian Elliotta748eaf2015-04-28 15:50:36 -06002631int WINAPI WinMain(HINSTANCE hInstance,
2632 HINSTANCE hPrevInstance,
2633 LPSTR pCmdLine,
2634 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002635{
2636 MSG msg; // message
2637 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002638 int argc;
2639 char** argv;
2640 char** env;
2641 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002642
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002643 __getmainargs(&argc,&argv,&env,0,&new_mode);
2644
2645 demo_init(&demo, argc, argv);
2646 demo.connection = hInstance;
2647 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002648 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002649 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002650
2651 demo_prepare(&demo);
2652
2653 done = false; //initialize loop condition variable
2654 /* main message loop*/
2655 while(!done)
2656 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002657 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002658 if (msg.message == WM_QUIT) //check for a quit message
2659 {
2660 done = true; //if found, quit app
2661 }
2662 else
2663 {
2664 /* Translate and dispatch to event queue*/
2665 TranslateMessage(&msg);
2666 DispatchMessage(&msg);
2667 }
2668 }
2669
2670 demo_cleanup(&demo);
2671
Tony Barbourf9921732015-04-22 11:36:22 -06002672 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002673}
2674#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002675int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002676{
2677 struct demo demo;
2678
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002679 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002680 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002681 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002682
2683 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002684 demo_run(&demo);
2685
2686 demo_cleanup(&demo);
2687
2688 return 0;
2689}
Ian Elliott639ca472015-04-16 15:23:05 -06002690#endif // _WIN32