blob: fd74171c4e5f481b3251047ceb0afe625666f7db [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Ian Elliottaaae5352015-07-06 14:27:58 -060040#include <vk_wsi_swapchain.h>
41#include <vk_wsi_device_swapchain.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060042#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060043
Cody Northropfe3d8bc2015-03-17 14:54:35 -060044#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060046#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060047#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_BUFFER_COUNT 2
50#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliott07264132015-04-28 11:35:02 -060062#ifdef _WIN32
63#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
68
Ian Elliott07264132015-04-28 11:35:02 -060069#else // _WIN32
70
71#define ERR_EXIT(err_msg, err_class) \
72 do { \
73 printf(err_msg); \
74 fflush(stdout); \
75 exit(1); \
76 } while (0)
77#endif // _WIN32
78
Ian Elliottaaae5352015-07-06 14:27:58 -060079#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
80{ \
81 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
82 if (demo->fp##entrypoint == NULL) { \
83 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
84 "vkGetInstanceProcAddr Failure"); \
85 } \
86}
87
Ian Elliott673898b2015-06-22 15:07:49 -060088#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
89{ \
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -060090 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060091 if (demo->fp##entrypoint == NULL) { \
92 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
93 "vkGetDeviceProcAddr Failure"); \
94 } \
95}
96
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060097/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070098 * structure to track all objects related to a texture.
99 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600100struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600101 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700102
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600103 VkImage image;
104 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600105
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500106 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600107 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700108 int32_t tex_width, tex_height;
109};
110
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600111static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600112 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600113};
114
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600115struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600116 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600117 float mvp[4][4];
118 float position[12*3][4];
119 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120};
121
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600122struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600123 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600124 float mvp[4][4];
125 float position[12*3][4];
126 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600128
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600129//--------------------------------------------------------------------------------------
130// Mesh and VertexFormat Data
131//--------------------------------------------------------------------------------------
132struct Vertex
133{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600134 float posX, posY, posZ, posW; // Position data
135 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600136};
137
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600138struct VertexPosTex
139{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600140 float posX, posY, posZ, posW; // Position data
141 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142};
143
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600144#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600145#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600146
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600147static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800148 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600149 -1.0f,-1.0f, 1.0f,
150 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800151 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 -1.0f, 1.0f,-1.0f,
153 -1.0f,-1.0f,-1.0f,
154
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 1.0f, 1.0f,-1.0f,
157 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600160 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600164 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 -1.0f, 1.0f, 1.0f,
171 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 1.0f, 1.0f, 1.0f,
178 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f,-1.0f,-1.0f,
181 1.0f, 1.0f,-1.0f,
182
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600185 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f,-1.0f, 1.0f,
188 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600189};
190
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600191static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 0.0f, 0.0f, // -X side
193 1.0f, 0.0f,
194 1.0f, 1.0f,
195 1.0f, 1.0f,
196 0.0f, 1.0f,
197 0.0f, 0.0f,
198
199 1.0f, 0.0f, // -Z side
200 0.0f, 1.0f,
201 0.0f, 0.0f,
202 1.0f, 0.0f,
203 1.0f, 1.0f,
204 0.0f, 1.0f,
205
206 1.0f, 1.0f, // -Y side
207 1.0f, 0.0f,
208 0.0f, 0.0f,
209 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 0.0f, 0.0f,
211 0.0f, 1.0f,
212
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600214 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800215 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600216 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600218 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 1.0f, 1.0f, // +X side
221 0.0f, 1.0f,
222 0.0f, 0.0f,
223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 0.0f, 1.0f, // +Z side
228 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600230 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800231 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600233};
234
235void dumpMatrix(const char *note, mat4x4 MVP)
236{
237 int i;
238
239 printf("%s: \n", note);
240 for (i=0; i<4; i++) {
241 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
242 }
243 printf("\n");
244 fflush(stdout);
245}
246
247void dumpVec4(const char *note, vec4 vector)
248{
249 printf("%s: \n", note);
250 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
251 printf("\n");
252 fflush(stdout);
253}
254
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600255void dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600256 VkFlags msgFlags,
257 VkDbgObjectType objType,
258 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600259 size_t location,
260 int32_t msgCode,
261 const char* pLayerPrefix,
262 const char* pMsg,
263 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600264{
265 char *message = (char *) malloc(strlen(pMsg)+100);
266
267 assert (message);
268
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600269 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
270 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
271 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600272 // We know that we're submitting queues without fences, ignore this warning
273 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
274 return;
275 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600276 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600277 } else {
278 return;
279 }
280
281#ifdef _WIN32
282 MessageBox(NULL, message, "Alert", MB_OK);
283#else
284 printf("%s\n",message);
285 fflush(stdout);
286#endif
287 free(message);
288}
289
Ian Elliottaaae5352015-07-06 14:27:58 -0600290typedef struct _SwapChainBuffers {
291 VkImage image;
292 VkCmdBuffer cmd;
293 VkAttachmentView view;
294} SwapChainBuffers;
295
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600296struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600297#ifdef _WIN32
298#define APP_NAME_STR_LEN 80
299 HINSTANCE connection; // hInstance - Windows Instance
300 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
301 HWND window; // hWnd - window handle
302#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600303 xcb_connection_t *connection;
304 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800305 xcb_window_t window;
306 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliottaaae5352015-07-06 14:27:58 -0600307 VkPlatformHandleXcbWSI platform_handle_xcb;
Tony Barbour6839bec2015-07-13 16:37:21 -0600308#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600309 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700310 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600311 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600313 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600314 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkDevice device;
316 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700317 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600318 VkPhysicalDeviceProperties gpu_props;
Tony Barbour72304ef2015-04-16 15:59:00 -0600319 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600320 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325
Ian Elliottaaae5352015-07-06 14:27:58 -0600326 PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI;
327 PFN_vkGetSurfaceInfoWSI fpGetSurfaceInfoWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600328 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
329 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
330 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600331 PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600332 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600333 VkSurfaceDescriptionWindowWSI surface_description;
334 size_t swapChainImageCount;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800335 VkSwapChainWSI swap_chain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600336 SwapChainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337
Ian Elliottaaae5352015-07-06 14:27:58 -0600338 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600339
340 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600341 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600342
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600343 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500344 VkDeviceMemory mem;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800345 VkAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346 } depth;
347
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600348 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
350 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600351 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500352 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600353 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800354 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600355 } uniform_data;
356
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600357 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500358 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600360 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800361 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
Tony Barbour155cce82015-07-03 10:33:54 -0600364 VkDynamicViewportState viewport;
365 VkDynamicRasterState raster;
366 VkDynamicColorBlendState color_blend;
367 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600369 mat4x4 projection_matrix;
370 mat4x4 view_matrix;
371 mat4x4 model_matrix;
372
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600373 float spin_angle;
374 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600375 bool pause;
376
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600377 VkShaderModule vert_shader_module;
378 VkShaderModule frag_shader_module;
379
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkDescriptorPool desc_pool;
381 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800382
Chia-I Wuf973e322015-07-08 13:34:24 +0800383 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
384
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600386 int32_t curFrame;
387 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600388 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600389 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600390 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600391 VkDbgMsgCallback msg_callback;
392
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600393 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600394 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395};
396
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600397static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
398{
399 // Search memtypes to find first index with those properties
400 for (uint32_t i = 0; i < 32; i++) {
401 if ((typeBits & 1) == 1) {
402 // Type is available, does it match user properties?
403 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
404 *typeIndex = i;
405 return VK_SUCCESS;
406 }
407 }
408 typeBits >>= 1;
409 }
410 // No memory types matched, return failure
411 return VK_UNSUPPORTED;
412}
413
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600414static void demo_flush_init_cmd(struct demo *demo)
415{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600416 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600417
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600418 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419 return;
420
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600421 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600422 assert(!err);
423
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600424 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600425 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600426
Tony Barbour155cce82015-07-03 10:33:54 -0600427 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428 assert(!err);
429
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 assert(!err);
432
Tony Barbour155cce82015-07-03 10:33:54 -0600433 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600434 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435}
436
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437static void demo_set_image_layout(
438 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600439 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400440 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600441 VkImageLayout old_image_layout,
442 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600443{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600444 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600447 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600448 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600449 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600450 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800451 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452 .flags = 0,
453 };
454
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600456 assert(!err);
457
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600458 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600459 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600460 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600461 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600462 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600463 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 }
466
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600467 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600468 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600469 .pNext = NULL,
470 .outputMask = 0,
471 .inputMask = 0,
472 .oldLayout = old_image_layout,
473 .newLayout = new_image_layout,
474 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400475 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476 };
477
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600480 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 }
482
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600484 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600485 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600486 }
487
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600488 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600489
Tony Barbour50bbca42015-06-29 16:20:35 -0600490 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
491 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600493 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494}
495
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600496static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600497{
Chia-I Wuf973e322015-07-08 13:34:24 +0800498 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600499 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600500 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600501 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700503 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800504 const VkClearValue clear_values[2] = {
505 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
506 [1] = { .ds = { 1.0f, 0 } },
507 };
508 const VkRenderPassBeginInfo rp_begin = {
509 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
510 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800511 .renderPass = demo->render_pass,
512 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800513 .renderArea.offset.x = 0,
514 .renderArea.offset.y = 0,
515 .renderArea.extent.width = demo->width,
516 .renderArea.extent.height = demo->height,
517 .attachmentCount = 2,
518 .pAttachmentClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700519 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800520 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600521
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600522 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523 assert(!err);
524
Chia-I Wua74c5b22015-07-07 11:50:03 +0800525 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
526
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600528 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600529 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600530 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600531
Tony Barbour155cce82015-07-03 10:33:54 -0600532 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
533 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
534 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
535 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800538 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600540 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541 assert(!err);
542}
543
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600544
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600545void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600547 mat4x4 MVP, Model, VP;
548 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600549 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600550 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600551
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600552 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600555 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700556 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600557 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600558
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500559 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600560 assert(!err);
561
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600562 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600563
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500564 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565 assert(!err);
566}
567
568static void demo_draw(struct demo *demo)
569{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600570 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600571 VkSemaphore presentCompleteSemaphore;
572 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
573 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
574 .pNext = NULL,
575 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
576 };
Tony Barbour155cce82015-07-03 10:33:54 -0600577 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578
Ian Elliottaaae5352015-07-06 14:27:58 -0600579 err = vkCreateSemaphore(demo->device,
580 &presentCompleteSemaphoreCreateInfo,
581 &presentCompleteSemaphore);
582 assert(!err);
583
584 // Get the index of the next available swapchain image:
585 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
586 UINT64_MAX,
587 presentCompleteSemaphore,
588 &demo->current_buffer);
589 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
590 // return codes
591 assert(!err);
592
593 // Wait for the present complete semaphore to be signaled to ensure
594 // that the image won't be rendered to until the presentation
595 // engine has fully released ownership to the application, and it is
596 // okay to render to the image.
597 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
598
599// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600600 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600601 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600602 assert(!err);
603
Ian Elliottaaae5352015-07-06 14:27:58 -0600604 VkPresentInfoWSI present = {
605 .sType = VK_STRUCTURE_TYPE_QUEUE_PRESENT_INFO_WSI,
606 .pNext = NULL,
607 .swapChainCount = 1,
608 .swapChains = &demo->swap_chain,
609 .imageIndices = &demo->current_buffer,
610 };
611
612// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600613 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600614 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
615 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 assert(!err);
617
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600618 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600619 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800620
621 err = vkQueueWaitIdle(demo->queue);
622 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623}
624
625static void demo_prepare_buffers(struct demo *demo)
626{
Ian Elliottaaae5352015-07-06 14:27:58 -0600627 VkResult U_ASSERT_ONLY err;
628
629 // Check the surface properties and formats
630 size_t capsSize;
631 size_t presentModesSize;
632 err = demo->fpGetSurfaceInfoWSI(demo->device,
633 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
634 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, NULL);
635 assert(!err);
636 err = demo->fpGetSurfaceInfoWSI(demo->device,
637 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
638 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, NULL);
639 assert(!err);
640
641 VkSurfacePropertiesWSI *surfProperties =
642 (VkSurfacePropertiesWSI *)malloc(capsSize);
643 VkSurfacePresentModePropertiesWSI *presentModes =
644 (VkSurfacePresentModePropertiesWSI *)malloc(presentModesSize);
645
646 err = demo->fpGetSurfaceInfoWSI(demo->device,
647 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
648 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, surfProperties);
649 assert(!err);
650 err = demo->fpGetSurfaceInfoWSI(demo->device,
651 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
652 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, presentModes);
653 assert(!err);
654
655 VkExtent2D swapChainExtent;
656 // width and height are either both -1, or both not -1.
657 if (surfProperties->currentExtent.width == -1)
658 {
659 // If the surface size is undefined, the size is set to
660 // the size of the images requested.
661 swapChainExtent.width = demo->width;
662 swapChainExtent.height = demo->height;
663 }
664 else
665 {
666 // If the surface size is defined, the swap chain size must match
667 swapChainExtent = surfProperties->currentExtent;
668 }
669
670 // If mailbox mode is available, use it, as is the lowest-latency non-
671 // tearing mode. If not, fall back to IMMEDIATE which should always be
672 // available.
673 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
674 size_t presentModeCount = presentModesSize / sizeof(VkSurfacePresentModePropertiesWSI);
675 for (size_t i = 0; i < presentModeCount; i++) {
676 if (presentModes[i].presentMode == VK_PRESENT_MODE_MAILBOX_WSI) {
677 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
678 break;
679 }
680 }
681
Ian Elliott2d47da92015-07-13 12:20:56 -0600682#define WORK_AROUND_CODE
683#ifdef WORK_AROUND_CODE
684 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
685#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600686 // Determine the number of VkImage's to use in the swap chain (we desire to
687 // own only 1 image at a time, besides the images being displayed and
688 // queued for display):
689 uint32_t desiredNumberOfSwapChainImages = surfProperties->minImageCount + 1;
690 if ((surfProperties->maxImageCount > 0) &&
691 (desiredNumberOfSwapChainImages > surfProperties->maxImageCount))
692 {
693 // Application must settle for fewer images than desired:
694 desiredNumberOfSwapChainImages = surfProperties->maxImageCount;
695 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600696#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600697
698 VkSurfaceTransformFlagBitsWSI preTransform;
699 if (surfProperties->supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
700 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
701 } else {
702 preTransform = surfProperties->currentTransform;
703 }
704
Chia-I Wucbb564e2015-04-16 22:02:10 +0800705 const VkSwapChainCreateInfoWSI swap_chain = {
706 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
707 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600708 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
709 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800710 .imageFormat = demo->format,
711 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600712 .width = swapChainExtent.width,
713 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600714 },
Ian Elliottaaae5352015-07-06 14:27:58 -0600715 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800716 .imageArraySize = 1,
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 .presentMode = swapChainPresentMode,
718 .oldSwapChain.handle = 0,
719 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600721 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722
Jon Ashburn0b85d052015-05-21 18:13:33 -0600723 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800724 assert(!err);
725
Ian Elliottaaae5352015-07-06 14:27:58 -0600726 size_t swapChainImagesSize;
727 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
728 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
729 &swapChainImagesSize, NULL);
730 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800731
Ian Elliottaaae5352015-07-06 14:27:58 -0600732 VkSwapChainImagePropertiesWSI* swapChainImages = (VkSwapChainImagePropertiesWSI*)malloc(swapChainImagesSize);
733 assert(swapChainImages);
734 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
735 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
736 &swapChainImagesSize, swapChainImages);
737 assert(!err);
738
Ian Elliott2d47da92015-07-13 12:20:56 -0600739#ifdef WORK_AROUND_CODE
740 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
741#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600742 // The number of images within the swap chain is determined based on the size of the info returned
743 demo->swapChainImageCount = swapChainImagesSize / sizeof(VkSwapChainImagePropertiesWSI);
Ian Elliott2d47da92015-07-13 12:20:56 -0600744#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600745
746 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
747 assert(demo->buffers);
748
749 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800750 VkAttachmentViewCreateInfo color_attachment_view = {
751 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600752 .pNext = NULL,
753 .format = demo->format,
754 .mipLevel = 0,
755 .baseArraySlice = 0,
756 .arraySize = 1,
757 };
758
Ian Elliottaaae5352015-07-06 14:27:58 -0600759 demo->buffers[i].image = swapChainImages[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600760
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600761 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400762 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600763 VK_IMAGE_LAYOUT_UNDEFINED,
764 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600765
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600766 color_attachment_view.image = demo->buffers[i].image;
767
Chia-I Wua74c5b22015-07-07 11:50:03 +0800768 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600769 &color_attachment_view, &demo->buffers[i].view);
770 assert(!err);
771 }
772}
773
774static void demo_prepare_depth(struct demo *demo)
775{
Tony Barbour72304ef2015-04-16 15:59:00 -0600776 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600777 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600778 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600779 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600780 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781 .format = depth_format,
782 .extent = { demo->width, demo->height, 1 },
783 .mipLevels = 1,
784 .arraySize = 1,
785 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600786 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600787 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600788 .flags = 0,
789 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600790 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600791 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500792 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600793 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600794 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600795 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800796 VkAttachmentViewCreateInfo view = {
797 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600798 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600799 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600800 .format = depth_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801 .mipLevel = 0,
802 .baseArraySlice = 0,
803 .arraySize = 1,
804 .flags = 0,
805 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600806
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500807 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600808 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600809
810 demo->depth.format = depth_format;
811
812 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600813 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600814 &demo->depth.image);
815 assert(!err);
816
Tony Barbour155cce82015-07-03 10:33:54 -0600817 err = vkGetImageMemoryRequirements(demo->device,
818 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600819
820 mem_alloc.allocationSize = mem_reqs.size;
821 err = memory_type_from_properties(demo,
822 mem_reqs.memoryTypeBits,
823 VK_MEMORY_PROPERTY_DEVICE_ONLY,
824 &mem_alloc.memoryTypeIndex);
825 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600826
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500827 /* allocate memory */
828 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
829 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500831 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600832 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500833 demo->depth.mem, 0);
834 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600836 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400837 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600838 VK_IMAGE_LAYOUT_UNDEFINED,
839 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600840
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600841 /* create image view */
842 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800843 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600844 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600845}
846
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600847/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600848 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600849 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600850 * \param demo : Needed to access VK calls
851 * \param filename : the png file to be loaded
852 * \param width : width of png, to be updated as a side effect of this function
853 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600854 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600855 * \return bool : an opengl texture id. true if successful?,
856 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600857 *
858 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
859 * Modified to copy image to memory
860 *
861 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700862bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600863 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600864 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600865{
866 //header for testing if it is a png
867 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600868 int is_png, bit_depth, color_type, rowbytes;
869 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700870 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600871 png_structp png_ptr;
872 png_infop info_ptr, end_info;
873 png_byte *image_data;
874 png_bytep *row_pointers;
875
876 //open file as binary
877 FILE *fp = fopen(filename, "rb");
878 if (!fp) {
879 return false;
880 }
881
882 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600883 retval = fread(header, 1, 8, fp);
884 if (retval != 8) {
885 fclose(fp);
886 return false;
887 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600888
889 //test if png
890 is_png = !png_sig_cmp(header, 0, 8);
891 if (!is_png) {
892 fclose(fp);
893 return false;
894 }
895
896 //create png struct
897 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
898 NULL, NULL);
899 if (!png_ptr) {
900 fclose(fp);
901 return (false);
902 }
903
904 //create png info struct
905 info_ptr = png_create_info_struct(png_ptr);
906 if (!info_ptr) {
907 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
908 fclose(fp);
909 return (false);
910 }
911
912 //create png info struct
913 end_info = png_create_info_struct(png_ptr);
914 if (!end_info) {
915 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
916 fclose(fp);
917 return (false);
918 }
919
920 //png error stuff, not sure libpng man suggests this.
921 if (setjmp(png_jmpbuf(png_ptr))) {
922 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
923 fclose(fp);
924 return (false);
925 }
926
927 //init png reading
928 png_init_io(png_ptr, fp);
929
930 //let libpng know you already read the first 8 bytes
931 png_set_sig_bytes(png_ptr, 8);
932
933 // read all the info up to the image data
934 png_read_info(png_ptr, info_ptr);
935
936 // get info about png
937 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
938 NULL, NULL, NULL);
939
940 //update width and height based on png info
941 *width = twidth;
942 *height = theight;
943
944 // Require that incoming texture be 8bits per color component
945 // and 4 components (RGBA).
946 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
947 png_get_channels(png_ptr, info_ptr) != 4) {
948 return false;
949 }
950
951 if (rgba_data == NULL) {
952 // If data pointer is null, we just want the width & height
953 // clean up memory and close stuff
954 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
955 fclose(fp);
956
957 return true;
958 }
959
960 // Update the png info struct.
961 png_read_update_info(png_ptr, info_ptr);
962
963 // Row size in bytes.
964 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
965
966 // Allocate the image_data as a big block, to be given to opengl
967 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
968 if (!image_data) {
969 //clean up memory and close stuff
970 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
971 fclose(fp);
972 return false;
973 }
974
975 // row_pointers is for pointing to image_data for reading the png with libpng
976 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
977 if (!row_pointers) {
978 //clean up memory and close stuff
979 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
980 // delete[] image_data;
981 fclose(fp);
982 return false;
983 }
984 // set the individual row_pointers to point at the correct offsets of image_data
985 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700986 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600987
988 // read the png into image_data through row_pointers
989 png_read_image(png_ptr, row_pointers);
990
991 // clean up memory and close stuff
992 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
993 free(row_pointers);
994 free(image_data);
995 fclose(fp);
996
997 return true;
998}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600999
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001000static void demo_prepare_texture_image(struct demo *demo,
1001 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001002 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001003 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001004 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001005 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001006{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001007 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001008 int32_t tex_width;
1009 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001010 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011
David Pinedo30bd71d2015-04-23 08:16:57 -06001012 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1013 {
1014 printf("Failed to load textures\n");
1015 fflush(stdout);
1016 exit(1);
1017 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001019 tex_obj->tex_width = tex_width;
1020 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001022 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001023 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001025 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026 .format = tex_format,
1027 .extent = { tex_width, tex_height, 1 },
1028 .mipLevels = 1,
1029 .arraySize = 1,
1030 .samples = 1,
1031 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001032 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001033 .flags = 0,
1034 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001035 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001036 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001037 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001039 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040 };
1041
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001042 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001044 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001045 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046 assert(!err);
1047
Tony Barbour155cce82015-07-03 10:33:54 -06001048 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001049 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001050
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001051 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001053 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1054 assert(!err);
1055
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001056 /* allocate memory */
1057 err = vkAllocMemory(demo->device, &mem_alloc,
1058 &(tex_obj->mem));
1059 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001060
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001061 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001062 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001063 tex_obj->mem, 0);
1064 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065
Tony Barbour72304ef2015-04-16 15:59:00 -06001066 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001067 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001068 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069 .mipLevel = 0,
1070 .arraySlice = 0,
1071 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001072 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 void *data;
1074
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001075 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1076 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001077
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001078 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079 assert(!err);
1080
1081 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1082 fprintf(stderr, "Error loading texture: %s\n", filename);
1083 }
1084
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001085 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086 assert(!err);
1087 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001088
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001089 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001090 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001091 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001092 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001093 tex_obj->imageLayout);
1094 /* 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 -07001095}
1096
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001097static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001098{
1099 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001100 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001101 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001102}
1103
1104static void demo_prepare_textures(struct demo *demo)
1105{
Tony Barbour72304ef2015-04-16 15:59:00 -06001106 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001107 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001108 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001109 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001110
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001111 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001112 assert(!err);
1113
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115
FslNopper6a7e50e2015-05-06 21:42:01 +02001116 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117 /* Device can texture using linear textures */
1118 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001119 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001120 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001122 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123
1124 memset(&staging_texture, 0, sizeof(staging_texture));
1125 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001126 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127
1128 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001129 VK_IMAGE_TILING_OPTIMAL,
1130 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1131 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001133 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001134 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001135 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001136 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001138 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001139 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001140 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001141 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001143 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001144 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001145 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001146 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001147 .destOffset = { 0, 0, 0 },
1148 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1149 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001150 vkCmdCopyImage(demo->cmd,
1151 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1152 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001153 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001154
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001155 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001156 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001157 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001158 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001160 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001161
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001162 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001164 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1165 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166 }
1167
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001168 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001169 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001170 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001171 .magFilter = VK_TEX_FILTER_NEAREST,
1172 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001173 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001174 .addressU = VK_TEX_ADDRESS_CLAMP,
1175 .addressV = VK_TEX_ADDRESS_CLAMP,
1176 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001177 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001179 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001180 .minLod = 0.0f,
1181 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001182 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001183 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001184
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001185 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001187 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001188 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001189 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 .channels = { VK_CHANNEL_SWIZZLE_R,
1192 VK_CHANNEL_SWIZZLE_G,
1193 VK_CHANNEL_SWIZZLE_B,
1194 VK_CHANNEL_SWIZZLE_A, },
1195 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001196 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001197
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001199 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200 &demo->textures[i].sampler);
1201 assert(!err);
1202
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203 /* create image view */
1204 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001205 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 &demo->textures[i].view);
1207 assert(!err);
1208 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001211void demo_prepare_cube_data_buffer(struct demo *demo)
1212{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001213 VkBufferCreateInfo buf_info;
1214 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001215 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001216 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001217 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001218 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001219 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001220 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001221 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001222 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001223 int i;
1224 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001225 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001226 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001227
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001228 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001229 mat4x4_mul(MVP, VP, demo->model_matrix);
1230 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001231// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001232
1233 for (i=0; i<12*3; i++) {
1234 data.position[i][0] = g_vertex_buffer_data[i*3];
1235 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1236 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1237 data.position[i][3] = 1.0f;
1238 data.attr[i][0] = g_uv_buffer_data[2*i];
1239 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1240 data.attr[i][2] = 0;
1241 data.attr[i][3] = 0;
1242 }
1243
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001244 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001245 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001246 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001247 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001248 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001249 assert(!err);
1250
Tony Barbour155cce82015-07-03 10:33:54 -06001251 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001252 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001253
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001254 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001255 err = memory_type_from_properties(demo,
1256 mem_reqs.memoryTypeBits,
1257 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1258 &alloc_info.memoryTypeIndex);
1259 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001260
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001261 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1262 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001263
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001264 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1265 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001266
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001267 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001268
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001269 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1270 assert(!err);
1271
Tony Barbour155cce82015-07-03 10:33:54 -06001272 err = vkBindBufferMemory(demo->device,
1273 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001274 demo->uniform_data.mem, 0);
1275 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001276
1277 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001278 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001279 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001280 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001281 view_info.offset = 0;
1282 view_info.range = sizeof(data);
1283
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001284 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001285 assert(!err);
1286
Chia-I Wuae721ba2015-05-25 16:27:55 +08001287 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001288}
1289
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001290static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001291{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001292 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001293 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001294 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001295 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001296 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001297 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001298 },
1299 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001300 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001301 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001302 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001303 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001304 },
1305 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001306 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001307 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001308 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001309 .count = 2,
1310 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001311 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001312 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001314 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001315 &descriptor_layout, &demo->desc_layout);
1316 assert(!err);
1317
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001318 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1319 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1320 .pNext = NULL,
1321 .descriptorSetCount = 1,
1322 .pSetLayouts = &demo->desc_layout,
1323 };
1324
1325 err = vkCreatePipelineLayout(demo->device,
1326 &pPipelineLayoutCreateInfo,
1327 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001329}
1330
Chia-I Wuf973e322015-07-08 13:34:24 +08001331static void demo_prepare_render_pass(struct demo *demo)
1332{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001333 const VkAttachmentDescription attachments[2] = {
1334 [0] = {
1335 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1336 .pNext = NULL,
1337 .format = demo->format,
1338 .samples = 1,
1339 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1340 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1341 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1342 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1343 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1344 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1345 },
1346 [1] = {
1347 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1348 .pNext = NULL,
1349 .format = demo->depth.format,
1350 .samples = 1,
1351 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1352 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1353 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1354 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1355 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1356 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1357 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001358 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001359 const VkAttachmentReference color_reference = {
1360 .attachment = 0,
1361 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1362 };
1363 const VkSubpassDescription subpass = {
1364 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1365 .pNext = NULL,
1366 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1367 .flags = 0,
1368 .inputCount = 0,
1369 .inputAttachments = NULL,
1370 .colorCount = 1,
1371 .colorAttachments = &color_reference,
1372 .resolveAttachments = NULL,
1373 .depthStencilAttachment = {
1374 .attachment = 1,
1375 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1376 },
1377 .preserveCount = 0,
1378 .preserveAttachments = NULL,
1379 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001380 const VkRenderPassCreateInfo rp_info = {
1381 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1382 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001383 .attachmentCount = 2,
1384 .pAttachments = attachments,
1385 .subpassCount = 1,
1386 .pSubpasses = &subpass,
1387 .dependencyCount = 0,
1388 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001389 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001390 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001391
1392 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1393 assert(!err);
1394}
1395
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001396static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001397 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001398 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001399 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001400 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001401{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001402 VkShaderModuleCreateInfo moduleCreateInfo;
1403 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001404 VkShader shader;
1405 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001407
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001408 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1409 moduleCreateInfo.pNext = NULL;
1410
1411 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1412 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001413 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414
Cody Northrop1fedb212015-05-28 11:27:16 -06001415 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001416 moduleCreateInfo.codeSize = size;
1417 moduleCreateInfo.pCode = code;
1418 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001419 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001420 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001421 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001422 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001423
1424 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001425 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001426 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001427 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001428 } else {
1429 // Create fake SPV structure to feed GLSL
1430 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001431 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1432 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1433 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001434
1435 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001436 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1437 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1438 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1439 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001440
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001441 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001442 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001443 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001444 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001445
1446 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001447 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001448 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001449 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001450 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001451 return shader;
1452}
1453
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001454char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001455{
1456 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001457 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001458 void *shader_code;
1459
1460 FILE *fp = fopen(filename, "rb");
1461 if (!fp) return NULL;
1462
1463 fseek(fp, 0L, SEEK_END);
1464 size = ftell(fp);
1465
1466 fseek(fp, 0L, SEEK_SET);
1467
1468 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001469 retval = fread(shader_code, size, 1, fp);
1470 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001471
1472 *psize = size;
1473
1474 return shader_code;
1475}
1476
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001477static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478{
Cody Northrop1fedb212015-05-28 11:27:16 -06001479 if (!demo->use_glsl) {
1480 void *vertShaderCode;
1481 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001482
Cody Northrop1fedb212015-05-28 11:27:16 -06001483 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001484
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001485 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001486 vertShaderCode, size);
1487 } else {
1488 static const char *vertShaderText =
1489 "#version 140\n"
1490 "#extension GL_ARB_separate_shader_objects : enable\n"
1491 "#extension GL_ARB_shading_language_420pack : enable\n"
1492 "\n"
1493 "layout(binding = 0) uniform buf {\n"
1494 " mat4 MVP;\n"
1495 " vec4 position[12*3];\n"
1496 " vec4 attr[12*3];\n"
1497 "} ubuf;\n"
1498 "\n"
1499 "layout (location = 0) out vec4 texcoord;\n"
1500 "\n"
1501 "void main() \n"
1502 "{\n"
1503 " texcoord = ubuf.attr[gl_VertexID];\n"
1504 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1505 "\n"
1506 " // GL->VK conventions\n"
1507 " gl_Position.y = -gl_Position.y;\n"
1508 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1509 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001510
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001511 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001512 (const void *) vertShaderText,
1513 strlen(vertShaderText));
1514 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001515}
1516
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001517static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001518{
Cody Northrop1fedb212015-05-28 11:27:16 -06001519 if (!demo->use_glsl) {
1520 void *fragShaderCode;
1521 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001522
Cody Northrop1fedb212015-05-28 11:27:16 -06001523 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001524
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001525 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001526 fragShaderCode, size);
1527 } else {
1528 static const char *fragShaderText =
1529 "#version 140\n"
1530 "#extension GL_ARB_separate_shader_objects : enable\n"
1531 "#extension GL_ARB_shading_language_420pack : enable\n"
1532 "layout (binding = 1) uniform sampler2D tex;\n"
1533 "\n"
1534 "layout (location = 0) in vec4 texcoord;\n"
1535 "layout (location = 0) out vec4 uFragColor;\n"
1536 "void main() {\n"
1537 " uFragColor = texture(tex, texcoord.xy);\n"
1538 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001539
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001540 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001541 (const void *) fragShaderText,
1542 strlen(fragShaderText));
1543 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544}
1545
1546static void demo_prepare_pipeline(struct demo *demo)
1547{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001548 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001549 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001550 VkPipelineInputAssemblyStateCreateInfo ia;
1551 VkPipelineRasterStateCreateInfo rs;
1552 VkPipelineColorBlendStateCreateInfo cb;
1553 VkPipelineDepthStencilStateCreateInfo ds;
1554 VkPipelineViewportStateCreateInfo vp;
1555 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001556 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001557
1558 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001559 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001560 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001563 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001564 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565
1566 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001567 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001568 rs.fillMode = VK_FILL_MODE_SOLID;
1569 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001570 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001571 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001572
1573 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001574 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1575 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001576 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001577 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001578 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001579 cb.attachmentCount = 1;
1580 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001581
Tony Barbour29645d02015-01-16 14:27:35 -07001582 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001583 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001584 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001585
1586 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001587 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001588 ds.depthTestEnable = VK_TRUE;
1589 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001590 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001591 ds.depthBoundsEnable = VK_FALSE;
1592 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1593 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001594 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001595 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001596 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597
Tony Barbour29645d02015-01-16 14:27:35 -07001598 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001599 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001600 ms.sampleMask = 1;
Tony Barbour3ca22502015-06-26 10:18:34 -06001601 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001602
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001603 // Two stages: vs and fs
1604 pipeline.stageCount = 2;
1605 VkPipelineShaderStageCreateInfo shaderStages[2];
1606 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1607
1608 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1609 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1610 shaderStages[0].shader = demo_prepare_vs(demo);
1611
1612 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1613 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1614 shaderStages[1].shader = demo_prepare_fs(demo);
1615
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001616 memset(&pipelineCache, 0, sizeof(pipelineCache));
1617 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001618
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001619 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1620 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001621
1622 pipeline.pVertexInputState = NULL;
1623 pipeline.pInputAssemblyState = &ia;
1624 pipeline.pRasterState = &rs;
1625 pipeline.pColorBlendState = &cb;
1626 pipeline.pMultisampleState = &ms;
1627 pipeline.pViewportState = &vp;
1628 pipeline.pDepthStencilState = &ds;
1629 pipeline.pStages = shaderStages;
1630
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001631 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001632 assert(!err);
1633
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001634 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001635 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001636 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001637 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1638 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639}
1640
1641static void demo_prepare_dynamic_states(struct demo *demo)
1642{
Tony Barbour155cce82015-07-03 10:33:54 -06001643 VkDynamicViewportStateCreateInfo viewport_create;
1644 VkDynamicRasterStateCreateInfo raster;
1645 VkDynamicColorBlendStateCreateInfo color_blend;
1646 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001647 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001648
Tony Barbour29645d02015-01-16 14:27:35 -07001649 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001650 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001651 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001652 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001653 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001654 viewport.height = (float) demo->height;
1655 viewport.width = (float) demo->width;
1656 viewport.minDepth = (float) 0.0f;
1657 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001658 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001659 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001660 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001661 scissor.extent.width = demo->width;
1662 scissor.extent.height = demo->height;
1663 scissor.offset.x = 0;
1664 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001665 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001666
1667 memset(&raster, 0, sizeof(raster));
Tony Barbour155cce82015-07-03 10:33:54 -06001668 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001669 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001670
1671 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour155cce82015-07-03 10:33:54 -06001672 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001673 color_blend.blendConst[0] = 1.0f;
1674 color_blend.blendConst[1] = 1.0f;
1675 color_blend.blendConst[2] = 1.0f;
1676 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001677
1678 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour155cce82015-07-03 10:33:54 -06001679 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001680 depth_stencil.minDepthBounds = 0.0f;
1681 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001682 depth_stencil.stencilBackRef = 0;
1683 depth_stencil.stencilFrontRef = 0;
1684 depth_stencil.stencilReadMask = 0xff;
1685 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001687 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688 assert(!err);
1689
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001690 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691 assert(!err);
1692
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001693 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 &color_blend, &demo->color_blend);
1695 assert(!err);
1696
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001697 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001698 &depth_stencil, &demo->depth_stencil);
1699 assert(!err);
1700}
1701
Chia-I Wu63ea9262015-03-26 13:14:16 +08001702static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001703{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001704 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001705 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001706 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001707 .count = 1,
1708 },
1709 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001710 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001711 .count = DEMO_TEXTURE_COUNT,
1712 },
1713 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001714 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001715 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001716 .pNext = NULL,
1717 .count = 2,
1718 .pTypeCount = type_counts,
1719 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001720 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001721
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001722 err = vkCreateDescriptorPool(demo->device,
1723 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001724 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001725 assert(!err);
1726}
1727
1728static void demo_prepare_descriptor_set(struct demo *demo)
1729{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001730 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1731 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001732 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001733 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001734 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001735
Mike Stroyanebae8322015-04-17 12:36:38 -06001736 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001737 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001738 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001739 &demo->desc_set, &count);
1740 assert(!err && count == 1);
1741
Chia-I Wuae721ba2015-05-25 16:27:55 +08001742 memset(&tex_descs, 0, sizeof(tex_descs));
1743 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1744 tex_descs[i].sampler = demo->textures[i].sampler;
1745 tex_descs[i].imageView = demo->textures[i].view;
1746 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1747 }
1748
1749 memset(&writes, 0, sizeof(writes));
1750
1751 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1752 writes[0].destSet = demo->desc_set;
1753 writes[0].count = 1;
1754 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1755 writes[0].pDescriptors = &demo->uniform_data.desc;
1756
1757 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1758 writes[1].destSet = demo->desc_set;
1759 writes[1].destBinding = 1;
1760 writes[1].count = DEMO_TEXTURE_COUNT;
1761 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1762 writes[1].pDescriptors = tex_descs;
1763
1764 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1765 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001766}
1767
Chia-I Wuf973e322015-07-08 13:34:24 +08001768static void demo_prepare_framebuffers(struct demo *demo)
1769{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001770 VkAttachmentBindInfo attachments[2] = {
1771 [0] = {
Tobin Ehlisd415ac32015-07-06 14:02:36 -06001772 .view.handle = VK_NULL_HANDLE,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001773 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1774 },
1775 [1] = {
1776 .view = demo->depth.view,
1777 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1778 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001779 };
1780 const VkFramebufferCreateInfo fb_info = {
1781 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1782 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001783 .renderPass = demo->render_pass,
1784 .attachmentCount = 2,
1785 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001786 .width = demo->width,
1787 .height = demo->height,
1788 .layers = 1,
1789 };
1790 VkResult U_ASSERT_ONLY err;
1791 uint32_t i;
1792
1793 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001794 attachments[0].view = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001795 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1796 assert(!err);
1797 }
1798}
1799
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001800static void demo_prepare(struct demo *demo)
1801{
Cody Northrop91e221b2015-07-09 18:08:32 -06001802 VkResult U_ASSERT_ONLY err;
1803
1804 const VkCmdPoolCreateInfo cmd_pool_info = {
1805 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1806 .pNext = NULL,
1807 .queueFamilyIndex = demo->graphics_queue_node_index,
1808 .flags = 0,
1809 };
1810 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1811 assert(!err);
1812
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001813 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001814 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001815 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001816 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001817 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001818 .flags = 0,
1819 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820
1821 demo_prepare_buffers(demo);
1822 demo_prepare_depth(demo);
1823 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001824 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001825
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001826 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001827 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001828 demo_prepare_pipeline(demo);
1829 demo_prepare_dynamic_states(demo);
1830
Ian Elliottaaae5352015-07-06 14:27:58 -06001831 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001832 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001833 assert(!err);
1834 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001835
Chia-I Wu63ea9262015-03-26 13:14:16 +08001836 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001837 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001838
Chia-I Wuf973e322015-07-08 13:34:24 +08001839 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001840
Ian Elliottaaae5352015-07-06 14:27:58 -06001841 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001842 demo->current_buffer = i;
1843 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1844 }
1845
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001846 /*
1847 * Prepare functions above may generate pipeline commands
1848 * that need to be flushed before beginning the render loop.
1849 */
1850 demo_flush_init_cmd(demo);
1851
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001852 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001853 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001854}
1855
David Pinedo2bb7c932015-06-18 17:03:14 -06001856static void demo_cleanup(struct demo *demo)
1857{
1858 uint32_t i;
1859
1860 demo->prepared = false;
1861
Tony Barbour155cce82015-07-03 10:33:54 -06001862 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1863 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1864 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001865 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001866 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001867
Tony Barbour155cce82015-07-03 10:33:54 -06001868 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1869 vkDestroyDynamicRasterState(demo->device, demo->raster);
1870 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1871 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001872
Tony Barbour155cce82015-07-03 10:33:54 -06001873 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001874 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001875 vkDestroyRenderPass(demo->device, demo->render_pass);
1876 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1877 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001878
1879 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001880 vkDestroyImageView(demo->device, demo->textures[i].view);
1881 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001882 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001883 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001884 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001885 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001886
Tony Barbour155cce82015-07-03 10:33:54 -06001887 vkDestroyAttachmentView(demo->device, demo->depth.view);
1888 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001889 vkFreeMemory(demo->device, demo->depth.mem);
1890
Tony Barbour155cce82015-07-03 10:33:54 -06001891 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1892 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001893 vkFreeMemory(demo->device, demo->uniform_data.mem);
1894
Ian Elliottaaae5352015-07-06 14:27:58 -06001895 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001896 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1897 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001898 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001899 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001900
Cody Northrop91e221b2015-07-09 18:08:32 -06001901 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001902 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001903 if (demo->validate) {
1904 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1905 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001906 vkDestroyInstance(demo->inst);
1907
1908#ifndef _WIN32
1909 xcb_destroy_window(demo->connection, demo->window);
1910 xcb_disconnect(demo->connection);
1911#endif // _WIN32
1912}
1913
1914// On MS-Windows, make this a global, so it's available to WndProc()
1915struct demo demo;
1916
Ian Elliott639ca472015-04-16 15:23:05 -06001917#ifdef _WIN32
1918static void demo_run(struct demo *demo)
1919{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001920 if (!demo->prepared)
1921 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001922 // Wait for work to finish before updating MVP.
1923 vkDeviceWaitIdle(demo->device);
1924 demo_update_data_buffer(demo);
1925
1926 demo_draw(demo);
1927
1928 // Wait for work to finish before updating MVP.
1929 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001930
David Pinedo2bb7c932015-06-18 17:03:14 -06001931 demo->curFrame++;
1932
1933 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1934 {
1935 demo->quit=true;
1936 demo_cleanup(demo);
1937 ExitProcess(0);
1938 }
1939
1940}
Ian Elliott639ca472015-04-16 15:23:05 -06001941
1942// MS-Windows event handling function:
1943LRESULT CALLBACK WndProc(HWND hWnd,
1944 UINT uMsg,
1945 WPARAM wParam,
1946 LPARAM lParam)
1947{
Ian Elliott639ca472015-04-16 15:23:05 -06001948 switch(uMsg)
1949 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001950 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001951 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001952 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001953 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001954 demo_run(&demo);
1955 return 0;
1956 default:
1957 break;
1958 }
1959 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1960}
1961
1962static void demo_create_window(struct demo *demo)
1963{
1964 WNDCLASSEX win_class;
1965
1966 // Initialize the window class structure:
1967 win_class.cbSize = sizeof(WNDCLASSEX);
1968 win_class.style = CS_HREDRAW | CS_VREDRAW;
1969 win_class.lpfnWndProc = WndProc;
1970 win_class.cbClsExtra = 0;
1971 win_class.cbWndExtra = 0;
1972 win_class.hInstance = demo->connection; // hInstance
1973 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1974 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1975 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1976 win_class.lpszMenuName = NULL;
1977 win_class.lpszClassName = demo->name;
1978 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1979 // Register window class:
1980 if (!RegisterClassEx(&win_class)) {
1981 // It didn't work, so try to give a useful error:
1982 printf("Unexpected error trying to start the application!\n");
1983 fflush(stdout);
1984 exit(1);
1985 }
1986 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001987 RECT wr = { 0, 0, demo->width, demo->height };
1988 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001989 demo->window = CreateWindowEx(0,
1990 demo->name, // class name
1991 demo->name, // app name
1992 WS_OVERLAPPEDWINDOW | // window style
1993 WS_VISIBLE |
1994 WS_SYSMENU,
1995 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001996 wr.right-wr.left, // width
1997 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001998 NULL, // handle to parent
1999 NULL, // handle to menu
2000 demo->connection, // hInstance
2001 NULL); // no extra parameters
2002 if (!demo->window) {
2003 // It didn't work, so try to give a useful error:
2004 printf("Cannot create a window in which to draw!\n");
2005 fflush(stdout);
2006 exit(1);
2007 }
2008}
2009#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010static void demo_handle_event(struct demo *demo,
2011 const xcb_generic_event_t *event)
2012{
Piers Daniell735ee532015-02-23 16:23:13 -07002013 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002014 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002015 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002016 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002018 case XCB_CLIENT_MESSAGE:
2019 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2020 (*demo->atom_wm_delete_window).atom) {
2021 demo->quit = true;
2022 }
2023 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002024 case XCB_KEY_RELEASE:
2025 {
2026 const xcb_key_release_event_t *key =
2027 (const xcb_key_release_event_t *) event;
2028
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002029 switch (key->detail) {
2030 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002032 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002033 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002034 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002035 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002036 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002037 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002038 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002039 case 0x41:
2040 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002041 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002042 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002043 }
2044 break;
2045 default:
2046 break;
2047 }
2048}
2049
2050static void demo_run(struct demo *demo)
2051{
2052 xcb_flush(demo->connection);
2053
2054 while (!demo->quit) {
2055 xcb_generic_event_t *event;
2056
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002057 if (demo->pause) {
2058 event = xcb_wait_for_event(demo->connection);
2059 } else {
2060 event = xcb_poll_for_event(demo->connection);
2061 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002062 if (event) {
2063 demo_handle_event(demo, event);
2064 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002065 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002066
2067 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002068 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002069 demo_update_data_buffer(demo);
2070
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002071 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002072
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002073 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002074 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002075 demo->curFrame++;
2076 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2077 demo->quit = true;
2078
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002079 }
2080}
2081
2082static void demo_create_window(struct demo *demo)
2083{
2084 uint32_t value_mask, value_list[32];
2085
2086 demo->window = xcb_generate_id(demo->connection);
2087
2088 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2089 value_list[0] = demo->screen->black_pixel;
2090 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2091 XCB_EVENT_MASK_EXPOSURE;
2092
2093 xcb_create_window(demo->connection,
2094 XCB_COPY_FROM_PARENT,
2095 demo->window, demo->screen->root,
2096 0, 0, demo->width, demo->height, 0,
2097 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2098 demo->screen->root_visual,
2099 value_mask, value_list);
2100
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002101 /* Magic code that will send notification when window is destroyed */
2102 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2103 "WM_PROTOCOLS");
2104 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2105
2106 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2107 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2108
2109 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2110 demo->window, (*reply).atom, 4, 32, 1,
2111 &(*demo->atom_wm_delete_window).atom);
2112 free(reply);
2113
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002114 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002115
2116 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2117 const uint32_t coords[] = {100, 100};
2118 xcb_configure_window(demo->connection, demo->window,
2119 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002120}
Ian Elliott639ca472015-04-16 15:23:05 -06002121#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002122
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002123/*
2124 * Return 1 (true) if all layer names specified in check_names
2125 * can be found in given layer properties.
2126 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002127static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002128 uint32_t layer_count, VkLayerProperties *layers)
2129{
2130 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002131 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002132 for (uint32_t j = 0; j < layer_count; j++) {
2133 if (!strcmp(check_names[i], layers[j].layerName)) {
2134 found = 1;
2135 }
2136 }
2137 if (!found) {
2138 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2139 return 0;
2140 }
2141 }
2142 return 1;
2143}
2144
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002145static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002146{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002147 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002148 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002149 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002150 VkLayerProperties *instance_layers;
2151 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002152 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002153 uint32_t instance_layer_count = 0;
2154 uint32_t enabled_extension_count = 0;
2155 uint32_t enabled_layer_count = 0;
2156
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002157 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002158 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002159 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002160 "ObjectTracker",
2161 "DrawState",
2162 "ParamChecker",
2163 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002164 };
2165
2166 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002167 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002168 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002169 "ObjectTracker",
2170 "DrawState",
2171 "ParamChecker",
2172 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002173 };
2174
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002175 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002176 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002178 assert(!err);
2179
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002180 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2181 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2182 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002183
2184 if (demo->validate) {
2185 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2186 instance_layer_count, instance_layers);
2187 if (!validation_found) {
2188 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2189 "required validation layer.\n\n"
2190 "Please look at the Getting Started guide for additional "
2191 "information.\n",
2192 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002193 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002194 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002195 }
2196
2197 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2198 assert(!err);
2199
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002200 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002201 memset(extension_names, 0, sizeof(extension_names));
2202 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2203 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2204 assert(!err);
2205 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002206 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002207 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002208 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002209 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002210 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2211 if (demo->validate) {
2212 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2213 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002214 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002215 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002216 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002217 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002218 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002219 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002220 "Vulkan installable client driver (ICD) installed?\nPlease "
2221 "look at the Getting Started guide for additional "
2222 "information.\n",
2223 "vkCreateInstance Failure");
2224 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002225 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002226 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002227 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002228 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002229 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002230 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002231 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002232 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002233 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002234 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002235 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002236 .pNext = NULL,
2237 .pAppInfo = &app,
2238 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002239 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002240 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002241 .extensionCount = enabled_extension_count,
2242 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002243 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002244 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002245 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002246 .queueCount = 1,
2247 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002248
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002249 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002250
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002251 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002252 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2253 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002254 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002255 "additional information.\n",
2256 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002257 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2258 ERR_EXIT("Cannot find a specified extension library"
2259 ".\nMake sure your layers path is set appropriately\n",
2260 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002261 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002262 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2263 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002264 "the Getting Started guide for additional information.\n",
2265 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002266 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002267
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002268 free(instance_layers);
2269 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002270
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002271 gpu_count = 1;
2272 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002273 assert(!err && gpu_count == 1);
2274
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002275 /* Look for validation layers */
2276 validation_found = 0;
2277 enabled_layer_count = 0;
2278 uint32_t device_layer_count = 0;
2279 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2280 assert(!err);
2281
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002282 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2283 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2284 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002285
2286 if (demo->validate) {
2287 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2288 device_layer_count, device_layers);
2289 if (!validation_found) {
2290 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2291 "a required validation layer.\n\n"
2292 "Please look at the Getting Started guide for additional "
2293 "information.\n",
2294 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002295 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002296 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002297 }
2298
2299 uint32_t device_extension_count = 0;
2300 VkExtensionProperties *device_extensions = NULL;
2301 err = vkGetPhysicalDeviceExtensionProperties(
2302 demo->gpu, NULL, &device_extension_count, NULL);
2303 assert(!err);
2304
2305 WSIextFound = 0;
2306 enabled_extension_count = 0;
2307 memset(extension_names, 0, sizeof(extension_names));
2308 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2309 err = vkGetPhysicalDeviceExtensionProperties(
2310 demo->gpu, NULL, &device_extension_count, device_extensions);
2311 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002312
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002313 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002314 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002315 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002316 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002317 }
2318 assert(enabled_extension_count < 64);
2319 }
2320 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002321 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2322 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002323 "Vulkan installable client driver (ICD) installed?\nPlease "
2324 "look at the Getting Started guide for additional "
2325 "information.\n",
2326 "vkCreateInstance Failure");
2327 }
2328
2329 VkDeviceCreateInfo device = {
2330 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2331 .pNext = NULL,
2332 .queueRecordCount = 1,
2333 .pRequestedQueues = &queue,
2334 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002335 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002336 .extensionCount = enabled_extension_count,
2337 .ppEnabledExtensionNames = (const char *const*) extension_names,
2338 .flags = 0,
2339 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002340
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002341 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002342 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2343 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002344 if (!demo->dbgCreateMsgCallback) {
2345 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2346 "vkGetProcAddr Failure");
2347 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002348 if (!demo->dbgDestroyMsgCallback) {
2349 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2350 "vkGetProcAddr Failure");
2351 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002352 err = demo->dbgCreateMsgCallback(
2353 demo->inst,
2354 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2355 dbgFunc, NULL,
2356 &demo->msg_callback);
2357 switch (err) {
2358 case VK_SUCCESS:
2359 break;
2360 case VK_ERROR_INVALID_POINTER:
2361 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2362 "dbgCreateMsgCallback Failure");
2363 break;
2364 case VK_ERROR_OUT_OF_HOST_MEMORY:
2365 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2366 "dbgCreateMsgCallback Failure");
2367 break;
2368 default:
2369 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2370 "dbgCreateMsgCallback Failure");
2371 break;
2372 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002373 }
2374
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002375 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002376 assert(!err);
2377
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002378 free(device_layers);
2379
Ian Elliottaaae5352015-07-06 14:27:58 -06002380 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
2381 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceInfoWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002382 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2383 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2384 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2385 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002386 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002387 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002388
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002389 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002390 assert(!err);
2391
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002392 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &demo->queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002393 assert(!err);
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002394 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002395
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002396 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(demo->queue_count * sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002397 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002398 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002399 assert(demo->queue_count >= 1);
2400}
2401
2402static void demo_init_vk_wsi(struct demo *demo)
2403{
2404 VkResult err;
2405 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002406
Ian Elliottaaae5352015-07-06 14:27:58 -06002407 // Construct the WSI surface description:
2408 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2409 demo->surface_description.pNext = NULL;
2410#ifdef _WIN32
2411 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2412 demo->surface_description.pPlatformHandle = demo->connection;
2413 demo->surface_description.pPlatformWindow = demo->window;
2414#else // _WIN32
2415 demo->platform_handle_xcb.connection = demo->connection;
2416 demo->platform_handle_xcb.root = demo->screen->root;
2417 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2418 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2419 demo->surface_description.pPlatformWindow = &demo->window;
2420#endif // _WIN32
2421
2422 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002423 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2424 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002425 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2426 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2427 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002428 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002429
2430 // Search for a graphics and a present queue in the array of queue
2431 // families, try to find one that supports both
2432 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2433 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002434 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002435 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2436 if (graphicsQueueNodeIndex == UINT32_MAX) {
2437 graphicsQueueNodeIndex = i;
2438 }
2439
2440 if (supportsPresent[i] == VK_TRUE) {
2441 graphicsQueueNodeIndex = i;
2442 presentQueueNodeIndex = i;
2443 break;
2444 }
2445 }
2446 }
2447 if (presentQueueNodeIndex == UINT32_MAX) {
2448 // If didn't find a queue that supports both graphics and present, then
2449 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002450 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002451 if (supportsPresent[i] == VK_TRUE) {
2452 presentQueueNodeIndex = i;
2453 break;
2454 }
2455 }
2456 }
2457 free(supportsPresent);
2458
2459 // Generate error if could not find both a graphics and a present queue
2460 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2461 ERR_EXIT("Could not find a graphics and a present queue\n",
2462 "WSI Initialization Failure");
2463 }
2464
2465 // TODO: Add support for separate queues, including presentation,
2466 // synchronization, and appropriate tracking for QueueSubmit
2467 // While it is possible for an application to use a separate graphics and a
2468 // present queues, this demo program assumes it is only using one:
2469 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2470 ERR_EXIT("Could not find a common graphics and a present queue\n",
2471 "WSI Initialization Failure");
2472 }
2473
2474 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002475
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002476 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002477 0, &demo->queue);
2478 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002479
Ian Elliottaaae5352015-07-06 14:27:58 -06002480 // Get the list of VkFormat's that are supported:
2481 size_t formatsSize;
2482 err = demo->fpGetSurfaceInfoWSI(demo->device,
2483 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2484 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2485 &formatsSize, NULL);
2486 assert(!err);
2487 VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize);
2488 err = demo->fpGetSurfaceInfoWSI(demo->device,
2489 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2490 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2491 &formatsSize, surfFormats);
2492 assert(!err);
2493 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2494 // the surface has no preferred format. Otherwise, at least one
2495 // supported format will be returned.
2496 size_t formatCount = formatsSize / sizeof(VkSurfaceFormatPropertiesWSI);
2497 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2498 {
2499 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2500 }
2501 else
2502 {
2503 assert(formatCount >= 1);
2504 demo->format = surfFormats[0].format;
2505 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06002506
David Pinedo2bb7c932015-06-18 17:03:14 -06002507 demo->quit = false;
2508 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002509
2510 // Get Memory information and properties
2511 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2512 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002513}
2514
2515static void demo_init_connection(struct demo *demo)
2516{
Ian Elliott639ca472015-04-16 15:23:05 -06002517#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002518 const xcb_setup_t *setup;
2519 xcb_screen_iterator_t iter;
2520 int scr;
2521
2522 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002523 if (demo->connection == NULL) {
2524 printf("Cannot find a compatible Vulkan installable client driver "
2525 "(ICD).\nExiting ...\n");
2526 fflush(stdout);
2527 exit(1);
2528 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002529
2530 setup = xcb_get_setup(demo->connection);
2531 iter = xcb_setup_roots_iterator(setup);
2532 while (scr-- > 0)
2533 xcb_screen_next(&iter);
2534
2535 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002536#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002537}
2538
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002539static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002540{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002541 vec3 eye = {0.0f, 3.0f, 5.0f};
2542 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002543 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002544
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002545 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002546 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002547
Piers Daniell735ee532015-02-23 16:23:13 -07002548 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002549 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002550 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002551 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002552 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002553 if (strcmp(argv[i], "--use_glsl") == 0) {
2554 demo->use_glsl = true;
2555 continue;
2556 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002557 if (strcmp(argv[i], "--validate") == 0) {
2558 demo->validate = true;
2559 continue;
2560 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002561 if (strcmp(argv[i], "--c") == 0 &&
2562 demo->frameCount == INT_MAX &&
2563 i < argc-1 &&
2564 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2565 demo->frameCount >= 0)
2566 {
2567 i++;
2568 continue;
2569 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002570
David Pinedo2bb7c932015-06-18 17:03:14 -06002571 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002572 fflush(stderr);
2573 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002574 }
2575
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002576 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002577 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002578
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002579 demo->width = 500;
2580 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002581
2582 demo->spin_angle = 0.01f;
2583 demo->spin_increment = 0.01f;
2584 demo->pause = false;
2585
Piers Daniell735ee532015-02-23 16:23:13 -07002586 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002587 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2588 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002589}
2590
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002591
Ian Elliott639ca472015-04-16 15:23:05 -06002592#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002593extern int __getmainargs(
2594 int * _Argc,
2595 char *** _Argv,
2596 char *** _Env,
2597 int _DoWildCard,
2598 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002599
Ian Elliotta748eaf2015-04-28 15:50:36 -06002600int WINAPI WinMain(HINSTANCE hInstance,
2601 HINSTANCE hPrevInstance,
2602 LPSTR pCmdLine,
2603 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002604{
2605 MSG msg; // message
2606 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002607 int argc;
2608 char** argv;
2609 char** env;
2610 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002611
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002612 __getmainargs(&argc,&argv,&env,0,&new_mode);
2613
2614 demo_init(&demo, argc, argv);
2615 demo.connection = hInstance;
2616 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002617 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002618 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002619
2620 demo_prepare(&demo);
2621
2622 done = false; //initialize loop condition variable
2623 /* main message loop*/
2624 while(!done)
2625 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002626 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002627 if (msg.message == WM_QUIT) //check for a quit message
2628 {
2629 done = true; //if found, quit app
2630 }
2631 else
2632 {
2633 /* Translate and dispatch to event queue*/
2634 TranslateMessage(&msg);
2635 DispatchMessage(&msg);
2636 }
2637 }
2638
2639 demo_cleanup(&demo);
2640
Tony Barbourf9921732015-04-22 11:36:22 -06002641 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002642}
2643#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002644int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002645{
2646 struct demo demo;
2647
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002648 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002649 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002650 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002651
2652 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002653 demo_run(&demo);
2654
2655 demo_cleanup(&demo);
2656
2657 return 0;
2658}
Ian Elliott639ca472015-04-16 15:23:05 -06002659#endif // _WIN32