blob: 5ec968324dde1aaac1cc24c5a4a81f96d0d8bde7 [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,
Courtney Goeltzenleuchtera8df1c02015-07-28 08:59:17 -0600503 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700504 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800505 const VkClearValue clear_values[2] = {
506 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
507 [1] = { .ds = { 1.0f, 0 } },
508 };
509 const VkRenderPassBeginInfo rp_begin = {
510 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
511 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800512 .renderPass = demo->render_pass,
513 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800514 .renderArea.offset.x = 0,
515 .renderArea.offset.y = 0,
516 .renderArea.extent.width = demo->width,
517 .renderArea.extent.height = demo->height,
518 .attachmentCount = 2,
519 .pAttachmentClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700520 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800521 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600523 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600524 assert(!err);
525
Chia-I Wua74c5b22015-07-07 11:50:03 +0800526 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
527
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600528 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600530 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600531 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600532
Tony Barbour155cce82015-07-03 10:33:54 -0600533 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
534 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
535 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
536 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600538 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800539 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600540
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542 assert(!err);
543}
544
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600545
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600546void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600548 mat4x4 MVP, Model, VP;
549 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600550 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600551 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600553 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600554
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600555 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600556 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700557 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600558 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600559
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500560 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561 assert(!err);
562
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600563 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500565 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566 assert(!err);
567}
568
569static void demo_draw(struct demo *demo)
570{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600571 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600572 VkSemaphore presentCompleteSemaphore;
573 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
574 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
575 .pNext = NULL,
576 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
577 };
Tony Barbour155cce82015-07-03 10:33:54 -0600578 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579
Ian Elliottaaae5352015-07-06 14:27:58 -0600580 err = vkCreateSemaphore(demo->device,
581 &presentCompleteSemaphoreCreateInfo,
582 &presentCompleteSemaphore);
583 assert(!err);
584
585 // Get the index of the next available swapchain image:
586 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
587 UINT64_MAX,
588 presentCompleteSemaphore,
589 &demo->current_buffer);
590 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
591 // return codes
592 assert(!err);
593
594 // Wait for the present complete semaphore to be signaled to ensure
595 // that the image won't be rendered to until the presentation
596 // engine has fully released ownership to the application, and it is
597 // okay to render to the image.
598 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
599
600// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600601 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600602 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603 assert(!err);
604
Ian Elliottaaae5352015-07-06 14:27:58 -0600605 VkPresentInfoWSI present = {
606 .sType = VK_STRUCTURE_TYPE_QUEUE_PRESENT_INFO_WSI,
607 .pNext = NULL,
608 .swapChainCount = 1,
609 .swapChains = &demo->swap_chain,
610 .imageIndices = &demo->current_buffer,
611 };
612
613// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburn0b85d052015-05-21 18:13:33 -0600614 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliottaaae5352015-07-06 14:27:58 -0600615 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
616 // return codes
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600617 assert(!err);
618
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600619 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600620 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800621
622 err = vkQueueWaitIdle(demo->queue);
623 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624}
625
626static void demo_prepare_buffers(struct demo *demo)
627{
Ian Elliottaaae5352015-07-06 14:27:58 -0600628 VkResult U_ASSERT_ONLY err;
629
630 // Check the surface properties and formats
631 size_t capsSize;
632 size_t presentModesSize;
633 err = demo->fpGetSurfaceInfoWSI(demo->device,
634 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
635 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, NULL);
636 assert(!err);
637 err = demo->fpGetSurfaceInfoWSI(demo->device,
638 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
639 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, NULL);
640 assert(!err);
641
642 VkSurfacePropertiesWSI *surfProperties =
643 (VkSurfacePropertiesWSI *)malloc(capsSize);
644 VkSurfacePresentModePropertiesWSI *presentModes =
645 (VkSurfacePresentModePropertiesWSI *)malloc(presentModesSize);
646
647 err = demo->fpGetSurfaceInfoWSI(demo->device,
648 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
649 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, surfProperties);
650 assert(!err);
651 err = demo->fpGetSurfaceInfoWSI(demo->device,
652 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
653 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, presentModes);
654 assert(!err);
655
656 VkExtent2D swapChainExtent;
657 // width and height are either both -1, or both not -1.
658 if (surfProperties->currentExtent.width == -1)
659 {
660 // If the surface size is undefined, the size is set to
661 // the size of the images requested.
662 swapChainExtent.width = demo->width;
663 swapChainExtent.height = demo->height;
664 }
665 else
666 {
667 // If the surface size is defined, the swap chain size must match
668 swapChainExtent = surfProperties->currentExtent;
669 }
670
671 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600672 // tearing mode. If not, try IMMEDIATE which will usually be available,
673 // and is fastest (though it tears). If not, fall back to FIFO which is
674 // always available.
675 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliottaaae5352015-07-06 14:27:58 -0600676 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 }
Ian Elliott3ebce342015-07-27 13:53:11 -0600682 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
683 (presentModes[i].presentMode == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
684 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
685 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600686 }
687
Ian Elliott2d47da92015-07-13 12:20:56 -0600688#define WORK_AROUND_CODE
689#ifdef WORK_AROUND_CODE
690 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
691#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600692 // Determine the number of VkImage's to use in the swap chain (we desire to
693 // own only 1 image at a time, besides the images being displayed and
694 // queued for display):
695 uint32_t desiredNumberOfSwapChainImages = surfProperties->minImageCount + 1;
696 if ((surfProperties->maxImageCount > 0) &&
697 (desiredNumberOfSwapChainImages > surfProperties->maxImageCount))
698 {
699 // Application must settle for fewer images than desired:
700 desiredNumberOfSwapChainImages = surfProperties->maxImageCount;
701 }
Ian Elliott2d47da92015-07-13 12:20:56 -0600702#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600703
704 VkSurfaceTransformFlagBitsWSI preTransform;
705 if (surfProperties->supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
706 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
707 } else {
708 preTransform = surfProperties->currentTransform;
709 }
710
Chia-I Wucbb564e2015-04-16 22:02:10 +0800711 const VkSwapChainCreateInfoWSI swap_chain = {
712 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
713 .pNext = NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600714 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
715 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800716 .imageFormat = demo->format,
717 .imageExtent = {
Ian Elliottaaae5352015-07-06 14:27:58 -0600718 .width = swapChainExtent.width,
719 .height = swapChainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720 },
Ian Elliottaaae5352015-07-06 14:27:58 -0600721 .preTransform = preTransform,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800722 .imageArraySize = 1,
Ian Elliottaaae5352015-07-06 14:27:58 -0600723 .presentMode = swapChainPresentMode,
724 .oldSwapChain.handle = 0,
725 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600726 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600727 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600728
Jon Ashburn0b85d052015-05-21 18:13:33 -0600729 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800730 assert(!err);
731
Ian Elliottaaae5352015-07-06 14:27:58 -0600732 size_t swapChainImagesSize;
733 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
734 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
735 &swapChainImagesSize, NULL);
736 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800737
Ian Elliottaaae5352015-07-06 14:27:58 -0600738 VkSwapChainImagePropertiesWSI* swapChainImages = (VkSwapChainImagePropertiesWSI*)malloc(swapChainImagesSize);
739 assert(swapChainImages);
740 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
741 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
742 &swapChainImagesSize, swapChainImages);
743 assert(!err);
744
Ian Elliott2d47da92015-07-13 12:20:56 -0600745#ifdef WORK_AROUND_CODE
746 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
747#else // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600748 // The number of images within the swap chain is determined based on the size of the info returned
749 demo->swapChainImageCount = swapChainImagesSize / sizeof(VkSwapChainImagePropertiesWSI);
Ian Elliott2d47da92015-07-13 12:20:56 -0600750#endif // WORK_AROUND_CODE
Ian Elliottaaae5352015-07-06 14:27:58 -0600751
752 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
753 assert(demo->buffers);
754
755 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800756 VkAttachmentViewCreateInfo color_attachment_view = {
757 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600758 .pNext = NULL,
759 .format = demo->format,
760 .mipLevel = 0,
761 .baseArraySlice = 0,
762 .arraySize = 1,
763 };
764
Ian Elliottaaae5352015-07-06 14:27:58 -0600765 demo->buffers[i].image = swapChainImages[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600766
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600767 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400768 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600769 VK_IMAGE_LAYOUT_UNDEFINED,
770 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600771
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600772 color_attachment_view.image = demo->buffers[i].image;
773
Chia-I Wua74c5b22015-07-07 11:50:03 +0800774 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600775 &color_attachment_view, &demo->buffers[i].view);
776 assert(!err);
777 }
778}
779
780static void demo_prepare_depth(struct demo *demo)
781{
Tony Barbour72304ef2015-04-16 15:59:00 -0600782 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600783 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600784 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600785 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600786 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600787 .format = depth_format,
788 .extent = { demo->width, demo->height, 1 },
789 .mipLevels = 1,
790 .arraySize = 1,
791 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600792 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600793 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600794 .flags = 0,
795 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600796 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600797 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500798 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600799 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600800 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800802 VkAttachmentViewCreateInfo view = {
803 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600804 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600805 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600806 .format = depth_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600807 .mipLevel = 0,
808 .baseArraySlice = 0,
809 .arraySize = 1,
810 .flags = 0,
811 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600812
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500813 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600814 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600815
816 demo->depth.format = depth_format;
817
818 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600819 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600820 &demo->depth.image);
821 assert(!err);
822
Tony Barbour155cce82015-07-03 10:33:54 -0600823 err = vkGetImageMemoryRequirements(demo->device,
824 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600825
826 mem_alloc.allocationSize = mem_reqs.size;
827 err = memory_type_from_properties(demo,
828 mem_reqs.memoryTypeBits,
829 VK_MEMORY_PROPERTY_DEVICE_ONLY,
830 &mem_alloc.memoryTypeIndex);
831 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600832
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500833 /* allocate memory */
834 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
835 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500837 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600838 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500839 demo->depth.mem, 0);
840 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600841
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600842 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400843 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600844 VK_IMAGE_LAYOUT_UNDEFINED,
845 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600846
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847 /* create image view */
848 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800849 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600850 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851}
852
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600853/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600854 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600855 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600856 * \param demo : Needed to access VK calls
857 * \param filename : the png file to be loaded
858 * \param width : width of png, to be updated as a side effect of this function
859 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600860 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600861 * \return bool : an opengl texture id. true if successful?,
862 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600863 *
864 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
865 * Modified to copy image to memory
866 *
867 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700868bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600869 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600870 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600871{
872 //header for testing if it is a png
873 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600874 int is_png, bit_depth, color_type, rowbytes;
875 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700876 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600877 png_structp png_ptr;
878 png_infop info_ptr, end_info;
879 png_byte *image_data;
880 png_bytep *row_pointers;
881
882 //open file as binary
883 FILE *fp = fopen(filename, "rb");
884 if (!fp) {
885 return false;
886 }
887
888 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600889 retval = fread(header, 1, 8, fp);
890 if (retval != 8) {
891 fclose(fp);
892 return false;
893 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600894
895 //test if png
896 is_png = !png_sig_cmp(header, 0, 8);
897 if (!is_png) {
898 fclose(fp);
899 return false;
900 }
901
902 //create png struct
903 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
904 NULL, NULL);
905 if (!png_ptr) {
906 fclose(fp);
907 return (false);
908 }
909
910 //create png info struct
911 info_ptr = png_create_info_struct(png_ptr);
912 if (!info_ptr) {
913 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
914 fclose(fp);
915 return (false);
916 }
917
918 //create png info struct
919 end_info = png_create_info_struct(png_ptr);
920 if (!end_info) {
921 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
922 fclose(fp);
923 return (false);
924 }
925
926 //png error stuff, not sure libpng man suggests this.
927 if (setjmp(png_jmpbuf(png_ptr))) {
928 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
929 fclose(fp);
930 return (false);
931 }
932
933 //init png reading
934 png_init_io(png_ptr, fp);
935
936 //let libpng know you already read the first 8 bytes
937 png_set_sig_bytes(png_ptr, 8);
938
939 // read all the info up to the image data
940 png_read_info(png_ptr, info_ptr);
941
942 // get info about png
943 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
944 NULL, NULL, NULL);
945
946 //update width and height based on png info
947 *width = twidth;
948 *height = theight;
949
950 // Require that incoming texture be 8bits per color component
951 // and 4 components (RGBA).
952 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
953 png_get_channels(png_ptr, info_ptr) != 4) {
954 return false;
955 }
956
957 if (rgba_data == NULL) {
958 // If data pointer is null, we just want the width & height
959 // clean up memory and close stuff
960 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
961 fclose(fp);
962
963 return true;
964 }
965
966 // Update the png info struct.
967 png_read_update_info(png_ptr, info_ptr);
968
969 // Row size in bytes.
970 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
971
972 // Allocate the image_data as a big block, to be given to opengl
973 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
974 if (!image_data) {
975 //clean up memory and close stuff
976 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
977 fclose(fp);
978 return false;
979 }
980
981 // row_pointers is for pointing to image_data for reading the png with libpng
982 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
983 if (!row_pointers) {
984 //clean up memory and close stuff
985 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
986 // delete[] image_data;
987 fclose(fp);
988 return false;
989 }
990 // set the individual row_pointers to point at the correct offsets of image_data
991 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600993
994 // read the png into image_data through row_pointers
995 png_read_image(png_ptr, row_pointers);
996
997 // clean up memory and close stuff
998 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
999 free(row_pointers);
1000 free(image_data);
1001 fclose(fp);
1002
1003 return true;
1004}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001005
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006static void demo_prepare_texture_image(struct demo *demo,
1007 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001008 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001009 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001010 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001011 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001012{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001013 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001014 int32_t tex_width;
1015 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001016 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017
David Pinedo30bd71d2015-04-23 08:16:57 -06001018 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1019 {
1020 printf("Failed to load textures\n");
1021 fflush(stdout);
1022 exit(1);
1023 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001025 tex_obj->tex_width = tex_width;
1026 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001027
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001028 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001029 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001031 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032 .format = tex_format,
1033 .extent = { tex_width, tex_height, 1 },
1034 .mipLevels = 1,
1035 .arraySize = 1,
1036 .samples = 1,
1037 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001038 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001039 .flags = 0,
1040 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001041 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001042 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001043 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001044 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001045 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001046 };
1047
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001048 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001049
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001050 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001051 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052 assert(!err);
1053
Tony Barbour155cce82015-07-03 10:33:54 -06001054 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001055 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -07001056
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001057 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001058
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001059 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1060 assert(!err);
1061
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001062 /* allocate memory */
1063 err = vkAllocMemory(demo->device, &mem_alloc,
1064 &(tex_obj->mem));
1065 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001067 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001068 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001069 tex_obj->mem, 0);
1070 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071
Tony Barbour72304ef2015-04-16 15:59:00 -06001072 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001073 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001074 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075 .mipLevel = 0,
1076 .arraySlice = 0,
1077 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001078 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079 void *data;
1080
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001081 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1082 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001084 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085 assert(!err);
1086
1087 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1088 fprintf(stderr, "Error loading texture: %s\n", filename);
1089 }
1090
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001091 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001092 assert(!err);
1093 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001094
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001095 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001096 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -04001097 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001098 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001099 tex_obj->imageLayout);
1100 /* 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 -07001101}
1102
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001103static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001104{
1105 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001106 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001107 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001108}
1109
1110static void demo_prepare_textures(struct demo *demo)
1111{
Tony Barbour72304ef2015-04-16 15:59:00 -06001112 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001113 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001114 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001115 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001116
Courtney Goeltzenleuchter4a593f12015-07-12 12:52:09 -06001117 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118 assert(!err);
1119
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001120 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121
FslNopper6a7e50e2015-05-06 21:42:01 +02001122 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123 /* Device can texture using linear textures */
1124 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001125 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001126 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001128 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129
1130 memset(&staging_texture, 0, sizeof(staging_texture));
1131 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001132 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133
1134 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001135 VK_IMAGE_TILING_OPTIMAL,
1136 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1137 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001139 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001140 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001141 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001142 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001144 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001145 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001146 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001147 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001149 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001150 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001152 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001153 .destOffset = { 0, 0, 0 },
1154 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1155 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001156 vkCmdCopyImage(demo->cmd,
1157 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1158 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001159 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001161 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001162 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001163 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001164 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001166 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001168 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001169 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001170 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1171 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172 }
1173
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001174 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001175 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001176 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001177 .magFilter = VK_TEX_FILTER_NEAREST,
1178 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001179 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001180 .addressU = VK_TEX_ADDRESS_CLAMP,
1181 .addressV = VK_TEX_ADDRESS_CLAMP,
1182 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001183 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001185 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186 .minLod = 0.0f,
1187 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001188 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001189 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001190
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001191 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001192 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001193 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001194 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001195 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001196 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001197 .channels = { VK_CHANNEL_SWIZZLE_R,
1198 VK_CHANNEL_SWIZZLE_G,
1199 VK_CHANNEL_SWIZZLE_B,
1200 VK_CHANNEL_SWIZZLE_A, },
1201 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001202 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001203
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001204 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001205 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 &demo->textures[i].sampler);
1207 assert(!err);
1208
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209 /* create image view */
1210 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001211 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212 &demo->textures[i].view);
1213 assert(!err);
1214 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001215}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001216
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001217void demo_prepare_cube_data_buffer(struct demo *demo)
1218{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001219 VkBufferCreateInfo buf_info;
1220 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001221 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001222 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001223 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001224 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001225 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001226 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001227 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001228 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001229 int i;
1230 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001231 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001232 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001233
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001234 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001235 mat4x4_mul(MVP, VP, demo->model_matrix);
1236 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001237// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001238
1239 for (i=0; i<12*3; i++) {
1240 data.position[i][0] = g_vertex_buffer_data[i*3];
1241 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1242 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1243 data.position[i][3] = 1.0f;
1244 data.attr[i][0] = g_uv_buffer_data[2*i];
1245 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1246 data.attr[i][2] = 0;
1247 data.attr[i][3] = 0;
1248 }
1249
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001250 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001251 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001252 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001253 buf_info.size = sizeof(data);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001254 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001255 assert(!err);
1256
Tony Barbour155cce82015-07-03 10:33:54 -06001257 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchter0a82c0b2015-07-15 11:17:08 -06001258 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001259
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001260 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001261 err = memory_type_from_properties(demo,
1262 mem_reqs.memoryTypeBits,
1263 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1264 &alloc_info.memoryTypeIndex);
1265 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001266
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001267 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1268 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001269
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001270 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1271 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001272
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001273 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001274
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001275 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1276 assert(!err);
1277
Tony Barbour155cce82015-07-03 10:33:54 -06001278 err = vkBindBufferMemory(demo->device,
1279 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001280 demo->uniform_data.mem, 0);
1281 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001282
1283 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001284 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001285 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001286 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001287 view_info.offset = 0;
1288 view_info.range = sizeof(data);
1289
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001290 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001291 assert(!err);
1292
Chia-I Wuae721ba2015-05-25 16:27:55 +08001293 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001294}
1295
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001296static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001297{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001298 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001299 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001300 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001301 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001302 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001303 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001304 },
1305 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001306 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001307 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001308 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001309 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001310 },
1311 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001312 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001313 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001314 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001315 .count = 2,
1316 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001317 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001318 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001319
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001320 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001321 &descriptor_layout, &demo->desc_layout);
1322 assert(!err);
1323
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001324 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1325 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1326 .pNext = NULL,
1327 .descriptorSetCount = 1,
1328 .pSetLayouts = &demo->desc_layout,
1329 };
1330
1331 err = vkCreatePipelineLayout(demo->device,
1332 &pPipelineLayoutCreateInfo,
1333 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335}
1336
Chia-I Wuf973e322015-07-08 13:34:24 +08001337static void demo_prepare_render_pass(struct demo *demo)
1338{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001339 const VkAttachmentDescription attachments[2] = {
1340 [0] = {
1341 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1342 .pNext = NULL,
1343 .format = demo->format,
1344 .samples = 1,
1345 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1346 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1347 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1348 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1349 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1350 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1351 },
1352 [1] = {
1353 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1354 .pNext = NULL,
1355 .format = demo->depth.format,
1356 .samples = 1,
1357 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1358 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1359 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1360 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1361 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1362 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1363 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001364 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001365 const VkAttachmentReference color_reference = {
1366 .attachment = 0,
1367 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1368 };
1369 const VkSubpassDescription subpass = {
1370 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1371 .pNext = NULL,
1372 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1373 .flags = 0,
1374 .inputCount = 0,
1375 .inputAttachments = NULL,
1376 .colorCount = 1,
1377 .colorAttachments = &color_reference,
1378 .resolveAttachments = NULL,
1379 .depthStencilAttachment = {
1380 .attachment = 1,
1381 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1382 },
1383 .preserveCount = 0,
1384 .preserveAttachments = NULL,
1385 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001386 const VkRenderPassCreateInfo rp_info = {
1387 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1388 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001389 .attachmentCount = 2,
1390 .pAttachments = attachments,
1391 .subpassCount = 1,
1392 .pSubpasses = &subpass,
1393 .dependencyCount = 0,
1394 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001395 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001396 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001397
1398 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1399 assert(!err);
1400}
1401
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001402static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001403 VkShaderStage stage,
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001404 VkShaderModule* pShaderModule,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001405 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001406 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001408 VkShaderModuleCreateInfo moduleCreateInfo;
1409 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001410 VkShader shader;
1411 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001413
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001414 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1415 moduleCreateInfo.pNext = NULL;
1416
1417 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1418 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001419 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001420
Cody Northrop1fedb212015-05-28 11:27:16 -06001421 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001422 moduleCreateInfo.codeSize = size;
1423 moduleCreateInfo.pCode = code;
1424 moduleCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001425 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001426 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001427 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001428 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001429
1430 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001431 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001432 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001433 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001434 } else {
1435 // Create fake SPV structure to feed GLSL
1436 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001437 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1438 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1439 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001440
1441 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001442 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1443 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1444 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1445 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001446
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001447 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001448 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001449 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001450 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001451
1452 shaderCreateInfo.flags = 0;
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001453 shaderCreateInfo.module = *pShaderModule;
Piers Daniell8611f132015-07-16 09:35:35 -06001454 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001455 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001456 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457 return shader;
1458}
1459
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001460char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001461{
1462 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001463 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001464 void *shader_code;
1465
1466 FILE *fp = fopen(filename, "rb");
1467 if (!fp) return NULL;
1468
1469 fseek(fp, 0L, SEEK_END);
1470 size = ftell(fp);
1471
1472 fseek(fp, 0L, SEEK_SET);
1473
1474 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001475 retval = fread(shader_code, size, 1, fp);
1476 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001477
1478 *psize = size;
1479
1480 return shader_code;
1481}
1482
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001483static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001484{
Cody Northrop1fedb212015-05-28 11:27:16 -06001485 if (!demo->use_glsl) {
1486 void *vertShaderCode;
1487 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001488
Cody Northrop1fedb212015-05-28 11:27:16 -06001489 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001490
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001491 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001492 vertShaderCode, size);
1493 } else {
1494 static const char *vertShaderText =
1495 "#version 140\n"
1496 "#extension GL_ARB_separate_shader_objects : enable\n"
1497 "#extension GL_ARB_shading_language_420pack : enable\n"
1498 "\n"
1499 "layout(binding = 0) uniform buf {\n"
1500 " mat4 MVP;\n"
1501 " vec4 position[12*3];\n"
1502 " vec4 attr[12*3];\n"
1503 "} ubuf;\n"
1504 "\n"
1505 "layout (location = 0) out vec4 texcoord;\n"
1506 "\n"
1507 "void main() \n"
1508 "{\n"
1509 " texcoord = ubuf.attr[gl_VertexID];\n"
1510 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1511 "\n"
1512 " // GL->VK conventions\n"
1513 " gl_Position.y = -gl_Position.y;\n"
1514 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1515 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001516
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001517 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001518 (const void *) vertShaderText,
1519 strlen(vertShaderText));
1520 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001521}
1522
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001523static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524{
Cody Northrop1fedb212015-05-28 11:27:16 -06001525 if (!demo->use_glsl) {
1526 void *fragShaderCode;
1527 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001528
Cody Northrop1fedb212015-05-28 11:27:16 -06001529 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001530
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001531 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001532 fragShaderCode, size);
1533 } else {
1534 static const char *fragShaderText =
1535 "#version 140\n"
1536 "#extension GL_ARB_separate_shader_objects : enable\n"
1537 "#extension GL_ARB_shading_language_420pack : enable\n"
1538 "layout (binding = 1) uniform sampler2D tex;\n"
1539 "\n"
1540 "layout (location = 0) in vec4 texcoord;\n"
1541 "layout (location = 0) out vec4 uFragColor;\n"
1542 "void main() {\n"
1543 " uFragColor = texture(tex, texcoord.xy);\n"
1544 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001545
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001546 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop1fedb212015-05-28 11:27:16 -06001547 (const void *) fragShaderText,
1548 strlen(fragShaderText));
1549 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001550}
1551
1552static void demo_prepare_pipeline(struct demo *demo)
1553{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001554 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001555 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001556 VkPipelineInputAssemblyStateCreateInfo ia;
1557 VkPipelineRasterStateCreateInfo rs;
1558 VkPipelineColorBlendStateCreateInfo cb;
1559 VkPipelineDepthStencilStateCreateInfo ds;
1560 VkPipelineViewportStateCreateInfo vp;
1561 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001562 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563
1564 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001565 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001566 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001569 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001570 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571
1572 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001573 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001574 rs.fillMode = VK_FILL_MODE_SOLID;
1575 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001576 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001577 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001578
1579 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001580 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1581 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001582 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001583 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001584 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001585 cb.attachmentCount = 1;
1586 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587
Tony Barbour29645d02015-01-16 14:27:35 -07001588 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001589 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001590 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001591
1592 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001593 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001594 ds.depthTestEnable = VK_TRUE;
1595 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001596 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001597 ds.depthBoundsEnable = VK_FALSE;
1598 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1599 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001600 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001601 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001602 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001603
Tony Barbour29645d02015-01-16 14:27:35 -07001604 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001605 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001606 ms.sampleMask = 1;
Tony Barbour3ca22502015-06-26 10:18:34 -06001607 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001608
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001609 // Two stages: vs and fs
1610 pipeline.stageCount = 2;
1611 VkPipelineShaderStageCreateInfo shaderStages[2];
1612 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1613
1614 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1615 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1616 shaderStages[0].shader = demo_prepare_vs(demo);
1617
1618 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1619 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1620 shaderStages[1].shader = demo_prepare_fs(demo);
1621
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001622 memset(&pipelineCache, 0, sizeof(pipelineCache));
1623 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001624
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001625 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1626 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001627
1628 pipeline.pVertexInputState = NULL;
1629 pipeline.pInputAssemblyState = &ia;
1630 pipeline.pRasterState = &rs;
1631 pipeline.pColorBlendState = &cb;
1632 pipeline.pMultisampleState = &ms;
1633 pipeline.pViewportState = &vp;
1634 pipeline.pDepthStencilState = &ds;
1635 pipeline.pStages = shaderStages;
1636
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001637 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638 assert(!err);
1639
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001640 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001641 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001642 }
Tony Barbourb6dba5c2015-07-21 09:04:41 -06001643 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1644 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001645}
1646
1647static void demo_prepare_dynamic_states(struct demo *demo)
1648{
Tony Barbour155cce82015-07-03 10:33:54 -06001649 VkDynamicViewportStateCreateInfo viewport_create;
1650 VkDynamicRasterStateCreateInfo raster;
1651 VkDynamicColorBlendStateCreateInfo color_blend;
1652 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001653 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001654
Tony Barbour29645d02015-01-16 14:27:35 -07001655 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001656 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001657 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001658 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001659 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001660 viewport.height = (float) demo->height;
1661 viewport.width = (float) demo->width;
1662 viewport.minDepth = (float) 0.0f;
1663 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001664 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001665 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001666 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001667 scissor.extent.width = demo->width;
1668 scissor.extent.height = demo->height;
1669 scissor.offset.x = 0;
1670 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001671 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001672
1673 memset(&raster, 0, sizeof(raster));
Tony Barbour155cce82015-07-03 10:33:54 -06001674 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001675 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676
1677 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour155cce82015-07-03 10:33:54 -06001678 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001679 color_blend.blendConst[0] = 1.0f;
1680 color_blend.blendConst[1] = 1.0f;
1681 color_blend.blendConst[2] = 1.0f;
1682 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001683
1684 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour155cce82015-07-03 10:33:54 -06001685 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001686 depth_stencil.minDepthBounds = 0.0f;
1687 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001688 depth_stencil.stencilBackRef = 0;
1689 depth_stencil.stencilFrontRef = 0;
1690 depth_stencil.stencilReadMask = 0xff;
1691 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001692
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001693 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 assert(!err);
1695
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001696 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001697 assert(!err);
1698
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001699 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700 &color_blend, &demo->color_blend);
1701 assert(!err);
1702
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001703 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001704 &depth_stencil, &demo->depth_stencil);
1705 assert(!err);
1706}
1707
Chia-I Wu63ea9262015-03-26 13:14:16 +08001708static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001709{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001710 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001711 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001712 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001713 .count = 1,
1714 },
1715 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001716 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001717 .count = DEMO_TEXTURE_COUNT,
1718 },
1719 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001720 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001721 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001722 .pNext = NULL,
1723 .count = 2,
1724 .pTypeCount = type_counts,
1725 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001726 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001727
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001728 err = vkCreateDescriptorPool(demo->device,
1729 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001730 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001731 assert(!err);
1732}
1733
1734static void demo_prepare_descriptor_set(struct demo *demo)
1735{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001736 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1737 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001738 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001739 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001740
Mike Stroyanebae8322015-04-17 12:36:38 -06001741 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001742 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001743 1, &demo->desc_layout,
Cody Northrop15954692015-08-03 12:47:29 -06001744 &demo->desc_set);
1745 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001746
Chia-I Wuae721ba2015-05-25 16:27:55 +08001747 memset(&tex_descs, 0, sizeof(tex_descs));
1748 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1749 tex_descs[i].sampler = demo->textures[i].sampler;
1750 tex_descs[i].imageView = demo->textures[i].view;
1751 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1752 }
1753
1754 memset(&writes, 0, sizeof(writes));
1755
1756 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1757 writes[0].destSet = demo->desc_set;
1758 writes[0].count = 1;
1759 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1760 writes[0].pDescriptors = &demo->uniform_data.desc;
1761
1762 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1763 writes[1].destSet = demo->desc_set;
1764 writes[1].destBinding = 1;
1765 writes[1].count = DEMO_TEXTURE_COUNT;
1766 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1767 writes[1].pDescriptors = tex_descs;
1768
1769 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1770 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001771}
1772
Chia-I Wuf973e322015-07-08 13:34:24 +08001773static void demo_prepare_framebuffers(struct demo *demo)
1774{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001775 VkAttachmentBindInfo attachments[2] = {
1776 [0] = {
Tobin Ehlisd415ac32015-07-06 14:02:36 -06001777 .view.handle = VK_NULL_HANDLE,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001778 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1779 },
1780 [1] = {
1781 .view = demo->depth.view,
1782 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1783 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001784 };
1785 const VkFramebufferCreateInfo fb_info = {
1786 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1787 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001788 .renderPass = demo->render_pass,
1789 .attachmentCount = 2,
1790 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001791 .width = demo->width,
1792 .height = demo->height,
1793 .layers = 1,
1794 };
1795 VkResult U_ASSERT_ONLY err;
1796 uint32_t i;
1797
1798 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001799 attachments[0].view = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001800 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1801 assert(!err);
1802 }
1803}
1804
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001805static void demo_prepare(struct demo *demo)
1806{
Cody Northrop91e221b2015-07-09 18:08:32 -06001807 VkResult U_ASSERT_ONLY err;
1808
1809 const VkCmdPoolCreateInfo cmd_pool_info = {
1810 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1811 .pNext = NULL,
1812 .queueFamilyIndex = demo->graphics_queue_node_index,
1813 .flags = 0,
1814 };
1815 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1816 assert(!err);
1817
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001818 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001819 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001821 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001822 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001823 .flags = 0,
1824 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001825
1826 demo_prepare_buffers(demo);
1827 demo_prepare_depth(demo);
1828 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001829 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001831 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001832 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001833 demo_prepare_pipeline(demo);
1834 demo_prepare_dynamic_states(demo);
1835
Ian Elliottaaae5352015-07-06 14:27:58 -06001836 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001837 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001838 assert(!err);
1839 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001840
Chia-I Wu63ea9262015-03-26 13:14:16 +08001841 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001842 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001843
Chia-I Wuf973e322015-07-08 13:34:24 +08001844 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001845
Ian Elliottaaae5352015-07-06 14:27:58 -06001846 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001847 demo->current_buffer = i;
1848 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1849 }
1850
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001851 /*
1852 * Prepare functions above may generate pipeline commands
1853 * that need to be flushed before beginning the render loop.
1854 */
1855 demo_flush_init_cmd(demo);
1856
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001857 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001858 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001859}
1860
David Pinedo2bb7c932015-06-18 17:03:14 -06001861static void demo_cleanup(struct demo *demo)
1862{
1863 uint32_t i;
1864
1865 demo->prepared = false;
1866
Tony Barbour155cce82015-07-03 10:33:54 -06001867 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1868 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1869 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001870 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001871 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001872
Tony Barbour155cce82015-07-03 10:33:54 -06001873 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1874 vkDestroyDynamicRasterState(demo->device, demo->raster);
1875 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1876 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001877
Tony Barbour155cce82015-07-03 10:33:54 -06001878 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001879 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001880 vkDestroyRenderPass(demo->device, demo->render_pass);
1881 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1882 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001883
1884 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001885 vkDestroyImageView(demo->device, demo->textures[i].view);
1886 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001887 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001888 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001889 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001890 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedo2bb7c932015-06-18 17:03:14 -06001891
Tony Barbour155cce82015-07-03 10:33:54 -06001892 vkDestroyAttachmentView(demo->device, demo->depth.view);
1893 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001894 vkFreeMemory(demo->device, demo->depth.mem);
1895
Tony Barbour155cce82015-07-03 10:33:54 -06001896 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1897 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001898 vkFreeMemory(demo->device, demo->uniform_data.mem);
1899
Ian Elliottaaae5352015-07-06 14:27:58 -06001900 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001901 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1902 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001903 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001904 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001905
Cody Northrop91e221b2015-07-09 18:08:32 -06001906 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001907 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001908 if (demo->validate) {
1909 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1910 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001911 vkDestroyInstance(demo->inst);
1912
1913#ifndef _WIN32
1914 xcb_destroy_window(demo->connection, demo->window);
1915 xcb_disconnect(demo->connection);
1916#endif // _WIN32
1917}
1918
1919// On MS-Windows, make this a global, so it's available to WndProc()
1920struct demo demo;
1921
Ian Elliott639ca472015-04-16 15:23:05 -06001922#ifdef _WIN32
1923static void demo_run(struct demo *demo)
1924{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001925 if (!demo->prepared)
1926 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001927 // Wait for work to finish before updating MVP.
1928 vkDeviceWaitIdle(demo->device);
1929 demo_update_data_buffer(demo);
1930
1931 demo_draw(demo);
1932
1933 // Wait for work to finish before updating MVP.
1934 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001935
David Pinedo2bb7c932015-06-18 17:03:14 -06001936 demo->curFrame++;
1937
1938 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1939 {
1940 demo->quit=true;
1941 demo_cleanup(demo);
1942 ExitProcess(0);
1943 }
1944
1945}
Ian Elliott639ca472015-04-16 15:23:05 -06001946
1947// MS-Windows event handling function:
1948LRESULT CALLBACK WndProc(HWND hWnd,
1949 UINT uMsg,
1950 WPARAM wParam,
1951 LPARAM lParam)
1952{
Ian Elliott639ca472015-04-16 15:23:05 -06001953 switch(uMsg)
1954 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001955 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001956 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001957 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001958 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001959 demo_run(&demo);
1960 return 0;
1961 default:
1962 break;
1963 }
1964 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1965}
1966
1967static void demo_create_window(struct demo *demo)
1968{
1969 WNDCLASSEX win_class;
1970
1971 // Initialize the window class structure:
1972 win_class.cbSize = sizeof(WNDCLASSEX);
1973 win_class.style = CS_HREDRAW | CS_VREDRAW;
1974 win_class.lpfnWndProc = WndProc;
1975 win_class.cbClsExtra = 0;
1976 win_class.cbWndExtra = 0;
1977 win_class.hInstance = demo->connection; // hInstance
1978 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1979 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1980 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1981 win_class.lpszMenuName = NULL;
1982 win_class.lpszClassName = demo->name;
1983 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1984 // Register window class:
1985 if (!RegisterClassEx(&win_class)) {
1986 // It didn't work, so try to give a useful error:
1987 printf("Unexpected error trying to start the application!\n");
1988 fflush(stdout);
1989 exit(1);
1990 }
1991 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001992 RECT wr = { 0, 0, demo->width, demo->height };
1993 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001994 demo->window = CreateWindowEx(0,
1995 demo->name, // class name
1996 demo->name, // app name
1997 WS_OVERLAPPEDWINDOW | // window style
1998 WS_VISIBLE |
1999 WS_SYSMENU,
2000 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06002001 wr.right-wr.left, // width
2002 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06002003 NULL, // handle to parent
2004 NULL, // handle to menu
2005 demo->connection, // hInstance
2006 NULL); // no extra parameters
2007 if (!demo->window) {
2008 // It didn't work, so try to give a useful error:
2009 printf("Cannot create a window in which to draw!\n");
2010 fflush(stdout);
2011 exit(1);
2012 }
2013}
2014#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002015static void demo_handle_event(struct demo *demo,
2016 const xcb_generic_event_t *event)
2017{
Piers Daniell735ee532015-02-23 16:23:13 -07002018 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002019 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002020 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002021 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002022 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002023 case XCB_CLIENT_MESSAGE:
2024 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2025 (*demo->atom_wm_delete_window).atom) {
2026 demo->quit = true;
2027 }
2028 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002029 case XCB_KEY_RELEASE:
2030 {
2031 const xcb_key_release_event_t *key =
2032 (const xcb_key_release_event_t *) event;
2033
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002034 switch (key->detail) {
2035 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002036 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002037 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002038 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002039 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002040 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002041 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002042 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002043 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002044 case 0x41:
2045 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002046 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002047 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002048 }
2049 break;
2050 default:
2051 break;
2052 }
2053}
2054
2055static void demo_run(struct demo *demo)
2056{
2057 xcb_flush(demo->connection);
2058
2059 while (!demo->quit) {
2060 xcb_generic_event_t *event;
2061
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002062 if (demo->pause) {
2063 event = xcb_wait_for_event(demo->connection);
2064 } else {
2065 event = xcb_poll_for_event(demo->connection);
2066 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002067 if (event) {
2068 demo_handle_event(demo, event);
2069 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002070 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002071
2072 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002073 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002074 demo_update_data_buffer(demo);
2075
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002076 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002077
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002078 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002079 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002080 demo->curFrame++;
2081 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2082 demo->quit = true;
2083
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002084 }
2085}
2086
2087static void demo_create_window(struct demo *demo)
2088{
2089 uint32_t value_mask, value_list[32];
2090
2091 demo->window = xcb_generate_id(demo->connection);
2092
2093 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2094 value_list[0] = demo->screen->black_pixel;
2095 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2096 XCB_EVENT_MASK_EXPOSURE;
2097
2098 xcb_create_window(demo->connection,
2099 XCB_COPY_FROM_PARENT,
2100 demo->window, demo->screen->root,
2101 0, 0, demo->width, demo->height, 0,
2102 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2103 demo->screen->root_visual,
2104 value_mask, value_list);
2105
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002106 /* Magic code that will send notification when window is destroyed */
2107 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2108 "WM_PROTOCOLS");
2109 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2110
2111 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2112 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2113
2114 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2115 demo->window, (*reply).atom, 4, 32, 1,
2116 &(*demo->atom_wm_delete_window).atom);
2117 free(reply);
2118
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002119 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002120
2121 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2122 const uint32_t coords[] = {100, 100};
2123 xcb_configure_window(demo->connection, demo->window,
2124 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002125}
Ian Elliott639ca472015-04-16 15:23:05 -06002126#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002127
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002128/*
2129 * Return 1 (true) if all layer names specified in check_names
2130 * can be found in given layer properties.
2131 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002132static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002133 uint32_t layer_count, VkLayerProperties *layers)
2134{
2135 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002136 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002137 for (uint32_t j = 0; j < layer_count; j++) {
2138 if (!strcmp(check_names[i], layers[j].layerName)) {
2139 found = 1;
2140 }
2141 }
2142 if (!found) {
2143 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2144 return 0;
2145 }
2146 }
2147 return 1;
2148}
2149
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002150static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002151{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002152 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002153 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002154 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002155 VkLayerProperties *instance_layers;
2156 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002157 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002158 uint32_t instance_layer_count = 0;
2159 uint32_t enabled_extension_count = 0;
2160 uint32_t enabled_layer_count = 0;
2161
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002162 char *instance_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002163 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002164 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002165 "ObjectTracker",
2166 "DrawState",
2167 "ParamChecker",
2168 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002169 };
2170
2171 char *device_validation_layers[] = {
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002172 "Threading",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002173 "MemTracker",
Tony Barbourb6dba5c2015-07-21 09:04:41 -06002174 "ObjectTracker",
2175 "DrawState",
2176 "ParamChecker",
2177 "ShaderChecker",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002178 };
2179
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002180 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002181 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002182 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002183 assert(!err);
2184
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002185 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2186 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2187 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002188
2189 if (demo->validate) {
2190 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2191 instance_layer_count, instance_layers);
2192 if (!validation_found) {
2193 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2194 "required validation layer.\n\n"
2195 "Please look at the Getting Started guide for additional "
2196 "information.\n",
2197 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002198 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002199 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002200 }
2201
2202 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2203 assert(!err);
2204
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002205 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002206 memset(extension_names, 0, sizeof(extension_names));
2207 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2208 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2209 assert(!err);
2210 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002211 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002212 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002213 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002214 }
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002215 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002216 if (demo->validate) {
Courtney Goeltzenleuchtera59efa72015-07-30 11:32:46 -06002217 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002218 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002219 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002220 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002221 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002222 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002223 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliottaaae5352015-07-06 14:27:58 -06002224 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002225 "Vulkan installable client driver (ICD) installed?\nPlease "
2226 "look at the Getting Started guide for additional "
2227 "information.\n",
2228 "vkCreateInstance Failure");
2229 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002230 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002231 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002232 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002233 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002234 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002235 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002236 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002237 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002238 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002239 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002240 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002241 .pNext = NULL,
2242 .pAppInfo = &app,
2243 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002244 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002245 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002246 .extensionCount = enabled_extension_count,
2247 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002248 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002249 const VkDeviceQueueCreateInfo queue = {
Chris Forbesc90f4402015-07-11 19:11:39 +12002250 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002251 .queueCount = 1,
2252 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002253
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002254 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002255
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002256 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002257 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2258 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002259 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002260 "additional information.\n",
2261 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002262 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2263 ERR_EXIT("Cannot find a specified extension library"
2264 ".\nMake sure your layers path is set appropriately\n",
2265 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002266 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002267 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2268 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002269 "the Getting Started guide for additional information.\n",
2270 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002271 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002272
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002273 free(instance_layers);
2274 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002275
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002276 gpu_count = 1;
2277 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002278 assert(!err && gpu_count == 1);
2279
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002280 /* Look for validation layers */
2281 validation_found = 0;
2282 enabled_layer_count = 0;
2283 uint32_t device_layer_count = 0;
2284 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2285 assert(!err);
2286
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002287 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2288 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2289 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002290
2291 if (demo->validate) {
2292 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2293 device_layer_count, device_layers);
2294 if (!validation_found) {
2295 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2296 "a required validation layer.\n\n"
2297 "Please look at the Getting Started guide for additional "
2298 "information.\n",
2299 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002300 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002301 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002302 }
2303
2304 uint32_t device_extension_count = 0;
2305 VkExtensionProperties *device_extensions = NULL;
2306 err = vkGetPhysicalDeviceExtensionProperties(
2307 demo->gpu, NULL, &device_extension_count, NULL);
2308 assert(!err);
2309
2310 WSIextFound = 0;
2311 enabled_extension_count = 0;
2312 memset(extension_names, 0, sizeof(extension_names));
2313 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2314 err = vkGetPhysicalDeviceExtensionProperties(
2315 demo->gpu, NULL, &device_extension_count, device_extensions);
2316 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002317
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002318 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002319 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002320 WSIextFound = 1;
Ian Elliottaaae5352015-07-06 14:27:58 -06002321 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002322 }
2323 assert(enabled_extension_count < 64);
2324 }
2325 if (!WSIextFound) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002326 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2327 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002328 "Vulkan installable client driver (ICD) installed?\nPlease "
2329 "look at the Getting Started guide for additional "
2330 "information.\n",
2331 "vkCreateInstance Failure");
2332 }
2333
2334 VkDeviceCreateInfo device = {
2335 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2336 .pNext = NULL,
2337 .queueRecordCount = 1,
2338 .pRequestedQueues = &queue,
2339 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002340 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002341 .extensionCount = enabled_extension_count,
2342 .ppEnabledExtensionNames = (const char *const*) extension_names,
2343 .flags = 0,
2344 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002345
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002346 if (demo->validate) {
Courtney Goeltzenleuchter47610632015-07-12 14:35:22 -06002347 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2348 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002349 if (!demo->dbgCreateMsgCallback) {
2350 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2351 "vkGetProcAddr Failure");
2352 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002353 if (!demo->dbgDestroyMsgCallback) {
2354 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2355 "vkGetProcAddr Failure");
2356 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002357 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2358 if (!demo->dbgBreakCallback) {
2359 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2360 "vkGetProcAddr Failure");
2361 }
2362
2363 PFN_vkDbgMsgCallback callback;
2364
2365 if (!demo->use_break) {
2366 callback = dbgFunc;
2367 } else {
2368 callback = demo->dbgBreakCallback;
2369 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002370 err = demo->dbgCreateMsgCallback(
2371 demo->inst,
2372 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002373 callback, NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002374 &demo->msg_callback);
2375 switch (err) {
2376 case VK_SUCCESS:
2377 break;
2378 case VK_ERROR_INVALID_POINTER:
2379 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2380 "dbgCreateMsgCallback Failure");
2381 break;
2382 case VK_ERROR_OUT_OF_HOST_MEMORY:
2383 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2384 "dbgCreateMsgCallback Failure");
2385 break;
2386 default:
2387 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2388 "dbgCreateMsgCallback Failure");
2389 break;
2390 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002391 }
2392
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002393 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002394 assert(!err);
2395
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002396 free(device_layers);
2397
Ian Elliottaaae5352015-07-06 14:27:58 -06002398 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
2399 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceInfoWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002400 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2401 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2402 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2403 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
Ian Elliottaaae5352015-07-06 14:27:58 -06002404 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott673898b2015-06-22 15:07:49 -06002405 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002406
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002407 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002408 assert(!err);
2409
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002410 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &demo->queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002411 assert(!err);
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002412 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002413
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002414 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(demo->queue_count * sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002415 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002416 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002417 assert(demo->queue_count >= 1);
2418}
2419
2420static void demo_init_vk_wsi(struct demo *demo)
2421{
2422 VkResult err;
2423 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002424
Ian Elliottaaae5352015-07-06 14:27:58 -06002425 // Construct the WSI surface description:
2426 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2427 demo->surface_description.pNext = NULL;
2428#ifdef _WIN32
2429 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2430 demo->surface_description.pPlatformHandle = demo->connection;
2431 demo->surface_description.pPlatformWindow = demo->window;
2432#else // _WIN32
2433 demo->platform_handle_xcb.connection = demo->connection;
2434 demo->platform_handle_xcb.root = demo->screen->root;
2435 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2436 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2437 demo->surface_description.pPlatformWindow = &demo->window;
2438#endif // _WIN32
2439
2440 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002441 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2442 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002443 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2444 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2445 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002446 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002447
2448 // Search for a graphics and a present queue in the array of queue
2449 // families, try to find one that supports both
2450 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2451 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002452 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002453 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2454 if (graphicsQueueNodeIndex == UINT32_MAX) {
2455 graphicsQueueNodeIndex = i;
2456 }
2457
2458 if (supportsPresent[i] == VK_TRUE) {
2459 graphicsQueueNodeIndex = i;
2460 presentQueueNodeIndex = i;
2461 break;
2462 }
2463 }
2464 }
2465 if (presentQueueNodeIndex == UINT32_MAX) {
2466 // If didn't find a queue that supports both graphics and present, then
2467 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002468 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002469 if (supportsPresent[i] == VK_TRUE) {
2470 presentQueueNodeIndex = i;
2471 break;
2472 }
2473 }
2474 }
2475 free(supportsPresent);
2476
2477 // Generate error if could not find both a graphics and a present queue
2478 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2479 ERR_EXIT("Could not find a graphics and a present queue\n",
2480 "WSI Initialization Failure");
2481 }
2482
2483 // TODO: Add support for separate queues, including presentation,
2484 // synchronization, and appropriate tracking for QueueSubmit
2485 // While it is possible for an application to use a separate graphics and a
2486 // present queues, this demo program assumes it is only using one:
2487 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2488 ERR_EXIT("Could not find a common graphics and a present queue\n",
2489 "WSI Initialization Failure");
2490 }
2491
2492 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002494 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002495 0, &demo->queue);
2496 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002497
Ian Elliottaaae5352015-07-06 14:27:58 -06002498 // Get the list of VkFormat's that are supported:
2499 size_t formatsSize;
2500 err = demo->fpGetSurfaceInfoWSI(demo->device,
2501 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2502 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2503 &formatsSize, NULL);
2504 assert(!err);
2505 VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize);
2506 err = demo->fpGetSurfaceInfoWSI(demo->device,
2507 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2508 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
2509 &formatsSize, surfFormats);
2510 assert(!err);
2511 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2512 // the surface has no preferred format. Otherwise, at least one
2513 // supported format will be returned.
2514 size_t formatCount = formatsSize / sizeof(VkSurfaceFormatPropertiesWSI);
2515 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2516 {
2517 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2518 }
2519 else
2520 {
2521 assert(formatCount >= 1);
2522 demo->format = surfFormats[0].format;
2523 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06002524
David Pinedo2bb7c932015-06-18 17:03:14 -06002525 demo->quit = false;
2526 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002527
2528 // Get Memory information and properties
2529 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2530 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002531}
2532
2533static void demo_init_connection(struct demo *demo)
2534{
Ian Elliott639ca472015-04-16 15:23:05 -06002535#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002536 const xcb_setup_t *setup;
2537 xcb_screen_iterator_t iter;
2538 int scr;
2539
2540 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002541 if (demo->connection == NULL) {
2542 printf("Cannot find a compatible Vulkan installable client driver "
2543 "(ICD).\nExiting ...\n");
2544 fflush(stdout);
2545 exit(1);
2546 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002547
2548 setup = xcb_get_setup(demo->connection);
2549 iter = xcb_setup_roots_iterator(setup);
2550 while (scr-- > 0)
2551 xcb_screen_next(&iter);
2552
2553 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002554#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002555}
2556
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002557static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002558{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002559 vec3 eye = {0.0f, 3.0f, 5.0f};
2560 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002561 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002562
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002563 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002564 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002565
Piers Daniell735ee532015-02-23 16:23:13 -07002566 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002567 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002568 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002569 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002570 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002571 if (strcmp(argv[i], "--use_glsl") == 0) {
2572 demo->use_glsl = true;
2573 continue;
2574 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002575 if (strcmp(argv[i], "--break") == 0) {
2576 demo->use_break = true;
2577 continue;
2578 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002579 if (strcmp(argv[i], "--validate") == 0) {
2580 demo->validate = true;
2581 continue;
2582 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002583 if (strcmp(argv[i], "--c") == 0 &&
2584 demo->frameCount == INT_MAX &&
2585 i < argc-1 &&
2586 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2587 demo->frameCount >= 0)
2588 {
2589 i++;
2590 continue;
2591 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002592
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002593 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002594 fflush(stderr);
2595 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002596 }
2597
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002598 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002599 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002600
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002601 demo->width = 500;
2602 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002603
2604 demo->spin_angle = 0.01f;
2605 demo->spin_increment = 0.01f;
2606 demo->pause = false;
2607
Piers Daniell735ee532015-02-23 16:23:13 -07002608 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002609 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2610 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002611}
2612
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002613
Ian Elliott639ca472015-04-16 15:23:05 -06002614#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002615extern int __getmainargs(
2616 int * _Argc,
2617 char *** _Argv,
2618 char *** _Env,
2619 int _DoWildCard,
2620 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002621
Ian Elliotta748eaf2015-04-28 15:50:36 -06002622int WINAPI WinMain(HINSTANCE hInstance,
2623 HINSTANCE hPrevInstance,
2624 LPSTR pCmdLine,
2625 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002626{
2627 MSG msg; // message
2628 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002629 int argc;
2630 char** argv;
2631 char** env;
2632 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002633
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002634 __getmainargs(&argc,&argv,&env,0,&new_mode);
2635
2636 demo_init(&demo, argc, argv);
2637 demo.connection = hInstance;
2638 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002639 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002640 demo_init_vk_wsi(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002641
2642 demo_prepare(&demo);
2643
2644 done = false; //initialize loop condition variable
2645 /* main message loop*/
2646 while(!done)
2647 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002648 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002649 if (msg.message == WM_QUIT) //check for a quit message
2650 {
2651 done = true; //if found, quit app
2652 }
2653 else
2654 {
2655 /* Translate and dispatch to event queue*/
2656 TranslateMessage(&msg);
2657 DispatchMessage(&msg);
2658 }
2659 }
2660
2661 demo_cleanup(&demo);
2662
Tony Barbourf9921732015-04-22 11:36:22 -06002663 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002664}
2665#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002666int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002667{
2668 struct demo demo;
2669
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002670 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002671 demo_create_window(&demo);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002672 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002673
2674 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002675 demo_run(&demo);
2676
2677 demo_cleanup(&demo);
2678
2679 return 0;
2680}
Ian Elliott639ca472015-04-16 15:23:05 -06002681#endif // _WIN32