blob: 7bdef0fd4b91abdf9844fd5003de5c294bcd2308 [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 Elliott639ca472015-04-16 15:23:05 -0600312#endif // _WIN32
Ian Elliottaaae5352015-07-06 14:27:58 -0600313 VkPlatformHandleXcbWSI platform_handle_xcb;
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 Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600396};
397
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600398static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
399{
400 // Search memtypes to find first index with those properties
401 for (uint32_t i = 0; i < 32; i++) {
402 if ((typeBits & 1) == 1) {
403 // Type is available, does it match user properties?
404 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
405 *typeIndex = i;
406 return VK_SUCCESS;
407 }
408 }
409 typeBits >>= 1;
410 }
411 // No memory types matched, return failure
412 return VK_UNSUPPORTED;
413}
414
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600415static void demo_flush_init_cmd(struct demo *demo)
416{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600417 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600418
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600419 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600420 return;
421
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600422 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600423 assert(!err);
424
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600425 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600426 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427
Tony Barbour155cce82015-07-03 10:33:54 -0600428 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600429 assert(!err);
430
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432 assert(!err);
433
Tony Barbour155cce82015-07-03 10:33:54 -0600434 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600435 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436}
437
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438static void demo_set_image_layout(
439 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600440 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400441 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600442 VkImageLayout old_image_layout,
443 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600445 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600446
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600447 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600448 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600449 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600451 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800452 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600453 .flags = 0,
454 };
455
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600456 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600457 assert(!err);
458
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600459 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600462 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600464 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600465 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600466 }
467
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600468 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470 .pNext = NULL,
471 .outputMask = 0,
472 .inputMask = 0,
473 .oldLayout = old_image_layout,
474 .newLayout = new_image_layout,
475 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400476 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 };
478
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600479 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600480 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600481 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600482 }
483
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600484 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600485 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600486 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487 }
488
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600489 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600490
Tony Barbour50bbca42015-06-29 16:20:35 -0600491 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
492 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600493
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600494 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495}
496
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600497static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498{
Chia-I Wuf973e322015-07-08 13:34:24 +0800499 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600500 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600501 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600502 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600503 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700504 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800505 const VkClearValue clear_values[2] = {
506 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
507 [1] = { .ds = { 1.0f, 0 } },
508 };
509 const VkRenderPassBeginInfo rp_begin = {
510 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
511 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800512 .renderPass = demo->render_pass,
513 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800514 .renderArea.offset.x = 0,
515 .renderArea.offset.y = 0,
516 .renderArea.extent.width = demo->width,
517 .renderArea.extent.height = demo->height,
518 .attachmentCount = 2,
519 .pAttachmentClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700520 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800521 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600523 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600524 assert(!err);
525
Chia-I Wua74c5b22015-07-07 11:50:03 +0800526 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
527
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600528 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600530 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600531 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600532
Tony Barbour155cce82015-07-03 10:33:54 -0600533 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
534 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
535 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
536 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600538 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800539 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600540
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542 assert(!err);
543}
544
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600545
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600546void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600548 mat4x4 MVP, Model, VP;
549 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600550 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600551 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600553 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600554
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600555 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600556 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700557 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600558 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600559
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500560 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561 assert(!err);
562
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600563 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500565 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566 assert(!err);
567}
568
569static void demo_draw(struct demo *demo)
570{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600571 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600572 VkSemaphore presentCompleteSemaphore;
573 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
574 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
575 .pNext = NULL,
576 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
577 };
Tony Barbour155cce82015-07-03 10:33:54 -0600578 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579
Ian Elliottaaae5352015-07-06 14:27:58 -0600580 err = vkCreateSemaphore(demo->device,
581 &presentCompleteSemaphoreCreateInfo,
582 &presentCompleteSemaphore);
583 assert(!err);
584
585 // Get the index of the next available swapchain image:
586 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
587 UINT64_MAX,
588 presentCompleteSemaphore,
589 &demo->current_buffer);
590 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
591 // return codes
592 assert(!err);
593
594 // Wait for the present complete semaphore to be signaled to ensure
595 // that the image won't be rendered to until the presentation
596 // engine has fully released ownership to the application, and it is
597 // okay to render to the image.
598 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
599
600// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600601 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600602 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603 assert(!err);
604
Ian Elliottaaae5352015-07-06 14:27:58 -0600605 VkPresentInfoWSI present = {
606 .sType = VK_STRUCTURE_TYPE_QUEUE_PRESENT_INFO_WSI,
607 .pNext = NULL,
608 .swapChainCount = 1,
609 .swapChains = &demo->swap_chain,
610 .imageIndices = &demo->current_buffer,
611 };
612
613// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600614 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600615 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
616 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600617 assert(!err);
618
Ian Elliottaaae5352015-07-06 14:27:58 -0600619// FIXME: UNCOMMENT THE FOLLOWING LINE ONCE WE HAVE A NEW-ENOUGH "vulkan.h" HEADER:
620// err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
621 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800622
623 err = vkQueueWaitIdle(demo->queue);
624 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625}
626
627static void demo_prepare_buffers(struct demo *demo)
628{
Ian Elliottaaae5352015-07-06 14:27:58 -0600629 VkResult U_ASSERT_ONLY err;
630
631 // Check the surface properties and formats
632 size_t capsSize;
633 size_t presentModesSize;
634 err = demo->fpGetSurfaceInfoWSI(demo->device,
635 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
636 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, NULL);
637 assert(!err);
638 err = demo->fpGetSurfaceInfoWSI(demo->device,
639 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
640 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, NULL);
641 assert(!err);
642
643 VkSurfacePropertiesWSI *surfProperties =
644 (VkSurfacePropertiesWSI *)malloc(capsSize);
645 VkSurfacePresentModePropertiesWSI *presentModes =
646 (VkSurfacePresentModePropertiesWSI *)malloc(presentModesSize);
647
648 err = demo->fpGetSurfaceInfoWSI(demo->device,
649 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
650 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, surfProperties);
651 assert(!err);
652 err = demo->fpGetSurfaceInfoWSI(demo->device,
653 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
654 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, presentModes);
655 assert(!err);
656
657 VkExtent2D swapChainExtent;
658 // width and height are either both -1, or both not -1.
659 if (surfProperties->currentExtent.width == -1)
660 {
661 // If the surface size is undefined, the size is set to
662 // the size of the images requested.
663 swapChainExtent.width = demo->width;
664 swapChainExtent.height = demo->height;
665 }
666 else
667 {
668 // If the surface size is defined, the swap chain size must match
669 swapChainExtent = surfProperties->currentExtent;
670 }
671
672 // If mailbox mode is available, use it, as is the lowest-latency non-
673 // tearing mode. If not, fall back to IMMEDIATE which should always be
674 // available.
675 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
676 size_t presentModeCount = presentModesSize / sizeof(VkSurfacePresentModePropertiesWSI);
677 for (size_t i = 0; i < presentModeCount; i++) {
678 if (presentModes[i].presentMode == VK_PRESENT_MODE_MAILBOX_WSI) {
679 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
680 break;
681 }
682 }
683
684 // Determine the number of VkImage's to use in the swap chain (we desire to
685 // own only 1 image at a time, besides the images being displayed and
686 // queued for display):
687 uint32_t desiredNumberOfSwapChainImages = surfProperties->minImageCount + 1;
688 if ((surfProperties->maxImageCount > 0) &&
689 (desiredNumberOfSwapChainImages > surfProperties->maxImageCount))
690 {
691 // Application must settle for fewer images than desired:
692 desiredNumberOfSwapChainImages = surfProperties->maxImageCount;
693 }
694
695 VkSurfaceTransformFlagBitsWSI preTransform;
696 if (surfProperties->supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
697 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
698 } else {
699 preTransform = surfProperties->currentTransform;
700 }
701
Chia-I Wucbb564e2015-04-16 22:02:10 +0800702 const VkSwapChainCreateInfoWSI swap_chain = {
703 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
704 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600705 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
706 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800707 .imageFormat = demo->format,
708 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600709 .width = swapChainExtent.width,
710 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600711 },
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800713 .imageArraySize = 1,
Ian Elliottaaae5352015-07-06 14:27:58 -0600714 .presentMode = swapChainPresentMode,
715 .oldSwapChain.handle = 0,
716 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600717 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600718 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600719
Jon Ashburn0b85d052015-05-21 18:13:33 -0600720 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800721 assert(!err);
722
Ian Elliottaaae5352015-07-06 14:27:58 -0600723 size_t swapChainImagesSize;
724 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
725 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
726 &swapChainImagesSize, NULL);
727 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800728
Ian Elliottaaae5352015-07-06 14:27:58 -0600729 VkSwapChainImagePropertiesWSI* swapChainImages = (VkSwapChainImagePropertiesWSI*)malloc(swapChainImagesSize);
730 assert(swapChainImages);
731 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
732 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
733 &swapChainImagesSize, swapChainImages);
734 assert(!err);
735
736 // The number of images within the swap chain is determined based on the size of the info returned
737 demo->swapChainImageCount = swapChainImagesSize / sizeof(VkSwapChainImagePropertiesWSI);
738
739 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
740 assert(demo->buffers);
741
742 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800743 VkAttachmentViewCreateInfo color_attachment_view = {
744 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600745 .pNext = NULL,
746 .format = demo->format,
747 .mipLevel = 0,
748 .baseArraySlice = 0,
749 .arraySize = 1,
750 };
751
Ian Elliottaaae5352015-07-06 14:27:58 -0600752 demo->buffers[i].image = swapChainImages[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600754 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400755 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600756 VK_IMAGE_LAYOUT_UNDEFINED,
757 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600758
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600759 color_attachment_view.image = demo->buffers[i].image;
760
Chia-I Wua74c5b22015-07-07 11:50:03 +0800761 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600762 &color_attachment_view, &demo->buffers[i].view);
763 assert(!err);
764 }
765}
766
767static void demo_prepare_depth(struct demo *demo)
768{
Tony Barbour72304ef2015-04-16 15:59:00 -0600769 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600770 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600771 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600772 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600773 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600774 .format = depth_format,
775 .extent = { demo->width, demo->height, 1 },
776 .mipLevels = 1,
777 .arraySize = 1,
778 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600779 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600780 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781 .flags = 0,
782 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600783 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600784 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500785 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600786 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600787 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600788 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800789 VkAttachmentViewCreateInfo view = {
790 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600791 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600792 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600793 .mipLevel = 0,
794 .baseArraySlice = 0,
795 .arraySize = 1,
796 .flags = 0,
797 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600798
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500799 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600800 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801
802 demo->depth.format = depth_format;
803
804 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600805 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600806 &demo->depth.image);
807 assert(!err);
808
Tony Barbour155cce82015-07-03 10:33:54 -0600809 err = vkGetImageMemoryRequirements(demo->device,
810 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600811
812 mem_alloc.allocationSize = mem_reqs.size;
813 err = memory_type_from_properties(demo,
814 mem_reqs.memoryTypeBits,
815 VK_MEMORY_PROPERTY_DEVICE_ONLY,
816 &mem_alloc.memoryTypeIndex);
817 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600818
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500819 /* allocate memory */
820 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
821 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600822
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500823 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600824 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500825 demo->depth.mem, 0);
826 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600827
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600828 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400829 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600830 VK_IMAGE_LAYOUT_UNDEFINED,
831 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600832
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600833 /* create image view */
834 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800835 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600837}
838
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600839/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600840 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600841 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600842 * \param demo : Needed to access VK calls
843 * \param filename : the png file to be loaded
844 * \param width : width of png, to be updated as a side effect of this function
845 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600846 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600847 * \return bool : an opengl texture id. true if successful?,
848 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600849 *
850 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
851 * Modified to copy image to memory
852 *
853 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700854bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600855 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600856 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600857{
858 //header for testing if it is a png
859 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600860 int is_png, bit_depth, color_type, rowbytes;
861 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700862 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600863 png_structp png_ptr;
864 png_infop info_ptr, end_info;
865 png_byte *image_data;
866 png_bytep *row_pointers;
867
868 //open file as binary
869 FILE *fp = fopen(filename, "rb");
870 if (!fp) {
871 return false;
872 }
873
874 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600875 retval = fread(header, 1, 8, fp);
876 if (retval != 8) {
877 fclose(fp);
878 return false;
879 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600880
881 //test if png
882 is_png = !png_sig_cmp(header, 0, 8);
883 if (!is_png) {
884 fclose(fp);
885 return false;
886 }
887
888 //create png struct
889 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
890 NULL, NULL);
891 if (!png_ptr) {
892 fclose(fp);
893 return (false);
894 }
895
896 //create png info struct
897 info_ptr = png_create_info_struct(png_ptr);
898 if (!info_ptr) {
899 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
900 fclose(fp);
901 return (false);
902 }
903
904 //create png info struct
905 end_info = png_create_info_struct(png_ptr);
906 if (!end_info) {
907 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
908 fclose(fp);
909 return (false);
910 }
911
912 //png error stuff, not sure libpng man suggests this.
913 if (setjmp(png_jmpbuf(png_ptr))) {
914 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
915 fclose(fp);
916 return (false);
917 }
918
919 //init png reading
920 png_init_io(png_ptr, fp);
921
922 //let libpng know you already read the first 8 bytes
923 png_set_sig_bytes(png_ptr, 8);
924
925 // read all the info up to the image data
926 png_read_info(png_ptr, info_ptr);
927
928 // get info about png
929 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
930 NULL, NULL, NULL);
931
932 //update width and height based on png info
933 *width = twidth;
934 *height = theight;
935
936 // Require that incoming texture be 8bits per color component
937 // and 4 components (RGBA).
938 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
939 png_get_channels(png_ptr, info_ptr) != 4) {
940 return false;
941 }
942
943 if (rgba_data == NULL) {
944 // If data pointer is null, we just want the width & height
945 // clean up memory and close stuff
946 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
947 fclose(fp);
948
949 return true;
950 }
951
952 // Update the png info struct.
953 png_read_update_info(png_ptr, info_ptr);
954
955 // Row size in bytes.
956 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
957
958 // Allocate the image_data as a big block, to be given to opengl
959 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
960 if (!image_data) {
961 //clean up memory and close stuff
962 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
963 fclose(fp);
964 return false;
965 }
966
967 // row_pointers is for pointing to image_data for reading the png with libpng
968 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
969 if (!row_pointers) {
970 //clean up memory and close stuff
971 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
972 // delete[] image_data;
973 fclose(fp);
974 return false;
975 }
976 // set the individual row_pointers to point at the correct offsets of image_data
977 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600979
980 // read the png into image_data through row_pointers
981 png_read_image(png_ptr, row_pointers);
982
983 // clean up memory and close stuff
984 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
985 free(row_pointers);
986 free(image_data);
987 fclose(fp);
988
989 return true;
990}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600991
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992static void demo_prepare_texture_image(struct demo *demo,
993 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600994 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600995 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600996 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600997 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600999 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001000 int32_t tex_width;
1001 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001002 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001003
David Pinedo30bd71d2015-04-23 08:16:57 -06001004 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1005 {
1006 printf("Failed to load textures\n");
1007 fflush(stdout);
1008 exit(1);
1009 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001010
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001011 tex_obj->tex_width = tex_width;
1012 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001013
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001014 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001015 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001017 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018 .format = tex_format,
1019 .extent = { tex_width, tex_height, 1 },
1020 .mipLevels = 1,
1021 .arraySize = 1,
1022 .samples = 1,
1023 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001024 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001025 .flags = 0,
1026 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001027 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001028 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001029 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001031 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032 };
1033
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001034 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001036 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001037 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038 assert(!err);
1039
Tony Barbour155cce82015-07-03 10:33:54 -06001040 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001041 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001042
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001043 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001044
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001045 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1046 assert(!err);
1047
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001048 /* allocate memory */
1049 err = vkAllocMemory(demo->device, &mem_alloc,
1050 &(tex_obj->mem));
1051 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001053 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001054 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001055 tex_obj->mem, 0);
1056 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057
Tony Barbour72304ef2015-04-16 15:59:00 -06001058 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001059 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001060 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061 .mipLevel = 0,
1062 .arraySlice = 0,
1063 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001064 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065 void *data;
1066
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001067 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1068 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001070 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 assert(!err);
1072
1073 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1074 fprintf(stderr, "Error loading texture: %s\n", filename);
1075 }
1076
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001077 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 assert(!err);
1079 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001080
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001081 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001082 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001083 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001085 tex_obj->imageLayout);
1086 /* 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 -07001087}
1088
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001089static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001090{
1091 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001092 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001093 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001094}
1095
1096static void demo_prepare_textures(struct demo *demo)
1097{
Tony Barbour72304ef2015-04-16 15:59:00 -06001098 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001099 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001100 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001101 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001102
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001103 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001104 assert(!err);
1105
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001106 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107
FslNopper6a7e50e2015-05-06 21:42:01 +02001108 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001109 /* Device can texture using linear textures */
1110 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001111 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001112 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001113 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001114 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115
1116 memset(&staging_texture, 0, sizeof(staging_texture));
1117 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001118 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119
1120 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001121 VK_IMAGE_TILING_OPTIMAL,
1122 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1123 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001124
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001125 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001126 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001127 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001128 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001130 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001131 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001132 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001133 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001135 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001136 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001138 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139 .destOffset = { 0, 0, 0 },
1140 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1141 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001142 vkCmdCopyImage(demo->cmd,
1143 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1144 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001145 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001146
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001147 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001148 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001149 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001150 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001152 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001153
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001154 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001156 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1157 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001158 }
1159
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001160 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001161 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001163 .magFilter = VK_TEX_FILTER_NEAREST,
1164 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001165 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001166 .addressU = VK_TEX_ADDRESS_CLAMP,
1167 .addressV = VK_TEX_ADDRESS_CLAMP,
1168 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001169 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001171 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001172 .minLod = 0.0f,
1173 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001174 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001175 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001176
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001177 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001178 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001179 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001180 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001181 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001183 .channels = { VK_CHANNEL_SWIZZLE_R,
1184 VK_CHANNEL_SWIZZLE_G,
1185 VK_CHANNEL_SWIZZLE_B,
1186 VK_CHANNEL_SWIZZLE_A, },
1187 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001188 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001189
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001190 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001192 &demo->textures[i].sampler);
1193 assert(!err);
1194
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001195 /* create image view */
1196 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001197 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198 &demo->textures[i].view);
1199 assert(!err);
1200 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001202
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001203void demo_prepare_cube_data_buffer(struct demo *demo)
1204{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001205 VkBufferCreateInfo buf_info;
1206 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001207 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001208 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001209 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001210 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001211 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001212 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001213 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001214 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001215 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001216 int i;
1217 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001218 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001219 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001220
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001221 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001222 mat4x4_mul(MVP, VP, demo->model_matrix);
1223 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001224// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001225
1226 for (i=0; i<12*3; i++) {
1227 data.position[i][0] = g_vertex_buffer_data[i*3];
1228 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1229 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1230 data.position[i][3] = 1.0f;
1231 data.attr[i][0] = g_uv_buffer_data[2*i];
1232 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1233 data.attr[i][2] = 0;
1234 data.attr[i][3] = 0;
1235 }
1236
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001237 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001238 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001239 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001240 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001241 assert(!err);
1242
Tony Barbour155cce82015-07-03 10:33:54 -06001243 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001244 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001245
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001246 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001247 err = memory_type_from_properties(demo,
1248 mem_reqs.memoryTypeBits,
1249 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1250 &alloc_info.memoryTypeIndex);
1251 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001252
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001253 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1254 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001255
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001256 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1257 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001258
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001259 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001260
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001261 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1262 assert(!err);
1263
Tony Barbour155cce82015-07-03 10:33:54 -06001264 err = vkBindBufferMemory(demo->device,
1265 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001266 demo->uniform_data.mem, 0);
1267 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001268
1269 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001270 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001271 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001272 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001273 view_info.offset = 0;
1274 view_info.range = sizeof(data);
1275
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001276 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001277 assert(!err);
1278
Chia-I Wuae721ba2015-05-25 16:27:55 +08001279 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001280}
1281
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001282static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001283{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001284 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001285 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001286 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001287 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001288 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001289 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001290 },
1291 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001292 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001293 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001294 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001295 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001296 },
1297 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001298 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001299 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001300 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001301 .count = 2,
1302 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001303 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001304 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001305
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001306 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001307 &descriptor_layout, &demo->desc_layout);
1308 assert(!err);
1309
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001310 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1311 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1312 .pNext = NULL,
1313 .descriptorSetCount = 1,
1314 .pSetLayouts = &demo->desc_layout,
1315 };
1316
1317 err = vkCreatePipelineLayout(demo->device,
1318 &pPipelineLayoutCreateInfo,
1319 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001320 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001321}
1322
Chia-I Wuf973e322015-07-08 13:34:24 +08001323static void demo_prepare_render_pass(struct demo *demo)
1324{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001325 const VkAttachmentDescription attachments[2] = {
1326 [0] = {
1327 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1328 .pNext = NULL,
1329 .format = demo->format,
1330 .samples = 1,
1331 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1332 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1333 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1334 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1335 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1336 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1337 },
1338 [1] = {
1339 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1340 .pNext = NULL,
1341 .format = demo->depth.format,
1342 .samples = 1,
1343 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1344 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1345 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1346 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1347 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1348 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1349 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001350 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001351 const VkAttachmentReference color_reference = {
1352 .attachment = 0,
1353 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1354 };
1355 const VkSubpassDescription subpass = {
1356 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1357 .pNext = NULL,
1358 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1359 .flags = 0,
1360 .inputCount = 0,
1361 .inputAttachments = NULL,
1362 .colorCount = 1,
1363 .colorAttachments = &color_reference,
1364 .resolveAttachments = NULL,
1365 .depthStencilAttachment = {
1366 .attachment = 1,
1367 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1368 },
1369 .preserveCount = 0,
1370 .preserveAttachments = NULL,
1371 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001372 const VkRenderPassCreateInfo rp_info = {
1373 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1374 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001375 .attachmentCount = 2,
1376 .pAttachments = attachments,
1377 .subpassCount = 1,
1378 .pSubpasses = &subpass,
1379 .dependencyCount = 0,
1380 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001381 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001382 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001383
1384 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1385 assert(!err);
1386}
1387
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001388static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001389 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001390 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001391 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001393 VkShaderModuleCreateInfo moduleCreateInfo;
1394 VkShaderCreateInfo shaderCreateInfo;
1395 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001396 VkShader shader;
1397 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001399
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001400 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1401 moduleCreateInfo.pNext = NULL;
1402
1403 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1404 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001405 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406
Cody Northrop1fedb212015-05-28 11:27:16 -06001407 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001408 moduleCreateInfo.codeSize = size;
1409 moduleCreateInfo.pCode = code;
1410 moduleCreateInfo.flags = 0;
1411 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001412 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001413 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001414 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001415
1416 shaderCreateInfo.flags = 0;
1417 shaderCreateInfo.module = shaderModule;
1418 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001419 } else {
1420 // Create fake SPV structure to feed GLSL
1421 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001422 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1423 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1424 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001425
1426 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001427 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1428 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1429 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1430 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001431
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001432 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001433 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001434 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001435 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001436
1437 shaderCreateInfo.flags = 0;
1438 shaderCreateInfo.module = shaderModule;
1439 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001440 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441 return shader;
1442}
1443
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001444char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001445{
1446 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001447 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001448 void *shader_code;
1449
1450 FILE *fp = fopen(filename, "rb");
1451 if (!fp) return NULL;
1452
1453 fseek(fp, 0L, SEEK_END);
1454 size = ftell(fp);
1455
1456 fseek(fp, 0L, SEEK_SET);
1457
1458 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001459 retval = fread(shader_code, size, 1, fp);
1460 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001461
1462 *psize = size;
1463
1464 return shader_code;
1465}
1466
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001467static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468{
Cody Northrop1fedb212015-05-28 11:27:16 -06001469 if (!demo->use_glsl) {
1470 void *vertShaderCode;
1471 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001472
Cody Northrop1fedb212015-05-28 11:27:16 -06001473 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001474
Cody Northrop1fedb212015-05-28 11:27:16 -06001475 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1476 vertShaderCode, size);
1477 } else {
1478 static const char *vertShaderText =
1479 "#version 140\n"
1480 "#extension GL_ARB_separate_shader_objects : enable\n"
1481 "#extension GL_ARB_shading_language_420pack : enable\n"
1482 "\n"
1483 "layout(binding = 0) uniform buf {\n"
1484 " mat4 MVP;\n"
1485 " vec4 position[12*3];\n"
1486 " vec4 attr[12*3];\n"
1487 "} ubuf;\n"
1488 "\n"
1489 "layout (location = 0) out vec4 texcoord;\n"
1490 "\n"
1491 "void main() \n"
1492 "{\n"
1493 " texcoord = ubuf.attr[gl_VertexID];\n"
1494 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1495 "\n"
1496 " // GL->VK conventions\n"
1497 " gl_Position.y = -gl_Position.y;\n"
1498 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1499 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001500
Cody Northrop1fedb212015-05-28 11:27:16 -06001501 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1502 (const void *) vertShaderText,
1503 strlen(vertShaderText));
1504 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001505}
1506
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001507static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001508{
Cody Northrop1fedb212015-05-28 11:27:16 -06001509 if (!demo->use_glsl) {
1510 void *fragShaderCode;
1511 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001512
Cody Northrop1fedb212015-05-28 11:27:16 -06001513 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001514
Cody Northrop1fedb212015-05-28 11:27:16 -06001515 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1516 fragShaderCode, size);
1517 } else {
1518 static const char *fragShaderText =
1519 "#version 140\n"
1520 "#extension GL_ARB_separate_shader_objects : enable\n"
1521 "#extension GL_ARB_shading_language_420pack : enable\n"
1522 "layout (binding = 1) uniform sampler2D tex;\n"
1523 "\n"
1524 "layout (location = 0) in vec4 texcoord;\n"
1525 "layout (location = 0) out vec4 uFragColor;\n"
1526 "void main() {\n"
1527 " uFragColor = texture(tex, texcoord.xy);\n"
1528 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001529
Cody Northrop1fedb212015-05-28 11:27:16 -06001530 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1531 (const void *) fragShaderText,
1532 strlen(fragShaderText));
1533 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001534}
1535
1536static void demo_prepare_pipeline(struct demo *demo)
1537{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001538 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001539 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001540 VkPipelineInputAssemblyStateCreateInfo ia;
1541 VkPipelineRasterStateCreateInfo rs;
1542 VkPipelineColorBlendStateCreateInfo cb;
1543 VkPipelineDepthStencilStateCreateInfo ds;
1544 VkPipelineViewportStateCreateInfo vp;
1545 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001546 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001547
1548 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001549 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001550 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001551
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001553 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001554 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001555
1556 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001557 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001558 rs.fillMode = VK_FILL_MODE_SOLID;
1559 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001560 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001561 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562
1563 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001564 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1565 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001566 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001567 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001568 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001569 cb.attachmentCount = 1;
1570 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571
Tony Barbour29645d02015-01-16 14:27:35 -07001572 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001573 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001574 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001575
1576 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001577 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001578 ds.depthTestEnable = VK_TRUE;
1579 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001580 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001581 ds.depthBoundsEnable = VK_FALSE;
1582 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1583 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001584 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001585 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001586 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587
Tony Barbour29645d02015-01-16 14:27:35 -07001588 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001589 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001590 ms.sampleMask = 1;
Tony Barbour3ca22502015-06-26 10:18:34 -06001591 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001592
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001593 // Two stages: vs and fs
1594 pipeline.stageCount = 2;
1595 VkPipelineShaderStageCreateInfo shaderStages[2];
1596 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1597
1598 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1599 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1600 shaderStages[0].shader = demo_prepare_vs(demo);
1601
1602 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1603 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1604 shaderStages[1].shader = demo_prepare_fs(demo);
1605
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001606 memset(&pipelineCache, 0, sizeof(pipelineCache));
1607 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001608
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001609 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1610 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001611
1612 pipeline.pVertexInputState = NULL;
1613 pipeline.pInputAssemblyState = &ia;
1614 pipeline.pRasterState = &rs;
1615 pipeline.pColorBlendState = &cb;
1616 pipeline.pMultisampleState = &ms;
1617 pipeline.pViewportState = &vp;
1618 pipeline.pDepthStencilState = &ds;
1619 pipeline.pStages = shaderStages;
1620
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001621 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001622 assert(!err);
1623
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001624 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001625 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001626 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627}
1628
1629static void demo_prepare_dynamic_states(struct demo *demo)
1630{
Tony Barbour155cce82015-07-03 10:33:54 -06001631 VkDynamicViewportStateCreateInfo viewport_create;
1632 VkDynamicRasterStateCreateInfo raster;
1633 VkDynamicColorBlendStateCreateInfo color_blend;
1634 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001635 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636
Tony Barbour29645d02015-01-16 14:27:35 -07001637 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001638 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001639 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001640 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001641 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001642 viewport.height = (float) demo->height;
1643 viewport.width = (float) demo->width;
1644 viewport.minDepth = (float) 0.0f;
1645 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001646 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001647 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001648 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001649 scissor.extent.width = demo->width;
1650 scissor.extent.height = demo->height;
1651 scissor.offset.x = 0;
1652 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001653 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001654
1655 memset(&raster, 0, sizeof(raster));
Tony Barbour155cce82015-07-03 10:33:54 -06001656 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001657 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001658
1659 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour155cce82015-07-03 10:33:54 -06001660 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001661 color_blend.blendConst[0] = 1.0f;
1662 color_blend.blendConst[1] = 1.0f;
1663 color_blend.blendConst[2] = 1.0f;
1664 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001665
1666 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour155cce82015-07-03 10:33:54 -06001667 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001668 depth_stencil.minDepthBounds = 0.0f;
1669 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001670 depth_stencil.stencilBackRef = 0;
1671 depth_stencil.stencilFrontRef = 0;
1672 depth_stencil.stencilReadMask = 0xff;
1673 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001674
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001675 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676 assert(!err);
1677
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001678 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001679 assert(!err);
1680
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001681 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001682 &color_blend, &demo->color_blend);
1683 assert(!err);
1684
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001685 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686 &depth_stencil, &demo->depth_stencil);
1687 assert(!err);
1688}
1689
Chia-I Wu63ea9262015-03-26 13:14:16 +08001690static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001691{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001692 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001693 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001694 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001695 .count = 1,
1696 },
1697 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001698 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001699 .count = DEMO_TEXTURE_COUNT,
1700 },
1701 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001702 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001703 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001704 .pNext = NULL,
1705 .count = 2,
1706 .pTypeCount = type_counts,
1707 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001708 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001709
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001710 err = vkCreateDescriptorPool(demo->device,
1711 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001712 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001713 assert(!err);
1714}
1715
1716static void demo_prepare_descriptor_set(struct demo *demo)
1717{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001718 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1719 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001720 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001721 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001722 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001723
Mike Stroyanebae8322015-04-17 12:36:38 -06001724 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001725 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001726 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001727 &demo->desc_set, &count);
1728 assert(!err && count == 1);
1729
Chia-I Wuae721ba2015-05-25 16:27:55 +08001730 memset(&tex_descs, 0, sizeof(tex_descs));
1731 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1732 tex_descs[i].sampler = demo->textures[i].sampler;
1733 tex_descs[i].imageView = demo->textures[i].view;
1734 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1735 }
1736
1737 memset(&writes, 0, sizeof(writes));
1738
1739 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1740 writes[0].destSet = demo->desc_set;
1741 writes[0].count = 1;
1742 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1743 writes[0].pDescriptors = &demo->uniform_data.desc;
1744
1745 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1746 writes[1].destSet = demo->desc_set;
1747 writes[1].destBinding = 1;
1748 writes[1].count = DEMO_TEXTURE_COUNT;
1749 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1750 writes[1].pDescriptors = tex_descs;
1751
1752 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1753 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001754}
1755
Chia-I Wuf973e322015-07-08 13:34:24 +08001756static void demo_prepare_framebuffers(struct demo *demo)
1757{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001758 VkAttachmentBindInfo attachments[2] = {
1759 [0] = {
Tobin Ehlisd415ac32015-07-06 14:02:36 -06001760 .view.handle = VK_NULL_HANDLE,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001761 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1762 },
1763 [1] = {
1764 .view = demo->depth.view,
1765 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1766 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001767 };
1768 const VkFramebufferCreateInfo fb_info = {
1769 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1770 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001771 .renderPass = demo->render_pass,
1772 .attachmentCount = 2,
1773 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001774 .width = demo->width,
1775 .height = demo->height,
1776 .layers = 1,
1777 };
1778 VkResult U_ASSERT_ONLY err;
1779 uint32_t i;
1780
1781 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001782 attachments[0].view = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001783 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1784 assert(!err);
1785 }
1786}
1787
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001788static void demo_prepare(struct demo *demo)
1789{
Cody Northrop91e221b2015-07-09 18:08:32 -06001790 VkResult U_ASSERT_ONLY err;
1791
1792 const VkCmdPoolCreateInfo cmd_pool_info = {
1793 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1794 .pNext = NULL,
1795 .queueFamilyIndex = demo->graphics_queue_node_index,
1796 .flags = 0,
1797 };
1798 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1799 assert(!err);
1800
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001801 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001802 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001803 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001804 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001805 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001806 .flags = 0,
1807 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001808
1809 demo_prepare_buffers(demo);
1810 demo_prepare_depth(demo);
1811 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001812 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001813
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001814 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001815 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001816 demo_prepare_pipeline(demo);
1817 demo_prepare_dynamic_states(demo);
1818
Ian Elliottaaae5352015-07-06 14:27:58 -06001819 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001820 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001821 assert(!err);
1822 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001823
Chia-I Wu63ea9262015-03-26 13:14:16 +08001824 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001825 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001826
Chia-I Wuf973e322015-07-08 13:34:24 +08001827 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001828
Ian Elliottaaae5352015-07-06 14:27:58 -06001829 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001830 demo->current_buffer = i;
1831 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1832 }
1833
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001834 /*
1835 * Prepare functions above may generate pipeline commands
1836 * that need to be flushed before beginning the render loop.
1837 */
1838 demo_flush_init_cmd(demo);
1839
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001840 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001841 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001842}
1843
David Pinedo2bb7c932015-06-18 17:03:14 -06001844static void demo_cleanup(struct demo *demo)
1845{
1846 uint32_t i;
1847
1848 demo->prepared = false;
1849
Tony Barbour155cce82015-07-03 10:33:54 -06001850 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1851 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1852 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001853 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001854 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001855
Tony Barbour155cce82015-07-03 10:33:54 -06001856 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1857 vkDestroyDynamicRasterState(demo->device, demo->raster);
1858 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1859 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001860
Tony Barbour155cce82015-07-03 10:33:54 -06001861 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001862 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001863 vkDestroyRenderPass(demo->device, demo->render_pass);
1864 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1865 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001866
1867 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001868 vkDestroyImageView(demo->device, demo->textures[i].view);
1869 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001870 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001871 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001872 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001873 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001874
Tony Barbour155cce82015-07-03 10:33:54 -06001875 vkDestroyAttachmentView(demo->device, demo->depth.view);
1876 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001877 vkFreeMemory(demo->device, demo->depth.mem);
1878
Tony Barbour155cce82015-07-03 10:33:54 -06001879 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1880 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001881 vkFreeMemory(demo->device, demo->uniform_data.mem);
1882
Ian Elliottaaae5352015-07-06 14:27:58 -06001883 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001884 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1885 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001886 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001887 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001888
Cody Northrop91e221b2015-07-09 18:08:32 -06001889 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001890 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001891 if (demo->validate) {
1892 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1893 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001894 vkDestroyInstance(demo->inst);
1895
1896#ifndef _WIN32
1897 xcb_destroy_window(demo->connection, demo->window);
1898 xcb_disconnect(demo->connection);
1899#endif // _WIN32
1900}
1901
1902// On MS-Windows, make this a global, so it's available to WndProc()
1903struct demo demo;
1904
Ian Elliott639ca472015-04-16 15:23:05 -06001905#ifdef _WIN32
1906static void demo_run(struct demo *demo)
1907{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001908 if (!demo->prepared)
1909 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001910 // Wait for work to finish before updating MVP.
1911 vkDeviceWaitIdle(demo->device);
1912 demo_update_data_buffer(demo);
1913
1914 demo_draw(demo);
1915
1916 // Wait for work to finish before updating MVP.
1917 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001918
David Pinedo2bb7c932015-06-18 17:03:14 -06001919 demo->curFrame++;
1920
1921 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1922 {
1923 demo->quit=true;
1924 demo_cleanup(demo);
1925 ExitProcess(0);
1926 }
1927
1928}
Ian Elliott639ca472015-04-16 15:23:05 -06001929
1930// MS-Windows event handling function:
1931LRESULT CALLBACK WndProc(HWND hWnd,
1932 UINT uMsg,
1933 WPARAM wParam,
1934 LPARAM lParam)
1935{
Ian Elliott639ca472015-04-16 15:23:05 -06001936 switch(uMsg)
1937 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001938 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001939 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001940 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001941 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001942 demo_run(&demo);
1943 return 0;
1944 default:
1945 break;
1946 }
1947 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1948}
1949
1950static void demo_create_window(struct demo *demo)
1951{
1952 WNDCLASSEX win_class;
1953
1954 // Initialize the window class structure:
1955 win_class.cbSize = sizeof(WNDCLASSEX);
1956 win_class.style = CS_HREDRAW | CS_VREDRAW;
1957 win_class.lpfnWndProc = WndProc;
1958 win_class.cbClsExtra = 0;
1959 win_class.cbWndExtra = 0;
1960 win_class.hInstance = demo->connection; // hInstance
1961 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1962 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1963 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1964 win_class.lpszMenuName = NULL;
1965 win_class.lpszClassName = demo->name;
1966 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1967 // Register window class:
1968 if (!RegisterClassEx(&win_class)) {
1969 // It didn't work, so try to give a useful error:
1970 printf("Unexpected error trying to start the application!\n");
1971 fflush(stdout);
1972 exit(1);
1973 }
1974 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001975 RECT wr = { 0, 0, demo->width, demo->height };
1976 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001977 demo->window = CreateWindowEx(0,
1978 demo->name, // class name
1979 demo->name, // app name
1980 WS_OVERLAPPEDWINDOW | // window style
1981 WS_VISIBLE |
1982 WS_SYSMENU,
1983 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001984 wr.right-wr.left, // width
1985 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001986 NULL, // handle to parent
1987 NULL, // handle to menu
1988 demo->connection, // hInstance
1989 NULL); // no extra parameters
1990 if (!demo->window) {
1991 // It didn't work, so try to give a useful error:
1992 printf("Cannot create a window in which to draw!\n");
1993 fflush(stdout);
1994 exit(1);
1995 }
1996}
1997#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001998static void demo_handle_event(struct demo *demo,
1999 const xcb_generic_event_t *event)
2000{
Piers Daniell735ee532015-02-23 16:23:13 -07002001 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002002 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002003 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002004 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002005 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002006 case XCB_CLIENT_MESSAGE:
2007 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2008 (*demo->atom_wm_delete_window).atom) {
2009 demo->quit = true;
2010 }
2011 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002012 case XCB_KEY_RELEASE:
2013 {
2014 const xcb_key_release_event_t *key =
2015 (const xcb_key_release_event_t *) event;
2016
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002017 switch (key->detail) {
2018 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002019 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002020 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002021 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002022 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002023 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002024 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002025 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002026 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002027 case 0x41:
2028 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002029 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002030 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031 }
2032 break;
2033 default:
2034 break;
2035 }
2036}
2037
2038static void demo_run(struct demo *demo)
2039{
2040 xcb_flush(demo->connection);
2041
2042 while (!demo->quit) {
2043 xcb_generic_event_t *event;
2044
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002045 if (demo->pause) {
2046 event = xcb_wait_for_event(demo->connection);
2047 } else {
2048 event = xcb_poll_for_event(demo->connection);
2049 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050 if (event) {
2051 demo_handle_event(demo, event);
2052 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002053 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002054
2055 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002056 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002057 demo_update_data_buffer(demo);
2058
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002059 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002060
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002061 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002062 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002063 demo->curFrame++;
2064 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2065 demo->quit = true;
2066
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002067 }
2068}
2069
2070static void demo_create_window(struct demo *demo)
2071{
2072 uint32_t value_mask, value_list[32];
2073
2074 demo->window = xcb_generate_id(demo->connection);
2075
2076 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2077 value_list[0] = demo->screen->black_pixel;
2078 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2079 XCB_EVENT_MASK_EXPOSURE;
2080
2081 xcb_create_window(demo->connection,
2082 XCB_COPY_FROM_PARENT,
2083 demo->window, demo->screen->root,
2084 0, 0, demo->width, demo->height, 0,
2085 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2086 demo->screen->root_visual,
2087 value_mask, value_list);
2088
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002089 /* Magic code that will send notification when window is destroyed */
2090 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2091 "WM_PROTOCOLS");
2092 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2093
2094 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2095 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2096
2097 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2098 demo->window, (*reply).atom, 4, 32, 1,
2099 &(*demo->atom_wm_delete_window).atom);
2100 free(reply);
2101
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002103
2104 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2105 const uint32_t coords[] = {100, 100};
2106 xcb_configure_window(demo->connection, demo->window,
2107 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002108}
Ian Elliott639ca472015-04-16 15:23:05 -06002109#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002110
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002111/*
2112 * Return 1 (true) if all layer names specified in check_names
2113 * can be found in given layer properties.
2114 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002115static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002116 uint32_t layer_count, VkLayerProperties *layers)
2117{
2118 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002119 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002120 for (uint32_t j = 0; j < layer_count; j++) {
2121 if (!strcmp(check_names[i], layers[j].layerName)) {
2122 found = 1;
2123 }
2124 }
2125 if (!found) {
2126 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2127 return 0;
2128 }
2129 }
2130 return 1;
2131}
2132
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002133static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002134{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002135 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002136 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002137 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002138 VkLayerProperties *instance_layers;
2139 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002140 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002141 uint32_t instance_layer_count = 0;
2142 uint32_t enabled_extension_count = 0;
2143 uint32_t enabled_layer_count = 0;
2144
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002145 char *instance_validation_layers[] = {
2146 "MemTracker",
2147 };
2148
2149 char *device_validation_layers[] = {
2150 "MemTracker",
2151 };
2152
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002153 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002154 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002155 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002156 assert(!err);
2157
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002158 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2159 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2160 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002161
2162 if (demo->validate) {
2163 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2164 instance_layer_count, instance_layers);
2165 if (!validation_found) {
2166 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2167 "required validation layer.\n\n"
2168 "Please look at the Getting Started guide for additional "
2169 "information.\n",
2170 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002171 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002172 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002173 }
2174
2175 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2176 assert(!err);
2177
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002178 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002179 memset(extension_names, 0, sizeof(extension_names));
2180 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2181 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2182 assert(!err);
2183 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002184 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002185 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002186 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002187 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002188 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2189 if (demo->validate) {
2190 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2191 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002192 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002193 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002194 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002195 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002196 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002197 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002198 "Vulkan installable client driver (ICD) installed?\nPlease "
2199 "look at the Getting Started guide for additional "
2200 "information.\n",
2201 "vkCreateInstance Failure");
2202 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002203 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002204 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002205 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002206 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002207 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002208 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002209 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002210 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002211 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002212 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002213 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002214 .pNext = NULL,
2215 .pAppInfo = &app,
2216 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002217 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002218 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002219 .extensionCount = enabled_extension_count,
2220 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002221 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002222 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002223 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002224 .queueCount = 1,
2225 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002226
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002227 uint32_t gpu_count;
2228 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002229 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002230
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002231 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002232 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2233 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002234 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002235 "additional information.\n",
2236 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002237 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2238 ERR_EXIT("Cannot find a specified extension library"
2239 ".\nMake sure your layers path is set appropriately\n",
2240 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002241 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002242 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2243 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002244 "the Getting Started guide for additional information.\n",
2245 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002246 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002247
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002248 free(instance_layers);
2249 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002250
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002251 gpu_count = 1;
2252 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002253 assert(!err && gpu_count == 1);
2254
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002255 /* Look for validation layers */
2256 validation_found = 0;
2257 enabled_layer_count = 0;
2258 uint32_t device_layer_count = 0;
2259 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2260 assert(!err);
2261
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002262 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2263 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2264 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002265
2266 if (demo->validate) {
2267 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2268 device_layer_count, device_layers);
2269 if (!validation_found) {
2270 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2271 "a required validation layer.\n\n"
2272 "Please look at the Getting Started guide for additional "
2273 "information.\n",
2274 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002275 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002276 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002277 }
2278
2279 uint32_t device_extension_count = 0;
2280 VkExtensionProperties *device_extensions = NULL;
2281 err = vkGetPhysicalDeviceExtensionProperties(
2282 demo->gpu, NULL, &device_extension_count, NULL);
2283 assert(!err);
2284
2285 WSIextFound = 0;
2286 enabled_extension_count = 0;
2287 memset(extension_names, 0, sizeof(extension_names));
2288 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2289 err = vkGetPhysicalDeviceExtensionProperties(
2290 demo->gpu, NULL, &device_extension_count, device_extensions);
2291 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002292
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002293 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002294 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002295 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002296 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002297 }
2298 assert(enabled_extension_count < 64);
2299 }
2300 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002301 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2302 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002303 "Vulkan installable client driver (ICD) installed?\nPlease "
2304 "look at the Getting Started guide for additional "
2305 "information.\n",
2306 "vkCreateInstance Failure");
2307 }
2308
2309 VkDeviceCreateInfo device = {
2310 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2311 .pNext = NULL,
2312 .queueRecordCount = 1,
2313 .pRequestedQueues = &queue,
2314 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002315 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002316 .extensionCount = enabled_extension_count,
2317 .ppEnabledExtensionNames = (const char *const*) extension_names,
2318 .flags = 0,
2319 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002320
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002321 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002322 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2323 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002324 if (!demo->dbgCreateMsgCallback) {
2325 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2326 "vkGetProcAddr Failure");
2327 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002328 if (!demo->dbgDestroyMsgCallback) {
2329 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2330 "vkGetProcAddr Failure");
2331 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002332 err = demo->dbgCreateMsgCallback(
2333 demo->inst,
2334 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2335 dbgFunc, NULL,
2336 &demo->msg_callback);
2337 switch (err) {
2338 case VK_SUCCESS:
2339 break;
2340 case VK_ERROR_INVALID_POINTER:
2341 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2342 "dbgCreateMsgCallback Failure");
2343 break;
2344 case VK_ERROR_OUT_OF_HOST_MEMORY:
2345 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2346 "dbgCreateMsgCallback Failure");
2347 break;
2348 default:
2349 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2350 "dbgCreateMsgCallback Failure");
2351 break;
2352 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002353 }
2354
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002355 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002356 assert(!err);
2357
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002358 free(device_layers);
2359
Ian Elliottaaae5352015-07-06 14:27:58 -06002360 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
2361 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceInfoWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002362 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2363 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2364 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2365 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002366 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002367 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002368
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002369 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002370 assert(!err);
2371
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002372 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002373 assert(!err);
2374
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002375 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2376 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002377 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002378 assert(queue_count >= 1);
2379
Ian Elliottaaae5352015-07-06 14:27:58 -06002380 // Construct the WSI surface description:
2381 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2382 demo->surface_description.pNext = NULL;
2383#ifdef _WIN32
2384 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2385 demo->surface_description.pPlatformHandle = demo->connection;
2386 demo->surface_description.pPlatformWindow = demo->window;
2387#else // _WIN32
2388 demo->platform_handle_xcb.connection = demo->connection;
2389 demo->platform_handle_xcb.root = demo->screen->root;
2390 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2391 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2392 demo->surface_description.pPlatformWindow = &demo->window;
2393#endif // _WIN32
2394
2395 // Iterate over each queue to learn whether it supports presenting to WSI:
2396 VkBool32* supportsPresent = (VkBool32 *)malloc(queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002397 for (i = 0; i < queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002398 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2399 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2400 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002401 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002402
2403 // Search for a graphics and a present queue in the array of queue
2404 // families, try to find one that supports both
2405 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2406 uint32_t presentQueueNodeIndex = UINT32_MAX;
2407 for (i = 0; i < queue_count; i++) {
2408 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2409 if (graphicsQueueNodeIndex == UINT32_MAX) {
2410 graphicsQueueNodeIndex = i;
2411 }
2412
2413 if (supportsPresent[i] == VK_TRUE) {
2414 graphicsQueueNodeIndex = i;
2415 presentQueueNodeIndex = i;
2416 break;
2417 }
2418 }
2419 }
2420 if (presentQueueNodeIndex == UINT32_MAX) {
2421 // If didn't find a queue that supports both graphics and present, then
2422 // find a separate present queue.
2423 for (size_t i = 0; i < queue_count; ++i) {
2424 if (supportsPresent[i] == VK_TRUE) {
2425 presentQueueNodeIndex = i;
2426 break;
2427 }
2428 }
2429 }
2430 free(supportsPresent);
2431
2432 // Generate error if could not find both a graphics and a present queue
2433 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2434 ERR_EXIT("Could not find a graphics and a present queue\n",
2435 "WSI Initialization Failure");
2436 }
2437
2438 // TODO: Add support for separate queues, including presentation,
2439 // synchronization, and appropriate tracking for QueueSubmit
2440 // While it is possible for an application to use a separate graphics and a
2441 // present queues, this demo program assumes it is only using one:
2442 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2443 ERR_EXIT("Could not find a common graphics and a present queue\n",
2444 "WSI Initialization Failure");
2445 }
2446
2447 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002448
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002449 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002450 0, &demo->queue);
2451 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002452
Ian Elliottaaae5352015-07-06 14:27:58 -06002453 // Get the list of VkFormat's that are supported:
2454 size_t formatsSize;
2455 err = demo->fpGetSurfaceInfoWSI(demo->device,
2456 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2457 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2458 &formatsSize, NULL);
2459 assert(!err);
2460 VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize);
2461 err = demo->fpGetSurfaceInfoWSI(demo->device,
2462 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2463 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2464 &formatsSize, surfFormats);
2465 assert(!err);
2466 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2467 // the surface has no preferred format. Otherwise, at least one
2468 // supported format will be returned.
2469 size_t formatCount = formatsSize / sizeof(VkSurfaceFormatPropertiesWSI);
2470 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2471 {
2472 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2473 }
2474 else
2475 {
2476 assert(formatCount >= 1);
2477 demo->format = surfFormats[0].format;
2478 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06002479
David Pinedo2bb7c932015-06-18 17:03:14 -06002480 demo->quit = false;
2481 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002482
2483 // Get Memory information and properties
2484 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2485 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002486}
2487
2488static void demo_init_connection(struct demo *demo)
2489{
Ian Elliott639ca472015-04-16 15:23:05 -06002490#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002491 const xcb_setup_t *setup;
2492 xcb_screen_iterator_t iter;
2493 int scr;
2494
2495 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002496 if (demo->connection == NULL) {
2497 printf("Cannot find a compatible Vulkan installable client driver "
2498 "(ICD).\nExiting ...\n");
2499 fflush(stdout);
2500 exit(1);
2501 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002502
2503 setup = xcb_get_setup(demo->connection);
2504 iter = xcb_setup_roots_iterator(setup);
2505 while (scr-- > 0)
2506 xcb_screen_next(&iter);
2507
2508 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002509#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002510}
2511
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002512static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002513{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002514 vec3 eye = {0.0f, 3.0f, 5.0f};
2515 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002516 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002517
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002518 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002519 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002520
Piers Daniell735ee532015-02-23 16:23:13 -07002521 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002522 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002523 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002524 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002525 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002526 if (strcmp(argv[i], "--use_glsl") == 0) {
2527 demo->use_glsl = true;
2528 continue;
2529 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002530 if (strcmp(argv[i], "--validate") == 0) {
2531 demo->validate = true;
2532 continue;
2533 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002534 if (strcmp(argv[i], "--c") == 0 &&
2535 demo->frameCount == INT_MAX &&
2536 i < argc-1 &&
2537 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2538 demo->frameCount >= 0)
2539 {
2540 i++;
2541 continue;
2542 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002543
David Pinedo2bb7c932015-06-18 17:03:14 -06002544 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002545 fflush(stderr);
2546 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002547 }
2548
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002549 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002550 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002551
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002552 demo->width = 500;
2553 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002554
2555 demo->spin_angle = 0.01f;
2556 demo->spin_increment = 0.01f;
2557 demo->pause = false;
2558
Piers Daniell735ee532015-02-23 16:23:13 -07002559 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002560 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2561 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002562}
2563
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002564
Ian Elliott639ca472015-04-16 15:23:05 -06002565#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002566extern int __getmainargs(
2567 int * _Argc,
2568 char *** _Argv,
2569 char *** _Env,
2570 int _DoWildCard,
2571 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002572
Ian Elliotta748eaf2015-04-28 15:50:36 -06002573int WINAPI WinMain(HINSTANCE hInstance,
2574 HINSTANCE hPrevInstance,
2575 LPSTR pCmdLine,
2576 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002577{
2578 MSG msg; // message
2579 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002580 int argc;
2581 char** argv;
2582 char** env;
2583 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002584
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002585 __getmainargs(&argc,&argv,&env,0,&new_mode);
2586
2587 demo_init(&demo, argc, argv);
2588 demo.connection = hInstance;
2589 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002590 demo_create_window(&demo);
2591
2592 demo_prepare(&demo);
2593
2594 done = false; //initialize loop condition variable
2595 /* main message loop*/
2596 while(!done)
2597 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002598 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002599 if (msg.message == WM_QUIT) //check for a quit message
2600 {
2601 done = true; //if found, quit app
2602 }
2603 else
2604 {
2605 /* Translate and dispatch to event queue*/
2606 TranslateMessage(&msg);
2607 DispatchMessage(&msg);
2608 }
2609 }
2610
2611 demo_cleanup(&demo);
2612
Tony Barbourf9921732015-04-22 11:36:22 -06002613 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002614}
2615#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002616int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002617{
2618 struct demo demo;
2619
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002620 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002621 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002622
2623 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002624 demo_run(&demo);
2625
2626 demo_cleanup(&demo);
2627
2628 return 0;
2629}
Ian Elliott639ca472015-04-16 15:23:05 -06002630#endif // _WIN32