blob: ecbdbd2c3323ee2d15a062002e563410ca23d545 [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,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600468 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600469 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600470 }
471
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600472 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600473 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600474 .pNext = NULL,
475 .outputMask = 0,
476 .inputMask = 0,
477 .oldLayout = old_image_layout,
478 .newLayout = new_image_layout,
479 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400480 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600481 };
482
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600483 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600484 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600485 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600486 }
487
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600488 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600489 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600490 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600491 }
492
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600493 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600494
Tony Barbourc2e987e2015-06-29 16:20:35 -0600495 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
496 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600497
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600498 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600499}
500
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600501static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600502{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800503 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600504 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600505 .pNext = NULL,
Courtney Goeltzenleuchter6d4b7522015-07-28 08:59:17 -0600506 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700507 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800508 const VkClearValue clear_values[2] = {
509 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
510 [1] = { .ds = { 1.0f, 0 } },
511 };
512 const VkRenderPassBeginInfo rp_begin = {
513 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
514 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800515 .renderPass = demo->render_pass,
516 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800517 .renderArea.offset.x = 0,
518 .renderArea.offset.y = 0,
519 .renderArea.extent.width = demo->width,
520 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600521 .clearValueCount = 2,
522 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700523 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800524 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600525
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600526 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600527 assert(!err);
528
Chia-I Wuc278df82015-07-07 11:50:03 +0800529 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
530
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600531 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600532 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600533 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600534 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600535
Tony Barbourde4124d2015-07-03 10:33:54 -0600536 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
537 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
538 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
539 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600540
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600541 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800542 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600543
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600544 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600545 assert(!err);
546}
547
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600548
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600549void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600550{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600551 mat4x4 MVP, Model, VP;
552 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600553 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600554 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600555
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600556 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600557
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600558 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600559 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700560 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600561 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600562
Mark Lobodzinski23182612015-05-29 09:32:35 -0500563 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600564 assert(!err);
565
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600566 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600567
Mark Lobodzinski23182612015-05-29 09:32:35 -0500568 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600569 assert(!err);
570}
571
572static void demo_draw(struct demo *demo)
573{
Tony Barbour22a30862015-04-22 09:02:32 -0600574 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600575 VkSemaphore presentCompleteSemaphore;
576 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
577 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
578 .pNext = NULL,
579 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
580 };
Tony Barbourde4124d2015-07-03 10:33:54 -0600581 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600582
Ian Elliotte36b2082015-07-06 14:27:58 -0600583 err = vkCreateSemaphore(demo->device,
584 &presentCompleteSemaphoreCreateInfo,
585 &presentCompleteSemaphore);
586 assert(!err);
587
588 // Get the index of the next available swapchain image:
589 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
590 UINT64_MAX,
591 presentCompleteSemaphore,
592 &demo->current_buffer);
593 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
594 // return codes
595 assert(!err);
596
597 // Wait for the present complete semaphore to be signaled to ensure
598 // that the image won't be rendered to until the presentation
599 // engine has fully released ownership to the application, and it is
600 // okay to render to the image.
601 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
602
603// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600604 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbourde4124d2015-07-03 10:33:54 -0600605 nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600606 assert(!err);
607
Ian Elliotte36b2082015-07-06 14:27:58 -0600608 VkPresentInfoWSI present = {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600609 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
Ian Elliotte36b2082015-07-06 14:27:58 -0600610 .pNext = NULL,
611 .swapChainCount = 1,
612 .swapChains = &demo->swap_chain,
613 .imageIndices = &demo->current_buffer,
614 };
615
616// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburncedc15f2015-05-21 18:13:33 -0600617 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliotte36b2082015-07-06 14:27:58 -0600618 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
619 // return codes
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600620 assert(!err);
621
Tony Barbour9400f092015-07-21 09:04:41 -0600622 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliotte36b2082015-07-06 14:27:58 -0600623 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800624
625 err = vkQueueWaitIdle(demo->queue);
626 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600627}
628
629static void demo_prepare_buffers(struct demo *demo)
630{
Ian Elliotte36b2082015-07-06 14:27:58 -0600631 VkResult U_ASSERT_ONLY err;
632
633 // Check the surface properties and formats
Ian Elliott7fe115d2015-08-07 15:56:59 -0600634 VkSurfacePropertiesWSI surfProperties;
635 err = demo->fpGetSurfacePropertiesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600636 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600637 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600638 assert(!err);
639
Ian Elliott7fe115d2015-08-07 15:56:59 -0600640 uint32_t presentModeCount;
641 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600642 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600643 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600644 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600645 VkPresentModeWSI *presentModes =
646 (VkPresentModeWSI *)malloc(presentModeCount * sizeof(VkPresentModeWSI));
647 assert(presentModes);
648 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600649 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600650 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600651 assert(!err);
652
653 VkExtent2D swapChainExtent;
654 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600655 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600656 {
657 // If the surface size is undefined, the size is set to
658 // the size of the images requested.
659 swapChainExtent.width = demo->width;
660 swapChainExtent.height = demo->height;
661 }
662 else
663 {
664 // If the surface size is defined, the swap chain size must match
Ian Elliott7fe115d2015-08-07 15:56:59 -0600665 swapChainExtent = surfProperties.currentExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600666 }
667
668 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3fc49c22015-07-27 13:53:11 -0600669 // tearing mode. If not, try IMMEDIATE which will usually be available,
670 // and is fastest (though it tears). If not, fall back to FIFO which is
671 // always available.
672 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600673 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600674 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600675 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
676 break;
677 }
Ian Elliott3fc49c22015-07-27 13:53:11 -0600678 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
Ian Elliott7fe115d2015-08-07 15:56:59 -0600679 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
Ian Elliott3fc49c22015-07-27 13:53:11 -0600680 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
681 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600682 }
683
Ian Elliotta71c0052015-07-13 12:20:56 -0600684#define WORK_AROUND_CODE
685#ifdef WORK_AROUND_CODE
Ian Elliott7fe115d2015-08-07 15:56:59 -0600686 // After the proper code was created, other parts of this demo were
687 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
688 // images, etc. Live with that for now.
689 // TODO: Rework this demo code to live with the number of buffers returned
690 // by vkCreateSwapChainWSI().
Ian Elliotta71c0052015-07-13 12:20:56 -0600691 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
692#else // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600693 // Determine the number of VkImage's to use in the swap chain (we desire to
694 // own only 1 image at a time, besides the images being displayed and
695 // queued for display):
Ian Elliott7fe115d2015-08-07 15:56:59 -0600696 uint32_t desiredNumberOfSwapChainImages = surfProperties.minImageCount + 1;
697 if ((surfProperties.maxImageCount > 0) &&
698 (desiredNumberOfSwapChainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600699 {
700 // Application must settle for fewer images than desired:
Ian Elliott7fe115d2015-08-07 15:56:59 -0600701 desiredNumberOfSwapChainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600702 }
Ian Elliotta71c0052015-07-13 12:20:56 -0600703#endif // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600704
705 VkSurfaceTransformFlagBitsWSI preTransform;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600706 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600707 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
708 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600709 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600710 }
711
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800712 const VkSwapChainCreateInfoWSI swap_chain = {
713 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
714 .pNext = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600715 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
716 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800717 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600718 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800719 .imageExtent = {
Ian Elliotte36b2082015-07-06 14:27:58 -0600720 .width = swapChainExtent.width,
721 .height = swapChainExtent.height,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600722 },
Ian Elliotte36b2082015-07-06 14:27:58 -0600723 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800724 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600725 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
726 .queueFamilyCount = 0,
727 .pQueueFamilyIndices = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600728 .presentMode = swapChainPresentMode,
729 .oldSwapChain.handle = 0,
730 .clipped = true,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600731 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600732 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600733
Jon Ashburncedc15f2015-05-21 18:13:33 -0600734 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800735 assert(!err);
736
Ian Elliott7fe115d2015-08-07 15:56:59 -0600737 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
738 &demo->swapChainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600739 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800740
Ian Elliott7fe115d2015-08-07 15:56:59 -0600741 VkImage* swapChainImages =
742 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliotte36b2082015-07-06 14:27:58 -0600743 assert(swapChainImages);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600744 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
745 &demo->swapChainImageCount,
746 swapChainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600747 assert(!err);
Ian Elliotta71c0052015-07-13 12:20:56 -0600748#ifdef WORK_AROUND_CODE
Ian Elliott7fe115d2015-08-07 15:56:59 -0600749 // After the proper code was created, other parts of this demo were
750 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
751 // images, etc. Live with that for now.
752 // TODO: Rework this demo code to live with the number of buffers returned
753 // by vkCreateSwapChainWSI().
Ian Elliotta71c0052015-07-13 12:20:56 -0600754 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
Ian Elliotta71c0052015-07-13 12:20:56 -0600755#endif // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600756
757 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
758 assert(demo->buffers);
759
760 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800761 VkAttachmentViewCreateInfo color_attachment_view = {
762 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600763 .pNext = NULL,
764 .format = demo->format,
765 .mipLevel = 0,
766 .baseArraySlice = 0,
767 .arraySize = 1,
768 };
769
Ian Elliott7fe115d2015-08-07 15:56:59 -0600770 demo->buffers[i].image = swapChainImages[i];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600771
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600772 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400773 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600774 VK_IMAGE_LAYOUT_UNDEFINED,
775 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600776
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600777 color_attachment_view.image = demo->buffers[i].image;
778
Chia-I Wuc278df82015-07-07 11:50:03 +0800779 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600780 &color_attachment_view, &demo->buffers[i].view);
781 assert(!err);
782 }
783}
784
785static void demo_prepare_depth(struct demo *demo)
786{
Tony Barbour8205d902015-04-16 15:59:00 -0600787 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600788 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600789 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600790 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600791 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600792 .format = depth_format,
793 .extent = { demo->width, demo->height, 1 },
794 .mipLevels = 1,
795 .arraySize = 1,
796 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600797 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600798 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600799 .flags = 0,
800 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600801 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600802 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500803 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600804 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600805 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600806 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800807 VkAttachmentViewCreateInfo view = {
808 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600809 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600810 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4ab40c42015-07-15 17:41:38 -0600811 .format = depth_format,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600812 .mipLevel = 0,
813 .baseArraySlice = 0,
814 .arraySize = 1,
815 .flags = 0,
816 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600817
Mark Lobodzinski23182612015-05-29 09:32:35 -0500818 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600819 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600820
821 demo->depth.format = depth_format;
822
823 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600824 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600825 &demo->depth.image);
826 assert(!err);
827
Tony Barbourde4124d2015-07-03 10:33:54 -0600828 err = vkGetImageMemoryRequirements(demo->device,
829 demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600830
831 mem_alloc.allocationSize = mem_reqs.size;
832 err = memory_type_from_properties(demo,
833 mem_reqs.memoryTypeBits,
834 VK_MEMORY_PROPERTY_DEVICE_ONLY,
835 &mem_alloc.memoryTypeIndex);
836 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600837
Mark Lobodzinski23182612015-05-29 09:32:35 -0500838 /* allocate memory */
839 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
840 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600841
Mark Lobodzinski23182612015-05-29 09:32:35 -0500842 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600843 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500844 demo->depth.mem, 0);
845 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600846
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600847 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400848 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600849 VK_IMAGE_LAYOUT_UNDEFINED,
850 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600851
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600852 /* create image view */
853 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800854 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600855 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600856}
857
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600858/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600859 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600860 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600861 * \param demo : Needed to access VK calls
862 * \param filename : the png file to be loaded
863 * \param width : width of png, to be updated as a side effect of this function
864 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600865 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600866 * \return bool : an opengl texture id. true if successful?,
867 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600868 *
869 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
870 * Modified to copy image to memory
871 *
872 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700873bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600874 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600875 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600876{
877 //header for testing if it is a png
878 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600879 int is_png, bit_depth, color_type, rowbytes;
880 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700881 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600882 png_structp png_ptr;
883 png_infop info_ptr, end_info;
884 png_byte *image_data;
885 png_bytep *row_pointers;
886
887 //open file as binary
888 FILE *fp = fopen(filename, "rb");
889 if (!fp) {
890 return false;
891 }
892
893 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600894 retval = fread(header, 1, 8, fp);
895 if (retval != 8) {
896 fclose(fp);
897 return false;
898 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600899
900 //test if png
901 is_png = !png_sig_cmp(header, 0, 8);
902 if (!is_png) {
903 fclose(fp);
904 return false;
905 }
906
907 //create png struct
908 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
909 NULL, NULL);
910 if (!png_ptr) {
911 fclose(fp);
912 return (false);
913 }
914
915 //create png info struct
916 info_ptr = png_create_info_struct(png_ptr);
917 if (!info_ptr) {
918 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
919 fclose(fp);
920 return (false);
921 }
922
923 //create png info struct
924 end_info = png_create_info_struct(png_ptr);
925 if (!end_info) {
926 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
927 fclose(fp);
928 return (false);
929 }
930
931 //png error stuff, not sure libpng man suggests this.
932 if (setjmp(png_jmpbuf(png_ptr))) {
933 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
934 fclose(fp);
935 return (false);
936 }
937
938 //init png reading
939 png_init_io(png_ptr, fp);
940
941 //let libpng know you already read the first 8 bytes
942 png_set_sig_bytes(png_ptr, 8);
943
944 // read all the info up to the image data
945 png_read_info(png_ptr, info_ptr);
946
947 // get info about png
948 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
949 NULL, NULL, NULL);
950
951 //update width and height based on png info
952 *width = twidth;
953 *height = theight;
954
955 // Require that incoming texture be 8bits per color component
956 // and 4 components (RGBA).
957 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
958 png_get_channels(png_ptr, info_ptr) != 4) {
959 return false;
960 }
961
962 if (rgba_data == NULL) {
963 // If data pointer is null, we just want the width & height
964 // clean up memory and close stuff
965 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
966 fclose(fp);
967
968 return true;
969 }
970
971 // Update the png info struct.
972 png_read_update_info(png_ptr, info_ptr);
973
974 // Row size in bytes.
975 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
976
977 // Allocate the image_data as a big block, to be given to opengl
978 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
979 if (!image_data) {
980 //clean up memory and close stuff
981 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
982 fclose(fp);
983 return false;
984 }
985
986 // row_pointers is for pointing to image_data for reading the png with libpng
987 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
988 if (!row_pointers) {
989 //clean up memory and close stuff
990 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
991 // delete[] image_data;
992 fclose(fp);
993 return false;
994 }
995 // set the individual row_pointers to point at the correct offsets of image_data
996 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700997 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600998
999 // read the png into image_data through row_pointers
1000 png_read_image(png_ptr, row_pointers);
1001
1002 // clean up memory and close stuff
1003 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1004 free(row_pointers);
1005 free(image_data);
1006 fclose(fp);
1007
1008 return true;
1009}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001010
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001011static void demo_prepare_texture_image(struct demo *demo,
1012 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001013 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001014 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001015 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001016 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001017{
Mike Stroyande24a6f2015-06-15 14:21:03 -06001018 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001019 int32_t tex_width;
1020 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -06001021 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001022
David Pinedo61e77382015-04-23 08:16:57 -06001023 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1024 {
1025 printf("Failed to load textures\n");
1026 fflush(stdout);
1027 exit(1);
1028 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001029
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001030 tex_obj->tex_width = tex_width;
1031 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001032
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001033 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001034 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001035 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -06001036 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001037 .format = tex_format,
1038 .extent = { tex_width, tex_height, 1 },
1039 .mipLevels = 1,
1040 .arraySize = 1,
1041 .samples = 1,
1042 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001043 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001044 .flags = 0,
1045 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001046 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001047 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001048 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001049 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001050 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001051 };
1052
Mark Lobodzinski23182612015-05-29 09:32:35 -05001053 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001054
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001055 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001056 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001057 assert(!err);
1058
Tony Barbourde4124d2015-07-03 10:33:54 -06001059 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbour426b9052015-06-24 16:06:58 -06001060 assert(!err);
Piers Daniell886be472015-02-23 16:23:13 -07001061
Mark Lobodzinski23182612015-05-29 09:32:35 -05001062 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001063
Mark Lobodzinski72346292015-07-02 16:49:40 -06001064 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1065 assert(!err);
1066
Mark Lobodzinski23182612015-05-29 09:32:35 -05001067 /* allocate memory */
1068 err = vkAllocMemory(demo->device, &mem_alloc,
1069 &(tex_obj->mem));
1070 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001071
Mark Lobodzinski23182612015-05-29 09:32:35 -05001072 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -06001073 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001074 tex_obj->mem, 0);
1075 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001076
Tony Barbour8205d902015-04-16 15:59:00 -06001077 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001078 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001079 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001080 .mipLevel = 0,
1081 .arraySlice = 0,
1082 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001083 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001084 void *data;
1085
Tony Barbour426b9052015-06-24 16:06:58 -06001086 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1087 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001088
Mark Lobodzinski23182612015-05-29 09:32:35 -05001089 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001090 assert(!err);
1091
1092 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1093 fprintf(stderr, "Error loading texture: %s\n", filename);
1094 }
1095
Mark Lobodzinski23182612015-05-29 09:32:35 -05001096 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001097 assert(!err);
1098 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001099
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001100 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001101 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001102 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001103 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001104 tex_obj->imageLayout);
1105 /* 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 -07001106}
1107
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001108static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001109{
1110 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -05001111 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001112 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001113}
1114
1115static void demo_prepare_textures(struct demo *demo)
1116{
Tony Barbour8205d902015-04-16 15:59:00 -06001117 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001118 VkFormatProperties props;
Tony Barbour22a30862015-04-22 09:02:32 -06001119 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001120 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001121
Courtney Goeltzenleuchter4da96aa2015-07-12 12:52:09 -06001122 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001123 assert(!err);
1124
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001125 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001126
FslNopper434db6a2015-05-06 21:42:01 +02001127 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001128 /* Device can texture using linear textures */
1129 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001130 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001131 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001132 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001133 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001134
1135 memset(&staging_texture, 0, sizeof(staging_texture));
1136 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001137 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001138
1139 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001140 VK_IMAGE_TILING_OPTIMAL,
1141 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1142 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001143
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001144 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001145 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001146 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001147 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001148
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001149 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001150 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001151 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001152 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001153
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001154 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001155 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001156 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001157 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001158 .destOffset = { 0, 0, 0 },
1159 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1160 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001161 vkCmdCopyImage(demo->cmd,
1162 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1163 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001164 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001165
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001166 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001167 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001168 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001169 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001170
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001171 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001172
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001173 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001174 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001175 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1176 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001177 }
1178
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001179 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001180 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001181 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001182 .magFilter = VK_TEX_FILTER_NEAREST,
1183 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001184 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001185 .addressU = VK_TEX_ADDRESS_CLAMP,
1186 .addressV = VK_TEX_ADDRESS_CLAMP,
1187 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001188 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001189 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001190 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001191 .minLod = 0.0f,
1192 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001193 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001194 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001195
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001196 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001197 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001198 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -06001199 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001200 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001201 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001202 .channels = { VK_CHANNEL_SWIZZLE_R,
1203 VK_CHANNEL_SWIZZLE_G,
1204 VK_CHANNEL_SWIZZLE_B,
1205 VK_CHANNEL_SWIZZLE_A, },
1206 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001207 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001208
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001209 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001210 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001211 &demo->textures[i].sampler);
1212 assert(!err);
1213
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001214 /* create image view */
1215 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001216 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001217 &demo->textures[i].view);
1218 assert(!err);
1219 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001220}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001221
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001222void demo_prepare_cube_data_buffer(struct demo *demo)
1223{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001224 VkBufferCreateInfo buf_info;
1225 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001226 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001227 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001228 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001229 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001230 .memoryTypeIndex = 0,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001231 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001232 VkMemoryRequirements mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001233 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001234 int i;
1235 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001236 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001237 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001238
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001239 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001240 mat4x4_mul(MVP, VP, demo->model_matrix);
1241 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001242// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001243
1244 for (i=0; i<12*3; i++) {
1245 data.position[i][0] = g_vertex_buffer_data[i*3];
1246 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1247 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1248 data.position[i][3] = 1.0f;
1249 data.attr[i][0] = g_uv_buffer_data[2*i];
1250 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1251 data.attr[i][2] = 0;
1252 data.attr[i][3] = 0;
1253 }
1254
Chia-I Wu714df452015-01-01 07:55:04 +08001255 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001256 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001257 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop86049392015-07-16 10:29:32 -06001258 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001259 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001260 assert(!err);
1261
Tony Barbourde4124d2015-07-03 10:33:54 -06001262 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchteraca30a52015-07-15 11:17:08 -06001263 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001264
Mark Lobodzinski23182612015-05-29 09:32:35 -05001265 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -06001266 err = memory_type_from_properties(demo,
1267 mem_reqs.memoryTypeBits,
1268 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1269 &alloc_info.memoryTypeIndex);
1270 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001271
Mark Lobodzinski23182612015-05-29 09:32:35 -05001272 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1273 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001274
Mark Lobodzinski23182612015-05-29 09:32:35 -05001275 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1276 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001277
Mark Lobodzinski23182612015-05-29 09:32:35 -05001278 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001279
Mark Lobodzinski23182612015-05-29 09:32:35 -05001280 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1281 assert(!err);
1282
Tony Barbourde4124d2015-07-03 10:33:54 -06001283 err = vkBindBufferMemory(demo->device,
1284 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001285 demo->uniform_data.mem, 0);
1286 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001287
1288 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001289 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001290 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001291 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001292 view_info.offset = 0;
1293 view_info.range = sizeof(data);
1294
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001295 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001296 assert(!err);
1297
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001298 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001299}
1300
Chia-I Wuf8385062015-01-04 16:27:24 +08001301static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001302{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001303 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001304 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001305 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001306 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001307 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001308 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001309 },
1310 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001311 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001312 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001313 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001314 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001315 },
1316 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001317 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001318 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001319 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001320 .count = 2,
1321 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001322 };
Tony Barbour22a30862015-04-22 09:02:32 -06001323 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001324
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001325 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001326 &descriptor_layout, &demo->desc_layout);
1327 assert(!err);
1328
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001329 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1330 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1331 .pNext = NULL,
1332 .descriptorSetCount = 1,
1333 .pSetLayouts = &demo->desc_layout,
1334 };
1335
1336 err = vkCreatePipelineLayout(demo->device,
1337 &pPipelineLayoutCreateInfo,
1338 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001339 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001340}
1341
Chia-I Wu76cd4222015-07-08 13:34:24 +08001342static void demo_prepare_render_pass(struct demo *demo)
1343{
Chia-I Wuc278df82015-07-07 11:50:03 +08001344 const VkAttachmentDescription attachments[2] = {
1345 [0] = {
1346 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1347 .pNext = NULL,
1348 .format = demo->format,
1349 .samples = 1,
1350 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1351 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1352 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1353 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1354 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1355 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1356 },
1357 [1] = {
1358 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1359 .pNext = NULL,
1360 .format = demo->depth.format,
1361 .samples = 1,
1362 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1363 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1364 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1365 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1366 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1367 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1368 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001369 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001370 const VkAttachmentReference color_reference = {
1371 .attachment = 0,
1372 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1373 };
1374 const VkSubpassDescription subpass = {
1375 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1376 .pNext = NULL,
1377 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1378 .flags = 0,
1379 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001380 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001381 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001382 .pColorAttachments = &color_reference,
1383 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001384 .depthStencilAttachment = {
1385 .attachment = 1,
1386 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1387 },
1388 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001389 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001390 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001391 const VkRenderPassCreateInfo rp_info = {
1392 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1393 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001394 .attachmentCount = 2,
1395 .pAttachments = attachments,
1396 .subpassCount = 1,
1397 .pSubpasses = &subpass,
1398 .dependencyCount = 0,
1399 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001400 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001401 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001402
1403 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1404 assert(!err);
1405}
1406
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001407static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001408 VkShaderStage stage,
Tony Barbour9400f092015-07-21 09:04:41 -06001409 VkShaderModule* pShaderModule,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001410 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001411 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001412{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001413 VkShaderModuleCreateInfo moduleCreateInfo;
1414 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001415 VkShader shader;
1416 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001417
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001418
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001419 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1420 moduleCreateInfo.pNext = NULL;
1421
1422 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1423 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001424 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001425
Cody Northrop75db0322015-05-28 11:27:16 -06001426 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001427 moduleCreateInfo.codeSize = size;
1428 moduleCreateInfo.pCode = code;
1429 moduleCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001430 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001431 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001432 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001433 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001434
1435 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001436 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001437 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001438 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001439 } else {
1440 // Create fake SPV structure to feed GLSL
1441 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001442 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1443 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1444 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001445
1446 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001447 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1448 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1449 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1450 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001451
Tony Barbour9400f092015-07-21 09:04:41 -06001452 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001453 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001454 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001455 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001456
1457 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001458 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001459 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001460 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001461 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001462 return shader;
1463}
1464
Cody Northropacfb0492015-03-17 15:55:58 -06001465char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001466{
1467 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001468 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001469 void *shader_code;
1470
1471 FILE *fp = fopen(filename, "rb");
1472 if (!fp) return NULL;
1473
1474 fseek(fp, 0L, SEEK_END);
1475 size = ftell(fp);
1476
1477 fseek(fp, 0L, SEEK_SET);
1478
1479 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001480 retval = fread(shader_code, size, 1, fp);
1481 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001482
1483 *psize = size;
1484
1485 return shader_code;
1486}
1487
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001488static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001489{
Cody Northrop75db0322015-05-28 11:27:16 -06001490 if (!demo->use_glsl) {
1491 void *vertShaderCode;
1492 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001493
Cody Northrop75db0322015-05-28 11:27:16 -06001494 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001495
Tony Barbour9400f092015-07-21 09:04:41 -06001496 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001497 vertShaderCode, size);
1498 } else {
1499 static const char *vertShaderText =
1500 "#version 140\n"
1501 "#extension GL_ARB_separate_shader_objects : enable\n"
1502 "#extension GL_ARB_shading_language_420pack : enable\n"
1503 "\n"
1504 "layout(binding = 0) uniform buf {\n"
1505 " mat4 MVP;\n"
1506 " vec4 position[12*3];\n"
1507 " vec4 attr[12*3];\n"
1508 "} ubuf;\n"
1509 "\n"
1510 "layout (location = 0) out vec4 texcoord;\n"
1511 "\n"
1512 "void main() \n"
1513 "{\n"
1514 " texcoord = ubuf.attr[gl_VertexID];\n"
1515 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1516 "\n"
1517 " // GL->VK conventions\n"
1518 " gl_Position.y = -gl_Position.y;\n"
1519 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1520 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001521
Tony Barbour9400f092015-07-21 09:04:41 -06001522 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001523 (const void *) vertShaderText,
1524 strlen(vertShaderText));
1525 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001526}
1527
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001528static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001529{
Cody Northrop75db0322015-05-28 11:27:16 -06001530 if (!demo->use_glsl) {
1531 void *fragShaderCode;
1532 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001533
Cody Northrop75db0322015-05-28 11:27:16 -06001534 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001535
Tony Barbour9400f092015-07-21 09:04:41 -06001536 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001537 fragShaderCode, size);
1538 } else {
1539 static const char *fragShaderText =
1540 "#version 140\n"
1541 "#extension GL_ARB_separate_shader_objects : enable\n"
1542 "#extension GL_ARB_shading_language_420pack : enable\n"
1543 "layout (binding = 1) uniform sampler2D tex;\n"
1544 "\n"
1545 "layout (location = 0) in vec4 texcoord;\n"
1546 "layout (location = 0) out vec4 uFragColor;\n"
1547 "void main() {\n"
1548 " uFragColor = texture(tex, texcoord.xy);\n"
1549 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001550
Tony Barbour9400f092015-07-21 09:04:41 -06001551 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001552 (const void *) fragShaderText,
1553 strlen(fragShaderText));
1554 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001555}
1556
1557static void demo_prepare_pipeline(struct demo *demo)
1558{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001559 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001560 VkPipelineCacheCreateInfo pipelineCache;
Tony Barboure307f582015-07-10 15:29:03 -06001561 VkPipelineInputAssemblyStateCreateInfo ia;
1562 VkPipelineRasterStateCreateInfo rs;
1563 VkPipelineColorBlendStateCreateInfo cb;
1564 VkPipelineDepthStencilStateCreateInfo ds;
1565 VkPipelineViewportStateCreateInfo vp;
1566 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001567 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001568
1569 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001570 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001571 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001572
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001573 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001574 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001575 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001576
1577 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001578 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001579 rs.fillMode = VK_FILL_MODE_SOLID;
1580 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001581 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001582 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001583
1584 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001585 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1586 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001587 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001588 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001589 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001590 cb.attachmentCount = 1;
1591 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001592
Tony Barbourfa6cac72015-01-16 14:27:35 -07001593 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001594 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001595 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001596
1597 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001598 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001599 ds.depthTestEnable = VK_TRUE;
1600 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001601 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001602 ds.depthBoundsEnable = VK_FALSE;
1603 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1604 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001605 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001606 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001607 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001608
Tony Barbourfa6cac72015-01-16 14:27:35 -07001609 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001610 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001611 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001612 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001613
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001614 // Two stages: vs and fs
1615 pipeline.stageCount = 2;
1616 VkPipelineShaderStageCreateInfo shaderStages[2];
1617 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1618
1619 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1620 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1621 shaderStages[0].shader = demo_prepare_vs(demo);
1622
1623 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1624 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1625 shaderStages[1].shader = demo_prepare_fs(demo);
1626
Jon Ashburn0d60d272015-07-09 15:02:25 -06001627 memset(&pipelineCache, 0, sizeof(pipelineCache));
1628 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001629
Jon Ashburn0d60d272015-07-09 15:02:25 -06001630 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1631 assert(!err);
Tony Barboure307f582015-07-10 15:29:03 -06001632
1633 pipeline.pVertexInputState = NULL;
1634 pipeline.pInputAssemblyState = &ia;
1635 pipeline.pRasterState = &rs;
1636 pipeline.pColorBlendState = &cb;
1637 pipeline.pMultisampleState = &ms;
1638 pipeline.pViewportState = &vp;
1639 pipeline.pDepthStencilState = &ds;
1640 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001641 pipeline.renderPass = demo->render_pass;
Tony Barboure307f582015-07-10 15:29:03 -06001642
Jon Ashburn0d60d272015-07-09 15:02:25 -06001643 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001644 assert(!err);
1645
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001646 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001647 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001648 }
Tony Barbour9400f092015-07-21 09:04:41 -06001649 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1650 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001651}
1652
1653static void demo_prepare_dynamic_states(struct demo *demo)
1654{
Tony Barbourde4124d2015-07-03 10:33:54 -06001655 VkDynamicViewportStateCreateInfo viewport_create;
1656 VkDynamicRasterStateCreateInfo raster;
1657 VkDynamicColorBlendStateCreateInfo color_blend;
1658 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001659 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001660
Tony Barbourfa6cac72015-01-16 14:27:35 -07001661 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001662 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001663 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001664 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001665 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001666 viewport.height = (float) demo->height;
1667 viewport.width = (float) demo->width;
1668 viewport.minDepth = (float) 0.0f;
1669 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001670 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001671 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001672 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001673 scissor.extent.width = demo->width;
1674 scissor.extent.height = demo->height;
1675 scissor.offset.x = 0;
1676 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001677 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001678
1679 memset(&raster, 0, sizeof(raster));
Tony Barbourde4124d2015-07-03 10:33:54 -06001680 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001681 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001682
1683 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourde4124d2015-07-03 10:33:54 -06001684 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001685 color_blend.blendConst[0] = 1.0f;
1686 color_blend.blendConst[1] = 1.0f;
1687 color_blend.blendConst[2] = 1.0f;
1688 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001689
1690 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourde4124d2015-07-03 10:33:54 -06001691 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001692 depth_stencil.minDepthBounds = 0.0f;
1693 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001694 depth_stencil.stencilBackRef = 0;
1695 depth_stencil.stencilFrontRef = 0;
1696 depth_stencil.stencilReadMask = 0xff;
1697 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001698
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001699 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001700 assert(!err);
1701
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001702 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001703 assert(!err);
1704
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001705 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001706 &color_blend, &demo->color_blend);
1707 assert(!err);
1708
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001709 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001710 &depth_stencil, &demo->depth_stencil);
1711 assert(!err);
1712}
1713
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001714static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001715{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001716 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001717 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001718 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001719 .count = 1,
1720 },
1721 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001722 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001723 .count = DEMO_TEXTURE_COUNT,
1724 },
1725 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001726 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001727 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001728 .pNext = NULL,
1729 .count = 2,
1730 .pTypeCount = type_counts,
1731 };
Tony Barbour22a30862015-04-22 09:02:32 -06001732 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001733
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001734 err = vkCreateDescriptorPool(demo->device,
1735 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001736 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001737 assert(!err);
1738}
1739
1740static void demo_prepare_descriptor_set(struct demo *demo)
1741{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001742 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1743 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001744 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001745 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001746
Mike Stroyan230e6252015-04-17 12:36:38 -06001747 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001748 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001749 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001750 &demo->desc_set);
1751 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001752
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001753 memset(&tex_descs, 0, sizeof(tex_descs));
1754 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1755 tex_descs[i].sampler = demo->textures[i].sampler;
1756 tex_descs[i].imageView = demo->textures[i].view;
1757 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1758 }
1759
1760 memset(&writes, 0, sizeof(writes));
1761
1762 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1763 writes[0].destSet = demo->desc_set;
1764 writes[0].count = 1;
1765 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1766 writes[0].pDescriptors = &demo->uniform_data.desc;
1767
1768 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1769 writes[1].destSet = demo->desc_set;
1770 writes[1].destBinding = 1;
1771 writes[1].count = DEMO_TEXTURE_COUNT;
1772 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1773 writes[1].pDescriptors = tex_descs;
1774
1775 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1776 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001777}
1778
Chia-I Wu76cd4222015-07-08 13:34:24 +08001779static void demo_prepare_framebuffers(struct demo *demo)
1780{
Cody Northropf110c6e2015-08-04 10:47:08 -06001781 VkAttachmentView attachments[2];
1782 attachments[1] = demo->depth.view;
1783
Chia-I Wu76cd4222015-07-08 13:34:24 +08001784 const VkFramebufferCreateInfo fb_info = {
1785 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1786 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001787 .renderPass = demo->render_pass,
1788 .attachmentCount = 2,
1789 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001790 .width = demo->width,
1791 .height = demo->height,
1792 .layers = 1,
1793 };
1794 VkResult U_ASSERT_ONLY err;
1795 uint32_t i;
1796
1797 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001798 attachments[0] = demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001799 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1800 assert(!err);
1801 }
1802}
1803
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001804static void demo_prepare(struct demo *demo)
1805{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001806 VkResult U_ASSERT_ONLY err;
1807
1808 const VkCmdPoolCreateInfo cmd_pool_info = {
1809 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1810 .pNext = NULL,
1811 .queueFamilyIndex = demo->graphics_queue_node_index,
1812 .flags = 0,
1813 };
1814 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1815 assert(!err);
1816
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001817 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001818 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001819 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001820 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001821 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001822 .flags = 0,
1823 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001824
1825 demo_prepare_buffers(demo);
1826 demo_prepare_depth(demo);
1827 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001828 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001829
Chia-I Wuf8385062015-01-04 16:27:24 +08001830 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001831 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001832 demo_prepare_pipeline(demo);
1833 demo_prepare_dynamic_states(demo);
1834
Ian Elliotte36b2082015-07-06 14:27:58 -06001835 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001836 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001837 assert(!err);
1838 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001839
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001840 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001841 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001842
Chia-I Wu76cd4222015-07-08 13:34:24 +08001843 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001844
Ian Elliotte36b2082015-07-06 14:27:58 -06001845 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001846 demo->current_buffer = i;
1847 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1848 }
1849
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001850 /*
1851 * Prepare functions above may generate pipeline commands
1852 * that need to be flushed before beginning the render loop.
1853 */
1854 demo_flush_init_cmd(demo);
1855
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001856 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001857 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001858}
1859
David Pinedoeeca2a22015-06-18 17:03:14 -06001860static void demo_cleanup(struct demo *demo)
1861{
1862 uint32_t i;
1863
1864 demo->prepared = false;
1865
Tony Barbourde4124d2015-07-03 10:33:54 -06001866 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1867 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1868 }
Tony Barbourb857d312015-07-10 10:50:45 -06001869 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06001870 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001871
Tony Barbourde4124d2015-07-03 10:33:54 -06001872 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1873 vkDestroyDynamicRasterState(demo->device, demo->raster);
1874 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1875 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedoeeca2a22015-06-18 17:03:14 -06001876
Tony Barbourde4124d2015-07-03 10:33:54 -06001877 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001878 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbourde4124d2015-07-03 10:33:54 -06001879 vkDestroyRenderPass(demo->device, demo->render_pass);
1880 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1881 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -06001882
1883 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001884 vkDestroyImageView(demo->device, demo->textures[i].view);
1885 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001886 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001887 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedoeeca2a22015-06-18 17:03:14 -06001888 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001889 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedoeeca2a22015-06-18 17:03:14 -06001890
Tony Barbourde4124d2015-07-03 10:33:54 -06001891 vkDestroyAttachmentView(demo->device, demo->depth.view);
1892 vkDestroyImage(demo->device, demo->depth.image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001893 vkFreeMemory(demo->device, demo->depth.mem);
1894
Tony Barbourde4124d2015-07-03 10:33:54 -06001895 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1896 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedoeeca2a22015-06-18 17:03:14 -06001897 vkFreeMemory(demo->device, demo->uniform_data.mem);
1898
Ian Elliotte36b2082015-07-06 14:27:58 -06001899 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001900 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1901 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001902 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001903 free(demo->buffers);
David Pinedoeeca2a22015-06-18 17:03:14 -06001904
Cody Northrop18ea11b2015-07-09 18:08:32 -06001905 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedoeeca2a22015-06-18 17:03:14 -06001906 vkDestroyDevice(demo->device);
Tony Barboura65ecc22015-06-30 14:14:19 -06001907 if (demo->validate) {
1908 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1909 }
David Pinedoeeca2a22015-06-18 17:03:14 -06001910 vkDestroyInstance(demo->inst);
1911
1912#ifndef _WIN32
1913 xcb_destroy_window(demo->connection, demo->window);
1914 xcb_disconnect(demo->connection);
1915#endif // _WIN32
1916}
1917
1918// On MS-Windows, make this a global, so it's available to WndProc()
1919struct demo demo;
1920
Ian Elliotte14e9f92015-04-16 15:23:05 -06001921#ifdef _WIN32
1922static void demo_run(struct demo *demo)
1923{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001924 if (!demo->prepared)
1925 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001926 // Wait for work to finish before updating MVP.
1927 vkDeviceWaitIdle(demo->device);
1928 demo_update_data_buffer(demo);
1929
1930 demo_draw(demo);
1931
1932 // Wait for work to finish before updating MVP.
1933 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001934
David Pinedoeeca2a22015-06-18 17:03:14 -06001935 demo->curFrame++;
1936
1937 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1938 {
1939 demo->quit=true;
1940 demo_cleanup(demo);
1941 ExitProcess(0);
1942 }
1943
1944}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001945
1946// MS-Windows event handling function:
1947LRESULT CALLBACK WndProc(HWND hWnd,
1948 UINT uMsg,
1949 WPARAM wParam,
1950 LPARAM lParam)
1951{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001952 switch(uMsg)
1953 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001954 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001955 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001956 break;
Ian Elliotte36b2082015-07-06 14:27:58 -06001957 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001958 demo_run(&demo);
1959 return 0;
1960 default:
1961 break;
1962 }
1963 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1964}
1965
1966static void demo_create_window(struct demo *demo)
1967{
1968 WNDCLASSEX win_class;
1969
1970 // Initialize the window class structure:
1971 win_class.cbSize = sizeof(WNDCLASSEX);
1972 win_class.style = CS_HREDRAW | CS_VREDRAW;
1973 win_class.lpfnWndProc = WndProc;
1974 win_class.cbClsExtra = 0;
1975 win_class.cbWndExtra = 0;
1976 win_class.hInstance = demo->connection; // hInstance
1977 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1978 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1979 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1980 win_class.lpszMenuName = NULL;
1981 win_class.lpszClassName = demo->name;
1982 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1983 // Register window class:
1984 if (!RegisterClassEx(&win_class)) {
1985 // It didn't work, so try to give a useful error:
1986 printf("Unexpected error trying to start the application!\n");
1987 fflush(stdout);
1988 exit(1);
1989 }
1990 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001991 RECT wr = { 0, 0, demo->width, demo->height };
1992 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001993 demo->window = CreateWindowEx(0,
1994 demo->name, // class name
1995 demo->name, // app name
1996 WS_OVERLAPPEDWINDOW | // window style
1997 WS_VISIBLE |
1998 WS_SYSMENU,
1999 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06002000 wr.right-wr.left, // width
2001 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06002002 NULL, // handle to parent
2003 NULL, // handle to menu
2004 demo->connection, // hInstance
2005 NULL); // no extra parameters
2006 if (!demo->window) {
2007 // It didn't work, so try to give a useful error:
2008 printf("Cannot create a window in which to draw!\n");
2009 fflush(stdout);
2010 exit(1);
2011 }
2012}
2013#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002014static void demo_handle_event(struct demo *demo,
2015 const xcb_generic_event_t *event)
2016{
Piers Daniell886be472015-02-23 16:23:13 -07002017 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002018 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002019 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002020 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002021 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002022 case XCB_CLIENT_MESSAGE:
2023 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2024 (*demo->atom_wm_delete_window).atom) {
2025 demo->quit = true;
2026 }
2027 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002028 case XCB_KEY_RELEASE:
2029 {
2030 const xcb_key_release_event_t *key =
2031 (const xcb_key_release_event_t *) event;
2032
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002033 switch (key->detail) {
2034 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002035 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002036 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002037 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002038 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002039 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002040 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002041 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002042 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002043 case 0x41:
2044 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07002045 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002046 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002047 }
2048 break;
2049 default:
2050 break;
2051 }
2052}
2053
2054static void demo_run(struct demo *demo)
2055{
2056 xcb_flush(demo->connection);
2057
2058 while (!demo->quit) {
2059 xcb_generic_event_t *event;
2060
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002061 if (demo->pause) {
2062 event = xcb_wait_for_event(demo->connection);
2063 } else {
2064 event = xcb_poll_for_event(demo->connection);
2065 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002066 if (event) {
2067 demo_handle_event(demo, event);
2068 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002069 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002070
2071 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002072 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002073 demo_update_data_buffer(demo);
2074
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002075 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07002076
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002077 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002078 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06002079 demo->curFrame++;
2080 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2081 demo->quit = true;
2082
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002083 }
2084}
2085
2086static void demo_create_window(struct demo *demo)
2087{
2088 uint32_t value_mask, value_list[32];
2089
2090 demo->window = xcb_generate_id(demo->connection);
2091
2092 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2093 value_list[0] = demo->screen->black_pixel;
2094 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2095 XCB_EVENT_MASK_EXPOSURE;
2096
2097 xcb_create_window(demo->connection,
2098 XCB_COPY_FROM_PARENT,
2099 demo->window, demo->screen->root,
2100 0, 0, demo->width, demo->height, 0,
2101 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2102 demo->screen->root_visual,
2103 value_mask, value_list);
2104
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002105 /* Magic code that will send notification when window is destroyed */
2106 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2107 "WM_PROTOCOLS");
2108 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2109
2110 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2111 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2112
2113 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2114 demo->window, (*reply).atom, 4, 32, 1,
2115 &(*demo->atom_wm_delete_window).atom);
2116 free(reply);
2117
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002118 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06002119
2120 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2121 const uint32_t coords[] = {100, 100};
2122 xcb_configure_window(demo->connection, demo->window,
2123 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002124}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002125#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002126
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002127/*
2128 * Return 1 (true) if all layer names specified in check_names
2129 * can be found in given layer properties.
2130 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002131static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002132 uint32_t layer_count, VkLayerProperties *layers)
2133{
2134 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002135 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002136 for (uint32_t j = 0; j < layer_count; j++) {
2137 if (!strcmp(check_names[i], layers[j].layerName)) {
2138 found = 1;
2139 }
2140 }
2141 if (!found) {
2142 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2143 return 0;
2144 }
2145 }
2146 return 1;
2147}
2148
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002149static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002150{
Tobin Ehlis3536b442015-04-16 18:04:57 -06002151 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002152 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002153 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002154 VkLayerProperties *instance_layers;
2155 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002156 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002157 uint32_t instance_layer_count = 0;
2158 uint32_t enabled_extension_count = 0;
2159 uint32_t enabled_layer_count = 0;
2160
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002161 char *instance_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002162 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002163 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002164 "ObjectTracker",
2165 "DrawState",
2166 "ParamChecker",
2167 "ShaderChecker",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002168 };
2169
2170 char *device_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002171 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002172 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002173 "ObjectTracker",
2174 "DrawState",
2175 "ParamChecker",
2176 "ShaderChecker",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002177 };
2178
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002179 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002180 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002181 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002182 assert(!err);
2183
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002184 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2185 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2186 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002187
2188 if (demo->validate) {
2189 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2190 instance_layer_count, instance_layers);
2191 if (!validation_found) {
2192 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2193 "required validation layer.\n\n"
2194 "Please look at the Getting Started guide for additional "
2195 "information.\n",
2196 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002197 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002198 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002199 }
2200
2201 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2202 assert(!err);
2203
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002204 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002205 memset(extension_names, 0, sizeof(extension_names));
2206 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2207 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2208 assert(!err);
2209 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002210 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002211 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06002212 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002213 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002214 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002215 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002216 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002217 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002218 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002219 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002220 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002221 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06002222 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliotte36b2082015-07-06 14:27:58 -06002223 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002224 "Vulkan installable client driver (ICD) installed?\nPlease "
2225 "look at the Getting Started guide for additional "
2226 "information.\n",
2227 "vkCreateInstance Failure");
2228 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002229 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002230 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002231 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002232 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002233 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002234 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002235 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002236 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002237 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002238 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002239 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002240 .pNext = NULL,
2241 .pAppInfo = &app,
2242 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002243 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002244 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002245 .extensionCount = enabled_extension_count,
2246 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002247 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06002248 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12002249 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002250 .queueCount = 1,
2251 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002252
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002253 uint32_t gpu_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002254
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002255 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002256 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2257 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002258 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002259 "additional information.\n",
2260 "vkCreateInstance Failure");
Tony Barbour5685ad72015-04-29 16:19:20 -06002261 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2262 ERR_EXIT("Cannot find a specified extension library"
2263 ".\nMake sure your layers path is set appropriately\n",
2264 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002265 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002266 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2267 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002268 "the Getting Started guide for additional information.\n",
2269 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002270 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002271
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002272 free(instance_layers);
2273 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002274
Jon Ashburn07b309a2015-04-15 11:31:12 -06002275 gpu_count = 1;
2276 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002277 assert(!err && gpu_count == 1);
2278
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002279 /* Look for validation layers */
2280 validation_found = 0;
2281 enabled_layer_count = 0;
2282 uint32_t device_layer_count = 0;
2283 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2284 assert(!err);
2285
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002286 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2287 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2288 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002289
2290 if (demo->validate) {
2291 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2292 device_layer_count, device_layers);
2293 if (!validation_found) {
2294 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2295 "a required validation layer.\n\n"
2296 "Please look at the Getting Started guide for additional "
2297 "information.\n",
2298 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002299 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002300 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002301 }
2302
2303 uint32_t device_extension_count = 0;
2304 VkExtensionProperties *device_extensions = NULL;
2305 err = vkGetPhysicalDeviceExtensionProperties(
2306 demo->gpu, NULL, &device_extension_count, NULL);
2307 assert(!err);
2308
2309 WSIextFound = 0;
2310 enabled_extension_count = 0;
2311 memset(extension_names, 0, sizeof(extension_names));
2312 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2313 err = vkGetPhysicalDeviceExtensionProperties(
2314 demo->gpu, NULL, &device_extension_count, device_extensions);
2315 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06002316
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002317 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002318 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002319 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06002320 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002321 }
2322 assert(enabled_extension_count < 64);
2323 }
2324 if (!WSIextFound) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002325 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2326 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002327 "Vulkan installable client driver (ICD) installed?\nPlease "
2328 "look at the Getting Started guide for additional "
2329 "information.\n",
2330 "vkCreateInstance Failure");
2331 }
2332
2333 VkDeviceCreateInfo device = {
2334 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2335 .pNext = NULL,
2336 .queueRecordCount = 1,
2337 .pRequestedQueues = &queue,
2338 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002339 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002340 .extensionCount = enabled_extension_count,
2341 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002342 };
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002343
Tony Barbour5685ad72015-04-29 16:19:20 -06002344 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06002345 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2346 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002347 if (!demo->dbgCreateMsgCallback) {
2348 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2349 "vkGetProcAddr Failure");
2350 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002351 if (!demo->dbgDestroyMsgCallback) {
2352 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2353 "vkGetProcAddr Failure");
2354 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002355 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2356 if (!demo->dbgBreakCallback) {
2357 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2358 "vkGetProcAddr Failure");
2359 }
2360
2361 PFN_vkDbgMsgCallback callback;
2362
2363 if (!demo->use_break) {
2364 callback = dbgFunc;
2365 } else {
2366 callback = demo->dbgBreakCallback;
2367 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002368 err = demo->dbgCreateMsgCallback(
2369 demo->inst,
2370 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002371 callback, NULL,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002372 &demo->msg_callback);
2373 switch (err) {
2374 case VK_SUCCESS:
2375 break;
2376 case VK_ERROR_INVALID_POINTER:
2377 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2378 "dbgCreateMsgCallback Failure");
2379 break;
2380 case VK_ERROR_OUT_OF_HOST_MEMORY:
2381 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2382 "dbgCreateMsgCallback Failure");
2383 break;
2384 default:
2385 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2386 "dbgCreateMsgCallback Failure");
2387 break;
2388 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002389 }
2390
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002391 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002392 assert(!err);
2393
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002394 free(device_layers);
2395
Ian Elliotte36b2082015-07-06 14:27:58 -06002396 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002397 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
2398 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
2399 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06002400 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2401 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2402 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002403 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliotte36b2082015-07-06 14:27:58 -06002404 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06002405 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburncedc15f2015-05-21 18:13:33 -06002406
Tony Barbour426b9052015-06-24 16:06:58 -06002407 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002408 assert(!err);
2409
Cody Northropef72e2a2015-08-03 17:04:53 -06002410 /* Call with NULL data to get count */
2411 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002412 assert(!err);
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002413 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002414
Cody Northropef72e2a2015-08-03 17:04:53 -06002415 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2416 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002417 assert(!err);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002418 assert(demo->queue_count >= 1);
2419}
2420
2421static void demo_init_vk_wsi(struct demo *demo)
2422{
2423 VkResult err;
2424 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002425
Ian Elliotte36b2082015-07-06 14:27:58 -06002426 // Construct the WSI surface description:
2427 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2428 demo->surface_description.pNext = NULL;
2429#ifdef _WIN32
2430 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2431 demo->surface_description.pPlatformHandle = demo->connection;
2432 demo->surface_description.pPlatformWindow = demo->window;
2433#else // _WIN32
2434 demo->platform_handle_xcb.connection = demo->connection;
2435 demo->platform_handle_xcb.root = demo->screen->root;
2436 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2437 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2438 demo->surface_description.pPlatformWindow = &demo->window;
2439#endif // _WIN32
2440
2441 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002442 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2443 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002444 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2445 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2446 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002447 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002448
2449 // Search for a graphics and a present queue in the array of queue
2450 // families, try to find one that supports both
2451 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2452 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002453 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002454 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2455 if (graphicsQueueNodeIndex == UINT32_MAX) {
2456 graphicsQueueNodeIndex = i;
2457 }
2458
2459 if (supportsPresent[i] == VK_TRUE) {
2460 graphicsQueueNodeIndex = i;
2461 presentQueueNodeIndex = i;
2462 break;
2463 }
2464 }
2465 }
2466 if (presentQueueNodeIndex == UINT32_MAX) {
2467 // If didn't find a queue that supports both graphics and present, then
2468 // find a separate present queue.
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002469 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002470 if (supportsPresent[i] == VK_TRUE) {
2471 presentQueueNodeIndex = i;
2472 break;
2473 }
2474 }
2475 }
2476 free(supportsPresent);
2477
2478 // Generate error if could not find both a graphics and a present queue
2479 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2480 ERR_EXIT("Could not find a graphics and a present queue\n",
2481 "WSI Initialization Failure");
2482 }
2483
2484 // TODO: Add support for separate queues, including presentation,
2485 // synchronization, and appropriate tracking for QueueSubmit
2486 // While it is possible for an application to use a separate graphics and a
2487 // present queues, this demo program assumes it is only using one:
2488 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2489 ERR_EXIT("Could not find a common graphics and a present queue\n",
2490 "WSI Initialization Failure");
2491 }
2492
2493 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002494
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002495 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002496 0, &demo->queue);
2497 assert(!err);
Tony Barbour5685ad72015-04-29 16:19:20 -06002498
Ian Elliotte36b2082015-07-06 14:27:58 -06002499 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002500 uint32_t formatCount;
2501 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002502 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002503 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002504 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002505 VkSurfaceFormatWSI *surfFormats =
2506 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
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, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002510 assert(!err);
2511 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2512 // the surface has no preferred format. Otherwise, at least one
2513 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002514 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2515 {
2516 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2517 }
2518 else
2519 {
2520 assert(formatCount >= 1);
2521 demo->format = surfFormats[0].format;
2522 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002523 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002524
David Pinedoeeca2a22015-06-18 17:03:14 -06002525 demo->quit = false;
2526 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002527
2528 // Get Memory information and properties
2529 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2530 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002531}
2532
2533static void demo_init_connection(struct demo *demo)
2534{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002535#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002536 const xcb_setup_t *setup;
2537 xcb_screen_iterator_t iter;
2538 int scr;
2539
2540 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002541 if (demo->connection == NULL) {
2542 printf("Cannot find a compatible Vulkan installable client driver "
2543 "(ICD).\nExiting ...\n");
2544 fflush(stdout);
2545 exit(1);
2546 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002547
2548 setup = xcb_get_setup(demo->connection);
2549 iter = xcb_setup_roots_iterator(setup);
2550 while (scr-- > 0)
2551 xcb_screen_next(&iter);
2552
2553 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002554#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002555}
2556
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002557static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002558{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002559 vec3 eye = {0.0f, 3.0f, 5.0f};
2560 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002561 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002562
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002563 memset(demo, 0, sizeof(*demo));
David Pinedoeeca2a22015-06-18 17:03:14 -06002564 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002565
Piers Daniell886be472015-02-23 16:23:13 -07002566 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002567 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002568 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002569 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002570 }
Cody Northrop75db0322015-05-28 11:27:16 -06002571 if (strcmp(argv[i], "--use_glsl") == 0) {
2572 demo->use_glsl = true;
2573 continue;
2574 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002575 if (strcmp(argv[i], "--break") == 0) {
2576 demo->use_break = true;
2577 continue;
2578 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002579 if (strcmp(argv[i], "--validate") == 0) {
2580 demo->validate = true;
2581 continue;
2582 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002583 if (strcmp(argv[i], "--c") == 0 &&
2584 demo->frameCount == INT_MAX &&
2585 i < argc-1 &&
2586 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2587 demo->frameCount >= 0)
2588 {
2589 i++;
2590 continue;
2591 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002592
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002593 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002594 fflush(stderr);
2595 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002596 }
2597
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002598 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002599 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002600
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002601 demo->width = 500;
2602 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002603
2604 demo->spin_angle = 0.01f;
2605 demo->spin_increment = 0.01f;
2606 demo->pause = false;
2607
Piers Daniell886be472015-02-23 16:23:13 -07002608 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002609 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2610 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002611}
2612
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002613
Ian Elliotte14e9f92015-04-16 15:23:05 -06002614#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002615extern int __getmainargs(
2616 int * _Argc,
2617 char *** _Argv,
2618 char *** _Env,
2619 int _DoWildCard,
2620 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002621
Ian Elliott421107f2015-04-28 15:50:36 -06002622int WINAPI WinMain(HINSTANCE hInstance,
2623 HINSTANCE hPrevInstance,
2624 LPSTR pCmdLine,
2625 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002626{
2627 MSG msg; // message
2628 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002629 int argc;
2630 char** argv;
2631 char** env;
2632 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002633
Tony Barbour5685ad72015-04-29 16:19:20 -06002634 __getmainargs(&argc,&argv,&env,0,&new_mode);
2635
2636 demo_init(&demo, argc, argv);
2637 demo.connection = hInstance;
2638 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002639 demo_create_window(&demo);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002640 demo_init_vk_wsi(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002641
2642 demo_prepare(&demo);
2643
2644 done = false; //initialize loop condition variable
2645 /* main message loop*/
2646 while(!done)
2647 {
Ian Elliott421107f2015-04-28 15:50:36 -06002648 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002649 if (msg.message == WM_QUIT) //check for a quit message
2650 {
2651 done = true; //if found, quit app
2652 }
2653 else
2654 {
2655 /* Translate and dispatch to event queue*/
2656 TranslateMessage(&msg);
2657 DispatchMessage(&msg);
2658 }
2659 }
2660
2661 demo_cleanup(&demo);
2662
Tony Barboura938abb2015-04-22 11:36:22 -06002663 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002664}
2665#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002666int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002667{
2668 struct demo demo;
2669
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002670 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002671 demo_create_window(&demo);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002672 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002673
2674 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002675 demo_run(&demo);
2676
2677 demo_cleanup(&demo);
2678
2679 return 0;
2680}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002681#endif // _WIN32