blob: 7d1d10ffd8ea0c43a3b2fa001d611b2752579db4 [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
Mark Lobodzinskif871f772015-09-01 09:00:16 -060046#include "vk_sdk_platform.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060048#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060049
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050#define DEMO_BUFFER_COUNT 2
51#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060055#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
56
Tony Barbourfdc2d352015-04-22 09:02:32 -060057#if defined(NDEBUG) && defined(__GNUC__)
58#define U_ASSERT_ONLY __attribute__((unused))
59#else
60#define U_ASSERT_ONLY
61#endif
62
Ian Elliott07264132015-04-28 11:35:02 -060063#ifdef _WIN32
64#define ERR_EXIT(err_msg, err_class) \
65 do { \
66 MessageBox(NULL, err_msg, err_class, MB_OK); \
67 exit(1); \
68 } while (0)
69
Ian Elliott07264132015-04-28 11:35:02 -060070#else // _WIN32
71
72#define ERR_EXIT(err_msg, err_class) \
73 do { \
74 printf(err_msg); \
75 fflush(stdout); \
76 exit(1); \
77 } while (0)
78#endif // _WIN32
79
Ian Elliottaaae5352015-07-06 14:27:58 -060080#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
81{ \
82 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
83 if (demo->fp##entrypoint == NULL) { \
84 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
85 "vkGetInstanceProcAddr Failure"); \
86 } \
87}
88
Ian Elliott673898b2015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -060091 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060092 if (demo->fp##entrypoint == NULL) { \
93 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
94 "vkGetDeviceProcAddr Failure"); \
95 } \
96}
97
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060098/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070099 * structure to track all objects related to a texture.
100 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600101struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600102 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700103
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600104 VkImage image;
105 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600106
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500107 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600108 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700109 int32_t tex_width, tex_height;
110};
111
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600112static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600113 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600114};
115
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600116struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600117 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600118 float mvp[4][4];
119 float position[12*3][4];
120 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600121};
122
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600123struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600124 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600125 float mvp[4][4];
126 float position[12*3][4];
127 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600128};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600129
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600130//--------------------------------------------------------------------------------------
131// Mesh and VertexFormat Data
132//--------------------------------------------------------------------------------------
133struct Vertex
134{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600137};
138
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600139struct VertexPosTex
140{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600141 float posX, posY, posZ, posW; // Position data
142 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600143};
144
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600145#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600147
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600148static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800149 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150 -1.0f,-1.0f, 1.0f,
151 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800152 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600153 -1.0f, 1.0f,-1.0f,
154 -1.0f,-1.0f,-1.0f,
155
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 1.0f, 1.0f,-1.0f,
158 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800159 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600161 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600165 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f, 1.0f, 1.0f,
172 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600175 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 1.0f, 1.0f,
179 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 1.0f,-1.0f,-1.0f,
182 1.0f, 1.0f,-1.0f,
183
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 1.0f,-1.0f, 1.0f,
189 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600190};
191
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600192static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 0.0f, 0.0f, // -X side
194 1.0f, 0.0f,
195 1.0f, 1.0f,
196 1.0f, 1.0f,
197 0.0f, 1.0f,
198 0.0f, 0.0f,
199
200 1.0f, 0.0f, // -Z side
201 0.0f, 1.0f,
202 0.0f, 0.0f,
203 1.0f, 0.0f,
204 1.0f, 1.0f,
205 0.0f, 1.0f,
206
207 1.0f, 1.0f, // -Y side
208 1.0f, 0.0f,
209 0.0f, 0.0f,
210 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600211 0.0f, 0.0f,
212 0.0f, 1.0f,
213
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800214 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600215 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600217 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600218 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600219 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800221 1.0f, 1.0f, // +X side
222 0.0f, 1.0f,
223 0.0f, 0.0f,
224 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600226 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 0.0f, 1.0f, // +Z side
229 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600230 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600231 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600234};
235
236void dumpMatrix(const char *note, mat4x4 MVP)
237{
238 int i;
239
240 printf("%s: \n", note);
241 for (i=0; i<4; i++) {
242 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
243 }
244 printf("\n");
245 fflush(stdout);
246}
247
248void dumpVec4(const char *note, vec4 vector)
249{
250 printf("%s: \n", note);
251 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
252 printf("\n");
253 fflush(stdout);
254}
255
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600256void dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600257 VkFlags msgFlags,
258 VkDbgObjectType objType,
259 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600260 size_t location,
261 int32_t msgCode,
262 const char* pLayerPrefix,
263 const char* pMsg,
264 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600265{
266 char *message = (char *) malloc(strlen(pMsg)+100);
267
268 assert (message);
269
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600270 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
271 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
272 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600273 // We know that we're submitting queues without fences, ignore this warning
274 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
275 return;
276 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600277 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600278 } else {
279 return;
280 }
281
282#ifdef _WIN32
283 MessageBox(NULL, message, "Alert", MB_OK);
284#else
285 printf("%s\n",message);
286 fflush(stdout);
287#endif
288 free(message);
289}
290
Ian Elliottaaae5352015-07-06 14:27:58 -0600291typedef struct _SwapChainBuffers {
292 VkImage image;
293 VkCmdBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600294 VkImageView view;
Ian Elliottaaae5352015-07-06 14:27:58 -0600295} SwapChainBuffers;
296
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600297struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600298#ifdef _WIN32
299#define APP_NAME_STR_LEN 80
300 HINSTANCE connection; // hInstance - Windows Instance
301 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
302 HWND window; // hWnd - window handle
303#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600304 xcb_connection_t *connection;
305 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800306 xcb_window_t window;
307 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliottaaae5352015-07-06 14:27:58 -0600308 VkPlatformHandleXcbWSI platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600309#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600310 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700311 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600312 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600313
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600314 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600315 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600316 VkDevice device;
317 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700318 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600319 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600320 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600321 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600323 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600324 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600325 VkFormat format;
Ian Elliott8528eff2015-08-07 15:56:59 -0600326 VkColorSpaceWSI color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327
Ian Elliottaaae5352015-07-06 14:27:58 -0600328 PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI;
Ian Elliott8528eff2015-08-07 15:56:59 -0600329 PFN_vkGetSurfacePropertiesWSI fpGetSurfacePropertiesWSI;
330 PFN_vkGetSurfaceFormatsWSI fpGetSurfaceFormatsWSI;
331 PFN_vkGetSurfacePresentModesWSI fpGetSurfacePresentModesWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600332 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
333 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
Ian Elliott8528eff2015-08-07 15:56:59 -0600334 PFN_vkGetSwapChainImagesWSI fpGetSwapChainImagesWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600335 PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600336 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600337 VkSurfaceDescriptionWindowWSI surface_description;
Ian Elliott8528eff2015-08-07 15:56:59 -0600338 uint32_t swapChainImageCount;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800339 VkSwapChainWSI swap_chain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600340 SwapChainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341
Ian Elliottaaae5352015-07-06 14:27:58 -0600342 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600343
344 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600345 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600347 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500348 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600349 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350 } depth;
351
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600352 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
354 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600355 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500356 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600357 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800358 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600359 } uniform_data;
360
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600361 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500362 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600363 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600364 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800365 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367
Cody Northrop5e4125d2015-08-26 10:01:32 -0600368 VkDynamicViewportState dynamic_viewport;
369 VkDynamicLineWidthState dynamic_line_width;
370 VkDynamicDepthBiasState dynamic_depth_bias;
371 VkDynamicBlendState dynamic_blend;
372 VkDynamicDepthBoundsState dynamic_depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -0600373 VkDynamicStencilState dynamic_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600375 mat4x4 projection_matrix;
376 mat4x4 view_matrix;
377 mat4x4 model_matrix;
378
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600379 float spin_angle;
380 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600381 bool pause;
382
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600383 VkShaderModule vert_shader_module;
384 VkShaderModule frag_shader_module;
385
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkDescriptorPool desc_pool;
387 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800388
Chia-I Wuf973e322015-07-08 13:34:24 +0800389 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
390
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600391 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600392 int32_t curFrame;
393 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600394 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600395 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600396 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600397 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600398 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600399 VkDbgMsgCallback msg_callback;
400
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600401 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600402 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403};
404
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600405static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
406{
407 // Search memtypes to find first index with those properties
408 for (uint32_t i = 0; i < 32; i++) {
409 if ((typeBits & 1) == 1) {
410 // Type is available, does it match user properties?
411 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
412 *typeIndex = i;
413 return VK_SUCCESS;
414 }
415 }
416 typeBits >>= 1;
417 }
418 // No memory types matched, return failure
419 return VK_UNSUPPORTED;
420}
421
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600422static void demo_flush_init_cmd(struct demo *demo)
423{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600424 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600426 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427 return;
428
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600429 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430 assert(!err);
431
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600432 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600433 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600434
Tony Barbour155cce82015-07-03 10:33:54 -0600435 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436 assert(!err);
437
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600438 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439 assert(!err);
440
Tony Barbour155cce82015-07-03 10:33:54 -0600441 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600442 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600443}
444
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445static void demo_set_image_layout(
446 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600447 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400448 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 VkImageLayout old_image_layout,
450 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600452 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600453
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600454 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600455 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600456 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600457 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600458 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800459 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600460 .flags = 0,
461 };
462
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600464 assert(!err);
465
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600466 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600467 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600469 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600470 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600471 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600472 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600473 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600475 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476 }
477
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600478 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600479 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600480 .pNext = NULL,
481 .outputMask = 0,
482 .inputMask = 0,
483 .oldLayout = old_image_layout,
484 .newLayout = new_image_layout,
485 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400486 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487 };
488
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600490 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600491 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 }
493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600496 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600497 }
498
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600499 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600500
Tony Barbour50bbca42015-06-29 16:20:35 -0600501 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
502 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600504 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505}
506
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600507static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600508{
Chia-I Wuf973e322015-07-08 13:34:24 +0800509 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600510 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600511 .pNext = NULL,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600512 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600513 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600514 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600515 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700516 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800517 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600518 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
519 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800520 };
521 const VkRenderPassBeginInfo rp_begin = {
522 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
523 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800524 .renderPass = demo->render_pass,
525 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800526 .renderArea.offset.x = 0,
527 .renderArea.offset.y = 0,
528 .renderArea.extent.width = demo->width,
529 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600530 .clearValueCount = 2,
531 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700532 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800533 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600534
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600535 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536 assert(!err);
537
Chia-I Wua74c5b22015-07-07 11:50:03 +0800538 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
539
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600540 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600542 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600543 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544
Cody Northrop5e4125d2015-08-26 10:01:32 -0600545 vkCmdBindDynamicViewportState(cmd_buf, demo->dynamic_viewport);
546 vkCmdBindDynamicLineWidthState(cmd_buf, demo->dynamic_line_width);
547 vkCmdBindDynamicDepthBiasState(cmd_buf, demo->dynamic_depth_bias);
548 vkCmdBindDynamicBlendState(cmd_buf, demo->dynamic_blend);
549 vkCmdBindDynamicDepthBoundsState(cmd_buf, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -0600550 vkCmdBindDynamicStencilState(cmd_buf, demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600551
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600552 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800553 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600554
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600555 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600556 assert(!err);
557}
558
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600559
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600560void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600562 mat4x4 MVP, Model, VP;
563 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600564 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600565 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600567 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600569 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600570 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700571 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600572 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600573
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500574 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575 assert(!err);
576
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600577 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500579 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580 assert(!err);
581}
582
583static void demo_draw(struct demo *demo)
584{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600585 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600586 VkSemaphore presentCompleteSemaphore;
587 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
588 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
589 .pNext = NULL,
590 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
591 };
Tony Barbour155cce82015-07-03 10:33:54 -0600592 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600593
Ian Elliottaaae5352015-07-06 14:27:58 -0600594 err = vkCreateSemaphore(demo->device,
595 &presentCompleteSemaphoreCreateInfo,
596 &presentCompleteSemaphore);
597 assert(!err);
598
599 // Get the index of the next available swapchain image:
600 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
601 UINT64_MAX,
602 presentCompleteSemaphore,
603 &demo->current_buffer);
604 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
605 // return codes
606 assert(!err);
607
608 // Wait for the present complete semaphore to be signaled to ensure
609 // that the image won't be rendered to until the presentation
610 // engine has fully released ownership to the application, and it is
611 // okay to render to the image.
612 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
613
614// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600615 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600616 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600617 assert(!err);
618
Ian Elliottaaae5352015-07-06 14:27:58 -0600619 VkPresentInfoWSI present = {
Ian Elliott8528eff2015-08-07 15:56:59 -0600620 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
Ian Elliottaaae5352015-07-06 14:27:58 -0600621 .pNext = NULL,
622 .swapChainCount = 1,
623 .swapChains = &demo->swap_chain,
624 .imageIndices = &demo->current_buffer,
625 };
626
627// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600628 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600629 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
630 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600631 assert(!err);
632
Chia-I Wucbb564e2015-04-16 22:02:10 +0800633 err = vkQueueWaitIdle(demo->queue);
634 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600635
636 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
637 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638}
639
640static void demo_prepare_buffers(struct demo *demo)
641{
Ian Elliottaaae5352015-07-06 14:27:58 -0600642 VkResult U_ASSERT_ONLY err;
643
644 // Check the surface properties and formats
Ian Elliott8528eff2015-08-07 15:56:59 -0600645 VkSurfacePropertiesWSI surfProperties;
646 err = demo->fpGetSurfacePropertiesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600647 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600648 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600649 assert(!err);
650
Ian Elliott8528eff2015-08-07 15:56:59 -0600651 uint32_t presentModeCount;
652 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600653 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600654 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600655 assert(!err);
Ian Elliott8528eff2015-08-07 15:56:59 -0600656 VkPresentModeWSI *presentModes =
657 (VkPresentModeWSI *)malloc(presentModeCount * sizeof(VkPresentModeWSI));
658 assert(presentModes);
659 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -0600660 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600661 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600662 assert(!err);
663
664 VkExtent2D swapChainExtent;
665 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600666 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600667 {
668 // If the surface size is undefined, the size is set to
669 // the size of the images requested.
670 swapChainExtent.width = demo->width;
671 swapChainExtent.height = demo->height;
672 }
673 else
674 {
675 // If the surface size is defined, the swap chain size must match
Ian Elliott8528eff2015-08-07 15:56:59 -0600676 swapChainExtent = surfProperties.currentExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600677 }
678
679 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600680 // tearing mode. If not, try IMMEDIATE which will usually be available,
681 // and is fastest (though it tears). If not, fall back to FIFO which is
682 // always available.
683 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600684 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott8528eff2015-08-07 15:56:59 -0600685 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_WSI) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600686 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
687 break;
688 }
Ian Elliott3ebce342015-07-27 13:53:11 -0600689 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
Ian Elliott8528eff2015-08-07 15:56:59 -0600690 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
Ian Elliott3ebce342015-07-27 13:53:11 -0600691 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
692 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600693 }
694
Ian Elliott2d47da92015-07-13 12:20:56 -0600695#define WORK_AROUND_CODE
696#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600697 // After the proper code was created, other parts of this demo were
698 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
699 // images, etc. Live with that for now.
700 // TODO: Rework this demo code to live with the number of buffers returned
701 // by vkCreateSwapChainWSI().
Ian Elliott2d47da92015-07-13 12:20:56 -0600702 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
703#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600704 // Determine the number of VkImage's to use in the swap chain (we desire to
705 // own only 1 image at a time, besides the images being displayed and
706 // queued for display):
Ian Elliott8528eff2015-08-07 15:56:59 -0600707 uint32_t desiredNumberOfSwapChainImages = surfProperties.minImageCount + 1;
708 if ((surfProperties.maxImageCount > 0) &&
709 (desiredNumberOfSwapChainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600710 {
711 // Application must settle for fewer images than desired:
Ian Elliott8528eff2015-08-07 15:56:59 -0600712 desiredNumberOfSwapChainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600713 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600714#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600715
716 VkSurfaceTransformFlagBitsWSI preTransform;
Ian Elliott8528eff2015-08-07 15:56:59 -0600717 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600718 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
719 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600720 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 }
722
Chia-I Wucbb564e2015-04-16 22:02:10 +0800723 const VkSwapChainCreateInfoWSI swap_chain = {
724 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
725 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600726 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
727 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800728 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600729 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800730 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600731 .width = swapChainExtent.width,
732 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600734 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600735 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800736 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600737 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
738 .queueFamilyCount = 0,
739 .pQueueFamilyIndices = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600740 .presentMode = swapChainPresentMode,
741 .oldSwapChain.handle = 0,
742 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600744 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600745
Jon Ashburn0b85d052015-05-21 18:13:33 -0600746 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800747 assert(!err);
748
Ian Elliott8528eff2015-08-07 15:56:59 -0600749 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
750 &demo->swapChainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600751 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800752
Ian Elliott8528eff2015-08-07 15:56:59 -0600753 VkImage* swapChainImages =
754 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliottaaae5352015-07-06 14:27:58 -0600755 assert(swapChainImages);
Ian Elliott8528eff2015-08-07 15:56:59 -0600756 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
757 &demo->swapChainImageCount,
758 swapChainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600759 assert(!err);
Ian Elliott2d47da92015-07-13 12:20:56 -0600760#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600761 // After the proper code was created, other parts of this demo were
762 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
763 // images, etc. Live with that for now.
764 // TODO: Rework this demo code to live with the number of buffers returned
765 // by vkCreateSwapChainWSI().
Ian Elliott2d47da92015-07-13 12:20:56 -0600766 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
Ian Elliott2d47da92015-07-13 12:20:56 -0600767#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600768
769 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
770 assert(demo->buffers);
771
772 for (i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600773 VkImageViewCreateInfo color_image_view = {
774 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600775 .pNext = NULL,
776 .format = demo->format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600777 .channels = {
778 .r = VK_CHANNEL_SWIZZLE_R,
779 .g = VK_CHANNEL_SWIZZLE_G,
780 .b = VK_CHANNEL_SWIZZLE_B,
781 .a = VK_CHANNEL_SWIZZLE_A,
782 },
783 .subresourceRange = {
784 .aspect = VK_IMAGE_ASPECT_COLOR,
785 .baseMipLevel = 0,
786 .mipLevels = 1,
787 .baseArraySlice = 0,
788 .arraySize = 1
789 },
790 .viewType = VK_IMAGE_VIEW_TYPE_2D,
791 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600792 };
793
Ian Elliott8528eff2015-08-07 15:56:59 -0600794 demo->buffers[i].image = swapChainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600795
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600796 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400797 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600798 VK_IMAGE_LAYOUT_UNDEFINED,
799 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600800
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600801 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600802
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600803 err = vkCreateImageView(demo->device,
804 &color_image_view, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600805 assert(!err);
806 }
807}
808
809static void demo_prepare_depth(struct demo *demo)
810{
Tony Barbour72304ef2015-04-16 15:59:00 -0600811 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600812 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600813 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600814 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600815 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600816 .format = depth_format,
817 .extent = { demo->width, demo->height, 1 },
818 .mipLevels = 1,
819 .arraySize = 1,
820 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600821 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600822 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600823 .flags = 0,
824 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600825 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600826 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500827 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600828 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600829 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830 };
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600831 VkImageViewCreateInfo view = {
832 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600833 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600834 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600835 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600836 .subresourceRange = {
837 .aspect = VK_IMAGE_ASPECT_COLOR,
838 .baseMipLevel = 0,
839 .mipLevels = 1,
840 .baseArraySlice = 0,
841 .arraySize = 1
842 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600843 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600844 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600845 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600846
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500847 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600848 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600849
850 demo->depth.format = depth_format;
851
852 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600853 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600854 &demo->depth.image);
855 assert(!err);
856
Tony Barbour155cce82015-07-03 10:33:54 -0600857 err = vkGetImageMemoryRequirements(demo->device,
858 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600859
860 mem_alloc.allocationSize = mem_reqs.size;
861 err = memory_type_from_properties(demo,
862 mem_reqs.memoryTypeBits,
863 VK_MEMORY_PROPERTY_DEVICE_ONLY,
864 &mem_alloc.memoryTypeIndex);
865 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600866
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500867 /* allocate memory */
868 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
869 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600870
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500871 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600872 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500873 demo->depth.mem, 0);
874 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600875
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600876 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400877 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600878 VK_IMAGE_LAYOUT_UNDEFINED,
879 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600880
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881 /* create image view */
882 view.image = demo->depth.image;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600883 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600884 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600885}
886
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600887/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600888 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600889 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600890 * \param demo : Needed to access VK calls
891 * \param filename : the png file to be loaded
892 * \param width : width of png, to be updated as a side effect of this function
893 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600894 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600895 * \return bool : an opengl texture id. true if successful?,
896 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600897 *
898 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
899 * Modified to copy image to memory
900 *
901 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700902bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600903 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600904 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600905{
906 //header for testing if it is a png
907 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600908 int is_png, bit_depth, color_type, rowbytes;
909 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700910 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600911 png_structp png_ptr;
912 png_infop info_ptr, end_info;
913 png_byte *image_data;
914 png_bytep *row_pointers;
915
916 //open file as binary
917 FILE *fp = fopen(filename, "rb");
918 if (!fp) {
919 return false;
920 }
921
922 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600923 retval = fread(header, 1, 8, fp);
924 if (retval != 8) {
925 fclose(fp);
926 return false;
927 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600928
929 //test if png
930 is_png = !png_sig_cmp(header, 0, 8);
931 if (!is_png) {
932 fclose(fp);
933 return false;
934 }
935
936 //create png struct
937 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
938 NULL, NULL);
939 if (!png_ptr) {
940 fclose(fp);
941 return (false);
942 }
943
944 //create png info struct
945 info_ptr = png_create_info_struct(png_ptr);
946 if (!info_ptr) {
947 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
948 fclose(fp);
949 return (false);
950 }
951
952 //create png info struct
953 end_info = png_create_info_struct(png_ptr);
954 if (!end_info) {
955 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
956 fclose(fp);
957 return (false);
958 }
959
960 //png error stuff, not sure libpng man suggests this.
961 if (setjmp(png_jmpbuf(png_ptr))) {
962 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
963 fclose(fp);
964 return (false);
965 }
966
967 //init png reading
968 png_init_io(png_ptr, fp);
969
970 //let libpng know you already read the first 8 bytes
971 png_set_sig_bytes(png_ptr, 8);
972
973 // read all the info up to the image data
974 png_read_info(png_ptr, info_ptr);
975
976 // get info about png
977 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
978 NULL, NULL, NULL);
979
980 //update width and height based on png info
981 *width = twidth;
982 *height = theight;
983
984 // Require that incoming texture be 8bits per color component
985 // and 4 components (RGBA).
986 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
987 png_get_channels(png_ptr, info_ptr) != 4) {
988 return false;
989 }
990
991 if (rgba_data == NULL) {
992 // If data pointer is null, we just want the width & height
993 // clean up memory and close stuff
994 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
995 fclose(fp);
996
997 return true;
998 }
999
1000 // Update the png info struct.
1001 png_read_update_info(png_ptr, info_ptr);
1002
1003 // Row size in bytes.
1004 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
1005
1006 // Allocate the image_data as a big block, to be given to opengl
1007 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
1008 if (!image_data) {
1009 //clean up memory and close stuff
1010 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1011 fclose(fp);
1012 return false;
1013 }
1014
1015 // row_pointers is for pointing to image_data for reading the png with libpng
1016 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
1017 if (!row_pointers) {
1018 //clean up memory and close stuff
1019 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1020 // delete[] image_data;
1021 fclose(fp);
1022 return false;
1023 }
1024 // set the individual row_pointers to point at the correct offsets of image_data
1025 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001027
1028 // read the png into image_data through row_pointers
1029 png_read_image(png_ptr, row_pointers);
1030
1031 // clean up memory and close stuff
1032 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1033 free(row_pointers);
1034 free(image_data);
1035 fclose(fp);
1036
1037 return true;
1038}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001039
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040static void demo_prepare_texture_image(struct demo *demo,
1041 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001042 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001043 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001044 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001045 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001046{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001047 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001048 int32_t tex_width;
1049 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001050 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001051
David Pinedo30bd71d2015-04-23 08:16:57 -06001052 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1053 {
1054 printf("Failed to load textures\n");
1055 fflush(stdout);
1056 exit(1);
1057 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001058
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001059 tex_obj->tex_width = tex_width;
1060 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001062 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001063 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001065 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066 .format = tex_format,
1067 .extent = { tex_width, tex_height, 1 },
1068 .mipLevels = 1,
1069 .arraySize = 1,
1070 .samples = 1,
1071 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001072 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 .flags = 0,
1074 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001075 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001076 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001077 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001079 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080 };
1081
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001082 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001085 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086 assert(!err);
1087
Tony Barbour155cce82015-07-03 10:33:54 -06001088 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001089 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001090
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001091 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001092
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001093 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1094 assert(!err);
1095
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001096 /* allocate memory */
1097 err = vkAllocMemory(demo->device, &mem_alloc,
1098 &(tex_obj->mem));
1099 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001100
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001101 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001102 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001103 tex_obj->mem, 0);
1104 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001105
Tony Barbour72304ef2015-04-16 15:59:00 -06001106 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001107 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001108 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001109 .mipLevel = 0,
1110 .arraySlice = 0,
1111 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001112 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001113 void *data;
1114
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001115 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1116 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001118 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 assert(!err);
1120
1121 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1122 fprintf(stderr, "Error loading texture: %s\n", filename);
1123 }
1124
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001125 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001126 assert(!err);
1127 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001128
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001129 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001130 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001131 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001132 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001133 tex_obj->imageLayout);
1134 /* 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 -07001135}
1136
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001137static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138{
1139 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001140 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001141 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142}
1143
1144static void demo_prepare_textures(struct demo *demo)
1145{
Tony Barbour72304ef2015-04-16 15:59:00 -06001146 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001147 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001148 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001149 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001150
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001151 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001152 assert(!err);
1153
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001154 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155
FslNopper6a7e50e2015-05-06 21:42:01 +02001156 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001157 /* Device can texture using linear textures */
1158 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001159 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001160 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001161 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001162 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163
1164 memset(&staging_texture, 0, sizeof(staging_texture));
1165 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001166 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167
1168 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001169 VK_IMAGE_TILING_OPTIMAL,
1170 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1171 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001173 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001174 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001175 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001176 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001177
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001178 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001179 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001180 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001181 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001183 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001184 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001185 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001187 .destOffset = { 0, 0, 0 },
1188 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1189 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001190 vkCmdCopyImage(demo->cmd,
1191 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1192 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001193 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001194
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001195 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001196 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001197 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001198 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001200 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001202 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001203 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001204 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1205 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001206 }
1207
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001208 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001209 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001211 .magFilter = VK_TEX_FILTER_NEAREST,
1212 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001213 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001214 .addressU = VK_TEX_ADDRESS_CLAMP,
1215 .addressV = VK_TEX_ADDRESS_CLAMP,
1216 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001217 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001218 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001219 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220 .minLod = 0.0f,
1221 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001222 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Cody Northrop25439e42015-08-11 15:50:55 -06001223 .texelCoords = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001225
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001226 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001227 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001229 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001230 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001232 .channels = { VK_CHANNEL_SWIZZLE_R,
1233 VK_CHANNEL_SWIZZLE_G,
1234 VK_CHANNEL_SWIZZLE_B,
1235 VK_CHANNEL_SWIZZLE_A, },
1236 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001237 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001239
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001240 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001241 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242 &demo->textures[i].sampler);
1243 assert(!err);
1244
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001245 /* create image view */
1246 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001247 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001248 &demo->textures[i].view);
1249 assert(!err);
1250 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001251}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001253void demo_prepare_cube_data_buffer(struct demo *demo)
1254{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001255 VkBufferCreateInfo buf_info;
1256 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001257 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001258 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001259 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001260 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001261 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001262 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001263 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001264 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001265 int i;
1266 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001267 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001268 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001269
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001270 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001271 mat4x4_mul(MVP, VP, demo->model_matrix);
1272 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001273// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001274
1275 for (i=0; i<12*3; i++) {
1276 data.position[i][0] = g_vertex_buffer_data[i*3];
1277 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1278 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1279 data.position[i][3] = 1.0f;
1280 data.attr[i][0] = g_uv_buffer_data[2*i];
1281 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1282 data.attr[i][2] = 0;
1283 data.attr[i][3] = 0;
1284 }
1285
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001286 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001287 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001288 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001289 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001290 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001291 assert(!err);
1292
Tony Barbour155cce82015-07-03 10:33:54 -06001293 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001294 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001295
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001296 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001297 err = memory_type_from_properties(demo,
1298 mem_reqs.memoryTypeBits,
1299 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1300 &alloc_info.memoryTypeIndex);
1301 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001302
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001303 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1304 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001305
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001306 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1307 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001308
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001309 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001310
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001311 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1312 assert(!err);
1313
Tony Barbour155cce82015-07-03 10:33:54 -06001314 err = vkBindBufferMemory(demo->device,
1315 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001316 demo->uniform_data.mem, 0);
1317 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001318
1319 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001320 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001321 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001322 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001323 view_info.offset = 0;
1324 view_info.range = sizeof(data);
1325
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001326 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001327 assert(!err);
1328
Chia-I Wuae721ba2015-05-25 16:27:55 +08001329 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001330}
1331
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001332static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001333{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001334 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001335 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001336 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001337 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001338 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001339 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001340 },
1341 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001342 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001343 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001344 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001345 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001346 },
1347 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001348 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001349 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001350 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001351 .count = 2,
1352 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001353 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001354 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001356 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001357 &descriptor_layout, &demo->desc_layout);
1358 assert(!err);
1359
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001360 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1361 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1362 .pNext = NULL,
1363 .descriptorSetCount = 1,
1364 .pSetLayouts = &demo->desc_layout,
1365 };
1366
1367 err = vkCreatePipelineLayout(demo->device,
1368 &pPipelineLayoutCreateInfo,
1369 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001370 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371}
1372
Chia-I Wuf973e322015-07-08 13:34:24 +08001373static void demo_prepare_render_pass(struct demo *demo)
1374{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001375 const VkAttachmentDescription attachments[2] = {
1376 [0] = {
1377 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1378 .pNext = NULL,
1379 .format = demo->format,
1380 .samples = 1,
1381 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1382 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1383 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1384 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1385 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1386 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1387 },
1388 [1] = {
1389 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1390 .pNext = NULL,
1391 .format = demo->depth.format,
1392 .samples = 1,
1393 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1394 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1395 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1396 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1397 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1398 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1399 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001400 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001401 const VkAttachmentReference color_reference = {
1402 .attachment = 0,
1403 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1404 };
1405 const VkSubpassDescription subpass = {
1406 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1407 .pNext = NULL,
1408 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1409 .flags = 0,
1410 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001411 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001412 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001413 .pColorAttachments = &color_reference,
1414 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001415 .depthStencilAttachment = {
1416 .attachment = 1,
1417 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1418 },
1419 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001420 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001421 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001422 const VkRenderPassCreateInfo rp_info = {
1423 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1424 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001425 .attachmentCount = 2,
1426 .pAttachments = attachments,
1427 .subpassCount = 1,
1428 .pSubpasses = &subpass,
1429 .dependencyCount = 0,
1430 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001431 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001432 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001433
1434 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1435 assert(!err);
1436}
1437
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001438static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001439 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001440 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001441 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001442 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001444 VkShaderModuleCreateInfo moduleCreateInfo;
1445 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001446 VkShader shader;
1447 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001448
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001449
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001450 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1451 moduleCreateInfo.pNext = NULL;
1452
1453 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1454 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001455 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456
Cody Northrop1fedb212015-05-28 11:27:16 -06001457 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001458 moduleCreateInfo.codeSize = size;
1459 moduleCreateInfo.pCode = code;
1460 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001461 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001462 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001463 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001464 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001465
1466 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001467 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001468 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001469 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001470 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001471 } else {
1472 // Create fake SPV structure to feed GLSL
1473 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001474 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1475 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1476 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001477
1478 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001479 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1480 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1481 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1482 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001483
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001484 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001485 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001486 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001487 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001488
1489 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001490 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001491 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001492 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001493 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001494 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001495 return shader;
1496}
1497
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001498char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001499{
1500 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001501 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001502 void *shader_code;
1503
1504 FILE *fp = fopen(filename, "rb");
1505 if (!fp) return NULL;
1506
1507 fseek(fp, 0L, SEEK_END);
1508 size = ftell(fp);
1509
1510 fseek(fp, 0L, SEEK_SET);
1511
1512 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001513 retval = fread(shader_code, size, 1, fp);
1514 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001515
1516 *psize = size;
1517
1518 return shader_code;
1519}
1520
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001521static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522{
Cody Northrop1fedb212015-05-28 11:27:16 -06001523 if (!demo->use_glsl) {
1524 void *vertShaderCode;
1525 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001526
Cody Northrop1fedb212015-05-28 11:27:16 -06001527 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -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 vertShaderCode, size);
1531 } else {
1532 static const char *vertShaderText =
1533 "#version 140\n"
1534 "#extension GL_ARB_separate_shader_objects : enable\n"
1535 "#extension GL_ARB_shading_language_420pack : enable\n"
1536 "\n"
1537 "layout(binding = 0) uniform buf {\n"
1538 " mat4 MVP;\n"
1539 " vec4 position[12*3];\n"
1540 " vec4 attr[12*3];\n"
1541 "} ubuf;\n"
1542 "\n"
1543 "layout (location = 0) out vec4 texcoord;\n"
1544 "\n"
1545 "void main() \n"
1546 "{\n"
1547 " texcoord = ubuf.attr[gl_VertexID];\n"
1548 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1549 "\n"
1550 " // GL->VK conventions\n"
1551 " gl_Position.y = -gl_Position.y;\n"
1552 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1553 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001554
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001555 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001556 (const void *) vertShaderText,
1557 strlen(vertShaderText));
1558 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001559}
1560
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001561static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562{
Cody Northrop1fedb212015-05-28 11:27:16 -06001563 if (!demo->use_glsl) {
1564 void *fragShaderCode;
1565 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001566
Cody Northrop1fedb212015-05-28 11:27:16 -06001567 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001568
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001569 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001570 fragShaderCode, size);
1571 } else {
1572 static const char *fragShaderText =
1573 "#version 140\n"
1574 "#extension GL_ARB_separate_shader_objects : enable\n"
1575 "#extension GL_ARB_shading_language_420pack : enable\n"
1576 "layout (binding = 1) uniform sampler2D tex;\n"
1577 "\n"
1578 "layout (location = 0) in vec4 texcoord;\n"
1579 "layout (location = 0) out vec4 uFragColor;\n"
1580 "void main() {\n"
1581 " uFragColor = texture(tex, texcoord.xy);\n"
1582 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001583
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001584 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001585 (const void *) fragShaderText,
1586 strlen(fragShaderText));
1587 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001588}
1589
1590static void demo_prepare_pipeline(struct demo *demo)
1591{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001592 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001593 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001594 VkPipelineInputAssemblyStateCreateInfo ia;
1595 VkPipelineRasterStateCreateInfo rs;
1596 VkPipelineColorBlendStateCreateInfo cb;
1597 VkPipelineDepthStencilStateCreateInfo ds;
1598 VkPipelineViewportStateCreateInfo vp;
1599 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001600 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601
1602 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001603 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001604 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001605
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001607 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001608 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001609
1610 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001611 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001612 rs.fillMode = VK_FILL_MODE_SOLID;
1613 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001614 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001615 rs.depthClipEnable = VK_TRUE;
Cody Northrop709c1742015-08-17 11:10:49 -06001616 rs.rasterizerDiscardEnable = VK_FALSE;
1617 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001618
1619 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001620 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1621 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001622 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001623 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001624 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001625 cb.attachmentCount = 1;
1626 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627
Tony Barbour29645d02015-01-16 14:27:35 -07001628 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001629 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001630 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001631
1632 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001633 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001634 ds.depthTestEnable = VK_TRUE;
1635 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001636 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001637 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001638 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1639 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001640 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001641 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001642 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001643
Tony Barbour29645d02015-01-16 14:27:35 -07001644 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001645 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001646 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001647 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001648
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001649 // Two stages: vs and fs
1650 pipeline.stageCount = 2;
1651 VkPipelineShaderStageCreateInfo shaderStages[2];
1652 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1653
1654 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1655 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1656 shaderStages[0].shader = demo_prepare_vs(demo);
1657
1658 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1659 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1660 shaderStages[1].shader = demo_prepare_fs(demo);
1661
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001662 memset(&pipelineCache, 0, sizeof(pipelineCache));
1663 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001664
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001665 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1666 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001667
1668 pipeline.pVertexInputState = NULL;
1669 pipeline.pInputAssemblyState = &ia;
1670 pipeline.pRasterState = &rs;
1671 pipeline.pColorBlendState = &cb;
1672 pipeline.pMultisampleState = &ms;
1673 pipeline.pViewportState = &vp;
1674 pipeline.pDepthStencilState = &ds;
1675 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001676 pipeline.renderPass = demo->render_pass;
Tony Barbour194bf282015-07-10 15:29:03 -06001677
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001678 pipeline.renderPass = demo->render_pass;
1679
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001680 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001681 assert(!err);
1682
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001683 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001684 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001685 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001686 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1687 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688}
1689
1690static void demo_prepare_dynamic_states(struct demo *demo)
1691{
Tony Barbour155cce82015-07-03 10:33:54 -06001692 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001693 VkDynamicLineWidthStateCreateInfo line_width;
1694 VkDynamicDepthBiasStateCreateInfo depth_bias;
1695 VkDynamicBlendStateCreateInfo blend;
1696 VkDynamicDepthBoundsStateCreateInfo depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -06001697 VkDynamicStencilStateCreateInfo stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001698 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001699
Tony Barbour29645d02015-01-16 14:27:35 -07001700 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001701 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001702 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001703 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001704 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001705 viewport.height = (float) demo->height;
1706 viewport.width = (float) demo->width;
1707 viewport.minDepth = (float) 0.0f;
1708 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001709 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001710 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001711 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001712 scissor.extent.width = demo->width;
1713 scissor.extent.height = demo->height;
1714 scissor.offset.x = 0;
1715 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001716 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717
Cody Northrop5e4125d2015-08-26 10:01:32 -06001718 memset(&line_width, 0, sizeof(line_width));
1719 line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
1720 line_width.lineWidth = 1.0;
Cody Northrop709c1742015-08-17 11:10:49 -06001721
Cody Northrop5e4125d2015-08-26 10:01:32 -06001722 memset(&depth_bias, 0, sizeof(depth_bias));
1723 depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
1724 depth_bias.depthBias = 0.0f;
1725 depth_bias.depthBiasClamp = 0.0f;
1726 depth_bias.slopeScaledDepthBias = 0.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727
Cody Northrop5e4125d2015-08-26 10:01:32 -06001728 memset(&blend, 0, sizeof(blend));
1729 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
1730 blend.blendConst[0] = 1.0f;
1731 blend.blendConst[1] = 1.0f;
1732 blend.blendConst[2] = 1.0f;
1733 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734
Cody Northrop5e4125d2015-08-26 10:01:32 -06001735 memset(&depth_bounds, 0, sizeof(depth_bounds));
1736 depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
1737 depth_bounds.minDepthBounds = 0.0f;
1738 depth_bounds.maxDepthBounds = 1.0f;
Cody Northrop36a642f2015-08-18 15:21:16 -06001739
1740 memset(&stencil, 0, sizeof(stencil));
1741 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
1742 stencil.stencilReference = 0;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001743 stencil.stencilCompareMask = 0xff;
Cody Northrop36a642f2015-08-18 15:21:16 -06001744 stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745
Cody Northrop5e4125d2015-08-26 10:01:32 -06001746 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 assert(!err);
1748
Cody Northrop5e4125d2015-08-26 10:01:32 -06001749 err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width);
Cody Northrop709c1742015-08-17 11:10:49 -06001750 assert(!err);
1751
Cody Northrop5e4125d2015-08-26 10:01:32 -06001752 err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753 assert(!err);
1754
Cody Northrop5e4125d2015-08-26 10:01:32 -06001755 err = vkCreateDynamicBlendState(demo->device,
1756 &blend, &demo->dynamic_blend);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757 assert(!err);
1758
Cody Northrop5e4125d2015-08-26 10:01:32 -06001759 err = vkCreateDynamicDepthBoundsState(demo->device,
1760 &depth_bounds, &demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001761 assert(!err);
1762
1763 err = vkCreateDynamicStencilState(demo->device,
1764 &stencil, &stencil, &demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765 assert(!err);
1766}
1767
Chia-I Wu63ea9262015-03-26 13:14:16 +08001768static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001769{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001770 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001771 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001772 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001773 .count = 1,
1774 },
1775 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001776 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001777 .count = DEMO_TEXTURE_COUNT,
1778 },
1779 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001780 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001781 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001782 .pNext = NULL,
1783 .count = 2,
1784 .pTypeCount = type_counts,
1785 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001786 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001787
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001788 err = vkCreateDescriptorPool(demo->device,
1789 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001790 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001791 assert(!err);
1792}
1793
1794static void demo_prepare_descriptor_set(struct demo *demo)
1795{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001796 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1797 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001798 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001799 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001800
Mike Stroyanebae8322015-04-17 12:36:38 -06001801 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001802 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001803 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001804 &demo->desc_set);
1805 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001806
Chia-I Wuae721ba2015-05-25 16:27:55 +08001807 memset(&tex_descs, 0, sizeof(tex_descs));
1808 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1809 tex_descs[i].sampler = demo->textures[i].sampler;
1810 tex_descs[i].imageView = demo->textures[i].view;
1811 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1812 }
1813
1814 memset(&writes, 0, sizeof(writes));
1815
1816 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1817 writes[0].destSet = demo->desc_set;
1818 writes[0].count = 1;
1819 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1820 writes[0].pDescriptors = &demo->uniform_data.desc;
1821
1822 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1823 writes[1].destSet = demo->desc_set;
1824 writes[1].destBinding = 1;
1825 writes[1].count = DEMO_TEXTURE_COUNT;
1826 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1827 writes[1].pDescriptors = tex_descs;
1828
1829 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1830 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001831}
1832
Chia-I Wuf973e322015-07-08 13:34:24 +08001833static void demo_prepare_framebuffers(struct demo *demo)
1834{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001835 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001836 attachments[1] = demo->depth.view;
1837
Chia-I Wuf973e322015-07-08 13:34:24 +08001838 const VkFramebufferCreateInfo fb_info = {
1839 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1840 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001841 .renderPass = demo->render_pass,
1842 .attachmentCount = 2,
1843 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001844 .width = demo->width,
1845 .height = demo->height,
1846 .layers = 1,
1847 };
1848 VkResult U_ASSERT_ONLY err;
1849 uint32_t i;
1850
1851 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001852 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001853 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1854 assert(!err);
1855 }
1856}
1857
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858static void demo_prepare(struct demo *demo)
1859{
Cody Northrop91e221b2015-07-09 18:08:32 -06001860 VkResult U_ASSERT_ONLY err;
1861
1862 const VkCmdPoolCreateInfo cmd_pool_info = {
1863 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1864 .pNext = NULL,
1865 .queueFamilyIndex = demo->graphics_queue_node_index,
1866 .flags = 0,
1867 };
1868 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1869 assert(!err);
1870
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001871 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001872 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001873 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001874 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001875 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001876 .flags = 0,
1877 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001878
1879 demo_prepare_buffers(demo);
1880 demo_prepare_depth(demo);
1881 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001882 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001883
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001884 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001885 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001886 demo_prepare_pipeline(demo);
1887 demo_prepare_dynamic_states(demo);
1888
Ian Elliottaaae5352015-07-06 14:27:58 -06001889 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001890 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001891 assert(!err);
1892 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001893
Chia-I Wu63ea9262015-03-26 13:14:16 +08001894 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001895 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001896
Chia-I Wuf973e322015-07-08 13:34:24 +08001897 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001898
Ian Elliottaaae5352015-07-06 14:27:58 -06001899 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001900 demo->current_buffer = i;
1901 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1902 }
1903
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001904 /*
1905 * Prepare functions above may generate pipeline commands
1906 * that need to be flushed before beginning the render loop.
1907 */
1908 demo_flush_init_cmd(demo);
1909
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001910 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001911 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001912}
1913
David Pinedo2bb7c932015-06-18 17:03:14 -06001914static void demo_cleanup(struct demo *demo)
1915{
1916 uint32_t i;
1917
1918 demo->prepared = false;
1919
Tony Barbour155cce82015-07-03 10:33:54 -06001920 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1921 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1922 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001923 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001924 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001925
Cody Northrop5e4125d2015-08-26 10:01:32 -06001926 vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport);
1927 vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width);
1928 vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias);
1929 vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend);
1930 vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001931 vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001932
Tony Barbour155cce82015-07-03 10:33:54 -06001933 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001934 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001935 vkDestroyRenderPass(demo->device, demo->render_pass);
1936 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1937 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001938
1939 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001940 vkDestroyImageView(demo->device, demo->textures[i].view);
1941 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001942 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001943 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001944 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001945 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001946
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001947 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbour155cce82015-07-03 10:33:54 -06001948 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001949 vkFreeMemory(demo->device, demo->depth.mem);
1950
Tony Barbour155cce82015-07-03 10:33:54 -06001951 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1952 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001953 vkFreeMemory(demo->device, demo->uniform_data.mem);
1954
Ian Elliottaaae5352015-07-06 14:27:58 -06001955 for (i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001956 vkDestroyImageView(demo->device, demo->buffers[i].view);
Tony Barbour155cce82015-07-03 10:33:54 -06001957 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001958 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001959 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001960
Cody Northrop91e221b2015-07-09 18:08:32 -06001961 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001962 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001963 if (demo->validate) {
1964 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1965 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001966 vkDestroyInstance(demo->inst);
1967
1968#ifndef _WIN32
1969 xcb_destroy_window(demo->connection, demo->window);
1970 xcb_disconnect(demo->connection);
1971#endif // _WIN32
1972}
1973
1974// On MS-Windows, make this a global, so it's available to WndProc()
1975struct demo demo;
1976
Ian Elliott639ca472015-04-16 15:23:05 -06001977#ifdef _WIN32
1978static void demo_run(struct demo *demo)
1979{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001980 if (!demo->prepared)
1981 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001982 // Wait for work to finish before updating MVP.
1983 vkDeviceWaitIdle(demo->device);
1984 demo_update_data_buffer(demo);
1985
1986 demo_draw(demo);
1987
1988 // Wait for work to finish before updating MVP.
1989 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001990
David Pinedo2bb7c932015-06-18 17:03:14 -06001991 demo->curFrame++;
1992
1993 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1994 {
1995 demo->quit=true;
1996 demo_cleanup(demo);
1997 ExitProcess(0);
1998 }
1999
2000}
Ian Elliott639ca472015-04-16 15:23:05 -06002001
2002// MS-Windows event handling function:
2003LRESULT CALLBACK WndProc(HWND hWnd,
2004 UINT uMsg,
2005 WPARAM wParam,
2006 LPARAM lParam)
2007{
Ian Elliott639ca472015-04-16 15:23:05 -06002008 switch(uMsg)
2009 {
Ian Elliottaaae5352015-07-06 14:27:58 -06002010 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06002011 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002012 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002013 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002014 demo_run(&demo);
2015 return 0;
2016 default:
2017 break;
2018 }
2019 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2020}
2021
2022static void demo_create_window(struct demo *demo)
2023{
2024 WNDCLASSEX win_class;
2025
2026 // Initialize the window class structure:
2027 win_class.cbSize = sizeof(WNDCLASSEX);
2028 win_class.style = CS_HREDRAW | CS_VREDRAW;
2029 win_class.lpfnWndProc = WndProc;
2030 win_class.cbClsExtra = 0;
2031 win_class.cbWndExtra = 0;
2032 win_class.hInstance = demo->connection; // hInstance
2033 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2034 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2035 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2036 win_class.lpszMenuName = NULL;
2037 win_class.lpszClassName = demo->name;
2038 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2039 // Register window class:
2040 if (!RegisterClassEx(&win_class)) {
2041 // It didn't work, so try to give a useful error:
2042 printf("Unexpected error trying to start the application!\n");
2043 fflush(stdout);
2044 exit(1);
2045 }
2046 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06002047 RECT wr = { 0, 0, demo->width, demo->height };
2048 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002049 demo->window = CreateWindowEx(0,
2050 demo->name, // class name
2051 demo->name, // app name
2052 WS_OVERLAPPEDWINDOW | // window style
2053 WS_VISIBLE |
2054 WS_SYSMENU,
2055 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06002056 wr.right-wr.left, // width
2057 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06002058 NULL, // handle to parent
2059 NULL, // handle to menu
2060 demo->connection, // hInstance
2061 NULL); // no extra parameters
2062 if (!demo->window) {
2063 // It didn't work, so try to give a useful error:
2064 printf("Cannot create a window in which to draw!\n");
2065 fflush(stdout);
2066 exit(1);
2067 }
2068}
2069#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002070static void demo_handle_event(struct demo *demo,
2071 const xcb_generic_event_t *event)
2072{
Piers Daniell735ee532015-02-23 16:23:13 -07002073 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002074 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002075 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002076 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002077 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002078 case XCB_CLIENT_MESSAGE:
2079 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2080 (*demo->atom_wm_delete_window).atom) {
2081 demo->quit = true;
2082 }
2083 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002084 case XCB_KEY_RELEASE:
2085 {
2086 const xcb_key_release_event_t *key =
2087 (const xcb_key_release_event_t *) event;
2088
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002089 switch (key->detail) {
2090 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002091 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002092 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002093 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002094 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002095 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002096 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002097 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002098 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002099 case 0x41:
2100 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002101 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002102 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002103 }
2104 break;
2105 default:
2106 break;
2107 }
2108}
2109
2110static void demo_run(struct demo *demo)
2111{
2112 xcb_flush(demo->connection);
2113
2114 while (!demo->quit) {
2115 xcb_generic_event_t *event;
2116
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002117 if (demo->pause) {
2118 event = xcb_wait_for_event(demo->connection);
2119 } else {
2120 event = xcb_poll_for_event(demo->connection);
2121 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002122 if (event) {
2123 demo_handle_event(demo, event);
2124 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002125 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002126
2127 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002128 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002129 demo_update_data_buffer(demo);
2130
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002131 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002132
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002133 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002134 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002135 demo->curFrame++;
2136 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2137 demo->quit = true;
2138
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002139 }
2140}
2141
2142static void demo_create_window(struct demo *demo)
2143{
2144 uint32_t value_mask, value_list[32];
2145
2146 demo->window = xcb_generate_id(demo->connection);
2147
2148 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2149 value_list[0] = demo->screen->black_pixel;
2150 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2151 XCB_EVENT_MASK_EXPOSURE;
2152
2153 xcb_create_window(demo->connection,
2154 XCB_COPY_FROM_PARENT,
2155 demo->window, demo->screen->root,
2156 0, 0, demo->width, demo->height, 0,
2157 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2158 demo->screen->root_visual,
2159 value_mask, value_list);
2160
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002161 /* Magic code that will send notification when window is destroyed */
2162 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2163 "WM_PROTOCOLS");
2164 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2165
2166 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2167 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2168
2169 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2170 demo->window, (*reply).atom, 4, 32, 1,
2171 &(*demo->atom_wm_delete_window).atom);
2172 free(reply);
2173
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002174 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002175
2176 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2177 const uint32_t coords[] = {100, 100};
2178 xcb_configure_window(demo->connection, demo->window,
2179 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002180}
Ian Elliott639ca472015-04-16 15:23:05 -06002181#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002182
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002183/*
2184 * Return 1 (true) if all layer names specified in check_names
2185 * can be found in given layer properties.
2186 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002187static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002188 uint32_t layer_count, VkLayerProperties *layers)
2189{
2190 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002191 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002192 for (uint32_t j = 0; j < layer_count; j++) {
2193 if (!strcmp(check_names[i], layers[j].layerName)) {
2194 found = 1;
2195 }
2196 }
2197 if (!found) {
2198 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2199 return 0;
2200 }
2201 }
2202 return 1;
2203}
2204
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002205static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002206{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002207 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002208 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002209 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002210 VkLayerProperties *instance_layers;
2211 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002212 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002213 uint32_t instance_layer_count = 0;
2214 uint32_t enabled_extension_count = 0;
2215 uint32_t enabled_layer_count = 0;
2216
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002217 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002218 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002219 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002220 "ObjectTracker",
2221 "DrawState",
2222 "ParamChecker",
2223 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002224 };
2225
2226 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002227 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002228 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002229 "ObjectTracker",
2230 "DrawState",
2231 "ParamChecker",
2232 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002233 };
2234
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002235 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002236 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002237 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002238 assert(!err);
2239
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002240 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2241 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2242 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002243
2244 if (demo->validate) {
2245 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2246 instance_layer_count, instance_layers);
2247 if (!validation_found) {
2248 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2249 "required validation layer.\n\n"
2250 "Please look at the Getting Started guide for additional "
2251 "information.\n",
2252 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002253 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002254 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002255 }
2256
2257 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2258 assert(!err);
2259
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002260 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002261 memset(extension_names, 0, sizeof(extension_names));
2262 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2263 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2264 assert(!err);
2265 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002266 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002267 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002268 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002269 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002270 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002271 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002272 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002273 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002274 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002275 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002276 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002277 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002278 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002279 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002280 "Vulkan installable client driver (ICD) installed?\nPlease "
2281 "look at the Getting Started guide for additional "
2282 "information.\n",
2283 "vkCreateInstance Failure");
2284 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002285 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002286 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002287 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002288 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002289 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002290 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002291 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002292 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002293 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002294 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002295 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002296 .pNext = NULL,
2297 .pAppInfo = &app,
2298 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002299 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002300 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002301 .extensionCount = enabled_extension_count,
2302 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002303 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002304 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002305 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002306 .queueCount = 1,
2307 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002308
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002309 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002310
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002311 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002312 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2313 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002314 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002315 "additional information.\n",
2316 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002317 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2318 ERR_EXIT("Cannot find a specified extension library"
2319 ".\nMake sure your layers path is set appropriately\n",
2320 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002321 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002322 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2323 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002324 "the Getting Started guide for additional information.\n",
2325 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002326 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002327
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002328 free(instance_layers);
2329 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002330
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002331 gpu_count = 1;
2332 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002333 assert(!err && gpu_count == 1);
2334
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002335 /* Look for validation layers */
2336 validation_found = 0;
2337 enabled_layer_count = 0;
2338 uint32_t device_layer_count = 0;
2339 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2340 assert(!err);
2341
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002342 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2343 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2344 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002345
2346 if (demo->validate) {
2347 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2348 device_layer_count, device_layers);
2349 if (!validation_found) {
2350 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2351 "a required validation layer.\n\n"
2352 "Please look at the Getting Started guide for additional "
2353 "information.\n",
2354 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002355 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002356 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002357 }
2358
2359 uint32_t device_extension_count = 0;
2360 VkExtensionProperties *device_extensions = NULL;
2361 err = vkGetPhysicalDeviceExtensionProperties(
2362 demo->gpu, NULL, &device_extension_count, NULL);
2363 assert(!err);
2364
2365 WSIextFound = 0;
2366 enabled_extension_count = 0;
2367 memset(extension_names, 0, sizeof(extension_names));
2368 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2369 err = vkGetPhysicalDeviceExtensionProperties(
2370 demo->gpu, NULL, &device_extension_count, device_extensions);
2371 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002372
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002373 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002374 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002375 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002376 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002377 }
2378 assert(enabled_extension_count < 64);
2379 }
2380 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002381 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2382 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002383 "Vulkan installable client driver (ICD) installed?\nPlease "
2384 "look at the Getting Started guide for additional "
2385 "information.\n",
2386 "vkCreateInstance Failure");
2387 }
2388
2389 VkDeviceCreateInfo device = {
2390 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2391 .pNext = NULL,
2392 .queueRecordCount = 1,
2393 .pRequestedQueues = &queue,
2394 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002395 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002396 .extensionCount = enabled_extension_count,
2397 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002398 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002399
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002400 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002401 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2402 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002403 if (!demo->dbgCreateMsgCallback) {
2404 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2405 "vkGetProcAddr Failure");
2406 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002407 if (!demo->dbgDestroyMsgCallback) {
2408 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2409 "vkGetProcAddr Failure");
2410 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002411 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2412 if (!demo->dbgBreakCallback) {
2413 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2414 "vkGetProcAddr Failure");
2415 }
2416
2417 PFN_vkDbgMsgCallback callback;
2418
2419 if (!demo->use_break) {
2420 callback = dbgFunc;
2421 } else {
2422 callback = demo->dbgBreakCallback;
2423 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002424 err = demo->dbgCreateMsgCallback(
2425 demo->inst,
2426 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002427 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002428 &demo->msg_callback);
2429 switch (err) {
2430 case VK_SUCCESS:
2431 break;
2432 case VK_ERROR_INVALID_POINTER:
2433 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2434 "dbgCreateMsgCallback Failure");
2435 break;
2436 case VK_ERROR_OUT_OF_HOST_MEMORY:
2437 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2438 "dbgCreateMsgCallback Failure");
2439 break;
2440 default:
2441 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2442 "dbgCreateMsgCallback Failure");
2443 break;
2444 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002445 }
2446
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002447 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002448 assert(!err);
2449
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002450 free(device_layers);
2451
Ian Elliottaaae5352015-07-06 14:27:58 -06002452 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott8528eff2015-08-07 15:56:59 -06002453 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
2454 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
2455 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002456 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2457 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2458 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott8528eff2015-08-07 15:56:59 -06002459 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002460 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002461 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002462
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002463 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002464 assert(!err);
2465
Cody Northrop4d3c11d2015-08-03 17:04:53 -06002466 /* Call with NULL data to get count */
2467 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002468 assert(!err);
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002469 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002470
Cody Northrop4d3c11d2015-08-03 17:04:53 -06002471 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2472 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002473 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002474 assert(demo->queue_count >= 1);
2475}
2476
2477static void demo_init_vk_wsi(struct demo *demo)
2478{
2479 VkResult err;
2480 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002481
Ian Elliottaaae5352015-07-06 14:27:58 -06002482 // Construct the WSI surface description:
2483 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2484 demo->surface_description.pNext = NULL;
2485#ifdef _WIN32
2486 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2487 demo->surface_description.pPlatformHandle = demo->connection;
2488 demo->surface_description.pPlatformWindow = demo->window;
2489#else // _WIN32
2490 demo->platform_handle_xcb.connection = demo->connection;
2491 demo->platform_handle_xcb.root = demo->screen->root;
2492 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2493 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2494 demo->surface_description.pPlatformWindow = &demo->window;
2495#endif // _WIN32
2496
2497 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002498 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2499 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002500 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2501 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2502 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002503 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002504
2505 // Search for a graphics and a present queue in the array of queue
2506 // families, try to find one that supports both
2507 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2508 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002509 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002510 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2511 if (graphicsQueueNodeIndex == UINT32_MAX) {
2512 graphicsQueueNodeIndex = i;
2513 }
2514
2515 if (supportsPresent[i] == VK_TRUE) {
2516 graphicsQueueNodeIndex = i;
2517 presentQueueNodeIndex = i;
2518 break;
2519 }
2520 }
2521 }
2522 if (presentQueueNodeIndex == UINT32_MAX) {
2523 // If didn't find a queue that supports both graphics and present, then
2524 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002525 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002526 if (supportsPresent[i] == VK_TRUE) {
2527 presentQueueNodeIndex = i;
2528 break;
2529 }
2530 }
2531 }
2532 free(supportsPresent);
2533
2534 // Generate error if could not find both a graphics and a present queue
2535 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2536 ERR_EXIT("Could not find a graphics and a present queue\n",
2537 "WSI Initialization Failure");
2538 }
2539
2540 // TODO: Add support for separate queues, including presentation,
2541 // synchronization, and appropriate tracking for QueueSubmit
2542 // While it is possible for an application to use a separate graphics and a
2543 // present queues, this demo program assumes it is only using one:
2544 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2545 ERR_EXIT("Could not find a common graphics and a present queue\n",
2546 "WSI Initialization Failure");
2547 }
2548
2549 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002551 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002552 0, &demo->queue);
2553 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002554
Ian Elliottaaae5352015-07-06 14:27:58 -06002555 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002556 uint32_t formatCount;
2557 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -06002558 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002559 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002560 assert(!err);
Ian Elliott8528eff2015-08-07 15:56:59 -06002561 VkSurfaceFormatWSI *surfFormats =
2562 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
2563 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliottaaae5352015-07-06 14:27:58 -06002564 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002565 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002566 assert(!err);
2567 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2568 // the surface has no preferred format. Otherwise, at least one
2569 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002570 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2571 {
2572 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2573 }
2574 else
2575 {
2576 assert(formatCount >= 1);
2577 demo->format = surfFormats[0].format;
2578 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002579 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002580
David Pinedo2bb7c932015-06-18 17:03:14 -06002581 demo->quit = false;
2582 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002583
2584 // Get Memory information and properties
2585 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2586 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002587}
2588
2589static void demo_init_connection(struct demo *demo)
2590{
Ian Elliott639ca472015-04-16 15:23:05 -06002591#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002592 const xcb_setup_t *setup;
2593 xcb_screen_iterator_t iter;
2594 int scr;
2595
2596 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002597 if (demo->connection == NULL) {
2598 printf("Cannot find a compatible Vulkan installable client driver "
2599 "(ICD).\nExiting ...\n");
2600 fflush(stdout);
2601 exit(1);
2602 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002603
2604 setup = xcb_get_setup(demo->connection);
2605 iter = xcb_setup_roots_iterator(setup);
2606 while (scr-- > 0)
2607 xcb_screen_next(&iter);
2608
2609 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002610#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002611}
2612
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002613static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002614{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002615 vec3 eye = {0.0f, 3.0f, 5.0f};
2616 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002617 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002618
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002619 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002620 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002621
Piers Daniell735ee532015-02-23 16:23:13 -07002622 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002623 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002624 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002625 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002626 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002627 if (strcmp(argv[i], "--use_glsl") == 0) {
2628 demo->use_glsl = true;
2629 continue;
2630 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002631 if (strcmp(argv[i], "--break") == 0) {
2632 demo->use_break = true;
2633 continue;
2634 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002635 if (strcmp(argv[i], "--validate") == 0) {
2636 demo->validate = true;
2637 continue;
2638 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002639 if (strcmp(argv[i], "--c") == 0 &&
2640 demo->frameCount == INT_MAX &&
2641 i < argc-1 &&
2642 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2643 demo->frameCount >= 0)
2644 {
2645 i++;
2646 continue;
2647 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002648
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002649 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002650 fflush(stderr);
2651 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002652 }
2653
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002654 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002655 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002656
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002657 demo->width = 500;
2658 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002659
2660 demo->spin_angle = 0.01f;
2661 demo->spin_increment = 0.01f;
2662 demo->pause = false;
2663
Piers Daniell735ee532015-02-23 16:23:13 -07002664 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002665 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2666 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002667}
2668
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002669
Ian Elliott639ca472015-04-16 15:23:05 -06002670#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002671extern int __getmainargs(
2672 int * _Argc,
2673 char *** _Argv,
2674 char *** _Env,
2675 int _DoWildCard,
2676 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002677
Ian Elliotta748eaf2015-04-28 15:50:36 -06002678int WINAPI WinMain(HINSTANCE hInstance,
2679 HINSTANCE hPrevInstance,
2680 LPSTR pCmdLine,
2681 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002682{
2683 MSG msg; // message
2684 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002685 int argc;
2686 char** argv;
2687 char** env;
2688 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002689
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002690 __getmainargs(&argc,&argv,&env,0,&new_mode);
2691
2692 demo_init(&demo, argc, argv);
2693 demo.connection = hInstance;
2694 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002695 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002696 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002697
2698 demo_prepare(&demo);
2699
2700 done = false; //initialize loop condition variable
2701 /* main message loop*/
2702 while(!done)
2703 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002704 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002705 if (msg.message == WM_QUIT) //check for a quit message
2706 {
2707 done = true; //if found, quit app
2708 }
2709 else
2710 {
2711 /* Translate and dispatch to event queue*/
2712 TranslateMessage(&msg);
2713 DispatchMessage(&msg);
2714 }
2715 }
2716
2717 demo_cleanup(&demo);
2718
Tony Barbourf9921732015-04-22 11:36:22 -06002719 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002720}
2721#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002722int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002723{
2724 struct demo demo;
2725
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002726 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002727 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002728 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002729
2730 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002731 demo_run(&demo);
2732
2733 demo_cleanup(&demo);
2734
2735 return 0;
2736}
Ian Elliott639ca472015-04-16 15:23:05 -06002737#endif // _WIN32