blob: ad49f9d883ccf08fdc835ff7b66ddc8afc68741d [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Ian Elliottaaae5352015-07-06 14:27:58 -060040#include <vk_wsi_swapchain.h>
41#include <vk_wsi_device_swapchain.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060042#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060043
Cody Northropfe3d8bc2015-03-17 14:54:35 -060044#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060046#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060047#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_BUFFER_COUNT 2
50#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliott07264132015-04-28 11:35:02 -060062#ifdef _WIN32
63#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
68
69// NOTE: If the following values (copied from "loader_platform.h") change, they
70// need to change here as well:
71#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
72#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
73
74#else // _WIN32
75
76#define ERR_EXIT(err_msg, err_class) \
77 do { \
78 printf(err_msg); \
79 fflush(stdout); \
80 exit(1); \
81 } while (0)
82#endif // _WIN32
83
Ian Elliottaaae5352015-07-06 14:27:58 -060084#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
85{ \
86 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
87 if (demo->fp##entrypoint == NULL) { \
88 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
89 "vkGetInstanceProcAddr Failure"); \
90 } \
91}
92
Ian Elliott673898b2015-06-22 15:07:49 -060093#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
94{ \
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -060095 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060096 if (demo->fp##entrypoint == NULL) { \
97 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
98 "vkGetDeviceProcAddr Failure"); \
99 } \
100}
101
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600102/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700103 * structure to track all objects related to a texture.
104 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600105struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600106 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700107
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600108 VkImage image;
109 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600110
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500111 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600112 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700113 int32_t tex_width, tex_height;
114};
115
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600117 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600118};
119
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600120struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600121 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600122 float mvp[4][4];
123 float position[12*3][4];
124 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600125};
126
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600127struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600128 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600129 float mvp[4][4];
130 float position[12*3][4];
131 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600132};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600133
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600134//--------------------------------------------------------------------------------------
135// Mesh and VertexFormat Data
136//--------------------------------------------------------------------------------------
137struct Vertex
138{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600139 float posX, posY, posZ, posW; // Position data
140 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600141};
142
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600143struct VertexPosTex
144{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600145 float posX, posY, posZ, posW; // Position data
146 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600147};
148
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600149#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600150#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600151
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600152static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800153 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 -1.0f,-1.0f, 1.0f,
155 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 -1.0f, 1.0f,-1.0f,
158 -1.0f,-1.0f,-1.0f,
159
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 1.0f, 1.0f,-1.0f,
162 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f,
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,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600169 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 -1.0f, 1.0f, 1.0f,
176 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600179 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 1.0f, 1.0f, 1.0f,
183 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f,-1.0f,-1.0f,
186 1.0f, 1.0f,-1.0f,
187
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600190 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 1.0f,-1.0f, 1.0f,
193 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600194};
195
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600196static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 0.0f, 0.0f, // -X side
198 1.0f, 0.0f,
199 1.0f, 1.0f,
200 1.0f, 1.0f,
201 0.0f, 1.0f,
202 0.0f, 0.0f,
203
204 1.0f, 0.0f, // -Z side
205 0.0f, 1.0f,
206 0.0f, 0.0f,
207 1.0f, 0.0f,
208 1.0f, 1.0f,
209 0.0f, 1.0f,
210
211 1.0f, 1.0f, // -Y side
212 1.0f, 0.0f,
213 0.0f, 0.0f,
214 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600215 0.0f, 0.0f,
216 0.0f, 1.0f,
217
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600219 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600221 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600223 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800225 1.0f, 1.0f, // +X side
226 0.0f, 1.0f,
227 0.0f, 0.0f,
228 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 0.0f, 1.0f, // +Z side
233 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600235 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800236 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600237 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600238};
239
240void dumpMatrix(const char *note, mat4x4 MVP)
241{
242 int i;
243
244 printf("%s: \n", note);
245 for (i=0; i<4; i++) {
246 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
247 }
248 printf("\n");
249 fflush(stdout);
250}
251
252void dumpVec4(const char *note, vec4 vector)
253{
254 printf("%s: \n", note);
255 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
256 printf("\n");
257 fflush(stdout);
258}
259
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600260void dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600261 VkFlags msgFlags,
262 VkDbgObjectType objType,
263 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600264 size_t location,
265 int32_t msgCode,
266 const char* pLayerPrefix,
267 const char* pMsg,
268 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600269{
270 char *message = (char *) malloc(strlen(pMsg)+100);
271
272 assert (message);
273
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600274 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
275 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
276 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600277 // We know that we're submitting queues without fences, ignore this warning
278 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
279 return;
280 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600281 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600282 } else {
283 return;
284 }
285
286#ifdef _WIN32
287 MessageBox(NULL, message, "Alert", MB_OK);
288#else
289 printf("%s\n",message);
290 fflush(stdout);
291#endif
292 free(message);
293}
294
Ian Elliottaaae5352015-07-06 14:27:58 -0600295typedef struct _SwapChainBuffers {
296 VkImage image;
297 VkCmdBuffer cmd;
298 VkAttachmentView view;
299} SwapChainBuffers;
300
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600301struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600302#ifdef _WIN32
303#define APP_NAME_STR_LEN 80
304 HINSTANCE connection; // hInstance - Windows Instance
305 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
306 HWND window; // hWnd - window handle
307#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600308 xcb_connection_t *connection;
309 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800310 xcb_window_t window;
311 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliottaaae5352015-07-06 14:27:58 -0600312 VkPlatformHandleXcbWSI platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600313#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600314 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700315 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600316 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600317
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600319 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600320 VkDevice device;
321 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700322 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600323 VkPhysicalDeviceProperties gpu_props;
Tony Barbour72304ef2015-04-16 15:59:00 -0600324 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600325 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600329 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330
Ian Elliottaaae5352015-07-06 14:27:58 -0600331 PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI;
332 PFN_vkGetSurfaceInfoWSI fpGetSurfaceInfoWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600333 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
334 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
335 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600336 PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600337 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600338 VkSurfaceDescriptionWindowWSI surface_description;
339 size_t swapChainImageCount;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800340 VkSwapChainWSI swap_chain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600341 SwapChainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600342
Ian Elliottaaae5352015-07-06 14:27:58 -0600343 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344
345 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600346 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600347
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600348 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500349 VkDeviceMemory mem;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800350 VkAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351 } depth;
352
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600353 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
355 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500357 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800359 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600360 } uniform_data;
361
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500363 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600365 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800366 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368
Tony Barbour155cce82015-07-03 10:33:54 -0600369 VkDynamicViewportState viewport;
370 VkDynamicRasterState raster;
371 VkDynamicColorBlendState color_blend;
372 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600374 mat4x4 projection_matrix;
375 mat4x4 view_matrix;
376 mat4x4 model_matrix;
377
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600378 float spin_angle;
379 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600380 bool pause;
381
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600382 VkDescriptorPool desc_pool;
383 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800384
Chia-I Wuf973e322015-07-08 13:34:24 +0800385 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
386
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600388 int32_t curFrame;
389 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600390 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600391 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600392 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600393 VkDbgMsgCallback msg_callback;
394
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600395 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600396 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600397};
398
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600399static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
400{
401 // Search memtypes to find first index with those properties
402 for (uint32_t i = 0; i < 32; i++) {
403 if ((typeBits & 1) == 1) {
404 // Type is available, does it match user properties?
405 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
406 *typeIndex = i;
407 return VK_SUCCESS;
408 }
409 }
410 typeBits >>= 1;
411 }
412 // No memory types matched, return failure
413 return VK_UNSUPPORTED;
414}
415
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600416static void demo_flush_init_cmd(struct demo *demo)
417{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600418 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600420 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421 return;
422
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600423 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424 assert(!err);
425
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600426 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600427 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428
Tony Barbour155cce82015-07-03 10:33:54 -0600429 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430 assert(!err);
431
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600432 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433 assert(!err);
434
Tony Barbour155cce82015-07-03 10:33:54 -0600435 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437}
438
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439static void demo_set_image_layout(
440 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600441 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400442 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600443 VkImageLayout old_image_layout,
444 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600446 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600448 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600450 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600452 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800453 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 .flags = 0,
455 };
456
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 assert(!err);
459
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600460 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600462 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600463 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600466 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600467 }
468
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600469 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600470 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471 .pNext = NULL,
472 .outputMask = 0,
473 .inputMask = 0,
474 .oldLayout = old_image_layout,
475 .newLayout = new_image_layout,
476 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400477 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600478 };
479
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600482 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600483 }
484
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600485 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600486 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600487 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488 }
489
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600490 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491
Tony Barbour50bbca42015-06-29 16:20:35 -0600492 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
493 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600495 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496}
497
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600498static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499{
Chia-I Wuf973e322015-07-08 13:34:24 +0800500 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600502 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600503 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700505 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800506 const VkClearValue clear_values[2] = {
507 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
508 [1] = { .ds = { 1.0f, 0 } },
509 };
510 const VkRenderPassBeginInfo rp_begin = {
511 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
512 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800513 .renderPass = demo->render_pass,
514 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800515 .renderArea.offset.x = 0,
516 .renderArea.offset.y = 0,
517 .renderArea.extent.width = demo->width,
518 .renderArea.extent.height = demo->height,
519 .attachmentCount = 2,
520 .pAttachmentClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700521 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800522 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525 assert(!err);
526
Chia-I Wua74c5b22015-07-07 11:50:03 +0800527 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
528
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600531 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600532 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533
Tony Barbour155cce82015-07-03 10:33:54 -0600534 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
535 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
536 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
537 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600539 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800540 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600542 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543 assert(!err);
544}
545
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600546
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600547void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600549 mat4x4 MVP, Model, VP;
550 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600551 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600552 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600556 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600557 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700558 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600559 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600560
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500561 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600562 assert(!err);
563
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600564 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500566 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 assert(!err);
568}
569
570static void demo_draw(struct demo *demo)
571{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600572 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600573 VkSemaphore presentCompleteSemaphore;
574 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
575 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
576 .pNext = NULL,
577 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
578 };
Tony Barbour155cce82015-07-03 10:33:54 -0600579 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580
Ian Elliottaaae5352015-07-06 14:27:58 -0600581 err = vkCreateSemaphore(demo->device,
582 &presentCompleteSemaphoreCreateInfo,
583 &presentCompleteSemaphore);
584 assert(!err);
585
586 // Get the index of the next available swapchain image:
587 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
588 UINT64_MAX,
589 presentCompleteSemaphore,
590 &demo->current_buffer);
591 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
592 // return codes
593 assert(!err);
594
595 // Wait for the present complete semaphore to be signaled to ensure
596 // that the image won't be rendered to until the presentation
597 // engine has fully released ownership to the application, and it is
598 // okay to render to the image.
599 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
600
601// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600602 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600603 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604 assert(!err);
605
Ian Elliottaaae5352015-07-06 14:27:58 -0600606 VkPresentInfoWSI present = {
607 .sType = VK_STRUCTURE_TYPE_QUEUE_PRESENT_INFO_WSI,
608 .pNext = NULL,
609 .swapChainCount = 1,
610 .swapChains = &demo->swap_chain,
611 .imageIndices = &demo->current_buffer,
612 };
613
614// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600615 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600616 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
617 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600618 assert(!err);
619
Ian Elliottaaae5352015-07-06 14:27:58 -0600620// FIXME: UNCOMMENT THE FOLLOWING LINE ONCE WE HAVE A NEW-ENOUGH "vulkan.h" HEADER:
621// err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
622 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800623
624 err = vkQueueWaitIdle(demo->queue);
625 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626}
627
628static void demo_prepare_buffers(struct demo *demo)
629{
Ian Elliottaaae5352015-07-06 14:27:58 -0600630 VkResult U_ASSERT_ONLY err;
631
632 // Check the surface properties and formats
633 size_t capsSize;
634 size_t presentModesSize;
635 err = demo->fpGetSurfaceInfoWSI(demo->device,
636 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
637 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, NULL);
638 assert(!err);
639 err = demo->fpGetSurfaceInfoWSI(demo->device,
640 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
641 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, NULL);
642 assert(!err);
643
644 VkSurfacePropertiesWSI *surfProperties =
645 (VkSurfacePropertiesWSI *)malloc(capsSize);
646 VkSurfacePresentModePropertiesWSI *presentModes =
647 (VkSurfacePresentModePropertiesWSI *)malloc(presentModesSize);
648
649 err = demo->fpGetSurfaceInfoWSI(demo->device,
650 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
651 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, surfProperties);
652 assert(!err);
653 err = demo->fpGetSurfaceInfoWSI(demo->device,
654 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
655 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, presentModes);
656 assert(!err);
657
658 VkExtent2D swapChainExtent;
659 // width and height are either both -1, or both not -1.
660 if (surfProperties->currentExtent.width == -1)
661 {
662 // If the surface size is undefined, the size is set to
663 // the size of the images requested.
664 swapChainExtent.width = demo->width;
665 swapChainExtent.height = demo->height;
666 }
667 else
668 {
669 // If the surface size is defined, the swap chain size must match
670 swapChainExtent = surfProperties->currentExtent;
671 }
672
673 // If mailbox mode is available, use it, as is the lowest-latency non-
674 // tearing mode. If not, fall back to IMMEDIATE which should always be
675 // available.
676 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
677 size_t presentModeCount = presentModesSize / sizeof(VkSurfacePresentModePropertiesWSI);
678 for (size_t i = 0; i < presentModeCount; i++) {
679 if (presentModes[i].presentMode == VK_PRESENT_MODE_MAILBOX_WSI) {
680 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
681 break;
682 }
683 }
684
Ian Elliott2d47da92015-07-13 12:20:56 -0600685#define WORK_AROUND_CODE
686#ifdef WORK_AROUND_CODE
687 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
688#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600689 // Determine the number of VkImage's to use in the swap chain (we desire to
690 // own only 1 image at a time, besides the images being displayed and
691 // queued for display):
692 uint32_t desiredNumberOfSwapChainImages = surfProperties->minImageCount + 1;
693 if ((surfProperties->maxImageCount > 0) &&
694 (desiredNumberOfSwapChainImages > surfProperties->maxImageCount))
695 {
696 // Application must settle for fewer images than desired:
697 desiredNumberOfSwapChainImages = surfProperties->maxImageCount;
698 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600699#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600700
701 VkSurfaceTransformFlagBitsWSI preTransform;
702 if (surfProperties->supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
703 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
704 } else {
705 preTransform = surfProperties->currentTransform;
706 }
707
Chia-I Wucbb564e2015-04-16 22:02:10 +0800708 const VkSwapChainCreateInfoWSI swap_chain = {
709 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
710 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600711 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
712 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800713 .imageFormat = demo->format,
714 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600715 .width = swapChainExtent.width,
716 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600717 },
Ian Elliottaaae5352015-07-06 14:27:58 -0600718 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800719 .imageArraySize = 1,
Ian Elliottaaae5352015-07-06 14:27:58 -0600720 .presentMode = swapChainPresentMode,
721 .oldSwapChain.handle = 0,
722 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600723 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600724 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600725
Jon Ashburn0b85d052015-05-21 18:13:33 -0600726 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800727 assert(!err);
728
Ian Elliottaaae5352015-07-06 14:27:58 -0600729 size_t swapChainImagesSize;
730 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
731 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
732 &swapChainImagesSize, NULL);
733 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800734
Ian Elliottaaae5352015-07-06 14:27:58 -0600735 VkSwapChainImagePropertiesWSI* swapChainImages = (VkSwapChainImagePropertiesWSI*)malloc(swapChainImagesSize);
736 assert(swapChainImages);
737 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
738 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
739 &swapChainImagesSize, swapChainImages);
740 assert(!err);
741
Ian Elliott2d47da92015-07-13 12:20:56 -0600742#ifdef WORK_AROUND_CODE
743 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
744#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600745 // The number of images within the swap chain is determined based on the size of the info returned
746 demo->swapChainImageCount = swapChainImagesSize / sizeof(VkSwapChainImagePropertiesWSI);
Ian Elliott2d47da92015-07-13 12:20:56 -0600747#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600748
749 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
750 assert(demo->buffers);
751
752 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800753 VkAttachmentViewCreateInfo color_attachment_view = {
754 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600755 .pNext = NULL,
756 .format = demo->format,
757 .mipLevel = 0,
758 .baseArraySlice = 0,
759 .arraySize = 1,
760 };
761
Ian Elliottaaae5352015-07-06 14:27:58 -0600762 demo->buffers[i].image = swapChainImages[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600763
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600764 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400765 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600766 VK_IMAGE_LAYOUT_UNDEFINED,
767 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600768
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600769 color_attachment_view.image = demo->buffers[i].image;
770
Chia-I Wua74c5b22015-07-07 11:50:03 +0800771 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600772 &color_attachment_view, &demo->buffers[i].view);
773 assert(!err);
774 }
775}
776
777static void demo_prepare_depth(struct demo *demo)
778{
Tony Barbour72304ef2015-04-16 15:59:00 -0600779 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600780 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600781 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600782 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600783 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600784 .format = depth_format,
785 .extent = { demo->width, demo->height, 1 },
786 .mipLevels = 1,
787 .arraySize = 1,
788 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600789 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600790 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600791 .flags = 0,
792 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600793 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600794 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500795 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600796 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600797 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600798 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800799 VkAttachmentViewCreateInfo view = {
800 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600802 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600803 .format = depth_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600804 .mipLevel = 0,
805 .baseArraySlice = 0,
806 .arraySize = 1,
807 .flags = 0,
808 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600809
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500810 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600811 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600812
813 demo->depth.format = depth_format;
814
815 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600816 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600817 &demo->depth.image);
818 assert(!err);
819
Tony Barbour155cce82015-07-03 10:33:54 -0600820 err = vkGetImageMemoryRequirements(demo->device,
821 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600822
823 mem_alloc.allocationSize = mem_reqs.size;
824 err = memory_type_from_properties(demo,
825 mem_reqs.memoryTypeBits,
826 VK_MEMORY_PROPERTY_DEVICE_ONLY,
827 &mem_alloc.memoryTypeIndex);
828 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600829
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500830 /* allocate memory */
831 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
832 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600833
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500834 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600835 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500836 demo->depth.mem, 0);
837 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600838
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600839 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400840 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600841 VK_IMAGE_LAYOUT_UNDEFINED,
842 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600843
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600844 /* create image view */
845 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800846 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600848}
849
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600850/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600851 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600852 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600853 * \param demo : Needed to access VK calls
854 * \param filename : the png file to be loaded
855 * \param width : width of png, to be updated as a side effect of this function
856 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600857 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600858 * \return bool : an opengl texture id. true if successful?,
859 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600860 *
861 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
862 * Modified to copy image to memory
863 *
864 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700865bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600866 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600867 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600868{
869 //header for testing if it is a png
870 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600871 int is_png, bit_depth, color_type, rowbytes;
872 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700873 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600874 png_structp png_ptr;
875 png_infop info_ptr, end_info;
876 png_byte *image_data;
877 png_bytep *row_pointers;
878
879 //open file as binary
880 FILE *fp = fopen(filename, "rb");
881 if (!fp) {
882 return false;
883 }
884
885 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600886 retval = fread(header, 1, 8, fp);
887 if (retval != 8) {
888 fclose(fp);
889 return false;
890 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600891
892 //test if png
893 is_png = !png_sig_cmp(header, 0, 8);
894 if (!is_png) {
895 fclose(fp);
896 return false;
897 }
898
899 //create png struct
900 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
901 NULL, NULL);
902 if (!png_ptr) {
903 fclose(fp);
904 return (false);
905 }
906
907 //create png info struct
908 info_ptr = png_create_info_struct(png_ptr);
909 if (!info_ptr) {
910 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
911 fclose(fp);
912 return (false);
913 }
914
915 //create png info struct
916 end_info = png_create_info_struct(png_ptr);
917 if (!end_info) {
918 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
919 fclose(fp);
920 return (false);
921 }
922
923 //png error stuff, not sure libpng man suggests this.
924 if (setjmp(png_jmpbuf(png_ptr))) {
925 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
926 fclose(fp);
927 return (false);
928 }
929
930 //init png reading
931 png_init_io(png_ptr, fp);
932
933 //let libpng know you already read the first 8 bytes
934 png_set_sig_bytes(png_ptr, 8);
935
936 // read all the info up to the image data
937 png_read_info(png_ptr, info_ptr);
938
939 // get info about png
940 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
941 NULL, NULL, NULL);
942
943 //update width and height based on png info
944 *width = twidth;
945 *height = theight;
946
947 // Require that incoming texture be 8bits per color component
948 // and 4 components (RGBA).
949 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
950 png_get_channels(png_ptr, info_ptr) != 4) {
951 return false;
952 }
953
954 if (rgba_data == NULL) {
955 // If data pointer is null, we just want the width & height
956 // clean up memory and close stuff
957 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
958 fclose(fp);
959
960 return true;
961 }
962
963 // Update the png info struct.
964 png_read_update_info(png_ptr, info_ptr);
965
966 // Row size in bytes.
967 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
968
969 // Allocate the image_data as a big block, to be given to opengl
970 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
971 if (!image_data) {
972 //clean up memory and close stuff
973 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
974 fclose(fp);
975 return false;
976 }
977
978 // row_pointers is for pointing to image_data for reading the png with libpng
979 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
980 if (!row_pointers) {
981 //clean up memory and close stuff
982 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
983 // delete[] image_data;
984 fclose(fp);
985 return false;
986 }
987 // set the individual row_pointers to point at the correct offsets of image_data
988 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600990
991 // read the png into image_data through row_pointers
992 png_read_image(png_ptr, row_pointers);
993
994 // clean up memory and close stuff
995 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
996 free(row_pointers);
997 free(image_data);
998 fclose(fp);
999
1000 return true;
1001}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001002
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001003static void demo_prepare_texture_image(struct demo *demo,
1004 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001005 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001006 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001007 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001008 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001009{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001010 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001011 int32_t tex_width;
1012 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001013 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001014
David Pinedo30bd71d2015-04-23 08:16:57 -06001015 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1016 {
1017 printf("Failed to load textures\n");
1018 fflush(stdout);
1019 exit(1);
1020 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001022 tex_obj->tex_width = tex_width;
1023 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001025 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001026 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001027 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001028 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029 .format = tex_format,
1030 .extent = { tex_width, tex_height, 1 },
1031 .mipLevels = 1,
1032 .arraySize = 1,
1033 .samples = 1,
1034 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001035 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036 .flags = 0,
1037 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001038 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001039 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001040 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001042 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043 };
1044
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001045 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001047 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001048 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001049 assert(!err);
1050
Tony Barbour155cce82015-07-03 10:33:54 -06001051 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001052 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001053
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001054 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001056 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1057 assert(!err);
1058
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001059 /* allocate memory */
1060 err = vkAllocMemory(demo->device, &mem_alloc,
1061 &(tex_obj->mem));
1062 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001064 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001065 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001066 tex_obj->mem, 0);
1067 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001068
Tony Barbour72304ef2015-04-16 15:59:00 -06001069 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001070 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001071 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072 .mipLevel = 0,
1073 .arraySlice = 0,
1074 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001075 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076 void *data;
1077
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001078 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1079 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001081 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082 assert(!err);
1083
1084 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1085 fprintf(stderr, "Error loading texture: %s\n", filename);
1086 }
1087
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001088 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001089 assert(!err);
1090 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001091
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001092 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001093 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001094 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001095 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001096 tex_obj->imageLayout);
1097 /* 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 -07001098}
1099
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001100static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001101{
1102 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001103 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001104 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001105}
1106
1107static void demo_prepare_textures(struct demo *demo)
1108{
Tony Barbour72304ef2015-04-16 15:59:00 -06001109 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001110 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001111 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001112 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001113
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001114 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115 assert(!err);
1116
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001117 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118
FslNopper6a7e50e2015-05-06 21:42:01 +02001119 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001120 /* Device can texture using linear textures */
1121 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001122 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001123 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001124 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001125 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001126
1127 memset(&staging_texture, 0, sizeof(staging_texture));
1128 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001129 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001130
1131 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001132 VK_IMAGE_TILING_OPTIMAL,
1133 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1134 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001135
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001136 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001137 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001138 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001139 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001140
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001141 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001142 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001143 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001144 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001145
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001146 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001147 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001149 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001150 .destOffset = { 0, 0, 0 },
1151 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1152 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001153 vkCmdCopyImage(demo->cmd,
1154 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1155 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001156 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001157
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001158 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001159 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001160 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001161 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001163 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001165 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001167 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1168 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001169 }
1170
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001171 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001172 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001174 .magFilter = VK_TEX_FILTER_NEAREST,
1175 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001176 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001177 .addressU = VK_TEX_ADDRESS_CLAMP,
1178 .addressV = VK_TEX_ADDRESS_CLAMP,
1179 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001180 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001181 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001182 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001183 .minLod = 0.0f,
1184 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001185 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001187
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001188 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001189 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001190 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001191 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001192 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001193 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001194 .channels = { VK_CHANNEL_SWIZZLE_R,
1195 VK_CHANNEL_SWIZZLE_G,
1196 VK_CHANNEL_SWIZZLE_B,
1197 VK_CHANNEL_SWIZZLE_A, },
1198 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001200
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001202 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203 &demo->textures[i].sampler);
1204 assert(!err);
1205
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 /* create image view */
1207 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001208 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209 &demo->textures[i].view);
1210 assert(!err);
1211 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001213
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001214void demo_prepare_cube_data_buffer(struct demo *demo)
1215{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001216 VkBufferCreateInfo buf_info;
1217 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001218 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001219 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001220 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001221 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001222 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001223 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001224 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001225 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001226 int i;
1227 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001228 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001229 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001230
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001231 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001232 mat4x4_mul(MVP, VP, demo->model_matrix);
1233 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001234// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001235
1236 for (i=0; i<12*3; i++) {
1237 data.position[i][0] = g_vertex_buffer_data[i*3];
1238 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1239 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1240 data.position[i][3] = 1.0f;
1241 data.attr[i][0] = g_uv_buffer_data[2*i];
1242 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1243 data.attr[i][2] = 0;
1244 data.attr[i][3] = 0;
1245 }
1246
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001247 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001248 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001249 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001250 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001251 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001252 assert(!err);
1253
Tony Barbour155cce82015-07-03 10:33:54 -06001254 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001255 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001256
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001257 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001258 err = memory_type_from_properties(demo,
1259 mem_reqs.memoryTypeBits,
1260 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1261 &alloc_info.memoryTypeIndex);
1262 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001263
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001264 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1265 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001266
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001267 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1268 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001269
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001270 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001271
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001272 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1273 assert(!err);
1274
Tony Barbour155cce82015-07-03 10:33:54 -06001275 err = vkBindBufferMemory(demo->device,
1276 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001277 demo->uniform_data.mem, 0);
1278 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001279
1280 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001281 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001282 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001283 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001284 view_info.offset = 0;
1285 view_info.range = sizeof(data);
1286
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001287 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001288 assert(!err);
1289
Chia-I Wuae721ba2015-05-25 16:27:55 +08001290 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001291}
1292
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001293static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001294{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001295 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001296 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001297 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001298 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001299 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001300 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001301 },
1302 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001303 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001304 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001305 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001306 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001307 },
1308 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001309 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001310 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001311 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001312 .count = 2,
1313 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001314 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001315 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001316
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001317 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001318 &descriptor_layout, &demo->desc_layout);
1319 assert(!err);
1320
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001321 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1322 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1323 .pNext = NULL,
1324 .descriptorSetCount = 1,
1325 .pSetLayouts = &demo->desc_layout,
1326 };
1327
1328 err = vkCreatePipelineLayout(demo->device,
1329 &pPipelineLayoutCreateInfo,
1330 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001331 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001332}
1333
Chia-I Wuf973e322015-07-08 13:34:24 +08001334static void demo_prepare_render_pass(struct demo *demo)
1335{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001336 const VkAttachmentDescription attachments[2] = {
1337 [0] = {
1338 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1339 .pNext = NULL,
1340 .format = demo->format,
1341 .samples = 1,
1342 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1343 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1344 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1345 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1346 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1347 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1348 },
1349 [1] = {
1350 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1351 .pNext = NULL,
1352 .format = demo->depth.format,
1353 .samples = 1,
1354 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1355 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1356 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1357 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1358 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1359 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1360 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001361 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001362 const VkAttachmentReference color_reference = {
1363 .attachment = 0,
1364 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1365 };
1366 const VkSubpassDescription subpass = {
1367 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1368 .pNext = NULL,
1369 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1370 .flags = 0,
1371 .inputCount = 0,
1372 .inputAttachments = NULL,
1373 .colorCount = 1,
1374 .colorAttachments = &color_reference,
1375 .resolveAttachments = NULL,
1376 .depthStencilAttachment = {
1377 .attachment = 1,
1378 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1379 },
1380 .preserveCount = 0,
1381 .preserveAttachments = NULL,
1382 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001383 const VkRenderPassCreateInfo rp_info = {
1384 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1385 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001386 .attachmentCount = 2,
1387 .pAttachments = attachments,
1388 .subpassCount = 1,
1389 .pSubpasses = &subpass,
1390 .dependencyCount = 0,
1391 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001392 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001393 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001394
1395 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1396 assert(!err);
1397}
1398
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001399static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001400 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001401 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001402 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001404 VkShaderModuleCreateInfo moduleCreateInfo;
1405 VkShaderCreateInfo shaderCreateInfo;
1406 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001407 VkShader shader;
1408 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001409
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001410
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001411 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1412 moduleCreateInfo.pNext = NULL;
1413
1414 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1415 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001416 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417
Cody Northrop1fedb212015-05-28 11:27:16 -06001418 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001419 moduleCreateInfo.codeSize = size;
1420 moduleCreateInfo.pCode = code;
1421 moduleCreateInfo.flags = 0;
1422 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001423 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001424 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001425 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001426
1427 shaderCreateInfo.flags = 0;
1428 shaderCreateInfo.module = shaderModule;
1429 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001430 } else {
1431 // Create fake SPV structure to feed GLSL
1432 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001433 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1434 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1435 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001436
1437 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001438 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1439 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1440 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1441 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001442
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001443 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001444 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001445 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001446 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001447
1448 shaderCreateInfo.flags = 0;
1449 shaderCreateInfo.module = shaderModule;
1450 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001451 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452 return shader;
1453}
1454
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001455char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001456{
1457 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001458 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001459 void *shader_code;
1460
1461 FILE *fp = fopen(filename, "rb");
1462 if (!fp) return NULL;
1463
1464 fseek(fp, 0L, SEEK_END);
1465 size = ftell(fp);
1466
1467 fseek(fp, 0L, SEEK_SET);
1468
1469 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001470 retval = fread(shader_code, size, 1, fp);
1471 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001472
1473 *psize = size;
1474
1475 return shader_code;
1476}
1477
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001478static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001479{
Cody Northrop1fedb212015-05-28 11:27:16 -06001480 if (!demo->use_glsl) {
1481 void *vertShaderCode;
1482 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001483
Cody Northrop1fedb212015-05-28 11:27:16 -06001484 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001485
Cody Northrop1fedb212015-05-28 11:27:16 -06001486 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1487 vertShaderCode, size);
1488 } else {
1489 static const char *vertShaderText =
1490 "#version 140\n"
1491 "#extension GL_ARB_separate_shader_objects : enable\n"
1492 "#extension GL_ARB_shading_language_420pack : enable\n"
1493 "\n"
1494 "layout(binding = 0) uniform buf {\n"
1495 " mat4 MVP;\n"
1496 " vec4 position[12*3];\n"
1497 " vec4 attr[12*3];\n"
1498 "} ubuf;\n"
1499 "\n"
1500 "layout (location = 0) out vec4 texcoord;\n"
1501 "\n"
1502 "void main() \n"
1503 "{\n"
1504 " texcoord = ubuf.attr[gl_VertexID];\n"
1505 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1506 "\n"
1507 " // GL->VK conventions\n"
1508 " gl_Position.y = -gl_Position.y;\n"
1509 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1510 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001511
Cody Northrop1fedb212015-05-28 11:27:16 -06001512 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1513 (const void *) vertShaderText,
1514 strlen(vertShaderText));
1515 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001516}
1517
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001518static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519{
Cody Northrop1fedb212015-05-28 11:27:16 -06001520 if (!demo->use_glsl) {
1521 void *fragShaderCode;
1522 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001523
Cody Northrop1fedb212015-05-28 11:27:16 -06001524 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001525
Cody Northrop1fedb212015-05-28 11:27:16 -06001526 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1527 fragShaderCode, size);
1528 } else {
1529 static const char *fragShaderText =
1530 "#version 140\n"
1531 "#extension GL_ARB_separate_shader_objects : enable\n"
1532 "#extension GL_ARB_shading_language_420pack : enable\n"
1533 "layout (binding = 1) uniform sampler2D tex;\n"
1534 "\n"
1535 "layout (location = 0) in vec4 texcoord;\n"
1536 "layout (location = 0) out vec4 uFragColor;\n"
1537 "void main() {\n"
1538 " uFragColor = texture(tex, texcoord.xy);\n"
1539 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001540
Cody Northrop1fedb212015-05-28 11:27:16 -06001541 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1542 (const void *) fragShaderText,
1543 strlen(fragShaderText));
1544 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001545}
1546
1547static void demo_prepare_pipeline(struct demo *demo)
1548{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001549 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001550 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001551 VkPipelineInputAssemblyStateCreateInfo ia;
1552 VkPipelineRasterStateCreateInfo rs;
1553 VkPipelineColorBlendStateCreateInfo cb;
1554 VkPipelineDepthStencilStateCreateInfo ds;
1555 VkPipelineViewportStateCreateInfo vp;
1556 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001557 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558
1559 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001560 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001561 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001564 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001565 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566
1567 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001568 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001569 rs.fillMode = VK_FILL_MODE_SOLID;
1570 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001571 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001572 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001573
1574 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001575 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1576 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001577 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001578 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001579 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001580 cb.attachmentCount = 1;
1581 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582
Tony Barbour29645d02015-01-16 14:27:35 -07001583 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001584 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001585 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001586
1587 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001588 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001589 ds.depthTestEnable = VK_TRUE;
1590 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001591 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001592 ds.depthBoundsEnable = VK_FALSE;
1593 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1594 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001595 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001596 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001597 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001598
Tony Barbour29645d02015-01-16 14:27:35 -07001599 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001600 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001601 ms.sampleMask = 1;
Tony Barbour3ca22502015-06-26 10:18:34 -06001602 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001603
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001604 // Two stages: vs and fs
1605 pipeline.stageCount = 2;
1606 VkPipelineShaderStageCreateInfo shaderStages[2];
1607 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1608
1609 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1610 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1611 shaderStages[0].shader = demo_prepare_vs(demo);
1612
1613 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1614 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1615 shaderStages[1].shader = demo_prepare_fs(demo);
1616
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001617 memset(&pipelineCache, 0, sizeof(pipelineCache));
1618 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001619
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001620 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1621 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001622
1623 pipeline.pVertexInputState = NULL;
1624 pipeline.pInputAssemblyState = &ia;
1625 pipeline.pRasterState = &rs;
1626 pipeline.pColorBlendState = &cb;
1627 pipeline.pMultisampleState = &ms;
1628 pipeline.pViewportState = &vp;
1629 pipeline.pDepthStencilState = &ds;
1630 pipeline.pStages = shaderStages;
1631
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001632 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001633 assert(!err);
1634
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001635 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001636 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001637 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638}
1639
1640static void demo_prepare_dynamic_states(struct demo *demo)
1641{
Tony Barbour155cce82015-07-03 10:33:54 -06001642 VkDynamicViewportStateCreateInfo viewport_create;
1643 VkDynamicRasterStateCreateInfo raster;
1644 VkDynamicColorBlendStateCreateInfo color_blend;
1645 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001646 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001647
Tony Barbour29645d02015-01-16 14:27:35 -07001648 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001649 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001650 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001651 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001652 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001653 viewport.height = (float) demo->height;
1654 viewport.width = (float) demo->width;
1655 viewport.minDepth = (float) 0.0f;
1656 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001657 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001658 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001659 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001660 scissor.extent.width = demo->width;
1661 scissor.extent.height = demo->height;
1662 scissor.offset.x = 0;
1663 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001664 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001665
1666 memset(&raster, 0, sizeof(raster));
Tony Barbour155cce82015-07-03 10:33:54 -06001667 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001668 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669
1670 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour155cce82015-07-03 10:33:54 -06001671 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001672 color_blend.blendConst[0] = 1.0f;
1673 color_blend.blendConst[1] = 1.0f;
1674 color_blend.blendConst[2] = 1.0f;
1675 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676
1677 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour155cce82015-07-03 10:33:54 -06001678 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001679 depth_stencil.minDepthBounds = 0.0f;
1680 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001681 depth_stencil.stencilBackRef = 0;
1682 depth_stencil.stencilFrontRef = 0;
1683 depth_stencil.stencilReadMask = 0xff;
1684 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001685
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001686 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687 assert(!err);
1688
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001689 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690 assert(!err);
1691
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001692 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001693 &color_blend, &demo->color_blend);
1694 assert(!err);
1695
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001696 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001697 &depth_stencil, &demo->depth_stencil);
1698 assert(!err);
1699}
1700
Chia-I Wu63ea9262015-03-26 13:14:16 +08001701static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001702{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001703 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001704 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001705 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001706 .count = 1,
1707 },
1708 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001709 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001710 .count = DEMO_TEXTURE_COUNT,
1711 },
1712 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001713 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001714 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001715 .pNext = NULL,
1716 .count = 2,
1717 .pTypeCount = type_counts,
1718 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001719 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001720
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001721 err = vkCreateDescriptorPool(demo->device,
1722 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001723 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001724 assert(!err);
1725}
1726
1727static void demo_prepare_descriptor_set(struct demo *demo)
1728{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001729 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1730 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001731 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001732 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001733 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001734
Mike Stroyanebae8322015-04-17 12:36:38 -06001735 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001736 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001737 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001738 &demo->desc_set, &count);
1739 assert(!err && count == 1);
1740
Chia-I Wuae721ba2015-05-25 16:27:55 +08001741 memset(&tex_descs, 0, sizeof(tex_descs));
1742 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1743 tex_descs[i].sampler = demo->textures[i].sampler;
1744 tex_descs[i].imageView = demo->textures[i].view;
1745 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1746 }
1747
1748 memset(&writes, 0, sizeof(writes));
1749
1750 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1751 writes[0].destSet = demo->desc_set;
1752 writes[0].count = 1;
1753 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1754 writes[0].pDescriptors = &demo->uniform_data.desc;
1755
1756 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1757 writes[1].destSet = demo->desc_set;
1758 writes[1].destBinding = 1;
1759 writes[1].count = DEMO_TEXTURE_COUNT;
1760 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1761 writes[1].pDescriptors = tex_descs;
1762
1763 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1764 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001765}
1766
Chia-I Wuf973e322015-07-08 13:34:24 +08001767static void demo_prepare_framebuffers(struct demo *demo)
1768{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001769 VkAttachmentBindInfo attachments[2] = {
1770 [0] = {
Tobin Ehlisd415ac32015-07-06 14:02:36 -06001771 .view.handle = VK_NULL_HANDLE,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001772 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1773 },
1774 [1] = {
1775 .view = demo->depth.view,
1776 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1777 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001778 };
1779 const VkFramebufferCreateInfo fb_info = {
1780 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1781 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001782 .renderPass = demo->render_pass,
1783 .attachmentCount = 2,
1784 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001785 .width = demo->width,
1786 .height = demo->height,
1787 .layers = 1,
1788 };
1789 VkResult U_ASSERT_ONLY err;
1790 uint32_t i;
1791
1792 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001793 attachments[0].view = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001794 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1795 assert(!err);
1796 }
1797}
1798
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001799static void demo_prepare(struct demo *demo)
1800{
Cody Northrop91e221b2015-07-09 18:08:32 -06001801 VkResult U_ASSERT_ONLY err;
1802
1803 const VkCmdPoolCreateInfo cmd_pool_info = {
1804 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1805 .pNext = NULL,
1806 .queueFamilyIndex = demo->graphics_queue_node_index,
1807 .flags = 0,
1808 };
1809 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1810 assert(!err);
1811
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001812 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001813 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001814 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001815 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001816 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817 .flags = 0,
1818 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001819
1820 demo_prepare_buffers(demo);
1821 demo_prepare_depth(demo);
1822 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001823 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001824
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001825 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001826 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827 demo_prepare_pipeline(demo);
1828 demo_prepare_dynamic_states(demo);
1829
Ian Elliottaaae5352015-07-06 14:27:58 -06001830 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001831 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001832 assert(!err);
1833 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001834
Chia-I Wu63ea9262015-03-26 13:14:16 +08001835 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001836 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001837
Chia-I Wuf973e322015-07-08 13:34:24 +08001838 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001839
Ian Elliottaaae5352015-07-06 14:27:58 -06001840 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001841 demo->current_buffer = i;
1842 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1843 }
1844
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001845 /*
1846 * Prepare functions above may generate pipeline commands
1847 * that need to be flushed before beginning the render loop.
1848 */
1849 demo_flush_init_cmd(demo);
1850
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001851 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001852 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853}
1854
David Pinedo2bb7c932015-06-18 17:03:14 -06001855static void demo_cleanup(struct demo *demo)
1856{
1857 uint32_t i;
1858
1859 demo->prepared = false;
1860
Tony Barbour155cce82015-07-03 10:33:54 -06001861 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1862 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1863 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001864 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001865 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001866
Tony Barbour155cce82015-07-03 10:33:54 -06001867 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1868 vkDestroyDynamicRasterState(demo->device, demo->raster);
1869 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1870 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001871
Tony Barbour155cce82015-07-03 10:33:54 -06001872 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001873 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001874 vkDestroyRenderPass(demo->device, demo->render_pass);
1875 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1876 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001877
1878 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001879 vkDestroyImageView(demo->device, demo->textures[i].view);
1880 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001881 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001882 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001883 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001884 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001885
Tony Barbour155cce82015-07-03 10:33:54 -06001886 vkDestroyAttachmentView(demo->device, demo->depth.view);
1887 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001888 vkFreeMemory(demo->device, demo->depth.mem);
1889
Tony Barbour155cce82015-07-03 10:33:54 -06001890 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1891 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001892 vkFreeMemory(demo->device, demo->uniform_data.mem);
1893
Ian Elliottaaae5352015-07-06 14:27:58 -06001894 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001895 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1896 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001897 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001898 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001899
Cody Northrop91e221b2015-07-09 18:08:32 -06001900 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001901 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001902 if (demo->validate) {
1903 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1904 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001905 vkDestroyInstance(demo->inst);
1906
1907#ifndef _WIN32
1908 xcb_destroy_window(demo->connection, demo->window);
1909 xcb_disconnect(demo->connection);
1910#endif // _WIN32
1911}
1912
1913// On MS-Windows, make this a global, so it's available to WndProc()
1914struct demo demo;
1915
Ian Elliott639ca472015-04-16 15:23:05 -06001916#ifdef _WIN32
1917static void demo_run(struct demo *demo)
1918{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001919 if (!demo->prepared)
1920 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001921 // Wait for work to finish before updating MVP.
1922 vkDeviceWaitIdle(demo->device);
1923 demo_update_data_buffer(demo);
1924
1925 demo_draw(demo);
1926
1927 // Wait for work to finish before updating MVP.
1928 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001929
David Pinedo2bb7c932015-06-18 17:03:14 -06001930 demo->curFrame++;
1931
1932 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1933 {
1934 demo->quit=true;
1935 demo_cleanup(demo);
1936 ExitProcess(0);
1937 }
1938
1939}
Ian Elliott639ca472015-04-16 15:23:05 -06001940
1941// MS-Windows event handling function:
1942LRESULT CALLBACK WndProc(HWND hWnd,
1943 UINT uMsg,
1944 WPARAM wParam,
1945 LPARAM lParam)
1946{
Ian Elliott639ca472015-04-16 15:23:05 -06001947 switch(uMsg)
1948 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001949 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001950 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001951 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001952 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001953 demo_run(&demo);
1954 return 0;
1955 default:
1956 break;
1957 }
1958 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1959}
1960
1961static void demo_create_window(struct demo *demo)
1962{
1963 WNDCLASSEX win_class;
1964
1965 // Initialize the window class structure:
1966 win_class.cbSize = sizeof(WNDCLASSEX);
1967 win_class.style = CS_HREDRAW | CS_VREDRAW;
1968 win_class.lpfnWndProc = WndProc;
1969 win_class.cbClsExtra = 0;
1970 win_class.cbWndExtra = 0;
1971 win_class.hInstance = demo->connection; // hInstance
1972 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1973 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1974 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1975 win_class.lpszMenuName = NULL;
1976 win_class.lpszClassName = demo->name;
1977 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1978 // Register window class:
1979 if (!RegisterClassEx(&win_class)) {
1980 // It didn't work, so try to give a useful error:
1981 printf("Unexpected error trying to start the application!\n");
1982 fflush(stdout);
1983 exit(1);
1984 }
1985 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001986 RECT wr = { 0, 0, demo->width, demo->height };
1987 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001988 demo->window = CreateWindowEx(0,
1989 demo->name, // class name
1990 demo->name, // app name
1991 WS_OVERLAPPEDWINDOW | // window style
1992 WS_VISIBLE |
1993 WS_SYSMENU,
1994 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001995 wr.right-wr.left, // width
1996 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001997 NULL, // handle to parent
1998 NULL, // handle to menu
1999 demo->connection, // hInstance
2000 NULL); // no extra parameters
2001 if (!demo->window) {
2002 // It didn't work, so try to give a useful error:
2003 printf("Cannot create a window in which to draw!\n");
2004 fflush(stdout);
2005 exit(1);
2006 }
2007}
2008#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002009static void demo_handle_event(struct demo *demo,
2010 const xcb_generic_event_t *event)
2011{
Piers Daniell735ee532015-02-23 16:23:13 -07002012 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002013 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002014 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002015 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002016 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002017 case XCB_CLIENT_MESSAGE:
2018 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2019 (*demo->atom_wm_delete_window).atom) {
2020 demo->quit = true;
2021 }
2022 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002023 case XCB_KEY_RELEASE:
2024 {
2025 const xcb_key_release_event_t *key =
2026 (const xcb_key_release_event_t *) event;
2027
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002028 switch (key->detail) {
2029 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002031 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002032 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002033 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002034 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002035 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002036 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002037 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002038 case 0x41:
2039 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002040 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002041 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002042 }
2043 break;
2044 default:
2045 break;
2046 }
2047}
2048
2049static void demo_run(struct demo *demo)
2050{
2051 xcb_flush(demo->connection);
2052
2053 while (!demo->quit) {
2054 xcb_generic_event_t *event;
2055
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002056 if (demo->pause) {
2057 event = xcb_wait_for_event(demo->connection);
2058 } else {
2059 event = xcb_poll_for_event(demo->connection);
2060 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002061 if (event) {
2062 demo_handle_event(demo, event);
2063 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002064 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002065
2066 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002067 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002068 demo_update_data_buffer(demo);
2069
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002070 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002071
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002072 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002073 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002074 demo->curFrame++;
2075 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2076 demo->quit = true;
2077
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002078 }
2079}
2080
2081static void demo_create_window(struct demo *demo)
2082{
2083 uint32_t value_mask, value_list[32];
2084
2085 demo->window = xcb_generate_id(demo->connection);
2086
2087 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2088 value_list[0] = demo->screen->black_pixel;
2089 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2090 XCB_EVENT_MASK_EXPOSURE;
2091
2092 xcb_create_window(demo->connection,
2093 XCB_COPY_FROM_PARENT,
2094 demo->window, demo->screen->root,
2095 0, 0, demo->width, demo->height, 0,
2096 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2097 demo->screen->root_visual,
2098 value_mask, value_list);
2099
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002100 /* Magic code that will send notification when window is destroyed */
2101 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2102 "WM_PROTOCOLS");
2103 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2104
2105 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2106 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2107
2108 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2109 demo->window, (*reply).atom, 4, 32, 1,
2110 &(*demo->atom_wm_delete_window).atom);
2111 free(reply);
2112
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002113 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002114
2115 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2116 const uint32_t coords[] = {100, 100};
2117 xcb_configure_window(demo->connection, demo->window,
2118 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002119}
Ian Elliott639ca472015-04-16 15:23:05 -06002120#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002121
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002122/*
2123 * Return 1 (true) if all layer names specified in check_names
2124 * can be found in given layer properties.
2125 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002126static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002127 uint32_t layer_count, VkLayerProperties *layers)
2128{
2129 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002130 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002131 for (uint32_t j = 0; j < layer_count; j++) {
2132 if (!strcmp(check_names[i], layers[j].layerName)) {
2133 found = 1;
2134 }
2135 }
2136 if (!found) {
2137 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2138 return 0;
2139 }
2140 }
2141 return 1;
2142}
2143
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002144static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002145{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002146 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002147 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002148 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002149 VkLayerProperties *instance_layers;
2150 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002151 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002152 uint32_t instance_layer_count = 0;
2153 uint32_t enabled_extension_count = 0;
2154 uint32_t enabled_layer_count = 0;
2155
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002156 char *instance_validation_layers[] = {
2157 "MemTracker",
2158 };
2159
2160 char *device_validation_layers[] = {
2161 "MemTracker",
2162 };
2163
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002164 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002165 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002166 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002167 assert(!err);
2168
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002169 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2170 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2171 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002172
2173 if (demo->validate) {
2174 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2175 instance_layer_count, instance_layers);
2176 if (!validation_found) {
2177 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2178 "required validation layer.\n\n"
2179 "Please look at the Getting Started guide for additional "
2180 "information.\n",
2181 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002182 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002183 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002184 }
2185
2186 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2187 assert(!err);
2188
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002189 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002190 memset(extension_names, 0, sizeof(extension_names));
2191 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2192 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2193 assert(!err);
2194 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002195 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002196 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002197 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002198 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002199 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2200 if (demo->validate) {
2201 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2202 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002203 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002204 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002205 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002206 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002207 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002208 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002209 "Vulkan installable client driver (ICD) installed?\nPlease "
2210 "look at the Getting Started guide for additional "
2211 "information.\n",
2212 "vkCreateInstance Failure");
2213 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002214 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002215 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002216 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002217 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002218 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002219 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002220 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002221 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002222 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002223 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002224 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002225 .pNext = NULL,
2226 .pAppInfo = &app,
2227 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002228 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002229 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002230 .extensionCount = enabled_extension_count,
2231 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002232 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002233 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002234 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002235 .queueCount = 1,
2236 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002237
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002238 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002240 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002241 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2242 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002243 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002244 "additional information.\n",
2245 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002246 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2247 ERR_EXIT("Cannot find a specified extension library"
2248 ".\nMake sure your layers path is set appropriately\n",
2249 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002250 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002251 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2252 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002253 "the Getting Started guide for additional information.\n",
2254 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002255 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002256
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002257 free(instance_layers);
2258 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002259
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002260 gpu_count = 1;
2261 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002262 assert(!err && gpu_count == 1);
2263
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002264 /* Look for validation layers */
2265 validation_found = 0;
2266 enabled_layer_count = 0;
2267 uint32_t device_layer_count = 0;
2268 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2269 assert(!err);
2270
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002271 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2272 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2273 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002274
2275 if (demo->validate) {
2276 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2277 device_layer_count, device_layers);
2278 if (!validation_found) {
2279 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2280 "a required validation layer.\n\n"
2281 "Please look at the Getting Started guide for additional "
2282 "information.\n",
2283 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002284 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002285 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002286 }
2287
2288 uint32_t device_extension_count = 0;
2289 VkExtensionProperties *device_extensions = NULL;
2290 err = vkGetPhysicalDeviceExtensionProperties(
2291 demo->gpu, NULL, &device_extension_count, NULL);
2292 assert(!err);
2293
2294 WSIextFound = 0;
2295 enabled_extension_count = 0;
2296 memset(extension_names, 0, sizeof(extension_names));
2297 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2298 err = vkGetPhysicalDeviceExtensionProperties(
2299 demo->gpu, NULL, &device_extension_count, device_extensions);
2300 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002301
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002302 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002303 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002304 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002305 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002306 }
2307 assert(enabled_extension_count < 64);
2308 }
2309 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002310 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2311 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002312 "Vulkan installable client driver (ICD) installed?\nPlease "
2313 "look at the Getting Started guide for additional "
2314 "information.\n",
2315 "vkCreateInstance Failure");
2316 }
2317
2318 VkDeviceCreateInfo device = {
2319 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2320 .pNext = NULL,
2321 .queueRecordCount = 1,
2322 .pRequestedQueues = &queue,
2323 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002324 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002325 .extensionCount = enabled_extension_count,
2326 .ppEnabledExtensionNames = (const char *const*) extension_names,
2327 .flags = 0,
2328 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002329
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002330 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002331 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2332 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002333 if (!demo->dbgCreateMsgCallback) {
2334 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2335 "vkGetProcAddr Failure");
2336 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002337 if (!demo->dbgDestroyMsgCallback) {
2338 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2339 "vkGetProcAddr Failure");
2340 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002341 err = demo->dbgCreateMsgCallback(
2342 demo->inst,
2343 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2344 dbgFunc, NULL,
2345 &demo->msg_callback);
2346 switch (err) {
2347 case VK_SUCCESS:
2348 break;
2349 case VK_ERROR_INVALID_POINTER:
2350 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2351 "dbgCreateMsgCallback Failure");
2352 break;
2353 case VK_ERROR_OUT_OF_HOST_MEMORY:
2354 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2355 "dbgCreateMsgCallback Failure");
2356 break;
2357 default:
2358 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2359 "dbgCreateMsgCallback Failure");
2360 break;
2361 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002362 }
2363
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002364 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002365 assert(!err);
2366
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002367 free(device_layers);
2368
Ian Elliottaaae5352015-07-06 14:27:58 -06002369 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
2370 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceInfoWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002371 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2372 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2373 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2374 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002375 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002376 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002377
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002378 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002379 assert(!err);
2380
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002381 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &demo->queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002382 assert(!err);
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002383 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002384
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002385 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(demo->queue_count * sizeof(VkPhysicalDeviceQueueProperties));
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002386 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002387 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002388 assert(queue_count >= 1);
2389
Ian Elliottaaae5352015-07-06 14:27:58 -06002390 // Construct the WSI surface description:
2391 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2392 demo->surface_description.pNext = NULL;
2393#ifdef _WIN32
2394 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2395 demo->surface_description.pPlatformHandle = demo->connection;
2396 demo->surface_description.pPlatformWindow = demo->window;
2397#else // _WIN32
2398 demo->platform_handle_xcb.connection = demo->connection;
2399 demo->platform_handle_xcb.root = demo->screen->root;
2400 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2401 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2402 demo->surface_description.pPlatformWindow = &demo->window;
2403#endif // _WIN32
2404
2405 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002406 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2407 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002408 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2409 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2410 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002411 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002412
2413 // Search for a graphics and a present queue in the array of queue
2414 // families, try to find one that supports both
2415 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2416 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002417 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002418 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2419 if (graphicsQueueNodeIndex == UINT32_MAX) {
2420 graphicsQueueNodeIndex = i;
2421 }
2422
2423 if (supportsPresent[i] == VK_TRUE) {
2424 graphicsQueueNodeIndex = i;
2425 presentQueueNodeIndex = i;
2426 break;
2427 }
2428 }
2429 }
2430 if (presentQueueNodeIndex == UINT32_MAX) {
2431 // If didn't find a queue that supports both graphics and present, then
2432 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002433 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002434 if (supportsPresent[i] == VK_TRUE) {
2435 presentQueueNodeIndex = i;
2436 break;
2437 }
2438 }
2439 }
2440 free(supportsPresent);
2441
2442 // Generate error if could not find both a graphics and a present queue
2443 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2444 ERR_EXIT("Could not find a graphics and a present queue\n",
2445 "WSI Initialization Failure");
2446 }
2447
2448 // TODO: Add support for separate queues, including presentation,
2449 // synchronization, and appropriate tracking for QueueSubmit
2450 // While it is possible for an application to use a separate graphics and a
2451 // present queues, this demo program assumes it is only using one:
2452 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2453 ERR_EXIT("Could not find a common graphics and a present queue\n",
2454 "WSI Initialization Failure");
2455 }
2456
2457 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002458
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002459 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002460 0, &demo->queue);
2461 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002462
Ian Elliottaaae5352015-07-06 14:27:58 -06002463 // Get the list of VkFormat's that are supported:
2464 size_t formatsSize;
2465 err = demo->fpGetSurfaceInfoWSI(demo->device,
2466 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2467 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2468 &formatsSize, NULL);
2469 assert(!err);
2470 VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize);
2471 err = demo->fpGetSurfaceInfoWSI(demo->device,
2472 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2473 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2474 &formatsSize, surfFormats);
2475 assert(!err);
2476 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2477 // the surface has no preferred format. Otherwise, at least one
2478 // supported format will be returned.
2479 size_t formatCount = formatsSize / sizeof(VkSurfaceFormatPropertiesWSI);
2480 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2481 {
2482 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2483 }
2484 else
2485 {
2486 assert(formatCount >= 1);
2487 demo->format = surfFormats[0].format;
2488 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06002489
David Pinedo2bb7c932015-06-18 17:03:14 -06002490 demo->quit = false;
2491 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002492
2493 // Get Memory information and properties
2494 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2495 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002496}
2497
2498static void demo_init_connection(struct demo *demo)
2499{
Ian Elliott639ca472015-04-16 15:23:05 -06002500#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002501 const xcb_setup_t *setup;
2502 xcb_screen_iterator_t iter;
2503 int scr;
2504
2505 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002506 if (demo->connection == NULL) {
2507 printf("Cannot find a compatible Vulkan installable client driver "
2508 "(ICD).\nExiting ...\n");
2509 fflush(stdout);
2510 exit(1);
2511 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002512
2513 setup = xcb_get_setup(demo->connection);
2514 iter = xcb_setup_roots_iterator(setup);
2515 while (scr-- > 0)
2516 xcb_screen_next(&iter);
2517
2518 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002519#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002520}
2521
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002522static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002523{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002524 vec3 eye = {0.0f, 3.0f, 5.0f};
2525 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002526 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002527
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002528 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002529 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002530
Piers Daniell735ee532015-02-23 16:23:13 -07002531 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002532 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002533 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002534 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002535 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002536 if (strcmp(argv[i], "--use_glsl") == 0) {
2537 demo->use_glsl = true;
2538 continue;
2539 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002540 if (strcmp(argv[i], "--validate") == 0) {
2541 demo->validate = true;
2542 continue;
2543 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002544 if (strcmp(argv[i], "--c") == 0 &&
2545 demo->frameCount == INT_MAX &&
2546 i < argc-1 &&
2547 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2548 demo->frameCount >= 0)
2549 {
2550 i++;
2551 continue;
2552 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002553
David Pinedo2bb7c932015-06-18 17:03:14 -06002554 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002555 fflush(stderr);
2556 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002557 }
2558
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002559 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002560 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002561
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002562 demo->width = 500;
2563 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002564
2565 demo->spin_angle = 0.01f;
2566 demo->spin_increment = 0.01f;
2567 demo->pause = false;
2568
Piers Daniell735ee532015-02-23 16:23:13 -07002569 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002570 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2571 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002572}
2573
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002574
Ian Elliott639ca472015-04-16 15:23:05 -06002575#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002576extern int __getmainargs(
2577 int * _Argc,
2578 char *** _Argv,
2579 char *** _Env,
2580 int _DoWildCard,
2581 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002582
Ian Elliotta748eaf2015-04-28 15:50:36 -06002583int WINAPI WinMain(HINSTANCE hInstance,
2584 HINSTANCE hPrevInstance,
2585 LPSTR pCmdLine,
2586 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002587{
2588 MSG msg; // message
2589 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002590 int argc;
2591 char** argv;
2592 char** env;
2593 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002594
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002595 __getmainargs(&argc,&argv,&env,0,&new_mode);
2596
2597 demo_init(&demo, argc, argv);
2598 demo.connection = hInstance;
2599 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002600 demo_create_window(&demo);
2601
2602 demo_prepare(&demo);
2603
2604 done = false; //initialize loop condition variable
2605 /* main message loop*/
2606 while(!done)
2607 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002608 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002609 if (msg.message == WM_QUIT) //check for a quit message
2610 {
2611 done = true; //if found, quit app
2612 }
2613 else
2614 {
2615 /* Translate and dispatch to event queue*/
2616 TranslateMessage(&msg);
2617 DispatchMessage(&msg);
2618 }
2619 }
2620
2621 demo_cleanup(&demo);
2622
Tony Barbourf9921732015-04-22 11:36:22 -06002623 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002624}
2625#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002626int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002627{
2628 struct demo demo;
2629
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002630 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002631 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002632
2633 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002634 demo_run(&demo);
2635
2636 demo_cleanup(&demo);
2637
2638 return 0;
2639}
Ian Elliott639ca472015-04-16 15:23:05 -06002640#endif // _WIN32