blob: d8038cfb12b20b1790b4ddd28e2e21532cf575b0 [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 Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600389 bool use_break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600390 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600391 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600392 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600393 VkDbgMsgCallback msg_callback;
394
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600395 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600396 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600397};
398
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600399static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
400{
401 // Search memtypes to find first index with those properties
402 for (uint32_t i = 0; i < 32; i++) {
403 if ((typeBits & 1) == 1) {
404 // Type is available, does it match user properties?
405 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
406 *typeIndex = i;
407 return VK_SUCCESS;
408 }
409 }
410 typeBits >>= 1;
411 }
412 // No memory types matched, return failure
413 return VK_UNSUPPORTED;
414}
415
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600416static void demo_flush_init_cmd(struct demo *demo)
417{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600418 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600420 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421 return;
422
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600423 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424 assert(!err);
425
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600426 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600427 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428
Tony Barbour155cce82015-07-03 10:33:54 -0600429 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430 assert(!err);
431
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600432 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433 assert(!err);
434
Tony Barbour155cce82015-07-03 10:33:54 -0600435 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437}
438
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439static void demo_set_image_layout(
440 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600441 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400442 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600443 VkImageLayout old_image_layout,
444 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600446 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600448 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600450 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600452 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800453 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 .flags = 0,
455 };
456
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 assert(!err);
459
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600460 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600462 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600463 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600466 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600467 }
468
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600469 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600470 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471 .pNext = NULL,
472 .outputMask = 0,
473 .inputMask = 0,
474 .oldLayout = old_image_layout,
475 .newLayout = new_image_layout,
476 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400477 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600478 };
479
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600482 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600483 }
484
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600485 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600486 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600487 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488 }
489
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600490 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491
Tony Barbour50bbca42015-06-29 16:20:35 -0600492 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
493 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494
Courtney Goeltzenleuchter0cab2fb2015-07-12 13:07:46 -0600495 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496}
497
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600498static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499{
Chia-I Wuf973e322015-07-08 13:34:24 +0800500 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600502 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600503 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700505 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800506 const VkClearValue clear_values[2] = {
507 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
508 [1] = { .ds = { 1.0f, 0 } },
509 };
510 const VkRenderPassBeginInfo rp_begin = {
511 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
512 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800513 .renderPass = demo->render_pass,
514 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800515 .renderArea.offset.x = 0,
516 .renderArea.offset.y = 0,
517 .renderArea.extent.width = demo->width,
518 .renderArea.extent.height = demo->height,
519 .attachmentCount = 2,
520 .pAttachmentClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700521 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800522 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525 assert(!err);
526
Chia-I Wua74c5b22015-07-07 11:50:03 +0800527 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
528
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600531 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600532 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533
Tony Barbour155cce82015-07-03 10:33:54 -0600534 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
535 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
536 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
537 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600539 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800540 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600542 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543 assert(!err);
544}
545
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600546
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600547void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600549 mat4x4 MVP, Model, VP;
550 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600551 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600552 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600556 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600557 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700558 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600559 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600560
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500561 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600562 assert(!err);
563
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600564 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500566 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 assert(!err);
568}
569
570static void demo_draw(struct demo *demo)
571{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600572 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600573 VkSemaphore presentCompleteSemaphore;
574 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
575 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
576 .pNext = NULL,
577 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
578 };
Tony Barbour155cce82015-07-03 10:33:54 -0600579 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580
Ian Elliottaaae5352015-07-06 14:27:58 -0600581 err = vkCreateSemaphore(demo->device,
582 &presentCompleteSemaphoreCreateInfo,
583 &presentCompleteSemaphore);
584 assert(!err);
585
586 // Get the index of the next available swapchain image:
587 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
588 UINT64_MAX,
589 presentCompleteSemaphore,
590 &demo->current_buffer);
591 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
592 // return codes
593 assert(!err);
594
595 // Wait for the present complete semaphore to be signaled to ensure
596 // that the image won't be rendered to until the presentation
597 // engine has fully released ownership to the application, and it is
598 // okay to render to the image.
599 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
600
601// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600602 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600603 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604 assert(!err);
605
Ian Elliottaaae5352015-07-06 14:27:58 -0600606 VkPresentInfoWSI present = {
607 .sType = VK_STRUCTURE_TYPE_QUEUE_PRESENT_INFO_WSI,
608 .pNext = NULL,
609 .swapChainCount = 1,
610 .swapChains = &demo->swap_chain,
611 .imageIndices = &demo->current_buffer,
612 };
613
614// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600615 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600616 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
617 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600618 assert(!err);
619
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600620 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600621 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800622
623 err = vkQueueWaitIdle(demo->queue);
624 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625}
626
627static void demo_prepare_buffers(struct demo *demo)
628{
Ian Elliottaaae5352015-07-06 14:27:58 -0600629 VkResult U_ASSERT_ONLY err;
630
631 // Check the surface properties and formats
632 size_t capsSize;
633 size_t presentModesSize;
634 err = demo->fpGetSurfaceInfoWSI(demo->device,
635 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
636 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, NULL);
637 assert(!err);
638 err = demo->fpGetSurfaceInfoWSI(demo->device,
639 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
640 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, NULL);
641 assert(!err);
642
643 VkSurfacePropertiesWSI *surfProperties =
644 (VkSurfacePropertiesWSI *)malloc(capsSize);
645 VkSurfacePresentModePropertiesWSI *presentModes =
646 (VkSurfacePresentModePropertiesWSI *)malloc(presentModesSize);
647
648 err = demo->fpGetSurfaceInfoWSI(demo->device,
649 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
650 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, surfProperties);
651 assert(!err);
652 err = demo->fpGetSurfaceInfoWSI(demo->device,
653 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
654 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, presentModes);
655 assert(!err);
656
657 VkExtent2D swapChainExtent;
658 // width and height are either both -1, or both not -1.
659 if (surfProperties->currentExtent.width == -1)
660 {
661 // If the surface size is undefined, the size is set to
662 // the size of the images requested.
663 swapChainExtent.width = demo->width;
664 swapChainExtent.height = demo->height;
665 }
666 else
667 {
668 // If the surface size is defined, the swap chain size must match
669 swapChainExtent = surfProperties->currentExtent;
670 }
671
672 // If mailbox mode is available, use it, as is the lowest-latency non-
673 // tearing mode. If not, fall back to IMMEDIATE which should always be
674 // available.
675 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
676 size_t presentModeCount = presentModesSize / sizeof(VkSurfacePresentModePropertiesWSI);
677 for (size_t i = 0; i < presentModeCount; i++) {
678 if (presentModes[i].presentMode == VK_PRESENT_MODE_MAILBOX_WSI) {
679 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
680 break;
681 }
682 }
683
Ian Elliott2d47da92015-07-13 12:20:56 -0600684#define WORK_AROUND_CODE
685#ifdef WORK_AROUND_CODE
686 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
687#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600688 // Determine the number of VkImage's to use in the swap chain (we desire to
689 // own only 1 image at a time, besides the images being displayed and
690 // queued for display):
691 uint32_t desiredNumberOfSwapChainImages = surfProperties->minImageCount + 1;
692 if ((surfProperties->maxImageCount > 0) &&
693 (desiredNumberOfSwapChainImages > surfProperties->maxImageCount))
694 {
695 // Application must settle for fewer images than desired:
696 desiredNumberOfSwapChainImages = surfProperties->maxImageCount;
697 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600698#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600699
700 VkSurfaceTransformFlagBitsWSI preTransform;
701 if (surfProperties->supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
702 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
703 } else {
704 preTransform = surfProperties->currentTransform;
705 }
706
Chia-I Wucbb564e2015-04-16 22:02:10 +0800707 const VkSwapChainCreateInfoWSI swap_chain = {
708 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
709 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600710 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
711 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800712 .imageFormat = demo->format,
713 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600714 .width = swapChainExtent.width,
715 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716 },
Ian Elliottaaae5352015-07-06 14:27:58 -0600717 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800718 .imageArraySize = 1,
Ian Elliottaaae5352015-07-06 14:27:58 -0600719 .presentMode = swapChainPresentMode,
720 .oldSwapChain.handle = 0,
721 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600723 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600724
Jon Ashburn0b85d052015-05-21 18:13:33 -0600725 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800726 assert(!err);
727
Ian Elliottaaae5352015-07-06 14:27:58 -0600728 size_t swapChainImagesSize;
729 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
730 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
731 &swapChainImagesSize, NULL);
732 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800733
Ian Elliottaaae5352015-07-06 14:27:58 -0600734 VkSwapChainImagePropertiesWSI* swapChainImages = (VkSwapChainImagePropertiesWSI*)malloc(swapChainImagesSize);
735 assert(swapChainImages);
736 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
737 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
738 &swapChainImagesSize, swapChainImages);
739 assert(!err);
740
Ian Elliott2d47da92015-07-13 12:20:56 -0600741#ifdef WORK_AROUND_CODE
742 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
743#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600744 // The number of images within the swap chain is determined based on the size of the info returned
745 demo->swapChainImageCount = swapChainImagesSize / sizeof(VkSwapChainImagePropertiesWSI);
Ian Elliott2d47da92015-07-13 12:20:56 -0600746#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600747
748 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
749 assert(demo->buffers);
750
751 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800752 VkAttachmentViewCreateInfo color_attachment_view = {
753 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600754 .pNext = NULL,
755 .format = demo->format,
756 .mipLevel = 0,
757 .baseArraySlice = 0,
758 .arraySize = 1,
759 };
760
Ian Elliottaaae5352015-07-06 14:27:58 -0600761 demo->buffers[i].image = swapChainImages[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600762
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600763 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400764 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600765 VK_IMAGE_LAYOUT_UNDEFINED,
766 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600767
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600768 color_attachment_view.image = demo->buffers[i].image;
769
Chia-I Wua74c5b22015-07-07 11:50:03 +0800770 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600771 &color_attachment_view, &demo->buffers[i].view);
772 assert(!err);
773 }
774}
775
776static void demo_prepare_depth(struct demo *demo)
777{
Tony Barbour72304ef2015-04-16 15:59:00 -0600778 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600779 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600780 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600782 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600783 .format = depth_format,
784 .extent = { demo->width, demo->height, 1 },
785 .mipLevels = 1,
786 .arraySize = 1,
787 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600788 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600789 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600790 .flags = 0,
791 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600792 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600793 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500794 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600795 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600796 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600797 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800798 VkAttachmentViewCreateInfo view = {
799 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600800 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600801 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600802 .format = depth_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600803 .mipLevel = 0,
804 .baseArraySlice = 0,
805 .arraySize = 1,
806 .flags = 0,
807 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600808
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500809 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600810 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600811
812 demo->depth.format = depth_format;
813
814 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600815 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600816 &demo->depth.image);
817 assert(!err);
818
Tony Barbour155cce82015-07-03 10:33:54 -0600819 err = vkGetImageMemoryRequirements(demo->device,
820 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600821
822 mem_alloc.allocationSize = mem_reqs.size;
823 err = memory_type_from_properties(demo,
824 mem_reqs.memoryTypeBits,
825 VK_MEMORY_PROPERTY_DEVICE_ONLY,
826 &mem_alloc.memoryTypeIndex);
827 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600828
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500829 /* allocate memory */
830 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
831 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600832
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500833 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600834 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500835 demo->depth.mem, 0);
836 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600837
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600838 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400839 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600840 VK_IMAGE_LAYOUT_UNDEFINED,
841 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600842
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600843 /* create image view */
844 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800845 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600846 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847}
848
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600849/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600850 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600851 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600852 * \param demo : Needed to access VK calls
853 * \param filename : the png file to be loaded
854 * \param width : width of png, to be updated as a side effect of this function
855 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600856 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600857 * \return bool : an opengl texture id. true if successful?,
858 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600859 *
860 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
861 * Modified to copy image to memory
862 *
863 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700864bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600865 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600866 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600867{
868 //header for testing if it is a png
869 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600870 int is_png, bit_depth, color_type, rowbytes;
871 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700872 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600873 png_structp png_ptr;
874 png_infop info_ptr, end_info;
875 png_byte *image_data;
876 png_bytep *row_pointers;
877
878 //open file as binary
879 FILE *fp = fopen(filename, "rb");
880 if (!fp) {
881 return false;
882 }
883
884 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600885 retval = fread(header, 1, 8, fp);
886 if (retval != 8) {
887 fclose(fp);
888 return false;
889 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600890
891 //test if png
892 is_png = !png_sig_cmp(header, 0, 8);
893 if (!is_png) {
894 fclose(fp);
895 return false;
896 }
897
898 //create png struct
899 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
900 NULL, NULL);
901 if (!png_ptr) {
902 fclose(fp);
903 return (false);
904 }
905
906 //create png info struct
907 info_ptr = png_create_info_struct(png_ptr);
908 if (!info_ptr) {
909 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
910 fclose(fp);
911 return (false);
912 }
913
914 //create png info struct
915 end_info = png_create_info_struct(png_ptr);
916 if (!end_info) {
917 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
918 fclose(fp);
919 return (false);
920 }
921
922 //png error stuff, not sure libpng man suggests this.
923 if (setjmp(png_jmpbuf(png_ptr))) {
924 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
925 fclose(fp);
926 return (false);
927 }
928
929 //init png reading
930 png_init_io(png_ptr, fp);
931
932 //let libpng know you already read the first 8 bytes
933 png_set_sig_bytes(png_ptr, 8);
934
935 // read all the info up to the image data
936 png_read_info(png_ptr, info_ptr);
937
938 // get info about png
939 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
940 NULL, NULL, NULL);
941
942 //update width and height based on png info
943 *width = twidth;
944 *height = theight;
945
946 // Require that incoming texture be 8bits per color component
947 // and 4 components (RGBA).
948 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
949 png_get_channels(png_ptr, info_ptr) != 4) {
950 return false;
951 }
952
953 if (rgba_data == NULL) {
954 // If data pointer is null, we just want the width & height
955 // clean up memory and close stuff
956 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
957 fclose(fp);
958
959 return true;
960 }
961
962 // Update the png info struct.
963 png_read_update_info(png_ptr, info_ptr);
964
965 // Row size in bytes.
966 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
967
968 // Allocate the image_data as a big block, to be given to opengl
969 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
970 if (!image_data) {
971 //clean up memory and close stuff
972 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
973 fclose(fp);
974 return false;
975 }
976
977 // row_pointers is for pointing to image_data for reading the png with libpng
978 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
979 if (!row_pointers) {
980 //clean up memory and close stuff
981 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
982 // delete[] image_data;
983 fclose(fp);
984 return false;
985 }
986 // set the individual row_pointers to point at the correct offsets of image_data
987 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700988 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600989
990 // read the png into image_data through row_pointers
991 png_read_image(png_ptr, row_pointers);
992
993 // clean up memory and close stuff
994 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
995 free(row_pointers);
996 free(image_data);
997 fclose(fp);
998
999 return true;
1000}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001001
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002static void demo_prepare_texture_image(struct demo *demo,
1003 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001004 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001005 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001006 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001007 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001008{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001009 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001010 int32_t tex_width;
1011 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001012 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001013
David Pinedo30bd71d2015-04-23 08:16:57 -06001014 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1015 {
1016 printf("Failed to load textures\n");
1017 fflush(stdout);
1018 exit(1);
1019 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001020
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001021 tex_obj->tex_width = tex_width;
1022 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001024 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001025 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001027 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028 .format = tex_format,
1029 .extent = { tex_width, tex_height, 1 },
1030 .mipLevels = 1,
1031 .arraySize = 1,
1032 .samples = 1,
1033 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001034 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035 .flags = 0,
1036 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001037 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001038 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001039 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001041 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001042 };
1043
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001044 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001045
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001047 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048 assert(!err);
1049
Tony Barbour155cce82015-07-03 10:33:54 -06001050 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001051 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001052
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001053 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001054
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001055 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1056 assert(!err);
1057
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001058 /* allocate memory */
1059 err = vkAllocMemory(demo->device, &mem_alloc,
1060 &(tex_obj->mem));
1061 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001062
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001063 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001064 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001065 tex_obj->mem, 0);
1066 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067
Tony Barbour72304ef2015-04-16 15:59:00 -06001068 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001069 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 .mipLevel = 0,
1072 .arraySlice = 0,
1073 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001074 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075 void *data;
1076
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001077 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1078 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001080 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081 assert(!err);
1082
1083 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1084 fprintf(stderr, "Error loading texture: %s\n", filename);
1085 }
1086
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001087 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001088 assert(!err);
1089 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001090
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001091 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001092 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001093 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001094 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001095 tex_obj->imageLayout);
1096 /* 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 -07001097}
1098
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001099static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001100{
1101 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001103 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001104}
1105
1106static void demo_prepare_textures(struct demo *demo)
1107{
Tony Barbour72304ef2015-04-16 15:59:00 -06001108 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001109 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001110 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001111 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001112
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001113 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001114 assert(!err);
1115
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001116 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117
FslNopper6a7e50e2015-05-06 21:42:01 +02001118 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 /* Device can texture using linear textures */
1120 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001121 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001122 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001124 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125
1126 memset(&staging_texture, 0, sizeof(staging_texture));
1127 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001128 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129
1130 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001131 VK_IMAGE_TILING_OPTIMAL,
1132 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1133 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001135 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001136 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001137 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001138 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001140 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001141 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001142 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001143 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001145 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001146 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001147 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001148 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001149 .destOffset = { 0, 0, 0 },
1150 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1151 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001152 vkCmdCopyImage(demo->cmd,
1153 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1154 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001155 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001156
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001157 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001158 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001159 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001160 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001161
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001162 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001164 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001166 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1167 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001168 }
1169
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001170 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001171 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001172 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001173 .magFilter = VK_TEX_FILTER_NEAREST,
1174 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001175 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001176 .addressU = VK_TEX_ADDRESS_CLAMP,
1177 .addressV = VK_TEX_ADDRESS_CLAMP,
1178 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001179 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001180 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001181 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001182 .minLod = 0.0f,
1183 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001184 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001185 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001186
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001187 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001188 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001189 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001190 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001191 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001192 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001193 .channels = { VK_CHANNEL_SWIZZLE_R,
1194 VK_CHANNEL_SWIZZLE_G,
1195 VK_CHANNEL_SWIZZLE_B,
1196 VK_CHANNEL_SWIZZLE_A, },
1197 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001199
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001201 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001202 &demo->textures[i].sampler);
1203 assert(!err);
1204
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001205 /* create image view */
1206 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001207 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001208 &demo->textures[i].view);
1209 assert(!err);
1210 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001211}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001213void demo_prepare_cube_data_buffer(struct demo *demo)
1214{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001215 VkBufferCreateInfo buf_info;
1216 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001217 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001218 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001219 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001220 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001221 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001222 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001223 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001224 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001225 int i;
1226 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001227 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001228 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001229
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001230 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001231 mat4x4_mul(MVP, VP, demo->model_matrix);
1232 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001233// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001234
1235 for (i=0; i<12*3; i++) {
1236 data.position[i][0] = g_vertex_buffer_data[i*3];
1237 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1238 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1239 data.position[i][3] = 1.0f;
1240 data.attr[i][0] = g_uv_buffer_data[2*i];
1241 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1242 data.attr[i][2] = 0;
1243 data.attr[i][3] = 0;
1244 }
1245
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001246 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001247 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001248 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001249 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001250 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001251 assert(!err);
1252
Tony Barbour155cce82015-07-03 10:33:54 -06001253 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001254 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001255
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001256 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001257 err = memory_type_from_properties(demo,
1258 mem_reqs.memoryTypeBits,
1259 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1260 &alloc_info.memoryTypeIndex);
1261 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001262
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001263 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1264 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001265
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001266 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1267 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001268
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001269 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001270
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001271 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1272 assert(!err);
1273
Tony Barbour155cce82015-07-03 10:33:54 -06001274 err = vkBindBufferMemory(demo->device,
1275 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001276 demo->uniform_data.mem, 0);
1277 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001278
1279 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001280 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001281 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001282 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001283 view_info.offset = 0;
1284 view_info.range = sizeof(data);
1285
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001286 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001287 assert(!err);
1288
Chia-I Wuae721ba2015-05-25 16:27:55 +08001289 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001290}
1291
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001292static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001294 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001295 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001296 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001297 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001298 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001299 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001300 },
1301 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001302 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001303 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001304 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001305 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001306 },
1307 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001308 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001309 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001310 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001311 .count = 2,
1312 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001313 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001314 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001316 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001317 &descriptor_layout, &demo->desc_layout);
1318 assert(!err);
1319
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001320 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1321 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1322 .pNext = NULL,
1323 .descriptorSetCount = 1,
1324 .pSetLayouts = &demo->desc_layout,
1325 };
1326
1327 err = vkCreatePipelineLayout(demo->device,
1328 &pPipelineLayoutCreateInfo,
1329 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001330 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001331}
1332
Chia-I Wuf973e322015-07-08 13:34:24 +08001333static void demo_prepare_render_pass(struct demo *demo)
1334{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001335 const VkAttachmentDescription attachments[2] = {
1336 [0] = {
1337 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1338 .pNext = NULL,
1339 .format = demo->format,
1340 .samples = 1,
1341 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1342 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1343 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1344 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1345 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1346 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1347 },
1348 [1] = {
1349 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1350 .pNext = NULL,
1351 .format = demo->depth.format,
1352 .samples = 1,
1353 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1354 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1355 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1356 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1357 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1358 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1359 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001360 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001361 const VkAttachmentReference color_reference = {
1362 .attachment = 0,
1363 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1364 };
1365 const VkSubpassDescription subpass = {
1366 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1367 .pNext = NULL,
1368 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1369 .flags = 0,
1370 .inputCount = 0,
1371 .inputAttachments = NULL,
1372 .colorCount = 1,
1373 .colorAttachments = &color_reference,
1374 .resolveAttachments = NULL,
1375 .depthStencilAttachment = {
1376 .attachment = 1,
1377 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1378 },
1379 .preserveCount = 0,
1380 .preserveAttachments = NULL,
1381 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001382 const VkRenderPassCreateInfo rp_info = {
1383 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1384 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001385 .attachmentCount = 2,
1386 .pAttachments = attachments,
1387 .subpassCount = 1,
1388 .pSubpasses = &subpass,
1389 .dependencyCount = 0,
1390 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001391 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001392 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001393
1394 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1395 assert(!err);
1396}
1397
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001398static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001399 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001400 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001401 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001402 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001404 VkShaderModuleCreateInfo moduleCreateInfo;
1405 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001406 VkShader shader;
1407 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001409
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001410 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1411 moduleCreateInfo.pNext = NULL;
1412
1413 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1414 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001415 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416
Cody Northrop1fedb212015-05-28 11:27:16 -06001417 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001418 moduleCreateInfo.codeSize = size;
1419 moduleCreateInfo.pCode = code;
1420 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001421 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001422 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001423 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001424 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001425
1426 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001427 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001428 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001429 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001430 } else {
1431 // Create fake SPV structure to feed GLSL
1432 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001433 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1434 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1435 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001436
1437 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001438 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1439 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1440 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1441 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001442
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001443 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001444 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001445 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001446 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001447
1448 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001449 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001450 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001451 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001452 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001453 return shader;
1454}
1455
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001456char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001457{
1458 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001459 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001460 void *shader_code;
1461
1462 FILE *fp = fopen(filename, "rb");
1463 if (!fp) return NULL;
1464
1465 fseek(fp, 0L, SEEK_END);
1466 size = ftell(fp);
1467
1468 fseek(fp, 0L, SEEK_SET);
1469
1470 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001471 retval = fread(shader_code, size, 1, fp);
1472 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001473
1474 *psize = size;
1475
1476 return shader_code;
1477}
1478
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001479static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001480{
Cody Northrop1fedb212015-05-28 11:27:16 -06001481 if (!demo->use_glsl) {
1482 void *vertShaderCode;
1483 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001484
Cody Northrop1fedb212015-05-28 11:27:16 -06001485 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001486
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001487 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001488 vertShaderCode, size);
1489 } else {
1490 static const char *vertShaderText =
1491 "#version 140\n"
1492 "#extension GL_ARB_separate_shader_objects : enable\n"
1493 "#extension GL_ARB_shading_language_420pack : enable\n"
1494 "\n"
1495 "layout(binding = 0) uniform buf {\n"
1496 " mat4 MVP;\n"
1497 " vec4 position[12*3];\n"
1498 " vec4 attr[12*3];\n"
1499 "} ubuf;\n"
1500 "\n"
1501 "layout (location = 0) out vec4 texcoord;\n"
1502 "\n"
1503 "void main() \n"
1504 "{\n"
1505 " texcoord = ubuf.attr[gl_VertexID];\n"
1506 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1507 "\n"
1508 " // GL->VK conventions\n"
1509 " gl_Position.y = -gl_Position.y;\n"
1510 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1511 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001512
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001513 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001514 (const void *) vertShaderText,
1515 strlen(vertShaderText));
1516 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001517}
1518
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001519static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520{
Cody Northrop1fedb212015-05-28 11:27:16 -06001521 if (!demo->use_glsl) {
1522 void *fragShaderCode;
1523 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001524
Cody Northrop1fedb212015-05-28 11:27:16 -06001525 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001526
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001527 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001528 fragShaderCode, size);
1529 } else {
1530 static const char *fragShaderText =
1531 "#version 140\n"
1532 "#extension GL_ARB_separate_shader_objects : enable\n"
1533 "#extension GL_ARB_shading_language_420pack : enable\n"
1534 "layout (binding = 1) uniform sampler2D tex;\n"
1535 "\n"
1536 "layout (location = 0) in vec4 texcoord;\n"
1537 "layout (location = 0) out vec4 uFragColor;\n"
1538 "void main() {\n"
1539 " uFragColor = texture(tex, texcoord.xy);\n"
1540 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001541
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001542 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001543 (const void *) fragShaderText,
1544 strlen(fragShaderText));
1545 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001546}
1547
1548static void demo_prepare_pipeline(struct demo *demo)
1549{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001550 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001551 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001552 VkPipelineInputAssemblyStateCreateInfo ia;
1553 VkPipelineRasterStateCreateInfo rs;
1554 VkPipelineColorBlendStateCreateInfo cb;
1555 VkPipelineDepthStencilStateCreateInfo ds;
1556 VkPipelineViewportStateCreateInfo vp;
1557 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001558 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001559
1560 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001561 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001562 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001564 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001565 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001566 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567
1568 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001569 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001570 rs.fillMode = VK_FILL_MODE_SOLID;
1571 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001572 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001573 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574
1575 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001576 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1577 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001578 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001579 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001580 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001581 cb.attachmentCount = 1;
1582 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583
Tony Barbour29645d02015-01-16 14:27:35 -07001584 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001585 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001586 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001587
1588 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001589 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001590 ds.depthTestEnable = VK_TRUE;
1591 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001592 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001593 ds.depthBoundsEnable = VK_FALSE;
1594 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1595 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001596 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001597 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001598 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001599
Tony Barbour29645d02015-01-16 14:27:35 -07001600 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001601 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001602 ms.sampleMask = 1;
Tony Barbour3ca22502015-06-26 10:18:34 -06001603 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001604
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001605 // Two stages: vs and fs
1606 pipeline.stageCount = 2;
1607 VkPipelineShaderStageCreateInfo shaderStages[2];
1608 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1609
1610 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1611 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1612 shaderStages[0].shader = demo_prepare_vs(demo);
1613
1614 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1615 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1616 shaderStages[1].shader = demo_prepare_fs(demo);
1617
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001618 memset(&pipelineCache, 0, sizeof(pipelineCache));
1619 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001620
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001621 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1622 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001623
1624 pipeline.pVertexInputState = NULL;
1625 pipeline.pInputAssemblyState = &ia;
1626 pipeline.pRasterState = &rs;
1627 pipeline.pColorBlendState = &cb;
1628 pipeline.pMultisampleState = &ms;
1629 pipeline.pViewportState = &vp;
1630 pipeline.pDepthStencilState = &ds;
1631 pipeline.pStages = shaderStages;
1632
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001633 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001634 assert(!err);
1635
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001636 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001637 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001638 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001639 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1640 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001641}
1642
1643static void demo_prepare_dynamic_states(struct demo *demo)
1644{
Tony Barbour155cce82015-07-03 10:33:54 -06001645 VkDynamicViewportStateCreateInfo viewport_create;
1646 VkDynamicRasterStateCreateInfo raster;
1647 VkDynamicColorBlendStateCreateInfo color_blend;
1648 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001649 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001650
Tony Barbour29645d02015-01-16 14:27:35 -07001651 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001652 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001653 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001654 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001655 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001656 viewport.height = (float) demo->height;
1657 viewport.width = (float) demo->width;
1658 viewport.minDepth = (float) 0.0f;
1659 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001660 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001661 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001662 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001663 scissor.extent.width = demo->width;
1664 scissor.extent.height = demo->height;
1665 scissor.offset.x = 0;
1666 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001667 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001668
1669 memset(&raster, 0, sizeof(raster));
Tony Barbour155cce82015-07-03 10:33:54 -06001670 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001671 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001672
1673 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour155cce82015-07-03 10:33:54 -06001674 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001675 color_blend.blendConst[0] = 1.0f;
1676 color_blend.blendConst[1] = 1.0f;
1677 color_blend.blendConst[2] = 1.0f;
1678 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001679
1680 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour155cce82015-07-03 10:33:54 -06001681 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001682 depth_stencil.minDepthBounds = 0.0f;
1683 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001684 depth_stencil.stencilBackRef = 0;
1685 depth_stencil.stencilFrontRef = 0;
1686 depth_stencil.stencilReadMask = 0xff;
1687 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001689 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690 assert(!err);
1691
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001692 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001693 assert(!err);
1694
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001695 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001696 &color_blend, &demo->color_blend);
1697 assert(!err);
1698
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001699 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700 &depth_stencil, &demo->depth_stencil);
1701 assert(!err);
1702}
1703
Chia-I Wu63ea9262015-03-26 13:14:16 +08001704static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001705{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001706 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001707 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001708 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001709 .count = 1,
1710 },
1711 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001712 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001713 .count = DEMO_TEXTURE_COUNT,
1714 },
1715 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001716 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001717 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001718 .pNext = NULL,
1719 .count = 2,
1720 .pTypeCount = type_counts,
1721 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001722 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001723
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001724 err = vkCreateDescriptorPool(demo->device,
1725 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001726 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001727 assert(!err);
1728}
1729
1730static void demo_prepare_descriptor_set(struct demo *demo)
1731{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001732 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1733 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001734 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001735 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001736 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001737
Mike Stroyanebae8322015-04-17 12:36:38 -06001738 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001739 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001740 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001741 &demo->desc_set, &count);
1742 assert(!err && count == 1);
1743
Chia-I Wuae721ba2015-05-25 16:27:55 +08001744 memset(&tex_descs, 0, sizeof(tex_descs));
1745 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1746 tex_descs[i].sampler = demo->textures[i].sampler;
1747 tex_descs[i].imageView = demo->textures[i].view;
1748 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1749 }
1750
1751 memset(&writes, 0, sizeof(writes));
1752
1753 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1754 writes[0].destSet = demo->desc_set;
1755 writes[0].count = 1;
1756 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1757 writes[0].pDescriptors = &demo->uniform_data.desc;
1758
1759 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1760 writes[1].destSet = demo->desc_set;
1761 writes[1].destBinding = 1;
1762 writes[1].count = DEMO_TEXTURE_COUNT;
1763 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1764 writes[1].pDescriptors = tex_descs;
1765
1766 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1767 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001768}
1769
Chia-I Wuf973e322015-07-08 13:34:24 +08001770static void demo_prepare_framebuffers(struct demo *demo)
1771{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001772 VkAttachmentBindInfo attachments[2] = {
1773 [0] = {
Tobin Ehlisd415ac32015-07-06 14:02:36 -06001774 .view.handle = VK_NULL_HANDLE,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001775 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1776 },
1777 [1] = {
1778 .view = demo->depth.view,
1779 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1780 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001781 };
1782 const VkFramebufferCreateInfo fb_info = {
1783 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1784 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001785 .renderPass = demo->render_pass,
1786 .attachmentCount = 2,
1787 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001788 .width = demo->width,
1789 .height = demo->height,
1790 .layers = 1,
1791 };
1792 VkResult U_ASSERT_ONLY err;
1793 uint32_t i;
1794
1795 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001796 attachments[0].view = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001797 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1798 assert(!err);
1799 }
1800}
1801
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001802static void demo_prepare(struct demo *demo)
1803{
Cody Northrop91e221b2015-07-09 18:08:32 -06001804 VkResult U_ASSERT_ONLY err;
1805
1806 const VkCmdPoolCreateInfo cmd_pool_info = {
1807 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1808 .pNext = NULL,
1809 .queueFamilyIndex = demo->graphics_queue_node_index,
1810 .flags = 0,
1811 };
1812 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1813 assert(!err);
1814
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001815 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001816 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001818 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001819 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820 .flags = 0,
1821 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822
1823 demo_prepare_buffers(demo);
1824 demo_prepare_depth(demo);
1825 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001826 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001828 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001829 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830 demo_prepare_pipeline(demo);
1831 demo_prepare_dynamic_states(demo);
1832
Ian Elliottaaae5352015-07-06 14:27:58 -06001833 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001834 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001835 assert(!err);
1836 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001837
Chia-I Wu63ea9262015-03-26 13:14:16 +08001838 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001839 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001840
Chia-I Wuf973e322015-07-08 13:34:24 +08001841 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001842
Ian Elliottaaae5352015-07-06 14:27:58 -06001843 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001844 demo->current_buffer = i;
1845 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1846 }
1847
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001848 /*
1849 * Prepare functions above may generate pipeline commands
1850 * that need to be flushed before beginning the render loop.
1851 */
1852 demo_flush_init_cmd(demo);
1853
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001854 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001855 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856}
1857
David Pinedo2bb7c932015-06-18 17:03:14 -06001858static void demo_cleanup(struct demo *demo)
1859{
1860 uint32_t i;
1861
1862 demo->prepared = false;
1863
Tony Barbour155cce82015-07-03 10:33:54 -06001864 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1865 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1866 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001867 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001868 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001869
Tony Barbour155cce82015-07-03 10:33:54 -06001870 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1871 vkDestroyDynamicRasterState(demo->device, demo->raster);
1872 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1873 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001874
Tony Barbour155cce82015-07-03 10:33:54 -06001875 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001876 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001877 vkDestroyRenderPass(demo->device, demo->render_pass);
1878 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1879 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001880
1881 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001882 vkDestroyImageView(demo->device, demo->textures[i].view);
1883 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001884 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001885 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001886 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001887 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001888
Tony Barbour155cce82015-07-03 10:33:54 -06001889 vkDestroyAttachmentView(demo->device, demo->depth.view);
1890 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001891 vkFreeMemory(demo->device, demo->depth.mem);
1892
Tony Barbour155cce82015-07-03 10:33:54 -06001893 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1894 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001895 vkFreeMemory(demo->device, demo->uniform_data.mem);
1896
Ian Elliottaaae5352015-07-06 14:27:58 -06001897 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001898 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1899 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001900 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001901 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001902
Cody Northrop91e221b2015-07-09 18:08:32 -06001903 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001904 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001905 if (demo->validate) {
1906 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1907 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001908 vkDestroyInstance(demo->inst);
1909
1910#ifndef _WIN32
1911 xcb_destroy_window(demo->connection, demo->window);
1912 xcb_disconnect(demo->connection);
1913#endif // _WIN32
1914}
1915
1916// On MS-Windows, make this a global, so it's available to WndProc()
1917struct demo demo;
1918
Ian Elliott639ca472015-04-16 15:23:05 -06001919#ifdef _WIN32
1920static void demo_run(struct demo *demo)
1921{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001922 if (!demo->prepared)
1923 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001924 // Wait for work to finish before updating MVP.
1925 vkDeviceWaitIdle(demo->device);
1926 demo_update_data_buffer(demo);
1927
1928 demo_draw(demo);
1929
1930 // Wait for work to finish before updating MVP.
1931 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001932
David Pinedo2bb7c932015-06-18 17:03:14 -06001933 demo->curFrame++;
1934
1935 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1936 {
1937 demo->quit=true;
1938 demo_cleanup(demo);
1939 ExitProcess(0);
1940 }
1941
1942}
Ian Elliott639ca472015-04-16 15:23:05 -06001943
1944// MS-Windows event handling function:
1945LRESULT CALLBACK WndProc(HWND hWnd,
1946 UINT uMsg,
1947 WPARAM wParam,
1948 LPARAM lParam)
1949{
Ian Elliott639ca472015-04-16 15:23:05 -06001950 switch(uMsg)
1951 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001952 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001953 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001954 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001955 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001956 demo_run(&demo);
1957 return 0;
1958 default:
1959 break;
1960 }
1961 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1962}
1963
1964static void demo_create_window(struct demo *demo)
1965{
1966 WNDCLASSEX win_class;
1967
1968 // Initialize the window class structure:
1969 win_class.cbSize = sizeof(WNDCLASSEX);
1970 win_class.style = CS_HREDRAW | CS_VREDRAW;
1971 win_class.lpfnWndProc = WndProc;
1972 win_class.cbClsExtra = 0;
1973 win_class.cbWndExtra = 0;
1974 win_class.hInstance = demo->connection; // hInstance
1975 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1976 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1977 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1978 win_class.lpszMenuName = NULL;
1979 win_class.lpszClassName = demo->name;
1980 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1981 // Register window class:
1982 if (!RegisterClassEx(&win_class)) {
1983 // It didn't work, so try to give a useful error:
1984 printf("Unexpected error trying to start the application!\n");
1985 fflush(stdout);
1986 exit(1);
1987 }
1988 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001989 RECT wr = { 0, 0, demo->width, demo->height };
1990 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001991 demo->window = CreateWindowEx(0,
1992 demo->name, // class name
1993 demo->name, // app name
1994 WS_OVERLAPPEDWINDOW | // window style
1995 WS_VISIBLE |
1996 WS_SYSMENU,
1997 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001998 wr.right-wr.left, // width
1999 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06002000 NULL, // handle to parent
2001 NULL, // handle to menu
2002 demo->connection, // hInstance
2003 NULL); // no extra parameters
2004 if (!demo->window) {
2005 // It didn't work, so try to give a useful error:
2006 printf("Cannot create a window in which to draw!\n");
2007 fflush(stdout);
2008 exit(1);
2009 }
2010}
2011#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002012static void demo_handle_event(struct demo *demo,
2013 const xcb_generic_event_t *event)
2014{
Piers Daniell735ee532015-02-23 16:23:13 -07002015 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002016 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002018 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002019 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002020 case XCB_CLIENT_MESSAGE:
2021 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2022 (*demo->atom_wm_delete_window).atom) {
2023 demo->quit = true;
2024 }
2025 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002026 case XCB_KEY_RELEASE:
2027 {
2028 const xcb_key_release_event_t *key =
2029 (const xcb_key_release_event_t *) event;
2030
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002031 switch (key->detail) {
2032 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002033 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002034 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002035 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002036 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002037 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002038 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002039 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002040 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002041 case 0x41:
2042 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002043 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002044 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002045 }
2046 break;
2047 default:
2048 break;
2049 }
2050}
2051
2052static void demo_run(struct demo *demo)
2053{
2054 xcb_flush(demo->connection);
2055
2056 while (!demo->quit) {
2057 xcb_generic_event_t *event;
2058
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002059 if (demo->pause) {
2060 event = xcb_wait_for_event(demo->connection);
2061 } else {
2062 event = xcb_poll_for_event(demo->connection);
2063 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064 if (event) {
2065 demo_handle_event(demo, event);
2066 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002067 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002068
2069 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002070 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002071 demo_update_data_buffer(demo);
2072
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002073 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002074
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002075 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002076 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002077 demo->curFrame++;
2078 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2079 demo->quit = true;
2080
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002081 }
2082}
2083
2084static void demo_create_window(struct demo *demo)
2085{
2086 uint32_t value_mask, value_list[32];
2087
2088 demo->window = xcb_generate_id(demo->connection);
2089
2090 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2091 value_list[0] = demo->screen->black_pixel;
2092 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2093 XCB_EVENT_MASK_EXPOSURE;
2094
2095 xcb_create_window(demo->connection,
2096 XCB_COPY_FROM_PARENT,
2097 demo->window, demo->screen->root,
2098 0, 0, demo->width, demo->height, 0,
2099 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2100 demo->screen->root_visual,
2101 value_mask, value_list);
2102
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002103 /* Magic code that will send notification when window is destroyed */
2104 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2105 "WM_PROTOCOLS");
2106 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2107
2108 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2109 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2110
2111 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2112 demo->window, (*reply).atom, 4, 32, 1,
2113 &(*demo->atom_wm_delete_window).atom);
2114 free(reply);
2115
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002116 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002117
2118 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2119 const uint32_t coords[] = {100, 100};
2120 xcb_configure_window(demo->connection, demo->window,
2121 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002122}
Ian Elliott639ca472015-04-16 15:23:05 -06002123#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002124
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002125/*
2126 * Return 1 (true) if all layer names specified in check_names
2127 * can be found in given layer properties.
2128 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002129static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002130 uint32_t layer_count, VkLayerProperties *layers)
2131{
2132 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002133 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002134 for (uint32_t j = 0; j < layer_count; j++) {
2135 if (!strcmp(check_names[i], layers[j].layerName)) {
2136 found = 1;
2137 }
2138 }
2139 if (!found) {
2140 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2141 return 0;
2142 }
2143 }
2144 return 1;
2145}
2146
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002147static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002148{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002149 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002150 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002151 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002152 VkLayerProperties *instance_layers;
2153 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002154 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002155 uint32_t instance_layer_count = 0;
2156 uint32_t enabled_extension_count = 0;
2157 uint32_t enabled_layer_count = 0;
2158
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002159 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002160 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002161 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002162 "ObjectTracker",
2163 "DrawState",
2164 "ParamChecker",
2165 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002166 };
2167
2168 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002169 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002170 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002171 "ObjectTracker",
2172 "DrawState",
2173 "ParamChecker",
2174 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002175 };
2176
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002178 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002179 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002180 assert(!err);
2181
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002182 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2183 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2184 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002185
2186 if (demo->validate) {
2187 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2188 instance_layer_count, instance_layers);
2189 if (!validation_found) {
2190 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2191 "required validation layer.\n\n"
2192 "Please look at the Getting Started guide for additional "
2193 "information.\n",
2194 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002195 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002196 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002197 }
2198
2199 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2200 assert(!err);
2201
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002202 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002203 memset(extension_names, 0, sizeof(extension_names));
2204 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2205 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2206 assert(!err);
2207 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002208 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002209 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002210 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002211 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002212 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2213 if (demo->validate) {
2214 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2215 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002216 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002217 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002218 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002219 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002220 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002221 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002222 "Vulkan installable client driver (ICD) installed?\nPlease "
2223 "look at the Getting Started guide for additional "
2224 "information.\n",
2225 "vkCreateInstance Failure");
2226 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002227 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002228 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002229 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002230 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002231 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002232 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002233 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002234 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002235 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002236 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002237 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002238 .pNext = NULL,
2239 .pAppInfo = &app,
2240 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002241 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002242 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002243 .extensionCount = enabled_extension_count,
2244 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002245 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002246 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002247 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002248 .queueCount = 1,
2249 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002250
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002251 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002252
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002253 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002254 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2255 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002256 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002257 "additional information.\n",
2258 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002259 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2260 ERR_EXIT("Cannot find a specified extension library"
2261 ".\nMake sure your layers path is set appropriately\n",
2262 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002263 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002264 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2265 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002266 "the Getting Started guide for additional information.\n",
2267 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002268 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002269
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002270 free(instance_layers);
2271 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002272
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002273 gpu_count = 1;
2274 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002275 assert(!err && gpu_count == 1);
2276
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002277 /* Look for validation layers */
2278 validation_found = 0;
2279 enabled_layer_count = 0;
2280 uint32_t device_layer_count = 0;
2281 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2282 assert(!err);
2283
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002284 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2285 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2286 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002287
2288 if (demo->validate) {
2289 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2290 device_layer_count, device_layers);
2291 if (!validation_found) {
2292 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2293 "a required validation layer.\n\n"
2294 "Please look at the Getting Started guide for additional "
2295 "information.\n",
2296 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002297 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002298 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002299 }
2300
2301 uint32_t device_extension_count = 0;
2302 VkExtensionProperties *device_extensions = NULL;
2303 err = vkGetPhysicalDeviceExtensionProperties(
2304 demo->gpu, NULL, &device_extension_count, NULL);
2305 assert(!err);
2306
2307 WSIextFound = 0;
2308 enabled_extension_count = 0;
2309 memset(extension_names, 0, sizeof(extension_names));
2310 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2311 err = vkGetPhysicalDeviceExtensionProperties(
2312 demo->gpu, NULL, &device_extension_count, device_extensions);
2313 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002314
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002315 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002316 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002317 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002318 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002319 }
2320 assert(enabled_extension_count < 64);
2321 }
2322 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002323 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2324 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002325 "Vulkan installable client driver (ICD) installed?\nPlease "
2326 "look at the Getting Started guide for additional "
2327 "information.\n",
2328 "vkCreateInstance Failure");
2329 }
2330
2331 VkDeviceCreateInfo device = {
2332 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2333 .pNext = NULL,
2334 .queueRecordCount = 1,
2335 .pRequestedQueues = &queue,
2336 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002337 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002338 .extensionCount = enabled_extension_count,
2339 .ppEnabledExtensionNames = (const char *const*) extension_names,
2340 .flags = 0,
2341 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002342
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002343 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002344 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2345 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002346 if (!demo->dbgCreateMsgCallback) {
2347 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2348 "vkGetProcAddr Failure");
2349 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002350 if (!demo->dbgDestroyMsgCallback) {
2351 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2352 "vkGetProcAddr Failure");
2353 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002354 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2355 if (!demo->dbgBreakCallback) {
2356 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2357 "vkGetProcAddr Failure");
2358 }
2359
2360 PFN_vkDbgMsgCallback callback;
2361
2362 if (!demo->use_break) {
2363 callback = dbgFunc;
2364 } else {
2365 callback = demo->dbgBreakCallback;
2366 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002367 err = demo->dbgCreateMsgCallback(
2368 demo->inst,
2369 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002370 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002371 &demo->msg_callback);
2372 switch (err) {
2373 case VK_SUCCESS:
2374 break;
2375 case VK_ERROR_INVALID_POINTER:
2376 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2377 "dbgCreateMsgCallback Failure");
2378 break;
2379 case VK_ERROR_OUT_OF_HOST_MEMORY:
2380 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2381 "dbgCreateMsgCallback Failure");
2382 break;
2383 default:
2384 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2385 "dbgCreateMsgCallback Failure");
2386 break;
2387 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002388 }
2389
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002390 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002391 assert(!err);
2392
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002393 free(device_layers);
2394
Ian Elliottaaae5352015-07-06 14:27:58 -06002395 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
2396 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceInfoWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002397 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2398 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2399 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2400 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002401 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002402 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002403
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002404 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002405 assert(!err);
2406
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002407 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &demo->queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002408 assert(!err);
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002409 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002410
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002411 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(demo->queue_count * sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002412 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002413 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002414 assert(demo->queue_count >= 1);
2415}
2416
2417static void demo_init_vk_wsi(struct demo *demo)
2418{
2419 VkResult err;
2420 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002421
Ian Elliottaaae5352015-07-06 14:27:58 -06002422 // Construct the WSI surface description:
2423 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2424 demo->surface_description.pNext = NULL;
2425#ifdef _WIN32
2426 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2427 demo->surface_description.pPlatformHandle = demo->connection;
2428 demo->surface_description.pPlatformWindow = demo->window;
2429#else // _WIN32
2430 demo->platform_handle_xcb.connection = demo->connection;
2431 demo->platform_handle_xcb.root = demo->screen->root;
2432 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2433 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2434 demo->surface_description.pPlatformWindow = &demo->window;
2435#endif // _WIN32
2436
2437 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002438 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2439 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002440 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2441 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2442 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002443 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002444
2445 // Search for a graphics and a present queue in the array of queue
2446 // families, try to find one that supports both
2447 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2448 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002449 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002450 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2451 if (graphicsQueueNodeIndex == UINT32_MAX) {
2452 graphicsQueueNodeIndex = i;
2453 }
2454
2455 if (supportsPresent[i] == VK_TRUE) {
2456 graphicsQueueNodeIndex = i;
2457 presentQueueNodeIndex = i;
2458 break;
2459 }
2460 }
2461 }
2462 if (presentQueueNodeIndex == UINT32_MAX) {
2463 // If didn't find a queue that supports both graphics and present, then
2464 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002465 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002466 if (supportsPresent[i] == VK_TRUE) {
2467 presentQueueNodeIndex = i;
2468 break;
2469 }
2470 }
2471 }
2472 free(supportsPresent);
2473
2474 // Generate error if could not find both a graphics and a present queue
2475 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2476 ERR_EXIT("Could not find a graphics and a present queue\n",
2477 "WSI Initialization Failure");
2478 }
2479
2480 // TODO: Add support for separate queues, including presentation,
2481 // synchronization, and appropriate tracking for QueueSubmit
2482 // While it is possible for an application to use a separate graphics and a
2483 // present queues, this demo program assumes it is only using one:
2484 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2485 ERR_EXIT("Could not find a common graphics and a present queue\n",
2486 "WSI Initialization Failure");
2487 }
2488
2489 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002490
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002491 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002492 0, &demo->queue);
2493 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002494
Ian Elliottaaae5352015-07-06 14:27:58 -06002495 // Get the list of VkFormat's that are supported:
2496 size_t formatsSize;
2497 err = demo->fpGetSurfaceInfoWSI(demo->device,
2498 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2499 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2500 &formatsSize, NULL);
2501 assert(!err);
2502 VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize);
2503 err = demo->fpGetSurfaceInfoWSI(demo->device,
2504 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2505 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2506 &formatsSize, surfFormats);
2507 assert(!err);
2508 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2509 // the surface has no preferred format. Otherwise, at least one
2510 // supported format will be returned.
2511 size_t formatCount = formatsSize / sizeof(VkSurfaceFormatPropertiesWSI);
2512 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2513 {
2514 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2515 }
2516 else
2517 {
2518 assert(formatCount >= 1);
2519 demo->format = surfFormats[0].format;
2520 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06002521
David Pinedo2bb7c932015-06-18 17:03:14 -06002522 demo->quit = false;
2523 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002524
2525 // Get Memory information and properties
2526 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2527 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002528}
2529
2530static void demo_init_connection(struct demo *demo)
2531{
Ian Elliott639ca472015-04-16 15:23:05 -06002532#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002533 const xcb_setup_t *setup;
2534 xcb_screen_iterator_t iter;
2535 int scr;
2536
2537 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002538 if (demo->connection == NULL) {
2539 printf("Cannot find a compatible Vulkan installable client driver "
2540 "(ICD).\nExiting ...\n");
2541 fflush(stdout);
2542 exit(1);
2543 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002544
2545 setup = xcb_get_setup(demo->connection);
2546 iter = xcb_setup_roots_iterator(setup);
2547 while (scr-- > 0)
2548 xcb_screen_next(&iter);
2549
2550 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002551#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002552}
2553
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002554static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002555{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002556 vec3 eye = {0.0f, 3.0f, 5.0f};
2557 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002558 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002559
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002560 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002561 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002562
Piers Daniell735ee532015-02-23 16:23:13 -07002563 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002564 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002565 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002566 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002567 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002568 if (strcmp(argv[i], "--use_glsl") == 0) {
2569 demo->use_glsl = true;
2570 continue;
2571 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002572 if (strcmp(argv[i], "--break") == 0) {
2573 demo->use_break = true;
2574 continue;
2575 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002576 if (strcmp(argv[i], "--validate") == 0) {
2577 demo->validate = true;
2578 continue;
2579 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002580 if (strcmp(argv[i], "--c") == 0 &&
2581 demo->frameCount == INT_MAX &&
2582 i < argc-1 &&
2583 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2584 demo->frameCount >= 0)
2585 {
2586 i++;
2587 continue;
2588 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002589
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002590 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002591 fflush(stderr);
2592 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002593 }
2594
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002595 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002596 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002597
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002598 demo->width = 500;
2599 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002600
2601 demo->spin_angle = 0.01f;
2602 demo->spin_increment = 0.01f;
2603 demo->pause = false;
2604
Piers Daniell735ee532015-02-23 16:23:13 -07002605 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002606 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2607 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002608}
2609
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002610
Ian Elliott639ca472015-04-16 15:23:05 -06002611#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002612extern int __getmainargs(
2613 int * _Argc,
2614 char *** _Argv,
2615 char *** _Env,
2616 int _DoWildCard,
2617 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002618
Ian Elliotta748eaf2015-04-28 15:50:36 -06002619int WINAPI WinMain(HINSTANCE hInstance,
2620 HINSTANCE hPrevInstance,
2621 LPSTR pCmdLine,
2622 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002623{
2624 MSG msg; // message
2625 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002626 int argc;
2627 char** argv;
2628 char** env;
2629 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002630
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002631 __getmainargs(&argc,&argv,&env,0,&new_mode);
2632
2633 demo_init(&demo, argc, argv);
2634 demo.connection = hInstance;
2635 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002636 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002637 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002638
2639 demo_prepare(&demo);
2640
2641 done = false; //initialize loop condition variable
2642 /* main message loop*/
2643 while(!done)
2644 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002645 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002646 if (msg.message == WM_QUIT) //check for a quit message
2647 {
2648 done = true; //if found, quit app
2649 }
2650 else
2651 {
2652 /* Translate and dispatch to event queue*/
2653 TranslateMessage(&msg);
2654 DispatchMessage(&msg);
2655 }
2656 }
2657
2658 demo_cleanup(&demo);
2659
Tony Barbourf9921732015-04-22 11:36:22 -06002660 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002661}
2662#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002663int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002664{
2665 struct demo demo;
2666
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002667 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002668 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002669 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002670
2671 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002672 demo_run(&demo);
2673
2674 demo_cleanup(&demo);
2675
2676 return 0;
2677}
Ian Elliott639ca472015-04-16 15:23:05 -06002678#endif // _WIN32