blob: 9c50feb6926fc07b1e91edd1186a9baf77374e64 [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 Elliotta0781972015-08-21 15:09:33 -060040#include <vk_ext_khr_swapchain.h>
41#include <vk_ext_khr_device_swapchain.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060042#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060043
Cody Northropfe3d8bc2015-03-17 14:54:35 -060044#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045
Mark Lobodzinskif871f772015-09-01 09:00:16 -060046#include "vk_sdk_platform.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060048#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060049
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050#define DEMO_BUFFER_COUNT 2
51#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060052#define APP_SHORT_NAME "cube"
53#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060055#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
56
Tony Barbourfdc2d352015-04-22 09:02:32 -060057#if defined(NDEBUG) && defined(__GNUC__)
58#define U_ASSERT_ONLY __attribute__((unused))
59#else
60#define U_ASSERT_ONLY
61#endif
62
Ian Elliott07264132015-04-28 11:35:02 -060063#ifdef _WIN32
64#define ERR_EXIT(err_msg, err_class) \
65 do { \
66 MessageBox(NULL, err_msg, err_class, MB_OK); \
67 exit(1); \
68 } while (0)
69
Ian Elliott07264132015-04-28 11:35:02 -060070#else // _WIN32
71
72#define ERR_EXIT(err_msg, err_class) \
73 do { \
74 printf(err_msg); \
75 fflush(stdout); \
76 exit(1); \
77 } while (0)
78#endif // _WIN32
79
Ian Elliottaaae5352015-07-06 14:27:58 -060080#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
81{ \
82 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
83 if (demo->fp##entrypoint == NULL) { \
84 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
85 "vkGetInstanceProcAddr Failure"); \
86 } \
87}
88
Ian Elliott673898b2015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -060091 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060092 if (demo->fp##entrypoint == NULL) { \
93 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
94 "vkGetDeviceProcAddr Failure"); \
95 } \
96}
97
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060098/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070099 * structure to track all objects related to a texture.
100 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600101struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600102 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700103
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600104 VkImage image;
105 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600106
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500107 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600108 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700109 int32_t tex_width, tex_height;
110};
111
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600112static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600113 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600114};
115
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600116struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600117 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600118 float mvp[4][4];
119 float position[12*3][4];
120 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600121};
122
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600123struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600124 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600125 float mvp[4][4];
126 float position[12*3][4];
127 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600128};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600129
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600130//--------------------------------------------------------------------------------------
131// Mesh and VertexFormat Data
132//--------------------------------------------------------------------------------------
133struct Vertex
134{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600137};
138
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600139struct VertexPosTex
140{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600141 float posX, posY, posZ, posW; // Position data
142 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600143};
144
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600145#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600147
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600148static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800149 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150 -1.0f,-1.0f, 1.0f,
151 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800152 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600153 -1.0f, 1.0f,-1.0f,
154 -1.0f,-1.0f,-1.0f,
155
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 1.0f, 1.0f,-1.0f,
158 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800159 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600161 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600165 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 -1.0f, 1.0f, 1.0f,
172 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600175 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 1.0f, 1.0f,
179 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 1.0f,-1.0f,-1.0f,
182 1.0f, 1.0f,-1.0f,
183
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 1.0f,-1.0f, 1.0f,
189 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600190};
191
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600192static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 0.0f, 0.0f, // -X side
194 1.0f, 0.0f,
195 1.0f, 1.0f,
196 1.0f, 1.0f,
197 0.0f, 1.0f,
198 0.0f, 0.0f,
199
200 1.0f, 0.0f, // -Z side
201 0.0f, 1.0f,
202 0.0f, 0.0f,
203 1.0f, 0.0f,
204 1.0f, 1.0f,
205 0.0f, 1.0f,
206
207 1.0f, 1.0f, // -Y side
208 1.0f, 0.0f,
209 0.0f, 0.0f,
210 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600211 0.0f, 0.0f,
212 0.0f, 1.0f,
213
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800214 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600215 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600217 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600218 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600219 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800221 1.0f, 1.0f, // +X side
222 0.0f, 1.0f,
223 0.0f, 0.0f,
224 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600226 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 0.0f, 1.0f, // +Z side
229 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600230 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600231 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600234};
235
236void dumpMatrix(const char *note, mat4x4 MVP)
237{
238 int i;
239
240 printf("%s: \n", note);
241 for (i=0; i<4; i++) {
242 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
243 }
244 printf("\n");
245 fflush(stdout);
246}
247
248void dumpVec4(const char *note, vec4 vector)
249{
250 printf("%s: \n", note);
251 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
252 printf("\n");
253 fflush(stdout);
254}
255
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600256VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600257 VkFlags msgFlags,
258 VkDbgObjectType objType,
259 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600260 size_t location,
261 int32_t msgCode,
262 const char* pLayerPrefix,
263 const char* pMsg,
264 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600265{
266 char *message = (char *) malloc(strlen(pMsg)+100);
267
268 assert (message);
269
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600270 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
271 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
272 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600273 // We know that we're submitting queues without fences, ignore this warning
274 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600275 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600276 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600277 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600278 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600279 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600280 }
281
282#ifdef _WIN32
283 MessageBox(NULL, message, "Alert", MB_OK);
284#else
285 printf("%s\n",message);
286 fflush(stdout);
287#endif
288 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600289
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600290 /*
291 * false indicates that layer should not bail-out of an
292 * API call that had validation failures. This may mean that the
293 * app dies inside the driver due to invalid parameter(s).
294 * That's what would happen without validation layers, so we'll
295 * keep that behavior here.
296 */
297 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600298}
299
Ian Elliotta0781972015-08-21 15:09:33 -0600300typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600301 VkImage image;
302 VkCmdBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600303 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600304} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600305
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600306struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600307#ifdef _WIN32
308#define APP_NAME_STR_LEN 80
309 HINSTANCE connection; // hInstance - Windows Instance
310 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
311 HWND window; // hWnd - window handle
312#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600313 xcb_connection_t *connection;
314 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800315 xcb_window_t window;
316 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotta0781972015-08-21 15:09:33 -0600317 VkPlatformHandleXcbKHR platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600318#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600319 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700320 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600321 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600323 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600324 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600325 VkDevice device;
326 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700327 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600328 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600329 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600330 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600334 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600335 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336
Ian Elliotta0781972015-08-21 15:09:33 -0600337 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
338 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
339 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
340 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
341 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
342 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
343 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
344 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
345 PFN_vkQueuePresentKHR fpQueuePresentKHR;
346 VkSurfaceDescriptionWindowKHR surface_description;
347 uint32_t swapchainImageCount;
348 VkSwapchainKHR swap_chain;
349 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350
Ian Elliottaaae5352015-07-06 14:27:58 -0600351 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352
353 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600354 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500357 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600358 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600359 } depth;
360
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600361 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600362
363 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500365 VkDeviceMemory mem;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800366 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600367 } uniform_data;
368
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500370 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600371 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600372 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800373 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
Cody Northrop5e4125d2015-08-26 10:01:32 -0600376 VkDynamicViewportState dynamic_viewport;
377 VkDynamicLineWidthState dynamic_line_width;
378 VkDynamicDepthBiasState dynamic_depth_bias;
379 VkDynamicBlendState dynamic_blend;
380 VkDynamicDepthBoundsState dynamic_depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -0600381 VkDynamicStencilState dynamic_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600382
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600383 mat4x4 projection_matrix;
384 mat4x4 view_matrix;
385 mat4x4 model_matrix;
386
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600387 float spin_angle;
388 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600389 bool pause;
390
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600391 VkShaderModule vert_shader_module;
392 VkShaderModule frag_shader_module;
393
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 VkDescriptorPool desc_pool;
395 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800396
Chia-I Wuf973e322015-07-08 13:34:24 +0800397 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
398
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600399 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600400 int32_t curFrame;
401 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600402 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600403 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600404 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600405 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600406 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600407 VkDbgMsgCallback msg_callback;
408
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600409 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600410 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600411};
412
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600413static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
414{
415 // Search memtypes to find first index with those properties
416 for (uint32_t i = 0; i < 32; i++) {
417 if ((typeBits & 1) == 1) {
418 // Type is available, does it match user properties?
419 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
420 *typeIndex = i;
421 return VK_SUCCESS;
422 }
423 }
424 typeBits >>= 1;
425 }
426 // No memory types matched, return failure
427 return VK_UNSUPPORTED;
428}
429
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430static void demo_flush_init_cmd(struct demo *demo)
431{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600432 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600434 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435 return;
436
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 assert(!err);
439
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600440 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600441 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600442
Tony Barbour155cce82015-07-03 10:33:54 -0600443 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444 assert(!err);
445
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447 assert(!err);
448
Tony Barbour155cce82015-07-03 10:33:54 -0600449 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600450 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451}
452
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600453static void demo_set_image_layout(
454 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600455 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600456 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600457 VkImageLayout old_image_layout,
458 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600460 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600462 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600463 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600466 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800467 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468 .flags = 0,
469 };
470
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600471 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472 assert(!err);
473
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600474 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600475 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600477 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600479 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600480 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600481 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600482 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600484 }
485
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600486 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600487 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488 .pNext = NULL,
489 .outputMask = 0,
490 .inputMask = 0,
491 .oldLayout = old_image_layout,
492 .newLayout = new_image_layout,
493 .image = image,
Cody Northrop0e951672015-09-14 13:48:12 -0600494 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 };
496
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600499 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600500 }
501
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600504 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505 }
506
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600507 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600508
Tony Barbour50bbca42015-06-29 16:20:35 -0600509 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
510 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600512 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600513}
514
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600515static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600516{
Chia-I Wuf973e322015-07-08 13:34:24 +0800517 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600518 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600519 .pNext = NULL,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600520 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600521 .renderPass = { VK_NULL_HANDLE },
Cody Northropa0a178c2015-08-11 11:35:58 -0600522 .subpass = 0,
Courtney Goeltzenleuchterdc093ab2015-08-26 14:48:00 -0600523 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700524 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800525 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600526 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
527 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800528 };
529 const VkRenderPassBeginInfo rp_begin = {
530 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
531 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800532 .renderPass = demo->render_pass,
533 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800534 .renderArea.offset.x = 0,
535 .renderArea.offset.y = 0,
536 .renderArea.extent.width = demo->width,
537 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600538 .clearValueCount = 2,
539 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700540 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800541 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600543 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544 assert(!err);
545
Chia-I Wua74c5b22015-07-07 11:50:03 +0800546 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
547
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600548 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600550 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600551 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552
Cody Northrop5e4125d2015-08-26 10:01:32 -0600553 vkCmdBindDynamicViewportState(cmd_buf, demo->dynamic_viewport);
554 vkCmdBindDynamicLineWidthState(cmd_buf, demo->dynamic_line_width);
555 vkCmdBindDynamicDepthBiasState(cmd_buf, demo->dynamic_depth_bias);
556 vkCmdBindDynamicBlendState(cmd_buf, demo->dynamic_blend);
557 vkCmdBindDynamicDepthBoundsState(cmd_buf, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -0600558 vkCmdBindDynamicStencilState(cmd_buf, demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600560 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800561 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600562
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600563 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564 assert(!err);
565}
566
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600567
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600568void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600569{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600570 mat4x4 MVP, Model, VP;
571 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600572 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600573 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600574
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600575 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600576
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600577 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600578 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700579 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600580 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600581
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500582 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600583 assert(!err);
584
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600585 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600587 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600588}
589
590static void demo_draw(struct demo *demo)
591{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600592 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600593 VkSemaphore presentCompleteSemaphore;
594 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
595 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
596 .pNext = NULL,
597 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
598 };
Tony Barbour155cce82015-07-03 10:33:54 -0600599 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600600
Ian Elliottaaae5352015-07-06 14:27:58 -0600601 err = vkCreateSemaphore(demo->device,
602 &presentCompleteSemaphoreCreateInfo,
603 &presentCompleteSemaphore);
604 assert(!err);
605
606 // Get the index of the next available swapchain image:
Ian Elliotta0781972015-08-21 15:09:33 -0600607 err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600608 UINT64_MAX,
609 presentCompleteSemaphore,
610 &demo->current_buffer);
Ian Elliotta0781972015-08-21 15:09:33 -0600611 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600612 // return codes
613 assert(!err);
614
615 // Wait for the present complete semaphore to be signaled to ensure
616 // that the image won't be rendered to until the presentation
617 // engine has fully released ownership to the application, and it is
618 // okay to render to the image.
619 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
620
Ian Elliotta0781972015-08-21 15:09:33 -0600621// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600622 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600623 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624 assert(!err);
625
Ian Elliotta0781972015-08-21 15:09:33 -0600626 VkPresentInfoKHR present = {
627 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600628 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600629 .swapchainCount = 1,
630 .swapchains = &demo->swap_chain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600631 .imageIndices = &demo->current_buffer,
632 };
633
634// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600635 err = demo->fpQueuePresentKHR(demo->queue, &present);
636 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliottaaae5352015-07-06 14:27:58 -0600637 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638 assert(!err);
639
Chia-I Wucbb564e2015-04-16 22:02:10 +0800640 err = vkQueueWaitIdle(demo->queue);
641 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600642
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600643 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644}
645
646static void demo_prepare_buffers(struct demo *demo)
647{
Ian Elliottaaae5352015-07-06 14:27:58 -0600648 VkResult U_ASSERT_ONLY err;
649
650 // Check the surface properties and formats
Ian Elliotta0781972015-08-21 15:09:33 -0600651 VkSurfacePropertiesKHR surfProperties;
652 err = demo->fpGetSurfacePropertiesKHR(demo->device,
653 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600654 &surfProperties);
Ian Elliottaaae5352015-07-06 14:27:58 -0600655 assert(!err);
656
Ian Elliott8528eff2015-08-07 15:56:59 -0600657 uint32_t presentModeCount;
Ian Elliotta0781972015-08-21 15:09:33 -0600658 err = demo->fpGetSurfacePresentModesKHR(demo->device,
659 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600660 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600661 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600662 VkPresentModeKHR *presentModes =
663 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600664 assert(presentModes);
Ian Elliotta0781972015-08-21 15:09:33 -0600665 err = demo->fpGetSurfacePresentModesKHR(demo->device,
666 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -0600667 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600668 assert(!err);
669
Ian Elliotta0781972015-08-21 15:09:33 -0600670 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600671 // width and height are either both -1, or both not -1.
Ian Elliott8528eff2015-08-07 15:56:59 -0600672 if (surfProperties.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600673 {
674 // If the surface size is undefined, the size is set to
675 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600676 swapchainExtent.width = demo->width;
677 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600678 }
679 else
680 {
681 // If the surface size is defined, the swap chain size must match
Ian Elliotta0781972015-08-21 15:09:33 -0600682 swapchainExtent = surfProperties.currentExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600683 }
684
685 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600686 // tearing mode. If not, try IMMEDIATE which will usually be available,
687 // and is fastest (though it tears). If not, fall back to FIFO which is
688 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600689 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600690 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600691 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
692 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600693 break;
694 }
Ian Elliotta0781972015-08-21 15:09:33 -0600695 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
696 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
697 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600698 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600699 }
700
Ian Elliott2d47da92015-07-13 12:20:56 -0600701#define WORK_AROUND_CODE
702#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600703 // After the proper code was created, other parts of this demo were
704 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
705 // images, etc. Live with that for now.
706 // TODO: Rework this demo code to live with the number of buffers returned
Ian Elliotta0781972015-08-21 15:09:33 -0600707 // by vkCreateSwapchainKHR().
708 uint32_t desiredNumberOfSwapchainImages = DEMO_BUFFER_COUNT;
Ian Elliott2d47da92015-07-13 12:20:56 -0600709#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600710 // Determine the number of VkImage's to use in the swap chain (we desire to
711 // own only 1 image at a time, besides the images being displayed and
712 // queued for display):
Ian Elliotta0781972015-08-21 15:09:33 -0600713 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott8528eff2015-08-07 15:56:59 -0600714 if ((surfProperties.maxImageCount > 0) &&
Ian Elliotta0781972015-08-21 15:09:33 -0600715 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600716 {
717 // Application must settle for fewer images than desired:
Ian Elliotta0781972015-08-21 15:09:33 -0600718 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600719 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600720#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600721
Tony Barbour9fd6d352015-09-16 15:01:32 -0600722 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta0781972015-08-21 15:09:33 -0600723 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
724 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600725 } else {
Ian Elliott8528eff2015-08-07 15:56:59 -0600726 preTransform = surfProperties.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600727 }
728
Ian Elliotta0781972015-08-21 15:09:33 -0600729 const VkSwapchainCreateInfoKHR swap_chain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600730 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800731 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600732 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
733 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800734 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600735 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800736 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600737 .width = swapchainExtent.width,
738 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600739 },
Cody Northrop11b48d02015-08-28 16:22:48 -0600740 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600741 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800742 .imageArraySize = 1,
Ian Elliott8528eff2015-08-07 15:56:59 -0600743 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
744 .queueFamilyCount = 0,
745 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600746 .presentMode = swapchainPresentMode,
747 .oldSwapchain.handle = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600748 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600749 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600750 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600751
Ian Elliotta0781972015-08-21 15:09:33 -0600752 err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800753 assert(!err);
754
Ian Elliotta0781972015-08-21 15:09:33 -0600755 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
756 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600757 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800758
Ian Elliotta0781972015-08-21 15:09:33 -0600759 VkImage* swapchainImages =
760 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
761 assert(swapchainImages);
762 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
763 &demo->swapchainImageCount,
764 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600765 assert(!err);
Ian Elliott2d47da92015-07-13 12:20:56 -0600766#ifdef WORK_AROUND_CODE
Ian Elliott8528eff2015-08-07 15:56:59 -0600767 // After the proper code was created, other parts of this demo were
768 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
769 // images, etc. Live with that for now.
770 // TODO: Rework this demo code to live with the number of buffers returned
Ian Elliotta0781972015-08-21 15:09:33 -0600771 // by vkCreateSwapchainKHR().
772 demo->swapchainImageCount = DEMO_BUFFER_COUNT;
Ian Elliott2d47da92015-07-13 12:20:56 -0600773#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600774
Ian Elliotta0781972015-08-21 15:09:33 -0600775 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600776 assert(demo->buffers);
777
Ian Elliotta0781972015-08-21 15:09:33 -0600778 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600779 VkImageViewCreateInfo color_image_view = {
780 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781 .pNext = NULL,
782 .format = demo->format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600783 .channels = {
784 .r = VK_CHANNEL_SWIZZLE_R,
785 .g = VK_CHANNEL_SWIZZLE_G,
786 .b = VK_CHANNEL_SWIZZLE_B,
787 .a = VK_CHANNEL_SWIZZLE_A,
788 },
789 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600790 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600791 .baseMipLevel = 0,
792 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600793 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600794 .arraySize = 1
795 },
796 .viewType = VK_IMAGE_VIEW_TYPE_2D,
797 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600798 };
799
Ian Elliotta0781972015-08-21 15:09:33 -0600800 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600802 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400803 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600804 VK_IMAGE_LAYOUT_UNDEFINED,
805 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600806
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600807 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600808
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600809 err = vkCreateImageView(demo->device,
810 &color_image_view, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600811 assert(!err);
812 }
813}
814
815static void demo_prepare_depth(struct demo *demo)
816{
Tony Barbour72304ef2015-04-16 15:59:00 -0600817 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600818 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600819 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600820 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600821 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600822 .format = depth_format,
823 .extent = { demo->width, demo->height, 1 },
824 .mipLevels = 1,
825 .arraySize = 1,
826 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600827 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600828 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600829 .flags = 0,
830 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600831 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600832 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500833 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600834 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600835 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836 };
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600837 VkImageViewCreateInfo view = {
838 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600839 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600840 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600841 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600842 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600843 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600844 .baseMipLevel = 0,
845 .mipLevels = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600846 .baseArrayLayer = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600847 .arraySize = 1
848 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600849 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600850 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600852
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500853 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600854 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600855
856 demo->depth.format = depth_format;
857
858 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600859 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600860 &demo->depth.image);
861 assert(!err);
862
Tony Barbour155cce82015-07-03 10:33:54 -0600863 err = vkGetImageMemoryRequirements(demo->device,
864 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600865
866 mem_alloc.allocationSize = mem_reqs.size;
867 err = memory_type_from_properties(demo,
868 mem_reqs.memoryTypeBits,
869 VK_MEMORY_PROPERTY_DEVICE_ONLY,
870 &mem_alloc.memoryTypeIndex);
871 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600872
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500873 /* allocate memory */
874 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
875 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600876
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500877 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600878 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500879 demo->depth.mem, 0);
880 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600882 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600883 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600884 VK_IMAGE_LAYOUT_UNDEFINED,
885 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600886
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600887 /* create image view */
888 view.image = demo->depth.image;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600889 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600890 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600891}
892
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600893/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600894 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600895 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600896 * \param demo : Needed to access VK calls
897 * \param filename : the png file to be loaded
898 * \param width : width of png, to be updated as a side effect of this function
899 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600900 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600901 * \return bool : an opengl texture id. true if successful?,
902 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600903 *
904 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
905 * Modified to copy image to memory
906 *
907 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700908bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600909 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600910 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600911{
912 //header for testing if it is a png
913 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600914 int is_png, bit_depth, color_type, rowbytes;
915 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700916 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600917 png_structp png_ptr;
918 png_infop info_ptr, end_info;
919 png_byte *image_data;
920 png_bytep *row_pointers;
921
922 //open file as binary
923 FILE *fp = fopen(filename, "rb");
924 if (!fp) {
925 return false;
926 }
927
928 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600929 retval = fread(header, 1, 8, fp);
930 if (retval != 8) {
931 fclose(fp);
932 return false;
933 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600934
935 //test if png
936 is_png = !png_sig_cmp(header, 0, 8);
937 if (!is_png) {
938 fclose(fp);
939 return false;
940 }
941
942 //create png struct
943 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
944 NULL, NULL);
945 if (!png_ptr) {
946 fclose(fp);
947 return (false);
948 }
949
950 //create png info struct
951 info_ptr = png_create_info_struct(png_ptr);
952 if (!info_ptr) {
953 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
954 fclose(fp);
955 return (false);
956 }
957
958 //create png info struct
959 end_info = png_create_info_struct(png_ptr);
960 if (!end_info) {
961 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
962 fclose(fp);
963 return (false);
964 }
965
966 //png error stuff, not sure libpng man suggests this.
967 if (setjmp(png_jmpbuf(png_ptr))) {
968 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
969 fclose(fp);
970 return (false);
971 }
972
973 //init png reading
974 png_init_io(png_ptr, fp);
975
976 //let libpng know you already read the first 8 bytes
977 png_set_sig_bytes(png_ptr, 8);
978
979 // read all the info up to the image data
980 png_read_info(png_ptr, info_ptr);
981
982 // get info about png
983 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
984 NULL, NULL, NULL);
985
986 //update width and height based on png info
987 *width = twidth;
988 *height = theight;
989
990 // Require that incoming texture be 8bits per color component
991 // and 4 components (RGBA).
992 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
993 png_get_channels(png_ptr, info_ptr) != 4) {
994 return false;
995 }
996
997 if (rgba_data == NULL) {
998 // If data pointer is null, we just want the width & height
999 // clean up memory and close stuff
1000 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1001 fclose(fp);
1002
1003 return true;
1004 }
1005
1006 // Update the png info struct.
1007 png_read_update_info(png_ptr, info_ptr);
1008
1009 // Row size in bytes.
1010 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
1011
1012 // Allocate the image_data as a big block, to be given to opengl
1013 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
1014 if (!image_data) {
1015 //clean up memory and close stuff
1016 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1017 fclose(fp);
1018 return false;
1019 }
1020
1021 // row_pointers is for pointing to image_data for reading the png with libpng
1022 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
1023 if (!row_pointers) {
1024 //clean up memory and close stuff
1025 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1026 // delete[] image_data;
1027 fclose(fp);
1028 return false;
1029 }
1030 // set the individual row_pointers to point at the correct offsets of image_data
1031 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001033
1034 // read the png into image_data through row_pointers
1035 png_read_image(png_ptr, row_pointers);
1036
1037 // clean up memory and close stuff
1038 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1039 free(row_pointers);
1040 free(image_data);
1041 fclose(fp);
1042
1043 return true;
1044}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001045
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046static void demo_prepare_texture_image(struct demo *demo,
1047 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001048 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001049 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001050 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001051 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001053 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001054 int32_t tex_width;
1055 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001056 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057
David Pinedo30bd71d2015-04-23 08:16:57 -06001058 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1059 {
1060 printf("Failed to load textures\n");
1061 fflush(stdout);
1062 exit(1);
1063 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001065 tex_obj->tex_width = tex_width;
1066 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001068 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001069 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001070 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001071 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072 .format = tex_format,
1073 .extent = { tex_width, tex_height, 1 },
1074 .mipLevels = 1,
1075 .arraySize = 1,
1076 .samples = 1,
1077 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001078 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079 .flags = 0,
1080 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001081 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001082 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001083 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001084 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001085 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086 };
1087
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001088 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001089
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001091 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001092 assert(!err);
1093
Tony Barbour155cce82015-07-03 10:33:54 -06001094 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001095 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001096
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001097 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001098
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001099 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1100 assert(!err);
1101
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 /* allocate memory */
1103 err = vkAllocMemory(demo->device, &mem_alloc,
1104 &(tex_obj->mem));
1105 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001106
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001107 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001108 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001109 tex_obj->mem, 0);
1110 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111
Tony Barbour72304ef2015-04-16 15:59:00 -06001112 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001113 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001114 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001116 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001118 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 void *data;
1120
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001121 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1122 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 assert(!err);
1126
1127 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1128 fprintf(stderr, "Error loading texture: %s\n", filename);
1129 }
1130
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001131 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001133
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001134 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001135 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001136 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001137 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001138 tex_obj->imageLayout);
1139 /* 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 -07001140}
1141
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001142static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143{
1144 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001145 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001146 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001147}
1148
1149static void demo_prepare_textures(struct demo *demo)
1150{
Tony Barbour72304ef2015-04-16 15:59:00 -06001151 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001152 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001153 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001154 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001155
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001156 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001157 assert(!err);
1158
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001159 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
FslNopper6a7e50e2015-05-06 21:42:01 +02001161 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162 /* Device can texture using linear textures */
1163 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001164 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001165 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001167 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168
1169 memset(&staging_texture, 0, sizeof(staging_texture));
1170 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001171 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172
1173 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001174 VK_IMAGE_TILING_OPTIMAL,
1175 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1176 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001177
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001178 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001179 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001180 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001181 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001183 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001184 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001185 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001187
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001188 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001189 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001192 .destOffset = { 0, 0, 0 },
1193 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1194 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001195 vkCmdCopyImage(demo->cmd,
1196 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1197 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001198 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001200 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001201 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001202 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001203 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001204
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001205 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001206
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001207 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001209 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1210 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001211 }
1212
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001213 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001214 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001215 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001216 .magFilter = VK_TEX_FILTER_NEAREST,
1217 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001218 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter781da8a2015-09-10 14:08:50 -06001219 .addressModeU = VK_TEX_ADDRESS_MODE_CLAMP,
1220 .addressModeV = VK_TEX_ADDRESS_MODE_CLAMP,
1221 .addressModeW = VK_TEX_ADDRESS_MODE_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001222 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001223 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001224 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 .minLod = 0.0f,
1226 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001227 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001228 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001230
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001231 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001232 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001233 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001234 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001235 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001236 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001237 .channels = { VK_CHANNEL_SWIZZLE_R,
1238 VK_CHANNEL_SWIZZLE_G,
1239 VK_CHANNEL_SWIZZLE_B,
1240 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001241 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001242 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001243 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001244
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001245 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001246 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 &demo->textures[i].sampler);
1248 assert(!err);
1249
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001250 /* create image view */
1251 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001252 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001253 &demo->textures[i].view);
1254 assert(!err);
1255 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001256}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001257
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001258void demo_prepare_cube_data_buffer(struct demo *demo)
1259{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001260 VkBufferCreateInfo buf_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001261 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001262 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001263 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001264 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001265 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001266 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001267 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001268 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001269 int i;
1270 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001271 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001272 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001273
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001274 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001275 mat4x4_mul(MVP, VP, demo->model_matrix);
1276 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001277// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001278
1279 for (i=0; i<12*3; i++) {
1280 data.position[i][0] = g_vertex_buffer_data[i*3];
1281 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1282 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1283 data.position[i][3] = 1.0f;
1284 data.attr[i][0] = g_uv_buffer_data[2*i];
1285 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1286 data.attr[i][2] = 0;
1287 data.attr[i][3] = 0;
1288 }
1289
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001290 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001291 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001292 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001293 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001294 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001295 assert(!err);
1296
Tony Barbour155cce82015-07-03 10:33:54 -06001297 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001298 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001299
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001300 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001301 err = memory_type_from_properties(demo,
1302 mem_reqs.memoryTypeBits,
1303 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1304 &alloc_info.memoryTypeIndex);
1305 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001306
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001307 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1308 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001309
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001310 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1311 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001312
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001313 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001314
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001315 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001316
Tony Barbour155cce82015-07-03 10:33:54 -06001317 err = vkBindBufferMemory(demo->device,
1318 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001319 demo->uniform_data.mem, 0);
1320 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001321
Courtney Goeltzenleuchter94415342015-09-14 14:14:21 -06001322 demo->uniform_data.desc.bufferInfo.buffer = demo->uniform_data.buf;
1323 demo->uniform_data.desc.bufferInfo.offset = 0;
1324 demo->uniform_data.desc.bufferInfo.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001325}
1326
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001327static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001329 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001330 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001331 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001332 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001333 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001334 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001335 },
1336 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001337 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001338 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001339 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001340 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001341 },
1342 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001343 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001344 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001345 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001346 .count = 2,
1347 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001348 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001349 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001351 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001352 &descriptor_layout, &demo->desc_layout);
1353 assert(!err);
1354
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001355 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1356 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1357 .pNext = NULL,
1358 .descriptorSetCount = 1,
1359 .pSetLayouts = &demo->desc_layout,
1360 };
1361
1362 err = vkCreatePipelineLayout(demo->device,
1363 &pPipelineLayoutCreateInfo,
1364 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001366}
1367
Chia-I Wuf973e322015-07-08 13:34:24 +08001368static void demo_prepare_render_pass(struct demo *demo)
1369{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001370 const VkAttachmentDescription attachments[2] = {
1371 [0] = {
1372 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1373 .pNext = NULL,
1374 .format = demo->format,
1375 .samples = 1,
1376 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1377 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1378 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1379 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1380 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1381 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1382 },
1383 [1] = {
1384 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1385 .pNext = NULL,
1386 .format = demo->depth.format,
1387 .samples = 1,
1388 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1389 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1390 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1391 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1392 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1393 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1394 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001395 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001396 const VkAttachmentReference color_reference = {
1397 .attachment = 0,
1398 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1399 };
1400 const VkSubpassDescription subpass = {
1401 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1402 .pNext = NULL,
1403 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1404 .flags = 0,
1405 .inputCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001406 .pInputAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001407 .colorCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001408 .pColorAttachments = &color_reference,
1409 .pResolveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001410 .depthStencilAttachment = {
1411 .attachment = 1,
1412 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1413 },
1414 .preserveCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001415 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001416 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001417 const VkRenderPassCreateInfo rp_info = {
1418 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1419 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001420 .attachmentCount = 2,
1421 .pAttachments = attachments,
1422 .subpassCount = 1,
1423 .pSubpasses = &subpass,
1424 .dependencyCount = 0,
1425 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001426 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001427 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001428
1429 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1430 assert(!err);
1431}
1432
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001433static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001434 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001435 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001436 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001437 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001439 VkShaderModuleCreateInfo moduleCreateInfo;
1440 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001441 VkShader shader;
1442 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001444
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001445 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1446 moduleCreateInfo.pNext = NULL;
1447
1448 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1449 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001450 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001451
Cody Northrop1fedb212015-05-28 11:27:16 -06001452 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001453 moduleCreateInfo.codeSize = size;
1454 moduleCreateInfo.pCode = code;
1455 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001456 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001457 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001458 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001459 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001460
1461 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001462 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001463 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001464 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001465 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001466 } else {
1467 // Create fake SPV structure to feed GLSL
1468 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001469 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1470 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1471 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001472
1473 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001474 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1475 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1476 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1477 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001478
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001479 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001480 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001481 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001482 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001483
1484 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001485 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001486 shaderCreateInfo.pName = "main";
Cody Northropa9e21942015-08-24 15:11:10 -06001487 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001488 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001489 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490 return shader;
1491}
1492
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001493char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001494{
1495 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001496 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001497 void *shader_code;
1498
1499 FILE *fp = fopen(filename, "rb");
1500 if (!fp) return NULL;
1501
1502 fseek(fp, 0L, SEEK_END);
1503 size = ftell(fp);
1504
1505 fseek(fp, 0L, SEEK_SET);
1506
1507 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001508 retval = fread(shader_code, size, 1, fp);
1509 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001510
1511 *psize = size;
1512
1513 return shader_code;
1514}
1515
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001516static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001517{
Cody Northrop1fedb212015-05-28 11:27:16 -06001518 if (!demo->use_glsl) {
1519 void *vertShaderCode;
1520 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001521
Cody Northrop1fedb212015-05-28 11:27:16 -06001522 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001523
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001524 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001525 vertShaderCode, size);
1526 } else {
1527 static const char *vertShaderText =
1528 "#version 140\n"
1529 "#extension GL_ARB_separate_shader_objects : enable\n"
1530 "#extension GL_ARB_shading_language_420pack : enable\n"
1531 "\n"
1532 "layout(binding = 0) uniform buf {\n"
1533 " mat4 MVP;\n"
1534 " vec4 position[12*3];\n"
1535 " vec4 attr[12*3];\n"
1536 "} ubuf;\n"
1537 "\n"
1538 "layout (location = 0) out vec4 texcoord;\n"
1539 "\n"
1540 "void main() \n"
1541 "{\n"
1542 " texcoord = ubuf.attr[gl_VertexID];\n"
1543 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1544 "\n"
1545 " // GL->VK conventions\n"
1546 " gl_Position.y = -gl_Position.y;\n"
1547 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1548 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001549
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001550 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001551 (const void *) vertShaderText,
1552 strlen(vertShaderText));
1553 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001554}
1555
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001556static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001557{
Cody Northrop1fedb212015-05-28 11:27:16 -06001558 if (!demo->use_glsl) {
1559 void *fragShaderCode;
1560 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001561
Cody Northrop1fedb212015-05-28 11:27:16 -06001562 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001563
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001564 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001565 fragShaderCode, size);
1566 } else {
1567 static const char *fragShaderText =
1568 "#version 140\n"
1569 "#extension GL_ARB_separate_shader_objects : enable\n"
1570 "#extension GL_ARB_shading_language_420pack : enable\n"
1571 "layout (binding = 1) uniform sampler2D tex;\n"
1572 "\n"
1573 "layout (location = 0) in vec4 texcoord;\n"
1574 "layout (location = 0) out vec4 uFragColor;\n"
1575 "void main() {\n"
1576 " uFragColor = texture(tex, texcoord.xy);\n"
1577 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001578
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001579 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001580 (const void *) fragShaderText,
1581 strlen(fragShaderText));
1582 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583}
1584
1585static void demo_prepare_pipeline(struct demo *demo)
1586{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001587 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001588 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001589 VkPipelineInputAssemblyStateCreateInfo ia;
1590 VkPipelineRasterStateCreateInfo rs;
1591 VkPipelineColorBlendStateCreateInfo cb;
1592 VkPipelineDepthStencilStateCreateInfo ds;
1593 VkPipelineViewportStateCreateInfo vp;
1594 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001595 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001596
1597 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001598 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001599 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001600
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001602 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001603 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001604
1605 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001606 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001607 rs.fillMode = VK_FILL_MODE_SOLID;
1608 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001609 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001610 rs.depthClipEnable = VK_TRUE;
Cody Northrop709c1742015-08-17 11:10:49 -06001611 rs.rasterizerDiscardEnable = VK_FALSE;
1612 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001613
1614 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001615 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1616 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001617 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001618 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001619 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001620 cb.attachmentCount = 1;
1621 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001622
Tony Barbour29645d02015-01-16 14:27:35 -07001623 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001624 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001625 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001626
1627 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001628 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001629 ds.depthTestEnable = VK_TRUE;
1630 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001631 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001632 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001633 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1634 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001635 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001636 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001637 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638
Tony Barbour29645d02015-01-16 14:27:35 -07001639 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001640 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001641 ms.pSampleMask = NULL;
Tony Barbour3ca22502015-06-26 10:18:34 -06001642 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001643
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001644 // Two stages: vs and fs
1645 pipeline.stageCount = 2;
1646 VkPipelineShaderStageCreateInfo shaderStages[2];
1647 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1648
1649 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1650 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1651 shaderStages[0].shader = demo_prepare_vs(demo);
1652
1653 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1654 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1655 shaderStages[1].shader = demo_prepare_fs(demo);
1656
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001657 memset(&pipelineCache, 0, sizeof(pipelineCache));
1658 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001659
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001660 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1661 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001662
1663 pipeline.pVertexInputState = NULL;
1664 pipeline.pInputAssemblyState = &ia;
1665 pipeline.pRasterState = &rs;
1666 pipeline.pColorBlendState = &cb;
1667 pipeline.pMultisampleState = &ms;
1668 pipeline.pViewportState = &vp;
1669 pipeline.pDepthStencilState = &ds;
1670 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001671 pipeline.renderPass = demo->render_pass;
Tony Barbour194bf282015-07-10 15:29:03 -06001672
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001673 pipeline.renderPass = demo->render_pass;
1674
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001675 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676 assert(!err);
1677
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001678 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001679 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001680 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001681 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1682 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001683}
1684
1685static void demo_prepare_dynamic_states(struct demo *demo)
1686{
Tony Barbour155cce82015-07-03 10:33:54 -06001687 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001688 VkDynamicLineWidthStateCreateInfo line_width;
1689 VkDynamicDepthBiasStateCreateInfo depth_bias;
1690 VkDynamicBlendStateCreateInfo blend;
1691 VkDynamicDepthBoundsStateCreateInfo depth_bounds;
Cody Northrop36a642f2015-08-18 15:21:16 -06001692 VkDynamicStencilStateCreateInfo stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001693 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694
Tony Barbour29645d02015-01-16 14:27:35 -07001695 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001696 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001697 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001698 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001699 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001700 viewport.height = (float) demo->height;
1701 viewport.width = (float) demo->width;
1702 viewport.minDepth = (float) 0.0f;
1703 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001704 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001705 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001706 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001707 scissor.extent.width = demo->width;
1708 scissor.extent.height = demo->height;
1709 scissor.offset.x = 0;
1710 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001711 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001712
Cody Northrop5e4125d2015-08-26 10:01:32 -06001713 memset(&line_width, 0, sizeof(line_width));
1714 line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
1715 line_width.lineWidth = 1.0;
Cody Northrop709c1742015-08-17 11:10:49 -06001716
Cody Northrop5e4125d2015-08-26 10:01:32 -06001717 memset(&depth_bias, 0, sizeof(depth_bias));
1718 depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
1719 depth_bias.depthBias = 0.0f;
1720 depth_bias.depthBiasClamp = 0.0f;
1721 depth_bias.slopeScaledDepthBias = 0.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001722
Cody Northrop5e4125d2015-08-26 10:01:32 -06001723 memset(&blend, 0, sizeof(blend));
1724 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
1725 blend.blendConst[0] = 1.0f;
1726 blend.blendConst[1] = 1.0f;
1727 blend.blendConst[2] = 1.0f;
1728 blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729
Cody Northrop5e4125d2015-08-26 10:01:32 -06001730 memset(&depth_bounds, 0, sizeof(depth_bounds));
1731 depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
1732 depth_bounds.minDepthBounds = 0.0f;
1733 depth_bounds.maxDepthBounds = 1.0f;
Cody Northrop36a642f2015-08-18 15:21:16 -06001734
1735 memset(&stencil, 0, sizeof(stencil));
1736 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
1737 stencil.stencilReference = 0;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001738 stencil.stencilCompareMask = 0xff;
Cody Northrop36a642f2015-08-18 15:21:16 -06001739 stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740
Cody Northrop5e4125d2015-08-26 10:01:32 -06001741 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 assert(!err);
1743
Cody Northrop5e4125d2015-08-26 10:01:32 -06001744 err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width);
Cody Northrop709c1742015-08-17 11:10:49 -06001745 assert(!err);
1746
Cody Northrop5e4125d2015-08-26 10:01:32 -06001747 err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748 assert(!err);
1749
Cody Northrop5e4125d2015-08-26 10:01:32 -06001750 err = vkCreateDynamicBlendState(demo->device,
1751 &blend, &demo->dynamic_blend);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752 assert(!err);
1753
Cody Northrop5e4125d2015-08-26 10:01:32 -06001754 err = vkCreateDynamicDepthBoundsState(demo->device,
1755 &depth_bounds, &demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001756 assert(!err);
1757
1758 err = vkCreateDynamicStencilState(demo->device,
1759 &stencil, &stencil, &demo->dynamic_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001760 assert(!err);
1761}
1762
Chia-I Wu63ea9262015-03-26 13:14:16 +08001763static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001764{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001765 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001766 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001767 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001768 .count = 1,
1769 },
1770 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001771 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001772 .count = DEMO_TEXTURE_COUNT,
1773 },
1774 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001775 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001776 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001777 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001778 .poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT,
1779 .maxSets = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001780 .count = 2,
1781 .pTypeCount = type_counts,
1782 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001783 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001784
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001785 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001786 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001787 assert(!err);
1788}
1789
1790static void demo_prepare_descriptor_set(struct demo *demo)
1791{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001792 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1793 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001794 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001795 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001796
Mike Stroyanebae8322015-04-17 12:36:38 -06001797 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001798 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001799 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001800 &demo->desc_set);
1801 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001802
Chia-I Wuae721ba2015-05-25 16:27:55 +08001803 memset(&tex_descs, 0, sizeof(tex_descs));
1804 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1805 tex_descs[i].sampler = demo->textures[i].sampler;
1806 tex_descs[i].imageView = demo->textures[i].view;
1807 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1808 }
1809
1810 memset(&writes, 0, sizeof(writes));
1811
1812 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1813 writes[0].destSet = demo->desc_set;
1814 writes[0].count = 1;
1815 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1816 writes[0].pDescriptors = &demo->uniform_data.desc;
1817
1818 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1819 writes[1].destSet = demo->desc_set;
1820 writes[1].destBinding = 1;
1821 writes[1].count = DEMO_TEXTURE_COUNT;
1822 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1823 writes[1].pDescriptors = tex_descs;
1824
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001825 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001826}
1827
Chia-I Wuf973e322015-07-08 13:34:24 +08001828static void demo_prepare_framebuffers(struct demo *demo)
1829{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001830 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001831 attachments[1] = demo->depth.view;
1832
Chia-I Wuf973e322015-07-08 13:34:24 +08001833 const VkFramebufferCreateInfo fb_info = {
1834 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1835 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001836 .renderPass = demo->render_pass,
1837 .attachmentCount = 2,
1838 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001839 .width = demo->width,
1840 .height = demo->height,
1841 .layers = 1,
1842 };
1843 VkResult U_ASSERT_ONLY err;
1844 uint32_t i;
1845
1846 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001847 attachments[0] = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001848 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1849 assert(!err);
1850 }
1851}
1852
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853static void demo_prepare(struct demo *demo)
1854{
Cody Northrop91e221b2015-07-09 18:08:32 -06001855 VkResult U_ASSERT_ONLY err;
1856
1857 const VkCmdPoolCreateInfo cmd_pool_info = {
1858 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1859 .pNext = NULL,
1860 .queueFamilyIndex = demo->graphics_queue_node_index,
1861 .flags = 0,
1862 };
1863 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1864 assert(!err);
1865
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001866 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001867 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001868 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001869 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001870 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001871 .flags = 0,
1872 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001873
1874 demo_prepare_buffers(demo);
1875 demo_prepare_depth(demo);
1876 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001877 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001878
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001879 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001880 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001881 demo_prepare_pipeline(demo);
1882 demo_prepare_dynamic_states(demo);
1883
Ian Elliotta0781972015-08-21 15:09:33 -06001884 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001885 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001886 assert(!err);
1887 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001888
Chia-I Wu63ea9262015-03-26 13:14:16 +08001889 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001890 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001891
Chia-I Wuf973e322015-07-08 13:34:24 +08001892 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001893
Ian Elliotta0781972015-08-21 15:09:33 -06001894 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001895 demo->current_buffer = i;
1896 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1897 }
1898
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001899 /*
1900 * Prepare functions above may generate pipeline commands
1901 * that need to be flushed before beginning the render loop.
1902 */
1903 demo_flush_init_cmd(demo);
1904
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001905 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001906 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001907}
1908
David Pinedo2bb7c932015-06-18 17:03:14 -06001909static void demo_cleanup(struct demo *demo)
1910{
1911 uint32_t i;
1912
1913 demo->prepared = false;
1914
Tony Barbour155cce82015-07-03 10:33:54 -06001915 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1916 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1917 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001918 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001919 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001920
Cody Northrop5e4125d2015-08-26 10:01:32 -06001921 vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport);
1922 vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width);
1923 vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias);
1924 vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend);
1925 vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds);
Cody Northrop36a642f2015-08-18 15:21:16 -06001926 vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001927
Tony Barbour155cce82015-07-03 10:33:54 -06001928 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001929 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001930 vkDestroyRenderPass(demo->device, demo->render_pass);
1931 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1932 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001933
1934 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001935 vkDestroyImageView(demo->device, demo->textures[i].view);
1936 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001937 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001938 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001939 }
Ian Elliotta0781972015-08-21 15:09:33 -06001940 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001941
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001942 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbour155cce82015-07-03 10:33:54 -06001943 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001944 vkFreeMemory(demo->device, demo->depth.mem);
1945
Tony Barbour155cce82015-07-03 10:33:54 -06001946 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001947 vkFreeMemory(demo->device, demo->uniform_data.mem);
1948
Ian Elliotta0781972015-08-21 15:09:33 -06001949 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001950 vkDestroyImageView(demo->device, demo->buffers[i].view);
Tony Barbour155cce82015-07-03 10:33:54 -06001951 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001952 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001953 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001954
Cody Northrop91e221b2015-07-09 18:08:32 -06001955 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001956 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001957 if (demo->validate) {
1958 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1959 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001960 vkDestroyInstance(demo->inst);
1961
1962#ifndef _WIN32
1963 xcb_destroy_window(demo->connection, demo->window);
1964 xcb_disconnect(demo->connection);
1965#endif // _WIN32
1966}
1967
1968// On MS-Windows, make this a global, so it's available to WndProc()
1969struct demo demo;
1970
Ian Elliott639ca472015-04-16 15:23:05 -06001971#ifdef _WIN32
1972static void demo_run(struct demo *demo)
1973{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001974 if (!demo->prepared)
1975 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001976 // Wait for work to finish before updating MVP.
1977 vkDeviceWaitIdle(demo->device);
1978 demo_update_data_buffer(demo);
1979
1980 demo_draw(demo);
1981
1982 // Wait for work to finish before updating MVP.
1983 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001984
David Pinedo2bb7c932015-06-18 17:03:14 -06001985 demo->curFrame++;
1986
1987 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1988 {
1989 demo->quit=true;
1990 demo_cleanup(demo);
1991 ExitProcess(0);
1992 }
1993
1994}
Ian Elliott639ca472015-04-16 15:23:05 -06001995
1996// MS-Windows event handling function:
1997LRESULT CALLBACK WndProc(HWND hWnd,
1998 UINT uMsg,
1999 WPARAM wParam,
2000 LPARAM lParam)
2001{
Ian Elliott639ca472015-04-16 15:23:05 -06002002 switch(uMsg)
2003 {
Ian Elliottaaae5352015-07-06 14:27:58 -06002004 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06002005 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002006 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002007 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06002008 demo_run(&demo);
2009 return 0;
2010 default:
2011 break;
2012 }
2013 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2014}
2015
2016static void demo_create_window(struct demo *demo)
2017{
2018 WNDCLASSEX win_class;
2019
2020 // Initialize the window class structure:
2021 win_class.cbSize = sizeof(WNDCLASSEX);
2022 win_class.style = CS_HREDRAW | CS_VREDRAW;
2023 win_class.lpfnWndProc = WndProc;
2024 win_class.cbClsExtra = 0;
2025 win_class.cbWndExtra = 0;
2026 win_class.hInstance = demo->connection; // hInstance
2027 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2028 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2029 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2030 win_class.lpszMenuName = NULL;
2031 win_class.lpszClassName = demo->name;
2032 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2033 // Register window class:
2034 if (!RegisterClassEx(&win_class)) {
2035 // It didn't work, so try to give a useful error:
2036 printf("Unexpected error trying to start the application!\n");
2037 fflush(stdout);
2038 exit(1);
2039 }
2040 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06002041 RECT wr = { 0, 0, demo->width, demo->height };
2042 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002043 demo->window = CreateWindowEx(0,
2044 demo->name, // class name
2045 demo->name, // app name
2046 WS_OVERLAPPEDWINDOW | // window style
2047 WS_VISIBLE |
2048 WS_SYSMENU,
2049 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06002050 wr.right-wr.left, // width
2051 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06002052 NULL, // handle to parent
2053 NULL, // handle to menu
2054 demo->connection, // hInstance
2055 NULL); // no extra parameters
2056 if (!demo->window) {
2057 // It didn't work, so try to give a useful error:
2058 printf("Cannot create a window in which to draw!\n");
2059 fflush(stdout);
2060 exit(1);
2061 }
2062}
2063#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064static void demo_handle_event(struct demo *demo,
2065 const xcb_generic_event_t *event)
2066{
Piers Daniell735ee532015-02-23 16:23:13 -07002067 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002068 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002069 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002070 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002071 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002072 case XCB_CLIENT_MESSAGE:
2073 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2074 (*demo->atom_wm_delete_window).atom) {
2075 demo->quit = true;
2076 }
2077 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002078 case XCB_KEY_RELEASE:
2079 {
2080 const xcb_key_release_event_t *key =
2081 (const xcb_key_release_event_t *) event;
2082
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002083 switch (key->detail) {
2084 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002085 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002086 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002087 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002088 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002089 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002090 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002091 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002092 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002093 case 0x41:
2094 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002095 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002096 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002097 }
2098 break;
2099 default:
2100 break;
2101 }
2102}
2103
2104static void demo_run(struct demo *demo)
2105{
2106 xcb_flush(demo->connection);
2107
2108 while (!demo->quit) {
2109 xcb_generic_event_t *event;
2110
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002111 if (demo->pause) {
2112 event = xcb_wait_for_event(demo->connection);
2113 } else {
2114 event = xcb_poll_for_event(demo->connection);
2115 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002116 if (event) {
2117 demo_handle_event(demo, event);
2118 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002119 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002120
2121 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002122 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002123 demo_update_data_buffer(demo);
2124
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002125 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002126
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002127 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002128 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002129 demo->curFrame++;
2130 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2131 demo->quit = true;
2132
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002133 }
2134}
2135
2136static void demo_create_window(struct demo *demo)
2137{
2138 uint32_t value_mask, value_list[32];
2139
2140 demo->window = xcb_generate_id(demo->connection);
2141
2142 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2143 value_list[0] = demo->screen->black_pixel;
2144 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2145 XCB_EVENT_MASK_EXPOSURE;
2146
2147 xcb_create_window(demo->connection,
2148 XCB_COPY_FROM_PARENT,
2149 demo->window, demo->screen->root,
2150 0, 0, demo->width, demo->height, 0,
2151 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2152 demo->screen->root_visual,
2153 value_mask, value_list);
2154
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002155 /* Magic code that will send notification when window is destroyed */
2156 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2157 "WM_PROTOCOLS");
2158 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2159
2160 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2161 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2162
2163 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2164 demo->window, (*reply).atom, 4, 32, 1,
2165 &(*demo->atom_wm_delete_window).atom);
2166 free(reply);
2167
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002168 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002169
2170 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2171 const uint32_t coords[] = {100, 100};
2172 xcb_configure_window(demo->connection, demo->window,
2173 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002174}
Ian Elliott639ca472015-04-16 15:23:05 -06002175#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002176
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002177/*
2178 * Return 1 (true) if all layer names specified in check_names
2179 * can be found in given layer properties.
2180 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002181static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002182 uint32_t layer_count, VkLayerProperties *layers)
2183{
2184 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002185 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002186 for (uint32_t j = 0; j < layer_count; j++) {
2187 if (!strcmp(check_names[i], layers[j].layerName)) {
2188 found = 1;
2189 }
2190 }
2191 if (!found) {
2192 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2193 return 0;
2194 }
2195 }
2196 return 1;
2197}
2198
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002199static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002200{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002201 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002202 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002203 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002204 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002205 VkLayerProperties *instance_layers;
2206 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002207 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002208 uint32_t instance_layer_count = 0;
2209 uint32_t enabled_extension_count = 0;
2210 uint32_t enabled_layer_count = 0;
2211
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002212 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002213 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002214 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002215 "ObjectTracker",
2216 "DrawState",
2217 "ParamChecker",
2218 "ShaderChecker",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002219 "DeviceLimits",
2220 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002221 };
2222
2223 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002224 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002225 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002226 "ObjectTracker",
2227 "DrawState",
2228 "ParamChecker",
2229 "ShaderChecker",
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002230 "DeviceLimits",
2231 "Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002232 };
2233
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002234 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002235 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002236 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002237 assert(!err);
2238
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002239 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002240 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002241 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002242
2243 if (demo->validate) {
2244 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2245 instance_layer_count, instance_layers);
2246 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002247 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002248 "required validation layer.\n\n"
2249 "Please look at the Getting Started guide for additional "
2250 "information.\n",
2251 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002252 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002253 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002254 }
2255
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002256 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002257 assert(!err);
2258
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002259 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002260 memset(extension_names, 0, sizeof(extension_names));
2261 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002262 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002263 assert(!err);
2264 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002265 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002266 WSIextFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002267 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002268 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002269 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002270 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002271 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002272 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002273 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002274 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002275 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002276 if (!WSIextFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002277 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002278 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002279 "Vulkan installable client driver (ICD) installed?\nPlease "
2280 "look at the Getting Started guide for additional "
2281 "information.\n",
2282 "vkCreateInstance Failure");
2283 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002284 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002285 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002287 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002288 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002289 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002290 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002291 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002292 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002293 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002294 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002295 .pNext = NULL,
2296 .pAppInfo = &app,
2297 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002298 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002299 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002300 .extensionCount = enabled_extension_count,
2301 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002302 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002303
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002304 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002305
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002306 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002307 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2308 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002309 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002310 "additional information.\n",
2311 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002312 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002313 ERR_EXIT("Cannot find a specified extension library"
2314 ".\nMake sure your layers path is set appropriately\n",
2315 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002316 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002317 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2318 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002319 "the Getting Started guide for additional information.\n",
2320 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002321 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002322
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002323 free(instance_layers);
2324 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002325
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002326 /* Make initial call to query gpu_count, then second call for gpu info*/
2327 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2328 assert(!err && gpu_count > 0);
2329 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2330 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2331 assert(!err);
2332 /* For cube demo we just grab the first physical device */
2333 demo->gpu = physical_devices[0];
2334 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002335
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002336 /* Look for validation layers */
2337 validation_found = 0;
2338 enabled_layer_count = 0;
2339 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002340 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002341 assert(!err);
2342
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002343 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002344 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002345 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002346
2347 if (demo->validate) {
2348 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2349 device_layer_count, device_layers);
2350 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002351 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002352 "a required validation layer.\n\n"
2353 "Please look at the Getting Started guide for additional "
2354 "information.\n",
2355 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002356 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002357 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002358 }
2359
2360 uint32_t device_extension_count = 0;
2361 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002362 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002363 demo->gpu, NULL, &device_extension_count, NULL);
2364 assert(!err);
2365
2366 WSIextFound = 0;
2367 enabled_extension_count = 0;
2368 memset(extension_names, 0, sizeof(extension_names));
2369 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002370 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002371 demo->gpu, NULL, &device_extension_count, device_extensions);
2372 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002373
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002374 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002375 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002376 WSIextFound = 1;
Ian Elliotta0781972015-08-21 15:09:33 -06002377 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002378 }
2379 assert(enabled_extension_count < 64);
2380 }
2381 if (!WSIextFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002382 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliotta0781972015-08-21 15:09:33 -06002383 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002384 "Vulkan installable client driver (ICD) installed?\nPlease "
2385 "look at the Getting Started guide for additional "
2386 "information.\n",
2387 "vkCreateInstance Failure");
2388 }
2389
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002390 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002391 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2392 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002393 if (!demo->dbgCreateMsgCallback) {
2394 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2395 "vkGetProcAddr Failure");
2396 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002397 if (!demo->dbgDestroyMsgCallback) {
2398 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2399 "vkGetProcAddr Failure");
2400 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002401 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2402 if (!demo->dbgBreakCallback) {
2403 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2404 "vkGetProcAddr Failure");
2405 }
2406
2407 PFN_vkDbgMsgCallback callback;
2408
2409 if (!demo->use_break) {
2410 callback = dbgFunc;
2411 } else {
2412 callback = demo->dbgBreakCallback;
2413 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002414 err = demo->dbgCreateMsgCallback(
2415 demo->inst,
2416 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002417 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002418 &demo->msg_callback);
2419 switch (err) {
2420 case VK_SUCCESS:
2421 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002422 case VK_ERROR_OUT_OF_HOST_MEMORY:
2423 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2424 "dbgCreateMsgCallback Failure");
2425 break;
2426 default:
2427 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2428 "dbgCreateMsgCallback Failure");
2429 break;
2430 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002431 }
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002432 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
2433 assert(!err);
2434
2435 /* Call with NULL data to get count */
2436 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
2437 assert(!err);
2438 assert(demo->queue_count >= 1);
2439
2440 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2441 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
2442 assert(!err);
2443 // Find a queue that supports gfx
2444 uint32_t gfx_queue_idx = 0;
2445 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2446 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2447 break;
2448 }
2449 assert(gfx_queue_idx < demo->queue_count);
2450 const VkDeviceQueueCreateInfo queue = {
2451 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2452 .pNext = NULL,
2453 .queueFamilyIndex = gfx_queue_idx,
2454 .queueCount = 1,
2455 };
2456
2457 VkDeviceCreateInfo device = {
2458 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2459 .pNext = NULL,
2460 .queueRecordCount = 1,
2461 .pRequestedQueues = &queue,
2462 .layerCount = enabled_layer_count,
2463 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
2464 .extensionCount = enabled_extension_count,
2465 .ppEnabledExtensionNames = (const char *const*) extension_names,
2466 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002467
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002468 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002469 assert(!err);
2470
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002471 free(device_layers);
2472
Ian Elliotta0781972015-08-21 15:09:33 -06002473 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2474 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2475 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2476 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2477 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002478 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2479 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2480 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2481 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002482}
2483
2484static void demo_init_vk_wsi(struct demo *demo)
2485{
2486 VkResult err;
2487 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002488
Ian Elliottaaae5352015-07-06 14:27:58 -06002489 // Construct the WSI surface description:
Ian Elliotta0781972015-08-21 15:09:33 -06002490 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002491 demo->surface_description.pNext = NULL;
2492#ifdef _WIN32
Ian Elliotta0781972015-08-21 15:09:33 -06002493 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002494 demo->surface_description.pPlatformHandle = demo->connection;
2495 demo->surface_description.pPlatformWindow = demo->window;
2496#else // _WIN32
2497 demo->platform_handle_xcb.connection = demo->connection;
2498 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliotta0781972015-08-21 15:09:33 -06002499 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06002500 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2501 demo->surface_description.pPlatformWindow = &demo->window;
2502#endif // _WIN32
2503
2504 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002505 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2506 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002507 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2508 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliottaaae5352015-07-06 14:27:58 -06002509 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002510 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002511
2512 // Search for a graphics and a present queue in the array of queue
2513 // families, try to find one that supports both
2514 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2515 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002516 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002517 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2518 if (graphicsQueueNodeIndex == UINT32_MAX) {
2519 graphicsQueueNodeIndex = i;
2520 }
2521
2522 if (supportsPresent[i] == VK_TRUE) {
2523 graphicsQueueNodeIndex = i;
2524 presentQueueNodeIndex = i;
2525 break;
2526 }
2527 }
2528 }
2529 if (presentQueueNodeIndex == UINT32_MAX) {
2530 // If didn't find a queue that supports both graphics and present, then
2531 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002532 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002533 if (supportsPresent[i] == VK_TRUE) {
2534 presentQueueNodeIndex = i;
2535 break;
2536 }
2537 }
2538 }
2539 free(supportsPresent);
2540
2541 // Generate error if could not find both a graphics and a present queue
2542 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2543 ERR_EXIT("Could not find a graphics and a present queue\n",
2544 "WSI Initialization Failure");
2545 }
2546
2547 // TODO: Add support for separate queues, including presentation,
2548 // synchronization, and appropriate tracking for QueueSubmit
2549 // While it is possible for an application to use a separate graphics and a
2550 // present queues, this demo program assumes it is only using one:
2551 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2552 ERR_EXIT("Could not find a common graphics and a present queue\n",
2553 "WSI Initialization Failure");
2554 }
2555
2556 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002557
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002558 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002559 0, &demo->queue);
2560 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002561
Ian Elliottaaae5352015-07-06 14:27:58 -06002562 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002563 uint32_t formatCount;
Ian Elliotta0781972015-08-21 15:09:33 -06002564 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2565 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002566 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002567 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002568 VkSurfaceFormatKHR *surfFormats =
2569 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2570 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2571 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott8528eff2015-08-07 15:56:59 -06002572 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002573 assert(!err);
2574 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2575 // the surface has no preferred format. Otherwise, at least one
2576 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002577 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2578 {
2579 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2580 }
2581 else
2582 {
2583 assert(formatCount >= 1);
2584 demo->format = surfFormats[0].format;
2585 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002586 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002587
David Pinedo2bb7c932015-06-18 17:03:14 -06002588 demo->quit = false;
2589 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002590
2591 // Get Memory information and properties
2592 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2593 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002594}
2595
2596static void demo_init_connection(struct demo *demo)
2597{
Ian Elliott639ca472015-04-16 15:23:05 -06002598#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002599 const xcb_setup_t *setup;
2600 xcb_screen_iterator_t iter;
2601 int scr;
2602
2603 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002604 if (demo->connection == NULL) {
2605 printf("Cannot find a compatible Vulkan installable client driver "
2606 "(ICD).\nExiting ...\n");
2607 fflush(stdout);
2608 exit(1);
2609 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002610
2611 setup = xcb_get_setup(demo->connection);
2612 iter = xcb_setup_roots_iterator(setup);
2613 while (scr-- > 0)
2614 xcb_screen_next(&iter);
2615
2616 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002617#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002618}
2619
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002620static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002621{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002622 vec3 eye = {0.0f, 3.0f, 5.0f};
2623 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002624 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002625
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002626 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002627 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002628
Piers Daniell735ee532015-02-23 16:23:13 -07002629 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002630 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002631 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002632 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002633 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002634 if (strcmp(argv[i], "--use_glsl") == 0) {
2635 demo->use_glsl = true;
2636 continue;
2637 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002638 if (strcmp(argv[i], "--break") == 0) {
2639 demo->use_break = true;
2640 continue;
2641 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002642 if (strcmp(argv[i], "--validate") == 0) {
2643 demo->validate = true;
2644 continue;
2645 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002646 if (strcmp(argv[i], "--c") == 0 &&
2647 demo->frameCount == INT_MAX &&
2648 i < argc-1 &&
2649 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2650 demo->frameCount >= 0)
2651 {
2652 i++;
2653 continue;
2654 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002655
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002656 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002657 fflush(stderr);
2658 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002659 }
2660
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002661 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002662 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002663
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002664 demo->width = 500;
2665 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002666
2667 demo->spin_angle = 0.01f;
2668 demo->spin_increment = 0.01f;
2669 demo->pause = false;
2670
Piers Daniell735ee532015-02-23 16:23:13 -07002671 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002672 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2673 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002674}
2675
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002676
Ian Elliott639ca472015-04-16 15:23:05 -06002677#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002678extern int __getmainargs(
2679 int * _Argc,
2680 char *** _Argv,
2681 char *** _Env,
2682 int _DoWildCard,
2683 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002684
Ian Elliotta748eaf2015-04-28 15:50:36 -06002685int WINAPI WinMain(HINSTANCE hInstance,
2686 HINSTANCE hPrevInstance,
2687 LPSTR pCmdLine,
2688 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002689{
2690 MSG msg; // message
2691 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002692 int argc;
2693 char** argv;
2694 char** env;
2695 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002696
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002697 __getmainargs(&argc,&argv,&env,0,&new_mode);
2698
2699 demo_init(&demo, argc, argv);
2700 demo.connection = hInstance;
2701 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002702 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002703 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002704
2705 demo_prepare(&demo);
2706
2707 done = false; //initialize loop condition variable
2708 /* main message loop*/
2709 while(!done)
2710 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002711 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002712 if (msg.message == WM_QUIT) //check for a quit message
2713 {
2714 done = true; //if found, quit app
2715 }
2716 else
2717 {
2718 /* Translate and dispatch to event queue*/
2719 TranslateMessage(&msg);
2720 DispatchMessage(&msg);
2721 }
2722 }
2723
2724 demo_cleanup(&demo);
2725
Tony Barbourf9921732015-04-22 11:36:22 -06002726 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002727}
2728#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002729int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002730{
2731 struct demo demo;
2732
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002733 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002734 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002735 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002736
2737 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002738 demo_run(&demo);
2739
2740 demo_cleanup(&demo);
2741
2742 return 0;
2743}
Ian Elliott639ca472015-04-16 15:23:05 -06002744#endif // _WIN32