blob: e2f2049e098d043b05181144a0670d05285e3b63 [file] [log] [blame]
Ian Elliott421107f2015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-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 Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060039#include <vulkan.h>
Ian Elliotte36b2082015-07-06 14:27:58 -060040#include <vk_wsi_swapchain.h>
41#include <vk_wsi_device_swapchain.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060042#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060043
Cody Northropd4e020a2015-03-17 14:54:35 -060044#include "icd-spv.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060045
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060046#include "linmath.h"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060047#include <png.h>
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060048
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060049#define DEMO_BUFFER_COUNT 2
50#define DEMO_TEXTURE_COUNT 1
Ian Elliott4e19ed02015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbour22a30862015-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 Elliottcaa9f272015-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 Elliottcaa9f272015-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 Elliotte36b2082015-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 Elliott1b6de092015-06-22 15:07:49 -060088#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
89{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060090 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-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 Goeltzenleuchter7e334982014-10-30 15:14:16 -060097/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070098 * structure to track all objects related to a texture.
99 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600100struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600101 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700102
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600103 VkImage image;
104 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600105
Mark Lobodzinski23182612015-05-29 09:32:35 -0500106 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600107 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700108 int32_t tex_width, tex_height;
109};
110
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600111static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600112 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600113};
114
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600115struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600116 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600117 float mvp[4][4];
118 float position[12*3][4];
119 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600120};
121
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600122struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600123 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600124 float mvp[4][4];
125 float position[12*3][4];
126 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600127};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600128
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600129//--------------------------------------------------------------------------------------
130// Mesh and VertexFormat Data
131//--------------------------------------------------------------------------------------
132struct Vertex
133{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600134 float posX, posY, posZ, posW; // Position data
135 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600136};
137
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600138struct VertexPosTex
139{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600140 float posX, posY, posZ, posW; // Position data
141 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600142};
143
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600144#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600145#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600146
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600147static const float g_vertex_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800148 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600149 -1.0f,-1.0f, 1.0f,
150 -1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800151 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600152 -1.0f, 1.0f,-1.0f,
153 -1.0f,-1.0f,-1.0f,
154
Chia-I Wuc3487c22015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600156 1.0f, 1.0f,-1.0f,
157 1.0f,-1.0f,-1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600159 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600160 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600161
Chia-I Wuc3487c22015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600163 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600164 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600166 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600167 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600168
Chia-I Wuc3487c22015-04-22 14:56:17 +0800169 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600170 -1.0f, 1.0f, 1.0f,
171 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800172 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600173 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600174 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600175
Chia-I Wuc3487c22015-04-22 14:56:17 +0800176 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600177 1.0f, 1.0f, 1.0f,
178 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800179 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600180 1.0f,-1.0f,-1.0f,
181 1.0f, 1.0f,-1.0f,
182
Chia-I Wuc3487c22015-04-22 14:56:17 +0800183 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600184 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600185 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800186 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600187 1.0f,-1.0f, 1.0f,
188 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600189};
190
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600191static const float g_uv_buffer_data[] = {
Chia-I Wuc3487c22015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600210 0.0f, 0.0f,
211 0.0f, 1.0f,
212
Chia-I Wuc3487c22015-04-22 14:56:17 +0800213 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600214 0.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800215 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600216 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600217 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600218 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600219
Chia-I Wuc3487c22015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600224 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600225 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600226
Chia-I Wuc3487c22015-04-22 14:56:17 +0800227 0.0f, 1.0f, // +Z side
228 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600229 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600230 0.0f, 0.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800231 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600232 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-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 Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600255void dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600256 VkFlags msgFlags,
257 VkDbgObjectType objType,
258 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-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 Barbour5685ad72015-04-29 16:19:20 -0600264{
265 char *message = (char *) malloc(strlen(pMsg)+100);
266
267 assert (message);
268
Courtney Goeltzenleuchter1c7c65d2015-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 Barboura65ecc22015-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 Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600276 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour5685ad72015-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 Elliotte36b2082015-07-06 14:27:58 -0600290typedef struct _SwapChainBuffers {
291 VkImage image;
292 VkCmdBuffer cmd;
293 VkAttachmentView view;
294} SwapChainBuffers;
295
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600296struct demo {
Ian Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600303 xcb_connection_t *connection;
304 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800305 xcb_window_t window;
306 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte36b2082015-07-06 14:27:58 -0600307 VkPlatformHandleXcbWSI platform_handle_xcb;
Tony Barbour7910de72015-07-13 16:37:21 -0600308#endif // _WIN32
Cody Northrop75db0322015-05-28 11:27:16 -0600309 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700310 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600311 bool use_glsl;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600312
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600313 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600314 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600315 VkDevice device;
316 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700317 uint32_t graphics_queue_node_index;
Tony Barbour426b9052015-06-24 16:06:58 -0600318 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600319 VkQueueFamilyProperties *queue_props;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600320 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600322 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600323 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600324 VkFormat format;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600325 VkColorSpaceWSI color_space;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600326
Ian Elliotte36b2082015-07-06 14:27:58 -0600327 PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600328 PFN_vkGetSurfacePropertiesWSI fpGetSurfacePropertiesWSI;
329 PFN_vkGetSurfaceFormatsWSI fpGetSurfaceFormatsWSI;
330 PFN_vkGetSurfacePresentModesWSI fpGetSurfacePresentModesWSI;
Jon Ashburncedc15f2015-05-21 18:13:33 -0600331 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
332 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600333 PFN_vkGetSwapChainImagesWSI fpGetSwapChainImagesWSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600334 PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI;
Jon Ashburncedc15f2015-05-21 18:13:33 -0600335 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600336 VkSurfaceDescriptionWindowWSI surface_description;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600337 uint32_t swapChainImageCount;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800338 VkSwapChainWSI swap_chain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600339 SwapChainBuffers *buffers;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600340
Ian Elliotte36b2082015-07-06 14:27:58 -0600341 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600342
343 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600344 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600345
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600346 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500347 VkDeviceMemory mem;
Chia-I Wuc278df82015-07-07 11:50:03 +0800348 VkAttachmentView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600349 } depth;
350
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600351 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600352
353 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600354 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500355 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600356 VkBufferView view;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800357 VkDescriptorInfo desc;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600358 } uniform_data;
359
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600360 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500361 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600362 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600363 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800364 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600365 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600366
Tony Barbourde4124d2015-07-03 10:33:54 -0600367 VkDynamicViewportState viewport;
368 VkDynamicRasterState raster;
369 VkDynamicColorBlendState color_blend;
370 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600371
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600372 mat4x4 projection_matrix;
373 mat4x4 view_matrix;
374 mat4x4 model_matrix;
375
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600376 float spin_angle;
377 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600378 bool pause;
379
Tony Barbour9400f092015-07-21 09:04:41 -0600380 VkShaderModule vert_shader_module;
381 VkShaderModule frag_shader_module;
382
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600383 VkDescriptorPool desc_pool;
384 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800385
Chia-I Wu76cd4222015-07-08 13:34:24 +0800386 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
387
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600388 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600389 int32_t curFrame;
390 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600391 bool validate;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600392 bool use_break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600393 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600394 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600395 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600396 VkDbgMsgCallback msg_callback;
397
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600398 uint32_t current_buffer;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -0600399 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600400};
401
Mark Lobodzinski72346292015-07-02 16:49:40 -0600402static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
403{
404 // Search memtypes to find first index with those properties
405 for (uint32_t i = 0; i < 32; i++) {
406 if ((typeBits & 1) == 1) {
407 // Type is available, does it match user properties?
408 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
409 *typeIndex = i;
410 return VK_SUCCESS;
411 }
412 }
413 typeBits >>= 1;
414 }
415 // No memory types matched, return failure
416 return VK_UNSUPPORTED;
417}
418
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600419static void demo_flush_init_cmd(struct demo *demo)
420{
Tony Barbour22a30862015-04-22 09:02:32 -0600421 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600422
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600423 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600424 return;
425
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600426 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600427 assert(!err);
428
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600429 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600430 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600431
Tony Barbourde4124d2015-07-03 10:33:54 -0600432 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600433 assert(!err);
434
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600435 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600436 assert(!err);
437
Tony Barbourde4124d2015-07-03 10:33:54 -0600438 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600439 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600440}
441
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600442static void demo_set_image_layout(
443 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600444 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400445 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600446 VkImageLayout old_image_layout,
447 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600448{
Tony Barbour22a30862015-04-22 09:02:32 -0600449 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600450
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600451 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600452 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600453 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600454 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600455 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800456 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600457 .flags = 0,
458 };
459
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600460 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600461 assert(!err);
462
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600463 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600464 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600465 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600466 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Cody Northrop16898b02015-08-11 11:35:58 -0600468 .renderPass = VK_NULL_HANDLE,
469 .subpass = 0,
470 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600471 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600472 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600473 }
474
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600475 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600476 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600477 .pNext = NULL,
478 .outputMask = 0,
479 .inputMask = 0,
480 .oldLayout = old_image_layout,
481 .newLayout = new_image_layout,
482 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400483 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600484 };
485
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600486 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600487 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600488 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600489 }
490
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600491 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600492 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600493 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600494 }
495
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600496 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600497
Tony Barbourc2e987e2015-06-29 16:20:35 -0600498 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
499 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600500
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600501 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600502}
503
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600504static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600505{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800506 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600507 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600508 .pNext = NULL,
Courtney Goeltzenleuchter6d4b7522015-07-28 08:59:17 -0600509 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Cody Northrop16898b02015-08-11 11:35:58 -0600510 .renderPass = VK_NULL_HANDLE,
511 .subpass = 0,
512 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700513 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800514 const VkClearValue clear_values[2] = {
515 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
516 [1] = { .ds = { 1.0f, 0 } },
517 };
518 const VkRenderPassBeginInfo rp_begin = {
519 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
520 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800521 .renderPass = demo->render_pass,
522 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800523 .renderArea.offset.x = 0,
524 .renderArea.offset.y = 0,
525 .renderArea.extent.width = demo->width,
526 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600527 .clearValueCount = 2,
528 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700529 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800530 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600531
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600532 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600533 assert(!err);
534
Chia-I Wuc278df82015-07-07 11:50:03 +0800535 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
536
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600537 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600538 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600539 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600540 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600541
Tony Barbourde4124d2015-07-03 10:33:54 -0600542 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
543 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
544 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
545 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600546
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600547 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800548 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600549
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600550 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600551 assert(!err);
552}
553
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600554
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600555void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600556{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600557 mat4x4 MVP, Model, VP;
558 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600559 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600560 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600561
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600562 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600563
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600564 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600565 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700566 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600567 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600568
Mark Lobodzinski23182612015-05-29 09:32:35 -0500569 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600570 assert(!err);
571
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600572 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600573
Mark Lobodzinski23182612015-05-29 09:32:35 -0500574 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600575 assert(!err);
576}
577
578static void demo_draw(struct demo *demo)
579{
Tony Barbour22a30862015-04-22 09:02:32 -0600580 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600581 VkSemaphore presentCompleteSemaphore;
582 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
583 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
584 .pNext = NULL,
585 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
586 };
Tony Barbourde4124d2015-07-03 10:33:54 -0600587 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600588
Ian Elliotte36b2082015-07-06 14:27:58 -0600589 err = vkCreateSemaphore(demo->device,
590 &presentCompleteSemaphoreCreateInfo,
591 &presentCompleteSemaphore);
592 assert(!err);
593
594 // Get the index of the next available swapchain image:
595 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
596 UINT64_MAX,
597 presentCompleteSemaphore,
598 &demo->current_buffer);
599 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
600 // return codes
601 assert(!err);
602
603 // Wait for the present complete semaphore to be signaled to ensure
604 // that the image won't be rendered to until the presentation
605 // engine has fully released ownership to the application, and it is
606 // okay to render to the image.
607 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
608
609// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600610 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbourde4124d2015-07-03 10:33:54 -0600611 nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600612 assert(!err);
613
Ian Elliotte36b2082015-07-06 14:27:58 -0600614 VkPresentInfoWSI present = {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600615 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
Ian Elliotte36b2082015-07-06 14:27:58 -0600616 .pNext = NULL,
617 .swapChainCount = 1,
618 .swapChains = &demo->swap_chain,
619 .imageIndices = &demo->current_buffer,
620 };
621
622// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburncedc15f2015-05-21 18:13:33 -0600623 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliotte36b2082015-07-06 14:27:58 -0600624 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
625 // return codes
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600626 assert(!err);
627
Tony Barbour9400f092015-07-21 09:04:41 -0600628 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliotte36b2082015-07-06 14:27:58 -0600629 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800630
631 err = vkQueueWaitIdle(demo->queue);
632 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600633}
634
635static void demo_prepare_buffers(struct demo *demo)
636{
Ian Elliotte36b2082015-07-06 14:27:58 -0600637 VkResult U_ASSERT_ONLY err;
638
639 // Check the surface properties and formats
Ian Elliott7fe115d2015-08-07 15:56:59 -0600640 VkSurfacePropertiesWSI surfProperties;
641 err = demo->fpGetSurfacePropertiesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600642 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600643 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600644 assert(!err);
645
Ian Elliott7fe115d2015-08-07 15:56:59 -0600646 uint32_t presentModeCount;
647 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600648 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600649 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600650 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600651 VkPresentModeWSI *presentModes =
652 (VkPresentModeWSI *)malloc(presentModeCount * sizeof(VkPresentModeWSI));
653 assert(presentModes);
654 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600655 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600656 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600657 assert(!err);
658
659 VkExtent2D swapChainExtent;
660 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600661 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600662 {
663 // If the surface size is undefined, the size is set to
664 // the size of the images requested.
665 swapChainExtent.width = demo->width;
666 swapChainExtent.height = demo->height;
667 }
668 else
669 {
670 // If the surface size is defined, the swap chain size must match
Ian Elliott7fe115d2015-08-07 15:56:59 -0600671 swapChainExtent = surfProperties.currentExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600672 }
673
674 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3fc49c22015-07-27 13:53:11 -0600675 // tearing mode. If not, try IMMEDIATE which will usually be available,
676 // and is fastest (though it tears). If not, fall back to FIFO which is
677 // always available.
678 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600679 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600680 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600681 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
682 break;
683 }
Ian Elliott3fc49c22015-07-27 13:53:11 -0600684 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
Ian Elliott7fe115d2015-08-07 15:56:59 -0600685 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
Ian Elliott3fc49c22015-07-27 13:53:11 -0600686 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
687 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600688 }
689
Ian Elliotta71c0052015-07-13 12:20:56 -0600690#define WORK_AROUND_CODE
691#ifdef WORK_AROUND_CODE
Ian Elliott7fe115d2015-08-07 15:56:59 -0600692 // After the proper code was created, other parts of this demo were
693 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
694 // images, etc. Live with that for now.
695 // TODO: Rework this demo code to live with the number of buffers returned
696 // by vkCreateSwapChainWSI().
Ian Elliotta71c0052015-07-13 12:20:56 -0600697 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
698#else // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600699 // Determine the number of VkImage's to use in the swap chain (we desire to
700 // own only 1 image at a time, besides the images being displayed and
701 // queued for display):
Ian Elliott7fe115d2015-08-07 15:56:59 -0600702 uint32_t desiredNumberOfSwapChainImages = surfProperties.minImageCount + 1;
703 if ((surfProperties.maxImageCount > 0) &&
704 (desiredNumberOfSwapChainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600705 {
706 // Application must settle for fewer images than desired:
Ian Elliott7fe115d2015-08-07 15:56:59 -0600707 desiredNumberOfSwapChainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600708 }
Ian Elliotta71c0052015-07-13 12:20:56 -0600709#endif // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600710
711 VkSurfaceTransformFlagBitsWSI preTransform;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600712 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600713 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
714 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600715 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600716 }
717
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800718 const VkSwapChainCreateInfoWSI swap_chain = {
719 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
720 .pNext = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600721 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
722 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800723 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600724 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800725 .imageExtent = {
Ian Elliotte36b2082015-07-06 14:27:58 -0600726 .width = swapChainExtent.width,
727 .height = swapChainExtent.height,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600728 },
Ian Elliotte36b2082015-07-06 14:27:58 -0600729 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800730 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600731 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
732 .queueFamilyCount = 0,
733 .pQueueFamilyIndices = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600734 .presentMode = swapChainPresentMode,
735 .oldSwapChain.handle = 0,
736 .clipped = true,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600737 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600738 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600739
Jon Ashburncedc15f2015-05-21 18:13:33 -0600740 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800741 assert(!err);
742
Ian Elliott7fe115d2015-08-07 15:56:59 -0600743 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
744 &demo->swapChainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600745 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800746
Ian Elliott7fe115d2015-08-07 15:56:59 -0600747 VkImage* swapChainImages =
748 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliotte36b2082015-07-06 14:27:58 -0600749 assert(swapChainImages);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600750 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
751 &demo->swapChainImageCount,
752 swapChainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600753 assert(!err);
Ian Elliotta71c0052015-07-13 12:20:56 -0600754#ifdef WORK_AROUND_CODE
Ian Elliott7fe115d2015-08-07 15:56:59 -0600755 // After the proper code was created, other parts of this demo were
756 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
757 // images, etc. Live with that for now.
758 // TODO: Rework this demo code to live with the number of buffers returned
759 // by vkCreateSwapChainWSI().
Ian Elliotta71c0052015-07-13 12:20:56 -0600760 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
Ian Elliotta71c0052015-07-13 12:20:56 -0600761#endif // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600762
763 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
764 assert(demo->buffers);
765
766 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800767 VkAttachmentViewCreateInfo color_attachment_view = {
768 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600769 .pNext = NULL,
770 .format = demo->format,
771 .mipLevel = 0,
772 .baseArraySlice = 0,
773 .arraySize = 1,
774 };
775
Ian Elliott7fe115d2015-08-07 15:56:59 -0600776 demo->buffers[i].image = swapChainImages[i];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600777
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600778 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400779 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600780 VK_IMAGE_LAYOUT_UNDEFINED,
781 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600782
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600783 color_attachment_view.image = demo->buffers[i].image;
784
Chia-I Wuc278df82015-07-07 11:50:03 +0800785 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600786 &color_attachment_view, &demo->buffers[i].view);
787 assert(!err);
788 }
789}
790
791static void demo_prepare_depth(struct demo *demo)
792{
Tony Barbour8205d902015-04-16 15:59:00 -0600793 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600794 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600795 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600796 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600797 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600798 .format = depth_format,
799 .extent = { demo->width, demo->height, 1 },
800 .mipLevels = 1,
801 .arraySize = 1,
802 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600803 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600804 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600805 .flags = 0,
806 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600807 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600808 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500809 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600810 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600811 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600812 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800813 VkAttachmentViewCreateInfo view = {
814 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600815 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600816 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4ab40c42015-07-15 17:41:38 -0600817 .format = depth_format,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600818 .mipLevel = 0,
819 .baseArraySlice = 0,
820 .arraySize = 1,
821 .flags = 0,
822 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600823
Mark Lobodzinski23182612015-05-29 09:32:35 -0500824 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600825 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600826
827 demo->depth.format = depth_format;
828
829 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600830 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600831 &demo->depth.image);
832 assert(!err);
833
Tony Barbourde4124d2015-07-03 10:33:54 -0600834 err = vkGetImageMemoryRequirements(demo->device,
835 demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600836
837 mem_alloc.allocationSize = mem_reqs.size;
838 err = memory_type_from_properties(demo,
839 mem_reqs.memoryTypeBits,
840 VK_MEMORY_PROPERTY_DEVICE_ONLY,
841 &mem_alloc.memoryTypeIndex);
842 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600843
Mark Lobodzinski23182612015-05-29 09:32:35 -0500844 /* allocate memory */
845 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
846 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600847
Mark Lobodzinski23182612015-05-29 09:32:35 -0500848 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600849 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500850 demo->depth.mem, 0);
851 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600852
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600853 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400854 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600855 VK_IMAGE_LAYOUT_UNDEFINED,
856 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600857
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600858 /* create image view */
859 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800860 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600861 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600862}
863
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600864/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600865 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600866 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600867 * \param demo : Needed to access VK calls
868 * \param filename : the png file to be loaded
869 * \param width : width of png, to be updated as a side effect of this function
870 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600871 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600872 * \return bool : an opengl texture id. true if successful?,
873 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600874 *
875 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
876 * Modified to copy image to memory
877 *
878 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700879bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600880 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600881 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600882{
883 //header for testing if it is a png
884 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600885 int is_png, bit_depth, color_type, rowbytes;
886 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700887 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600888 png_structp png_ptr;
889 png_infop info_ptr, end_info;
890 png_byte *image_data;
891 png_bytep *row_pointers;
892
893 //open file as binary
894 FILE *fp = fopen(filename, "rb");
895 if (!fp) {
896 return false;
897 }
898
899 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600900 retval = fread(header, 1, 8, fp);
901 if (retval != 8) {
902 fclose(fp);
903 return false;
904 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600905
906 //test if png
907 is_png = !png_sig_cmp(header, 0, 8);
908 if (!is_png) {
909 fclose(fp);
910 return false;
911 }
912
913 //create png struct
914 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
915 NULL, NULL);
916 if (!png_ptr) {
917 fclose(fp);
918 return (false);
919 }
920
921 //create png info struct
922 info_ptr = png_create_info_struct(png_ptr);
923 if (!info_ptr) {
924 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
925 fclose(fp);
926 return (false);
927 }
928
929 //create png info struct
930 end_info = png_create_info_struct(png_ptr);
931 if (!end_info) {
932 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
933 fclose(fp);
934 return (false);
935 }
936
937 //png error stuff, not sure libpng man suggests this.
938 if (setjmp(png_jmpbuf(png_ptr))) {
939 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
940 fclose(fp);
941 return (false);
942 }
943
944 //init png reading
945 png_init_io(png_ptr, fp);
946
947 //let libpng know you already read the first 8 bytes
948 png_set_sig_bytes(png_ptr, 8);
949
950 // read all the info up to the image data
951 png_read_info(png_ptr, info_ptr);
952
953 // get info about png
954 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
955 NULL, NULL, NULL);
956
957 //update width and height based on png info
958 *width = twidth;
959 *height = theight;
960
961 // Require that incoming texture be 8bits per color component
962 // and 4 components (RGBA).
963 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
964 png_get_channels(png_ptr, info_ptr) != 4) {
965 return false;
966 }
967
968 if (rgba_data == NULL) {
969 // If data pointer is null, we just want the width & height
970 // clean up memory and close stuff
971 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
972 fclose(fp);
973
974 return true;
975 }
976
977 // Update the png info struct.
978 png_read_update_info(png_ptr, info_ptr);
979
980 // Row size in bytes.
981 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
982
983 // Allocate the image_data as a big block, to be given to opengl
984 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
985 if (!image_data) {
986 //clean up memory and close stuff
987 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
988 fclose(fp);
989 return false;
990 }
991
992 // row_pointers is for pointing to image_data for reading the png with libpng
993 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
994 if (!row_pointers) {
995 //clean up memory and close stuff
996 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
997 // delete[] image_data;
998 fclose(fp);
999 return false;
1000 }
1001 // set the individual row_pointers to point at the correct offsets of image_data
1002 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001003 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001004
1005 // read the png into image_data through row_pointers
1006 png_read_image(png_ptr, row_pointers);
1007
1008 // clean up memory and close stuff
1009 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1010 free(row_pointers);
1011 free(image_data);
1012 fclose(fp);
1013
1014 return true;
1015}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001016
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001017static void demo_prepare_texture_image(struct demo *demo,
1018 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001019 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001020 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001021 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001022 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001023{
Mike Stroyande24a6f2015-06-15 14:21:03 -06001024 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001025 int32_t tex_width;
1026 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -06001027 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001028
David Pinedo61e77382015-04-23 08:16:57 -06001029 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1030 {
1031 printf("Failed to load textures\n");
1032 fflush(stdout);
1033 exit(1);
1034 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001035
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001036 tex_obj->tex_width = tex_width;
1037 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001038
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001039 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001040 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001041 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -06001042 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001043 .format = tex_format,
1044 .extent = { tex_width, tex_height, 1 },
1045 .mipLevels = 1,
1046 .arraySize = 1,
1047 .samples = 1,
1048 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001049 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001050 .flags = 0,
1051 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001052 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001053 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001054 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001055 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001056 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001057 };
1058
Mark Lobodzinski23182612015-05-29 09:32:35 -05001059 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001060
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001061 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001062 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001063 assert(!err);
1064
Tony Barbourde4124d2015-07-03 10:33:54 -06001065 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbour426b9052015-06-24 16:06:58 -06001066 assert(!err);
Piers Daniell886be472015-02-23 16:23:13 -07001067
Mark Lobodzinski23182612015-05-29 09:32:35 -05001068 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001069
Mark Lobodzinski72346292015-07-02 16:49:40 -06001070 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1071 assert(!err);
1072
Mark Lobodzinski23182612015-05-29 09:32:35 -05001073 /* allocate memory */
1074 err = vkAllocMemory(demo->device, &mem_alloc,
1075 &(tex_obj->mem));
1076 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001077
Mark Lobodzinski23182612015-05-29 09:32:35 -05001078 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -06001079 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001080 tex_obj->mem, 0);
1081 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001082
Tony Barbour8205d902015-04-16 15:59:00 -06001083 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001084 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001085 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001086 .mipLevel = 0,
1087 .arraySlice = 0,
1088 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001089 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001090 void *data;
1091
Tony Barbour426b9052015-06-24 16:06:58 -06001092 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1093 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001094
Mark Lobodzinski23182612015-05-29 09:32:35 -05001095 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001096 assert(!err);
1097
1098 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1099 fprintf(stderr, "Error loading texture: %s\n", filename);
1100 }
1101
Mark Lobodzinski23182612015-05-29 09:32:35 -05001102 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001103 assert(!err);
1104 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001105
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001106 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001107 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001108 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001109 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001110 tex_obj->imageLayout);
1111 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001112}
1113
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001114static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001115{
1116 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -05001117 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001118 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001119}
1120
1121static void demo_prepare_textures(struct demo *demo)
1122{
Tony Barbour8205d902015-04-16 15:59:00 -06001123 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001124 VkFormatProperties props;
Tony Barbour22a30862015-04-22 09:02:32 -06001125 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001126 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001127
Courtney Goeltzenleuchter4da96aa2015-07-12 12:52:09 -06001128 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001129 assert(!err);
1130
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001131 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001132
FslNopper434db6a2015-05-06 21:42:01 +02001133 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001134 /* Device can texture using linear textures */
1135 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001136 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001137 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001138 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001139 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001140
1141 memset(&staging_texture, 0, sizeof(staging_texture));
1142 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001143 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001144
1145 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001146 VK_IMAGE_TILING_OPTIMAL,
1147 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1148 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001149
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001150 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001151 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001152 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001153 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001154
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001155 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001156 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001157 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001158 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001159
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001160 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001161 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001162 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001163 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001164 .destOffset = { 0, 0, 0 },
1165 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1166 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001167 vkCmdCopyImage(demo->cmd,
1168 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1169 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001170 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001171
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001172 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001173 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001174 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001175 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001176
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001177 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001178
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001179 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001180 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001181 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1182 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001183 }
1184
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001185 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001186 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001187 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001188 .magFilter = VK_TEX_FILTER_NEAREST,
1189 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001190 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001191 .addressU = VK_TEX_ADDRESS_CLAMP,
1192 .addressV = VK_TEX_ADDRESS_CLAMP,
1193 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001194 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001195 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001196 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001197 .minLod = 0.0f,
1198 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001199 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001200 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001201
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001202 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001203 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001204 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -06001205 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001206 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001207 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001208 .channels = { VK_CHANNEL_SWIZZLE_R,
1209 VK_CHANNEL_SWIZZLE_G,
1210 VK_CHANNEL_SWIZZLE_B,
1211 VK_CHANNEL_SWIZZLE_A, },
1212 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001213 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001214
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001215 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001216 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001217 &demo->textures[i].sampler);
1218 assert(!err);
1219
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001220 /* create image view */
1221 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001222 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001223 &demo->textures[i].view);
1224 assert(!err);
1225 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001226}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001227
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001228void demo_prepare_cube_data_buffer(struct demo *demo)
1229{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001230 VkBufferCreateInfo buf_info;
1231 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001232 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001233 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001234 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001235 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001236 .memoryTypeIndex = 0,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001237 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001238 VkMemoryRequirements mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001239 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001240 int i;
1241 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001242 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001243 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001244
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001245 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001246 mat4x4_mul(MVP, VP, demo->model_matrix);
1247 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001248// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001249
1250 for (i=0; i<12*3; i++) {
1251 data.position[i][0] = g_vertex_buffer_data[i*3];
1252 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1253 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1254 data.position[i][3] = 1.0f;
1255 data.attr[i][0] = g_uv_buffer_data[2*i];
1256 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1257 data.attr[i][2] = 0;
1258 data.attr[i][3] = 0;
1259 }
1260
Chia-I Wu714df452015-01-01 07:55:04 +08001261 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001262 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001263 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop86049392015-07-16 10:29:32 -06001264 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001265 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001266 assert(!err);
1267
Tony Barbourde4124d2015-07-03 10:33:54 -06001268 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchteraca30a52015-07-15 11:17:08 -06001269 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001270
Mark Lobodzinski23182612015-05-29 09:32:35 -05001271 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -06001272 err = memory_type_from_properties(demo,
1273 mem_reqs.memoryTypeBits,
1274 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1275 &alloc_info.memoryTypeIndex);
1276 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001277
Mark Lobodzinski23182612015-05-29 09:32:35 -05001278 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1279 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001280
Mark Lobodzinski23182612015-05-29 09:32:35 -05001281 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1282 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001283
Mark Lobodzinski23182612015-05-29 09:32:35 -05001284 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001285
Mark Lobodzinski23182612015-05-29 09:32:35 -05001286 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1287 assert(!err);
1288
Tony Barbourde4124d2015-07-03 10:33:54 -06001289 err = vkBindBufferMemory(demo->device,
1290 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001291 demo->uniform_data.mem, 0);
1292 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001293
1294 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001295 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001296 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001297 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001298 view_info.offset = 0;
1299 view_info.range = sizeof(data);
1300
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001301 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001302 assert(!err);
1303
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001304 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001305}
1306
Chia-I Wuf8385062015-01-04 16:27:24 +08001307static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001308{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001309 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001310 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001311 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001312 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001313 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001314 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001315 },
1316 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001317 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001318 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001319 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001320 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001321 },
1322 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001323 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001324 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001325 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001326 .count = 2,
1327 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001328 };
Tony Barbour22a30862015-04-22 09:02:32 -06001329 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001330
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001331 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001332 &descriptor_layout, &demo->desc_layout);
1333 assert(!err);
1334
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001335 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1336 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1337 .pNext = NULL,
1338 .descriptorSetCount = 1,
1339 .pSetLayouts = &demo->desc_layout,
1340 };
1341
1342 err = vkCreatePipelineLayout(demo->device,
1343 &pPipelineLayoutCreateInfo,
1344 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001345 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001346}
1347
Chia-I Wu76cd4222015-07-08 13:34:24 +08001348static void demo_prepare_render_pass(struct demo *demo)
1349{
Chia-I Wuc278df82015-07-07 11:50:03 +08001350 const VkAttachmentDescription attachments[2] = {
1351 [0] = {
1352 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1353 .pNext = NULL,
1354 .format = demo->format,
1355 .samples = 1,
1356 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1357 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1358 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1359 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1360 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1361 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1362 },
1363 [1] = {
1364 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1365 .pNext = NULL,
1366 .format = demo->depth.format,
1367 .samples = 1,
1368 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1369 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1370 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1371 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1372 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1373 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1374 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001375 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001376 const VkAttachmentReference color_reference = {
1377 .attachment = 0,
1378 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1379 };
1380 const VkSubpassDescription subpass = {
1381 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1382 .pNext = NULL,
1383 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1384 .flags = 0,
1385 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001386 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001387 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001388 .pColorAttachments = &color_reference,
1389 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001390 .depthStencilAttachment = {
1391 .attachment = 1,
1392 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1393 },
1394 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001395 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001396 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001397 const VkRenderPassCreateInfo rp_info = {
1398 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1399 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001400 .attachmentCount = 2,
1401 .pAttachments = attachments,
1402 .subpassCount = 1,
1403 .pSubpasses = &subpass,
1404 .dependencyCount = 0,
1405 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001406 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001407 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001408
1409 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1410 assert(!err);
1411}
1412
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001413static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001414 VkShaderStage stage,
Tony Barbour9400f092015-07-21 09:04:41 -06001415 VkShaderModule* pShaderModule,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001416 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001417 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001418{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001419 VkShaderModuleCreateInfo moduleCreateInfo;
1420 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001421 VkShader shader;
1422 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001423
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001424
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001425 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1426 moduleCreateInfo.pNext = NULL;
1427
1428 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1429 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001430 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001431
Cody Northrop75db0322015-05-28 11:27:16 -06001432 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001433 moduleCreateInfo.codeSize = size;
1434 moduleCreateInfo.pCode = code;
1435 moduleCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001436 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001437 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001438 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001439 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001440
1441 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001442 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001443 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001444 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001445 } else {
1446 // Create fake SPV structure to feed GLSL
1447 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001448 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1449 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1450 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001451
1452 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001453 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1454 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1455 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1456 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001457
Tony Barbour9400f092015-07-21 09:04:41 -06001458 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001459 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001460 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001461 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001462
1463 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001464 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001465 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001466 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001467 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001468 return shader;
1469}
1470
Cody Northropacfb0492015-03-17 15:55:58 -06001471char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001472{
1473 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001474 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001475 void *shader_code;
1476
1477 FILE *fp = fopen(filename, "rb");
1478 if (!fp) return NULL;
1479
1480 fseek(fp, 0L, SEEK_END);
1481 size = ftell(fp);
1482
1483 fseek(fp, 0L, SEEK_SET);
1484
1485 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001486 retval = fread(shader_code, size, 1, fp);
1487 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001488
1489 *psize = size;
1490
1491 return shader_code;
1492}
1493
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001494static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001495{
Cody Northrop75db0322015-05-28 11:27:16 -06001496 if (!demo->use_glsl) {
1497 void *vertShaderCode;
1498 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001499
Cody Northrop75db0322015-05-28 11:27:16 -06001500 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001501
Tony Barbour9400f092015-07-21 09:04:41 -06001502 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001503 vertShaderCode, size);
1504 } else {
1505 static const char *vertShaderText =
1506 "#version 140\n"
1507 "#extension GL_ARB_separate_shader_objects : enable\n"
1508 "#extension GL_ARB_shading_language_420pack : enable\n"
1509 "\n"
1510 "layout(binding = 0) uniform buf {\n"
1511 " mat4 MVP;\n"
1512 " vec4 position[12*3];\n"
1513 " vec4 attr[12*3];\n"
1514 "} ubuf;\n"
1515 "\n"
1516 "layout (location = 0) out vec4 texcoord;\n"
1517 "\n"
1518 "void main() \n"
1519 "{\n"
1520 " texcoord = ubuf.attr[gl_VertexID];\n"
1521 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1522 "\n"
1523 " // GL->VK conventions\n"
1524 " gl_Position.y = -gl_Position.y;\n"
1525 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1526 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001527
Tony Barbour9400f092015-07-21 09:04:41 -06001528 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001529 (const void *) vertShaderText,
1530 strlen(vertShaderText));
1531 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001532}
1533
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001534static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001535{
Cody Northrop75db0322015-05-28 11:27:16 -06001536 if (!demo->use_glsl) {
1537 void *fragShaderCode;
1538 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001539
Cody Northrop75db0322015-05-28 11:27:16 -06001540 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001541
Tony Barbour9400f092015-07-21 09:04:41 -06001542 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001543 fragShaderCode, size);
1544 } else {
1545 static const char *fragShaderText =
1546 "#version 140\n"
1547 "#extension GL_ARB_separate_shader_objects : enable\n"
1548 "#extension GL_ARB_shading_language_420pack : enable\n"
1549 "layout (binding = 1) uniform sampler2D tex;\n"
1550 "\n"
1551 "layout (location = 0) in vec4 texcoord;\n"
1552 "layout (location = 0) out vec4 uFragColor;\n"
1553 "void main() {\n"
1554 " uFragColor = texture(tex, texcoord.xy);\n"
1555 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001556
Tony Barbour9400f092015-07-21 09:04:41 -06001557 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001558 (const void *) fragShaderText,
1559 strlen(fragShaderText));
1560 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001561}
1562
1563static void demo_prepare_pipeline(struct demo *demo)
1564{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001565 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001566 VkPipelineCacheCreateInfo pipelineCache;
Tony Barboure307f582015-07-10 15:29:03 -06001567 VkPipelineInputAssemblyStateCreateInfo ia;
1568 VkPipelineRasterStateCreateInfo rs;
1569 VkPipelineColorBlendStateCreateInfo cb;
1570 VkPipelineDepthStencilStateCreateInfo ds;
1571 VkPipelineViewportStateCreateInfo vp;
1572 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001573 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001574
1575 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001576 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001577 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001578
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001579 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001580 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001581 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001582
1583 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001584 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001585 rs.fillMode = VK_FILL_MODE_SOLID;
1586 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001587 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001588 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001589
1590 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001591 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1592 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001593 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001594 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001595 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001596 cb.attachmentCount = 1;
1597 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001598
Tony Barbourfa6cac72015-01-16 14:27:35 -07001599 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001600 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001601 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001602
1603 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001604 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001605 ds.depthTestEnable = VK_TRUE;
1606 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001607 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001608 ds.depthBoundsEnable = VK_FALSE;
1609 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1610 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001611 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001612 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001613 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001614
Tony Barbourfa6cac72015-01-16 14:27:35 -07001615 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001616 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001617 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001618 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001619
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001620 // Two stages: vs and fs
1621 pipeline.stageCount = 2;
1622 VkPipelineShaderStageCreateInfo shaderStages[2];
1623 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1624
1625 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1626 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1627 shaderStages[0].shader = demo_prepare_vs(demo);
1628
1629 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1630 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1631 shaderStages[1].shader = demo_prepare_fs(demo);
1632
Jon Ashburn0d60d272015-07-09 15:02:25 -06001633 memset(&pipelineCache, 0, sizeof(pipelineCache));
1634 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001635
Jon Ashburn0d60d272015-07-09 15:02:25 -06001636 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1637 assert(!err);
Tony Barboure307f582015-07-10 15:29:03 -06001638
1639 pipeline.pVertexInputState = NULL;
1640 pipeline.pInputAssemblyState = &ia;
1641 pipeline.pRasterState = &rs;
1642 pipeline.pColorBlendState = &cb;
1643 pipeline.pMultisampleState = &ms;
1644 pipeline.pViewportState = &vp;
1645 pipeline.pDepthStencilState = &ds;
1646 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001647 pipeline.renderPass = demo->render_pass;
Tony Barboure307f582015-07-10 15:29:03 -06001648
Jon Ashburn0d60d272015-07-09 15:02:25 -06001649 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001650 assert(!err);
1651
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001652 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001653 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001654 }
Tony Barbour9400f092015-07-21 09:04:41 -06001655 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1656 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001657}
1658
1659static void demo_prepare_dynamic_states(struct demo *demo)
1660{
Tony Barbourde4124d2015-07-03 10:33:54 -06001661 VkDynamicViewportStateCreateInfo viewport_create;
1662 VkDynamicRasterStateCreateInfo raster;
1663 VkDynamicColorBlendStateCreateInfo color_blend;
1664 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001665 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001666
Tony Barbourfa6cac72015-01-16 14:27:35 -07001667 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001668 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001669 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001670 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001671 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001672 viewport.height = (float) demo->height;
1673 viewport.width = (float) demo->width;
1674 viewport.minDepth = (float) 0.0f;
1675 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001676 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001677 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001678 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001679 scissor.extent.width = demo->width;
1680 scissor.extent.height = demo->height;
1681 scissor.offset.x = 0;
1682 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001683 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001684
1685 memset(&raster, 0, sizeof(raster));
Tony Barbourde4124d2015-07-03 10:33:54 -06001686 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001687 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001688
1689 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourde4124d2015-07-03 10:33:54 -06001690 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001691 color_blend.blendConst[0] = 1.0f;
1692 color_blend.blendConst[1] = 1.0f;
1693 color_blend.blendConst[2] = 1.0f;
1694 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001695
1696 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourde4124d2015-07-03 10:33:54 -06001697 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001698 depth_stencil.minDepthBounds = 0.0f;
1699 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001700 depth_stencil.stencilBackRef = 0;
1701 depth_stencil.stencilFrontRef = 0;
1702 depth_stencil.stencilReadMask = 0xff;
1703 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001704
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001705 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001706 assert(!err);
1707
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001708 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001709 assert(!err);
1710
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001711 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001712 &color_blend, &demo->color_blend);
1713 assert(!err);
1714
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001715 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001716 &depth_stencil, &demo->depth_stencil);
1717 assert(!err);
1718}
1719
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001720static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001721{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001722 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001723 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001724 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001725 .count = 1,
1726 },
1727 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001728 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001729 .count = DEMO_TEXTURE_COUNT,
1730 },
1731 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001732 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001733 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001734 .pNext = NULL,
1735 .count = 2,
1736 .pTypeCount = type_counts,
1737 };
Tony Barbour22a30862015-04-22 09:02:32 -06001738 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001739
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001740 err = vkCreateDescriptorPool(demo->device,
1741 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001742 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001743 assert(!err);
1744}
1745
1746static void demo_prepare_descriptor_set(struct demo *demo)
1747{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001748 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1749 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001750 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001751 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001752
Mike Stroyan230e6252015-04-17 12:36:38 -06001753 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001754 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001755 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001756 &demo->desc_set);
1757 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001758
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001759 memset(&tex_descs, 0, sizeof(tex_descs));
1760 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1761 tex_descs[i].sampler = demo->textures[i].sampler;
1762 tex_descs[i].imageView = demo->textures[i].view;
1763 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1764 }
1765
1766 memset(&writes, 0, sizeof(writes));
1767
1768 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1769 writes[0].destSet = demo->desc_set;
1770 writes[0].count = 1;
1771 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1772 writes[0].pDescriptors = &demo->uniform_data.desc;
1773
1774 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1775 writes[1].destSet = demo->desc_set;
1776 writes[1].destBinding = 1;
1777 writes[1].count = DEMO_TEXTURE_COUNT;
1778 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1779 writes[1].pDescriptors = tex_descs;
1780
1781 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1782 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001783}
1784
Chia-I Wu76cd4222015-07-08 13:34:24 +08001785static void demo_prepare_framebuffers(struct demo *demo)
1786{
Cody Northropf110c6e2015-08-04 10:47:08 -06001787 VkAttachmentView attachments[2];
1788 attachments[1] = demo->depth.view;
1789
Chia-I Wu76cd4222015-07-08 13:34:24 +08001790 const VkFramebufferCreateInfo fb_info = {
1791 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1792 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001793 .renderPass = demo->render_pass,
1794 .attachmentCount = 2,
1795 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001796 .width = demo->width,
1797 .height = demo->height,
1798 .layers = 1,
1799 };
1800 VkResult U_ASSERT_ONLY err;
1801 uint32_t i;
1802
1803 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001804 attachments[0] = demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001805 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1806 assert(!err);
1807 }
1808}
1809
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001810static void demo_prepare(struct demo *demo)
1811{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001812 VkResult U_ASSERT_ONLY err;
1813
1814 const VkCmdPoolCreateInfo cmd_pool_info = {
1815 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1816 .pNext = NULL,
1817 .queueFamilyIndex = demo->graphics_queue_node_index,
1818 .flags = 0,
1819 };
1820 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1821 assert(!err);
1822
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001823 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001824 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001825 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001826 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001827 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001828 .flags = 0,
1829 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001830
1831 demo_prepare_buffers(demo);
1832 demo_prepare_depth(demo);
1833 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001834 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001835
Chia-I Wuf8385062015-01-04 16:27:24 +08001836 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001837 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001838 demo_prepare_pipeline(demo);
1839 demo_prepare_dynamic_states(demo);
1840
Ian Elliotte36b2082015-07-06 14:27:58 -06001841 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001842 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001843 assert(!err);
1844 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001845
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001846 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001847 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001848
Chia-I Wu76cd4222015-07-08 13:34:24 +08001849 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001850
Ian Elliotte36b2082015-07-06 14:27:58 -06001851 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001852 demo->current_buffer = i;
1853 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1854 }
1855
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001856 /*
1857 * Prepare functions above may generate pipeline commands
1858 * that need to be flushed before beginning the render loop.
1859 */
1860 demo_flush_init_cmd(demo);
1861
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001862 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001863 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001864}
1865
David Pinedoeeca2a22015-06-18 17:03:14 -06001866static void demo_cleanup(struct demo *demo)
1867{
1868 uint32_t i;
1869
1870 demo->prepared = false;
1871
Tony Barbourde4124d2015-07-03 10:33:54 -06001872 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1873 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1874 }
Tony Barbourb857d312015-07-10 10:50:45 -06001875 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06001876 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001877
Tony Barbourde4124d2015-07-03 10:33:54 -06001878 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1879 vkDestroyDynamicRasterState(demo->device, demo->raster);
1880 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1881 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedoeeca2a22015-06-18 17:03:14 -06001882
Tony Barbourde4124d2015-07-03 10:33:54 -06001883 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001884 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbourde4124d2015-07-03 10:33:54 -06001885 vkDestroyRenderPass(demo->device, demo->render_pass);
1886 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1887 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -06001888
1889 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001890 vkDestroyImageView(demo->device, demo->textures[i].view);
1891 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001892 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001893 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedoeeca2a22015-06-18 17:03:14 -06001894 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001895 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedoeeca2a22015-06-18 17:03:14 -06001896
Tony Barbourde4124d2015-07-03 10:33:54 -06001897 vkDestroyAttachmentView(demo->device, demo->depth.view);
1898 vkDestroyImage(demo->device, demo->depth.image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001899 vkFreeMemory(demo->device, demo->depth.mem);
1900
Tony Barbourde4124d2015-07-03 10:33:54 -06001901 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1902 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedoeeca2a22015-06-18 17:03:14 -06001903 vkFreeMemory(demo->device, demo->uniform_data.mem);
1904
Ian Elliotte36b2082015-07-06 14:27:58 -06001905 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001906 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1907 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001908 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001909 free(demo->buffers);
David Pinedoeeca2a22015-06-18 17:03:14 -06001910
Cody Northrop18ea11b2015-07-09 18:08:32 -06001911 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedoeeca2a22015-06-18 17:03:14 -06001912 vkDestroyDevice(demo->device);
Tony Barboura65ecc22015-06-30 14:14:19 -06001913 if (demo->validate) {
1914 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1915 }
David Pinedoeeca2a22015-06-18 17:03:14 -06001916 vkDestroyInstance(demo->inst);
1917
1918#ifndef _WIN32
1919 xcb_destroy_window(demo->connection, demo->window);
1920 xcb_disconnect(demo->connection);
1921#endif // _WIN32
1922}
1923
1924// On MS-Windows, make this a global, so it's available to WndProc()
1925struct demo demo;
1926
Ian Elliotte14e9f92015-04-16 15:23:05 -06001927#ifdef _WIN32
1928static void demo_run(struct demo *demo)
1929{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001930 if (!demo->prepared)
1931 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001932 // Wait for work to finish before updating MVP.
1933 vkDeviceWaitIdle(demo->device);
1934 demo_update_data_buffer(demo);
1935
1936 demo_draw(demo);
1937
1938 // Wait for work to finish before updating MVP.
1939 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001940
David Pinedoeeca2a22015-06-18 17:03:14 -06001941 demo->curFrame++;
1942
1943 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1944 {
1945 demo->quit=true;
1946 demo_cleanup(demo);
1947 ExitProcess(0);
1948 }
1949
1950}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001951
1952// MS-Windows event handling function:
1953LRESULT CALLBACK WndProc(HWND hWnd,
1954 UINT uMsg,
1955 WPARAM wParam,
1956 LPARAM lParam)
1957{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001958 switch(uMsg)
1959 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001960 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001961 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001962 break;
Ian Elliotte36b2082015-07-06 14:27:58 -06001963 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001964 demo_run(&demo);
1965 return 0;
1966 default:
1967 break;
1968 }
1969 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1970}
1971
1972static void demo_create_window(struct demo *demo)
1973{
1974 WNDCLASSEX win_class;
1975
1976 // Initialize the window class structure:
1977 win_class.cbSize = sizeof(WNDCLASSEX);
1978 win_class.style = CS_HREDRAW | CS_VREDRAW;
1979 win_class.lpfnWndProc = WndProc;
1980 win_class.cbClsExtra = 0;
1981 win_class.cbWndExtra = 0;
1982 win_class.hInstance = demo->connection; // hInstance
1983 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1984 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1985 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1986 win_class.lpszMenuName = NULL;
1987 win_class.lpszClassName = demo->name;
1988 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1989 // Register window class:
1990 if (!RegisterClassEx(&win_class)) {
1991 // It didn't work, so try to give a useful error:
1992 printf("Unexpected error trying to start the application!\n");
1993 fflush(stdout);
1994 exit(1);
1995 }
1996 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001997 RECT wr = { 0, 0, demo->width, demo->height };
1998 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001999 demo->window = CreateWindowEx(0,
2000 demo->name, // class name
2001 demo->name, // app name
2002 WS_OVERLAPPEDWINDOW | // window style
2003 WS_VISIBLE |
2004 WS_SYSMENU,
2005 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06002006 wr.right-wr.left, // width
2007 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06002008 NULL, // handle to parent
2009 NULL, // handle to menu
2010 demo->connection, // hInstance
2011 NULL); // no extra parameters
2012 if (!demo->window) {
2013 // It didn't work, so try to give a useful error:
2014 printf("Cannot create a window in which to draw!\n");
2015 fflush(stdout);
2016 exit(1);
2017 }
2018}
2019#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002020static void demo_handle_event(struct demo *demo,
2021 const xcb_generic_event_t *event)
2022{
Piers Daniell886be472015-02-23 16:23:13 -07002023 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002024 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002025 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002026 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002027 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002028 case XCB_CLIENT_MESSAGE:
2029 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2030 (*demo->atom_wm_delete_window).atom) {
2031 demo->quit = true;
2032 }
2033 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002034 case XCB_KEY_RELEASE:
2035 {
2036 const xcb_key_release_event_t *key =
2037 (const xcb_key_release_event_t *) event;
2038
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002039 switch (key->detail) {
2040 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002041 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002042 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002043 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002044 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002045 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002046 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002047 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002048 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002049 case 0x41:
2050 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07002051 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002052 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002053 }
2054 break;
2055 default:
2056 break;
2057 }
2058}
2059
2060static void demo_run(struct demo *demo)
2061{
2062 xcb_flush(demo->connection);
2063
2064 while (!demo->quit) {
2065 xcb_generic_event_t *event;
2066
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002067 if (demo->pause) {
2068 event = xcb_wait_for_event(demo->connection);
2069 } else {
2070 event = xcb_poll_for_event(demo->connection);
2071 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002072 if (event) {
2073 demo_handle_event(demo, event);
2074 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002075 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002076
2077 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002078 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002079 demo_update_data_buffer(demo);
2080
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002081 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07002082
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002083 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002084 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06002085 demo->curFrame++;
2086 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2087 demo->quit = true;
2088
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002089 }
2090}
2091
2092static void demo_create_window(struct demo *demo)
2093{
2094 uint32_t value_mask, value_list[32];
2095
2096 demo->window = xcb_generate_id(demo->connection);
2097
2098 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2099 value_list[0] = demo->screen->black_pixel;
2100 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2101 XCB_EVENT_MASK_EXPOSURE;
2102
2103 xcb_create_window(demo->connection,
2104 XCB_COPY_FROM_PARENT,
2105 demo->window, demo->screen->root,
2106 0, 0, demo->width, demo->height, 0,
2107 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2108 demo->screen->root_visual,
2109 value_mask, value_list);
2110
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002111 /* Magic code that will send notification when window is destroyed */
2112 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2113 "WM_PROTOCOLS");
2114 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2115
2116 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2117 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2118
2119 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2120 demo->window, (*reply).atom, 4, 32, 1,
2121 &(*demo->atom_wm_delete_window).atom);
2122 free(reply);
2123
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002124 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06002125
2126 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2127 const uint32_t coords[] = {100, 100};
2128 xcb_configure_window(demo->connection, demo->window,
2129 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002130}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002131#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002132
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002133/*
2134 * Return 1 (true) if all layer names specified in check_names
2135 * can be found in given layer properties.
2136 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002137static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002138 uint32_t layer_count, VkLayerProperties *layers)
2139{
2140 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002141 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002142 for (uint32_t j = 0; j < layer_count; j++) {
2143 if (!strcmp(check_names[i], layers[j].layerName)) {
2144 found = 1;
2145 }
2146 }
2147 if (!found) {
2148 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2149 return 0;
2150 }
2151 }
2152 return 1;
2153}
2154
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002155static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002156{
Tobin Ehlis3536b442015-04-16 18:04:57 -06002157 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002158 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002159 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002160 VkLayerProperties *instance_layers;
2161 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002162 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002163 uint32_t instance_layer_count = 0;
2164 uint32_t enabled_extension_count = 0;
2165 uint32_t enabled_layer_count = 0;
2166
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002167 char *instance_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002168 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002169 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002170 "ObjectTracker",
2171 "DrawState",
2172 "ParamChecker",
2173 "ShaderChecker",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002174 };
2175
2176 char *device_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002177 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002178 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002179 "ObjectTracker",
2180 "DrawState",
2181 "ParamChecker",
2182 "ShaderChecker",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002183 };
2184
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002185 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002186 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002187 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002188 assert(!err);
2189
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002190 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2191 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2192 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002193
2194 if (demo->validate) {
2195 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2196 instance_layer_count, instance_layers);
2197 if (!validation_found) {
2198 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2199 "required validation layer.\n\n"
2200 "Please look at the Getting Started guide for additional "
2201 "information.\n",
2202 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002203 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002204 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002205 }
2206
2207 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2208 assert(!err);
2209
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002210 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002211 memset(extension_names, 0, sizeof(extension_names));
2212 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2213 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2214 assert(!err);
2215 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002216 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002217 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06002218 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002219 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002220 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002221 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002222 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002223 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002224 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002225 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002226 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002227 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06002228 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliotte36b2082015-07-06 14:27:58 -06002229 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002230 "Vulkan installable client driver (ICD) installed?\nPlease "
2231 "look at the Getting Started guide for additional "
2232 "information.\n",
2233 "vkCreateInstance Failure");
2234 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002235 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002236 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002237 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002238 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002239 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002240 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002241 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002242 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002243 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002244 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002245 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002246 .pNext = NULL,
2247 .pAppInfo = &app,
2248 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002249 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002250 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002251 .extensionCount = enabled_extension_count,
2252 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002253 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06002254 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12002255 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002256 .queueCount = 1,
2257 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002258
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002259 uint32_t gpu_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002260
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002261 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002262 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2263 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002264 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002265 "additional information.\n",
2266 "vkCreateInstance Failure");
Tony Barbour5685ad72015-04-29 16:19:20 -06002267 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2268 ERR_EXIT("Cannot find a specified extension library"
2269 ".\nMake sure your layers path is set appropriately\n",
2270 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002271 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002272 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2273 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002274 "the Getting Started guide for additional information.\n",
2275 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002276 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002277
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002278 free(instance_layers);
2279 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002280
Jon Ashburn07b309a2015-04-15 11:31:12 -06002281 gpu_count = 1;
2282 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002283 assert(!err && gpu_count == 1);
2284
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002285 /* Look for validation layers */
2286 validation_found = 0;
2287 enabled_layer_count = 0;
2288 uint32_t device_layer_count = 0;
2289 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2290 assert(!err);
2291
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002292 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2293 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2294 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002295
2296 if (demo->validate) {
2297 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2298 device_layer_count, device_layers);
2299 if (!validation_found) {
2300 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2301 "a required validation layer.\n\n"
2302 "Please look at the Getting Started guide for additional "
2303 "information.\n",
2304 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002305 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002306 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002307 }
2308
2309 uint32_t device_extension_count = 0;
2310 VkExtensionProperties *device_extensions = NULL;
2311 err = vkGetPhysicalDeviceExtensionProperties(
2312 demo->gpu, NULL, &device_extension_count, NULL);
2313 assert(!err);
2314
2315 WSIextFound = 0;
2316 enabled_extension_count = 0;
2317 memset(extension_names, 0, sizeof(extension_names));
2318 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2319 err = vkGetPhysicalDeviceExtensionProperties(
2320 demo->gpu, NULL, &device_extension_count, device_extensions);
2321 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06002322
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002323 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002324 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002325 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06002326 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002327 }
2328 assert(enabled_extension_count < 64);
2329 }
2330 if (!WSIextFound) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002331 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2332 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002333 "Vulkan installable client driver (ICD) installed?\nPlease "
2334 "look at the Getting Started guide for additional "
2335 "information.\n",
2336 "vkCreateInstance Failure");
2337 }
2338
2339 VkDeviceCreateInfo device = {
2340 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2341 .pNext = NULL,
2342 .queueRecordCount = 1,
2343 .pRequestedQueues = &queue,
2344 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002345 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002346 .extensionCount = enabled_extension_count,
2347 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002348 };
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002349
Tony Barbour5685ad72015-04-29 16:19:20 -06002350 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06002351 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2352 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002353 if (!demo->dbgCreateMsgCallback) {
2354 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2355 "vkGetProcAddr Failure");
2356 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002357 if (!demo->dbgDestroyMsgCallback) {
2358 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2359 "vkGetProcAddr Failure");
2360 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002361 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2362 if (!demo->dbgBreakCallback) {
2363 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2364 "vkGetProcAddr Failure");
2365 }
2366
2367 PFN_vkDbgMsgCallback callback;
2368
2369 if (!demo->use_break) {
2370 callback = dbgFunc;
2371 } else {
2372 callback = demo->dbgBreakCallback;
2373 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002374 err = demo->dbgCreateMsgCallback(
2375 demo->inst,
2376 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002377 callback, NULL,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002378 &demo->msg_callback);
2379 switch (err) {
2380 case VK_SUCCESS:
2381 break;
2382 case VK_ERROR_INVALID_POINTER:
2383 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2384 "dbgCreateMsgCallback Failure");
2385 break;
2386 case VK_ERROR_OUT_OF_HOST_MEMORY:
2387 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2388 "dbgCreateMsgCallback Failure");
2389 break;
2390 default:
2391 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2392 "dbgCreateMsgCallback Failure");
2393 break;
2394 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002395 }
2396
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002397 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002398 assert(!err);
2399
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002400 free(device_layers);
2401
Ian Elliotte36b2082015-07-06 14:27:58 -06002402 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002403 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
2404 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
2405 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06002406 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2407 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2408 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002409 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliotte36b2082015-07-06 14:27:58 -06002410 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06002411 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburncedc15f2015-05-21 18:13:33 -06002412
Tony Barbour426b9052015-06-24 16:06:58 -06002413 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002414 assert(!err);
2415
Cody Northropef72e2a2015-08-03 17:04:53 -06002416 /* Call with NULL data to get count */
2417 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002418 assert(!err);
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002419 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002420
Cody Northropef72e2a2015-08-03 17:04:53 -06002421 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2422 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002423 assert(!err);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002424 assert(demo->queue_count >= 1);
2425}
2426
2427static void demo_init_vk_wsi(struct demo *demo)
2428{
2429 VkResult err;
2430 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002431
Ian Elliotte36b2082015-07-06 14:27:58 -06002432 // Construct the WSI surface description:
2433 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2434 demo->surface_description.pNext = NULL;
2435#ifdef _WIN32
2436 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2437 demo->surface_description.pPlatformHandle = demo->connection;
2438 demo->surface_description.pPlatformWindow = demo->window;
2439#else // _WIN32
2440 demo->platform_handle_xcb.connection = demo->connection;
2441 demo->platform_handle_xcb.root = demo->screen->root;
2442 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2443 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2444 demo->surface_description.pPlatformWindow = &demo->window;
2445#endif // _WIN32
2446
2447 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002448 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2449 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002450 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2451 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2452 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002453 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002454
2455 // Search for a graphics and a present queue in the array of queue
2456 // families, try to find one that supports both
2457 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2458 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002459 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002460 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2461 if (graphicsQueueNodeIndex == UINT32_MAX) {
2462 graphicsQueueNodeIndex = i;
2463 }
2464
2465 if (supportsPresent[i] == VK_TRUE) {
2466 graphicsQueueNodeIndex = i;
2467 presentQueueNodeIndex = i;
2468 break;
2469 }
2470 }
2471 }
2472 if (presentQueueNodeIndex == UINT32_MAX) {
2473 // If didn't find a queue that supports both graphics and present, then
2474 // find a separate present queue.
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002475 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002476 if (supportsPresent[i] == VK_TRUE) {
2477 presentQueueNodeIndex = i;
2478 break;
2479 }
2480 }
2481 }
2482 free(supportsPresent);
2483
2484 // Generate error if could not find both a graphics and a present queue
2485 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2486 ERR_EXIT("Could not find a graphics and a present queue\n",
2487 "WSI Initialization Failure");
2488 }
2489
2490 // TODO: Add support for separate queues, including presentation,
2491 // synchronization, and appropriate tracking for QueueSubmit
2492 // While it is possible for an application to use a separate graphics and a
2493 // present queues, this demo program assumes it is only using one:
2494 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2495 ERR_EXIT("Could not find a common graphics and a present queue\n",
2496 "WSI Initialization Failure");
2497 }
2498
2499 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002500
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002501 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002502 0, &demo->queue);
2503 assert(!err);
Tony Barbour5685ad72015-04-29 16:19:20 -06002504
Ian Elliotte36b2082015-07-06 14:27:58 -06002505 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002506 uint32_t formatCount;
2507 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002508 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002509 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002510 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002511 VkSurfaceFormatWSI *surfFormats =
2512 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
2513 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002514 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002515 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002516 assert(!err);
2517 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2518 // the surface has no preferred format. Otherwise, at least one
2519 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002520 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2521 {
2522 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2523 }
2524 else
2525 {
2526 assert(formatCount >= 1);
2527 demo->format = surfFormats[0].format;
2528 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002529 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002530
David Pinedoeeca2a22015-06-18 17:03:14 -06002531 demo->quit = false;
2532 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002533
2534 // Get Memory information and properties
2535 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2536 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002537}
2538
2539static void demo_init_connection(struct demo *demo)
2540{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002541#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002542 const xcb_setup_t *setup;
2543 xcb_screen_iterator_t iter;
2544 int scr;
2545
2546 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002547 if (demo->connection == NULL) {
2548 printf("Cannot find a compatible Vulkan installable client driver "
2549 "(ICD).\nExiting ...\n");
2550 fflush(stdout);
2551 exit(1);
2552 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002553
2554 setup = xcb_get_setup(demo->connection);
2555 iter = xcb_setup_roots_iterator(setup);
2556 while (scr-- > 0)
2557 xcb_screen_next(&iter);
2558
2559 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002560#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002561}
2562
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002563static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002564{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002565 vec3 eye = {0.0f, 3.0f, 5.0f};
2566 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002567 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002568
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002569 memset(demo, 0, sizeof(*demo));
David Pinedoeeca2a22015-06-18 17:03:14 -06002570 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002571
Piers Daniell886be472015-02-23 16:23:13 -07002572 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002573 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002574 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002575 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002576 }
Cody Northrop75db0322015-05-28 11:27:16 -06002577 if (strcmp(argv[i], "--use_glsl") == 0) {
2578 demo->use_glsl = true;
2579 continue;
2580 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002581 if (strcmp(argv[i], "--break") == 0) {
2582 demo->use_break = true;
2583 continue;
2584 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002585 if (strcmp(argv[i], "--validate") == 0) {
2586 demo->validate = true;
2587 continue;
2588 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002589 if (strcmp(argv[i], "--c") == 0 &&
2590 demo->frameCount == INT_MAX &&
2591 i < argc-1 &&
2592 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2593 demo->frameCount >= 0)
2594 {
2595 i++;
2596 continue;
2597 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002598
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002599 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002600 fflush(stderr);
2601 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002602 }
2603
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002604 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002605 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002606
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002607 demo->width = 500;
2608 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002609
2610 demo->spin_angle = 0.01f;
2611 demo->spin_increment = 0.01f;
2612 demo->pause = false;
2613
Piers Daniell886be472015-02-23 16:23:13 -07002614 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002615 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2616 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002617}
2618
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002619
Ian Elliotte14e9f92015-04-16 15:23:05 -06002620#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002621extern int __getmainargs(
2622 int * _Argc,
2623 char *** _Argv,
2624 char *** _Env,
2625 int _DoWildCard,
2626 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002627
Ian Elliott421107f2015-04-28 15:50:36 -06002628int WINAPI WinMain(HINSTANCE hInstance,
2629 HINSTANCE hPrevInstance,
2630 LPSTR pCmdLine,
2631 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002632{
2633 MSG msg; // message
2634 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002635 int argc;
2636 char** argv;
2637 char** env;
2638 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002639
Tony Barbour5685ad72015-04-29 16:19:20 -06002640 __getmainargs(&argc,&argv,&env,0,&new_mode);
2641
2642 demo_init(&demo, argc, argv);
2643 demo.connection = hInstance;
2644 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002645 demo_create_window(&demo);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002646 demo_init_vk_wsi(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002647
2648 demo_prepare(&demo);
2649
2650 done = false; //initialize loop condition variable
2651 /* main message loop*/
2652 while(!done)
2653 {
Ian Elliott421107f2015-04-28 15:50:36 -06002654 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002655 if (msg.message == WM_QUIT) //check for a quit message
2656 {
2657 done = true; //if found, quit app
2658 }
2659 else
2660 {
2661 /* Translate and dispatch to event queue*/
2662 TranslateMessage(&msg);
2663 DispatchMessage(&msg);
2664 }
2665 }
2666
2667 demo_cleanup(&demo);
2668
Tony Barboura938abb2015-04-22 11:36:22 -06002669 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002670}
2671#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002672int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002673{
2674 struct demo demo;
2675
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002676 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002677 demo_create_window(&demo);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002678 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002679
2680 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002681 demo_run(&demo);
2682
2683 demo_cleanup(&demo);
2684
2685 return 0;
2686}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002687#endif // _WIN32