blob: 10c6057cf22113d58369529752861994bcaeb4fc [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;
Cody Northropf5bd2252015-08-17 11:10:49 -0600368 VkDynamicRasterLineState raster_line;
369 VkDynamicRasterDepthBiasState raster_depth_bias;
Tony Barbourde4124d2015-07-03 10:33:54 -0600370 VkDynamicColorBlendState color_blend;
371 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600372
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600373 mat4x4 projection_matrix;
374 mat4x4 view_matrix;
375 mat4x4 model_matrix;
376
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600377 float spin_angle;
378 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600379 bool pause;
380
Tony Barbour9400f092015-07-21 09:04:41 -0600381 VkShaderModule vert_shader_module;
382 VkShaderModule frag_shader_module;
383
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600384 VkDescriptorPool desc_pool;
385 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800386
Chia-I Wu76cd4222015-07-08 13:34:24 +0800387 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
388
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600389 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600390 int32_t curFrame;
391 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600392 bool validate;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600393 bool use_break;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600394 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600395 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -0600396 PFN_vkDbgMsgCallback dbgBreakCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600397 VkDbgMsgCallback msg_callback;
398
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600399 uint32_t current_buffer;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -0600400 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600401};
402
Mark Lobodzinski72346292015-07-02 16:49:40 -0600403static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
404{
405 // Search memtypes to find first index with those properties
406 for (uint32_t i = 0; i < 32; i++) {
407 if ((typeBits & 1) == 1) {
408 // Type is available, does it match user properties?
409 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
410 *typeIndex = i;
411 return VK_SUCCESS;
412 }
413 }
414 typeBits >>= 1;
415 }
416 // No memory types matched, return failure
417 return VK_UNSUPPORTED;
418}
419
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600420static void demo_flush_init_cmd(struct demo *demo)
421{
Tony Barbour22a30862015-04-22 09:02:32 -0600422 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600423
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600424 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600425 return;
426
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600427 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600428 assert(!err);
429
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600430 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600431 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600432
Tony Barbourde4124d2015-07-03 10:33:54 -0600433 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600434 assert(!err);
435
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600436 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600437 assert(!err);
438
Tony Barbourde4124d2015-07-03 10:33:54 -0600439 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600440 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600441}
442
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600443static void demo_set_image_layout(
444 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600445 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400446 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600447 VkImageLayout old_image_layout,
448 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600449{
Tony Barbour22a30862015-04-22 09:02:32 -0600450 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600451
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600452 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600453 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600454 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600455 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600456 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800457 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600458 .flags = 0,
459 };
460
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600461 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600462 assert(!err);
463
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600464 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600465 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600466 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600467 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600468 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Cody Northrop16898b02015-08-11 11:35:58 -0600469 .renderPass = VK_NULL_HANDLE,
470 .subpass = 0,
471 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600472 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600473 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600474 }
475
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600476 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600477 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600478 .pNext = NULL,
479 .outputMask = 0,
480 .inputMask = 0,
481 .oldLayout = old_image_layout,
482 .newLayout = new_image_layout,
483 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400484 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600485 };
486
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600487 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600488 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600489 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600490 }
491
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600492 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600493 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600494 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600495 }
496
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600497 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600498
Tony Barbourc2e987e2015-06-29 16:20:35 -0600499 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
500 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600501
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600502 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600503}
504
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600505static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600506{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800507 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600508 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600509 .pNext = NULL,
Courtney Goeltzenleuchter6d4b7522015-07-28 08:59:17 -0600510 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT,
Cody Northrop16898b02015-08-11 11:35:58 -0600511 .renderPass = VK_NULL_HANDLE,
512 .subpass = 0,
513 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700514 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800515 const VkClearValue clear_values[2] = {
516 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
517 [1] = { .ds = { 1.0f, 0 } },
518 };
519 const VkRenderPassBeginInfo rp_begin = {
520 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
521 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800522 .renderPass = demo->render_pass,
523 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800524 .renderArea.offset.x = 0,
525 .renderArea.offset.y = 0,
526 .renderArea.extent.width = demo->width,
527 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600528 .clearValueCount = 2,
529 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700530 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800531 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600532
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600533 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600534 assert(!err);
535
Chia-I Wuc278df82015-07-07 11:50:03 +0800536 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
537
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600538 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600539 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600540 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600541 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600542
Tony Barbourde4124d2015-07-03 10:33:54 -0600543 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
Cody Northropf5bd2252015-08-17 11:10:49 -0600544 vkCmdBindDynamicRasterLineState(cmd_buf, demo->raster_line);
545 vkCmdBindDynamicRasterDepthBiasState(cmd_buf, demo->raster_depth_bias);
Tony Barbourde4124d2015-07-03 10:33:54 -0600546 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
547 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600548
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600549 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800550 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600551
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600552 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600553 assert(!err);
554}
555
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600556
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600557void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600558{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600559 mat4x4 MVP, Model, VP;
560 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600561 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600562 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600563
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600564 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600565
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600566 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600567 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700568 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600569 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600570
Mark Lobodzinski23182612015-05-29 09:32:35 -0500571 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600572 assert(!err);
573
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600574 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600575
Mark Lobodzinski23182612015-05-29 09:32:35 -0500576 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600577 assert(!err);
578}
579
580static void demo_draw(struct demo *demo)
581{
Tony Barbour22a30862015-04-22 09:02:32 -0600582 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600583 VkSemaphore presentCompleteSemaphore;
584 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
585 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
586 .pNext = NULL,
587 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
588 };
Tony Barbourde4124d2015-07-03 10:33:54 -0600589 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600590
Ian Elliotte36b2082015-07-06 14:27:58 -0600591 err = vkCreateSemaphore(demo->device,
592 &presentCompleteSemaphoreCreateInfo,
593 &presentCompleteSemaphore);
594 assert(!err);
595
596 // Get the index of the next available swapchain image:
597 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
598 UINT64_MAX,
599 presentCompleteSemaphore,
600 &demo->current_buffer);
601 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
602 // return codes
603 assert(!err);
604
605 // Wait for the present complete semaphore to be signaled to ensure
606 // that the image won't be rendered to until the presentation
607 // engine has fully released ownership to the application, and it is
608 // okay to render to the image.
609 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
610
611// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600612 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbourde4124d2015-07-03 10:33:54 -0600613 nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600614 assert(!err);
615
Ian Elliotte36b2082015-07-06 14:27:58 -0600616 VkPresentInfoWSI present = {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600617 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
Ian Elliotte36b2082015-07-06 14:27:58 -0600618 .pNext = NULL,
619 .swapChainCount = 1,
620 .swapChains = &demo->swap_chain,
621 .imageIndices = &demo->current_buffer,
622 };
623
624// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburncedc15f2015-05-21 18:13:33 -0600625 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliotte36b2082015-07-06 14:27:58 -0600626 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
627 // return codes
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600628 assert(!err);
629
Tony Barbour9400f092015-07-21 09:04:41 -0600630 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliotte36b2082015-07-06 14:27:58 -0600631 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800632
633 err = vkQueueWaitIdle(demo->queue);
634 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600635}
636
637static void demo_prepare_buffers(struct demo *demo)
638{
Ian Elliotte36b2082015-07-06 14:27:58 -0600639 VkResult U_ASSERT_ONLY err;
640
641 // Check the surface properties and formats
Ian Elliott7fe115d2015-08-07 15:56:59 -0600642 VkSurfacePropertiesWSI surfProperties;
643 err = demo->fpGetSurfacePropertiesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600644 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600645 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600646 assert(!err);
647
Ian Elliott7fe115d2015-08-07 15:56:59 -0600648 uint32_t presentModeCount;
649 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600650 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600651 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600652 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600653 VkPresentModeWSI *presentModes =
654 (VkPresentModeWSI *)malloc(presentModeCount * sizeof(VkPresentModeWSI));
655 assert(presentModes);
656 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600657 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600658 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600659 assert(!err);
660
661 VkExtent2D swapChainExtent;
662 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600663 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600664 {
665 // If the surface size is undefined, the size is set to
666 // the size of the images requested.
667 swapChainExtent.width = demo->width;
668 swapChainExtent.height = demo->height;
669 }
670 else
671 {
672 // If the surface size is defined, the swap chain size must match
Ian Elliott7fe115d2015-08-07 15:56:59 -0600673 swapChainExtent = surfProperties.currentExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600674 }
675
676 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3fc49c22015-07-27 13:53:11 -0600677 // tearing mode. If not, try IMMEDIATE which will usually be available,
678 // and is fastest (though it tears). If not, fall back to FIFO which is
679 // always available.
680 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600681 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600682 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600683 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
684 break;
685 }
Ian Elliott3fc49c22015-07-27 13:53:11 -0600686 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
Ian Elliott7fe115d2015-08-07 15:56:59 -0600687 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
Ian Elliott3fc49c22015-07-27 13:53:11 -0600688 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
689 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600690 }
691
Ian Elliotta71c0052015-07-13 12:20:56 -0600692#define WORK_AROUND_CODE
693#ifdef WORK_AROUND_CODE
Ian Elliott7fe115d2015-08-07 15:56:59 -0600694 // After the proper code was created, other parts of this demo were
695 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
696 // images, etc. Live with that for now.
697 // TODO: Rework this demo code to live with the number of buffers returned
698 // by vkCreateSwapChainWSI().
Ian Elliotta71c0052015-07-13 12:20:56 -0600699 uint32_t desiredNumberOfSwapChainImages = DEMO_BUFFER_COUNT;
700#else // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600701 // Determine the number of VkImage's to use in the swap chain (we desire to
702 // own only 1 image at a time, besides the images being displayed and
703 // queued for display):
Ian Elliott7fe115d2015-08-07 15:56:59 -0600704 uint32_t desiredNumberOfSwapChainImages = surfProperties.minImageCount + 1;
705 if ((surfProperties.maxImageCount > 0) &&
706 (desiredNumberOfSwapChainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600707 {
708 // Application must settle for fewer images than desired:
Ian Elliott7fe115d2015-08-07 15:56:59 -0600709 desiredNumberOfSwapChainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600710 }
Ian Elliotta71c0052015-07-13 12:20:56 -0600711#endif // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600712
713 VkSurfaceTransformFlagBitsWSI preTransform;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600714 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600715 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
716 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600717 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600718 }
719
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800720 const VkSwapChainCreateInfoWSI swap_chain = {
721 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
722 .pNext = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600723 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
724 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800725 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600726 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800727 .imageExtent = {
Ian Elliotte36b2082015-07-06 14:27:58 -0600728 .width = swapChainExtent.width,
729 .height = swapChainExtent.height,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600730 },
Ian Elliotte36b2082015-07-06 14:27:58 -0600731 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800732 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600733 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
734 .queueFamilyCount = 0,
735 .pQueueFamilyIndices = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600736 .presentMode = swapChainPresentMode,
737 .oldSwapChain.handle = 0,
738 .clipped = true,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600739 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600740 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600741
Jon Ashburncedc15f2015-05-21 18:13:33 -0600742 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800743 assert(!err);
744
Ian Elliott7fe115d2015-08-07 15:56:59 -0600745 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
746 &demo->swapChainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600747 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800748
Ian Elliott7fe115d2015-08-07 15:56:59 -0600749 VkImage* swapChainImages =
750 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliotte36b2082015-07-06 14:27:58 -0600751 assert(swapChainImages);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600752 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
753 &demo->swapChainImageCount,
754 swapChainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600755 assert(!err);
Ian Elliotta71c0052015-07-13 12:20:56 -0600756#ifdef WORK_AROUND_CODE
Ian Elliott7fe115d2015-08-07 15:56:59 -0600757 // After the proper code was created, other parts of this demo were
758 // modified to only support DEMO_BUFFER_COUNT number of command buffers,
759 // images, etc. Live with that for now.
760 // TODO: Rework this demo code to live with the number of buffers returned
761 // by vkCreateSwapChainWSI().
Ian Elliotta71c0052015-07-13 12:20:56 -0600762 demo->swapChainImageCount = DEMO_BUFFER_COUNT;
Ian Elliotta71c0052015-07-13 12:20:56 -0600763#endif // WORK_AROUND_CODE
Ian Elliotte36b2082015-07-06 14:27:58 -0600764
765 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
766 assert(demo->buffers);
767
768 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800769 VkAttachmentViewCreateInfo color_attachment_view = {
770 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600771 .pNext = NULL,
772 .format = demo->format,
773 .mipLevel = 0,
774 .baseArraySlice = 0,
775 .arraySize = 1,
776 };
777
Ian Elliott7fe115d2015-08-07 15:56:59 -0600778 demo->buffers[i].image = swapChainImages[i];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600779
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600780 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400781 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600782 VK_IMAGE_LAYOUT_UNDEFINED,
783 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600784
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600785 color_attachment_view.image = demo->buffers[i].image;
786
Chia-I Wuc278df82015-07-07 11:50:03 +0800787 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600788 &color_attachment_view, &demo->buffers[i].view);
789 assert(!err);
790 }
791}
792
793static void demo_prepare_depth(struct demo *demo)
794{
Tony Barbour8205d902015-04-16 15:59:00 -0600795 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600796 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600797 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600798 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600799 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600800 .format = depth_format,
801 .extent = { demo->width, demo->height, 1 },
802 .mipLevels = 1,
803 .arraySize = 1,
804 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600805 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600806 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600807 .flags = 0,
808 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600809 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600810 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500811 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600812 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600813 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600814 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800815 VkAttachmentViewCreateInfo view = {
816 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600817 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600818 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4ab40c42015-07-15 17:41:38 -0600819 .format = depth_format,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600820 .mipLevel = 0,
821 .baseArraySlice = 0,
822 .arraySize = 1,
823 .flags = 0,
824 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600825
Mark Lobodzinski23182612015-05-29 09:32:35 -0500826 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600827 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600828
829 demo->depth.format = depth_format;
830
831 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600832 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600833 &demo->depth.image);
834 assert(!err);
835
Tony Barbourde4124d2015-07-03 10:33:54 -0600836 err = vkGetImageMemoryRequirements(demo->device,
837 demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600838
839 mem_alloc.allocationSize = mem_reqs.size;
840 err = memory_type_from_properties(demo,
841 mem_reqs.memoryTypeBits,
842 VK_MEMORY_PROPERTY_DEVICE_ONLY,
843 &mem_alloc.memoryTypeIndex);
844 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600845
Mark Lobodzinski23182612015-05-29 09:32:35 -0500846 /* allocate memory */
847 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
848 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600849
Mark Lobodzinski23182612015-05-29 09:32:35 -0500850 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600851 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500852 demo->depth.mem, 0);
853 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600854
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600855 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400856 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600857 VK_IMAGE_LAYOUT_UNDEFINED,
858 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600859
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600860 /* create image view */
861 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800862 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600863 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600864}
865
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600866/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600867 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600868 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600869 * \param demo : Needed to access VK calls
870 * \param filename : the png file to be loaded
871 * \param width : width of png, to be updated as a side effect of this function
872 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600873 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600874 * \return bool : an opengl texture id. true if successful?,
875 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600876 *
877 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
878 * Modified to copy image to memory
879 *
880 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700881bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600882 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600883 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600884{
885 //header for testing if it is a png
886 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600887 int is_png, bit_depth, color_type, rowbytes;
888 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700889 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600890 png_structp png_ptr;
891 png_infop info_ptr, end_info;
892 png_byte *image_data;
893 png_bytep *row_pointers;
894
895 //open file as binary
896 FILE *fp = fopen(filename, "rb");
897 if (!fp) {
898 return false;
899 }
900
901 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600902 retval = fread(header, 1, 8, fp);
903 if (retval != 8) {
904 fclose(fp);
905 return false;
906 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600907
908 //test if png
909 is_png = !png_sig_cmp(header, 0, 8);
910 if (!is_png) {
911 fclose(fp);
912 return false;
913 }
914
915 //create png struct
916 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
917 NULL, NULL);
918 if (!png_ptr) {
919 fclose(fp);
920 return (false);
921 }
922
923 //create png info struct
924 info_ptr = png_create_info_struct(png_ptr);
925 if (!info_ptr) {
926 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
927 fclose(fp);
928 return (false);
929 }
930
931 //create png info struct
932 end_info = png_create_info_struct(png_ptr);
933 if (!end_info) {
934 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
935 fclose(fp);
936 return (false);
937 }
938
939 //png error stuff, not sure libpng man suggests this.
940 if (setjmp(png_jmpbuf(png_ptr))) {
941 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
942 fclose(fp);
943 return (false);
944 }
945
946 //init png reading
947 png_init_io(png_ptr, fp);
948
949 //let libpng know you already read the first 8 bytes
950 png_set_sig_bytes(png_ptr, 8);
951
952 // read all the info up to the image data
953 png_read_info(png_ptr, info_ptr);
954
955 // get info about png
956 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
957 NULL, NULL, NULL);
958
959 //update width and height based on png info
960 *width = twidth;
961 *height = theight;
962
963 // Require that incoming texture be 8bits per color component
964 // and 4 components (RGBA).
965 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
966 png_get_channels(png_ptr, info_ptr) != 4) {
967 return false;
968 }
969
970 if (rgba_data == NULL) {
971 // If data pointer is null, we just want the width & height
972 // clean up memory and close stuff
973 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
974 fclose(fp);
975
976 return true;
977 }
978
979 // Update the png info struct.
980 png_read_update_info(png_ptr, info_ptr);
981
982 // Row size in bytes.
983 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
984
985 // Allocate the image_data as a big block, to be given to opengl
986 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
987 if (!image_data) {
988 //clean up memory and close stuff
989 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
990 fclose(fp);
991 return false;
992 }
993
994 // row_pointers is for pointing to image_data for reading the png with libpng
995 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
996 if (!row_pointers) {
997 //clean up memory and close stuff
998 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
999 // delete[] image_data;
1000 fclose(fp);
1001 return false;
1002 }
1003 // set the individual row_pointers to point at the correct offsets of image_data
1004 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001005 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001006
1007 // read the png into image_data through row_pointers
1008 png_read_image(png_ptr, row_pointers);
1009
1010 // clean up memory and close stuff
1011 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
1012 free(row_pointers);
1013 free(image_data);
1014 fclose(fp);
1015
1016 return true;
1017}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001018
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001019static void demo_prepare_texture_image(struct demo *demo,
1020 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001021 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001022 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001023 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001024 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001025{
Mike Stroyande24a6f2015-06-15 14:21:03 -06001026 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001027 int32_t tex_width;
1028 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -06001029 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001030
David Pinedo61e77382015-04-23 08:16:57 -06001031 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1032 {
1033 printf("Failed to load textures\n");
1034 fflush(stdout);
1035 exit(1);
1036 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001037
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001038 tex_obj->tex_width = tex_width;
1039 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001040
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001041 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001042 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001043 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -06001044 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001045 .format = tex_format,
1046 .extent = { tex_width, tex_height, 1 },
1047 .mipLevels = 1,
1048 .arraySize = 1,
1049 .samples = 1,
1050 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001051 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001052 .flags = 0,
1053 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001054 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001055 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001056 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001057 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001058 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001059 };
1060
Mark Lobodzinski23182612015-05-29 09:32:35 -05001061 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001062
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001063 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001064 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001065 assert(!err);
1066
Tony Barbourde4124d2015-07-03 10:33:54 -06001067 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbour426b9052015-06-24 16:06:58 -06001068 assert(!err);
Piers Daniell886be472015-02-23 16:23:13 -07001069
Mark Lobodzinski23182612015-05-29 09:32:35 -05001070 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001071
Mark Lobodzinski72346292015-07-02 16:49:40 -06001072 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
1073 assert(!err);
1074
Mark Lobodzinski23182612015-05-29 09:32:35 -05001075 /* allocate memory */
1076 err = vkAllocMemory(demo->device, &mem_alloc,
1077 &(tex_obj->mem));
1078 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001079
Mark Lobodzinski23182612015-05-29 09:32:35 -05001080 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -06001081 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001082 tex_obj->mem, 0);
1083 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001084
Tony Barbour8205d902015-04-16 15:59:00 -06001085 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001086 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001087 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001088 .mipLevel = 0,
1089 .arraySlice = 0,
1090 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001091 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001092 void *data;
1093
Tony Barbour426b9052015-06-24 16:06:58 -06001094 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1095 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001096
Mark Lobodzinski23182612015-05-29 09:32:35 -05001097 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001098 assert(!err);
1099
1100 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1101 fprintf(stderr, "Error loading texture: %s\n", filename);
1102 }
1103
Mark Lobodzinski23182612015-05-29 09:32:35 -05001104 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001105 assert(!err);
1106 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001107
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001108 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001109 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001110 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001111 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001112 tex_obj->imageLayout);
1113 /* 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 -07001114}
1115
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001116static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001117{
1118 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -05001119 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001120 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001121}
1122
1123static void demo_prepare_textures(struct demo *demo)
1124{
Tony Barbour8205d902015-04-16 15:59:00 -06001125 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001126 VkFormatProperties props;
Tony Barbour22a30862015-04-22 09:02:32 -06001127 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001128 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001129
Courtney Goeltzenleuchter4da96aa2015-07-12 12:52:09 -06001130 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001131 assert(!err);
1132
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001133 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001134
FslNopper434db6a2015-05-06 21:42:01 +02001135 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001136 /* Device can texture using linear textures */
1137 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001138 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001139 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001140 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001141 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001142
1143 memset(&staging_texture, 0, sizeof(staging_texture));
1144 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001145 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001146
1147 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001148 VK_IMAGE_TILING_OPTIMAL,
1149 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1150 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001151
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001152 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001153 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001154 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001155 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001156
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001157 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001158 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001159 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001160 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001161
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001162 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001163 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001164 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001165 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001166 .destOffset = { 0, 0, 0 },
1167 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1168 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001169 vkCmdCopyImage(demo->cmd,
1170 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1171 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001172 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001173
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001174 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001175 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001176 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001177 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001178
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001179 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001180
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001181 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001182 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001183 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1184 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001185 }
1186
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001187 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001188 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001189 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001190 .magFilter = VK_TEX_FILTER_NEAREST,
1191 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001192 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001193 .addressU = VK_TEX_ADDRESS_CLAMP,
1194 .addressV = VK_TEX_ADDRESS_CLAMP,
1195 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001196 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001197 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001198 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001199 .minLod = 0.0f,
1200 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001201 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Cody Northrop0bf4e1d2015-08-11 15:50:55 -06001202 .texelCoords = VK_FALSE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001203 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001204
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001205 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001206 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001207 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -06001208 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001209 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001210 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001211 .channels = { VK_CHANNEL_SWIZZLE_R,
1212 VK_CHANNEL_SWIZZLE_G,
1213 VK_CHANNEL_SWIZZLE_B,
1214 VK_CHANNEL_SWIZZLE_A, },
1215 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001216 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001217
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001218 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001219 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001220 &demo->textures[i].sampler);
1221 assert(!err);
1222
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001223 /* create image view */
1224 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001225 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001226 &demo->textures[i].view);
1227 assert(!err);
1228 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001229}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001230
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001231void demo_prepare_cube_data_buffer(struct demo *demo)
1232{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001233 VkBufferCreateInfo buf_info;
1234 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001235 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001236 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001237 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001238 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001239 .memoryTypeIndex = 0,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001240 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001241 VkMemoryRequirements mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001242 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001243 int i;
1244 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001245 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001246 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001247
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001248 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001249 mat4x4_mul(MVP, VP, demo->model_matrix);
1250 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001251// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001252
1253 for (i=0; i<12*3; i++) {
1254 data.position[i][0] = g_vertex_buffer_data[i*3];
1255 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1256 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1257 data.position[i][3] = 1.0f;
1258 data.attr[i][0] = g_uv_buffer_data[2*i];
1259 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1260 data.attr[i][2] = 0;
1261 data.attr[i][3] = 0;
1262 }
1263
Chia-I Wu714df452015-01-01 07:55:04 +08001264 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001265 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001266 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop86049392015-07-16 10:29:32 -06001267 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001268 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001269 assert(!err);
1270
Tony Barbourde4124d2015-07-03 10:33:54 -06001271 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Courtney Goeltzenleuchteraca30a52015-07-15 11:17:08 -06001272 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001273
Mark Lobodzinski23182612015-05-29 09:32:35 -05001274 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -06001275 err = memory_type_from_properties(demo,
1276 mem_reqs.memoryTypeBits,
1277 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1278 &alloc_info.memoryTypeIndex);
1279 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001280
Mark Lobodzinski23182612015-05-29 09:32:35 -05001281 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1282 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001283
Mark Lobodzinski23182612015-05-29 09:32:35 -05001284 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1285 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001286
Mark Lobodzinski23182612015-05-29 09:32:35 -05001287 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001288
Mark Lobodzinski23182612015-05-29 09:32:35 -05001289 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1290 assert(!err);
1291
Tony Barbourde4124d2015-07-03 10:33:54 -06001292 err = vkBindBufferMemory(demo->device,
1293 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001294 demo->uniform_data.mem, 0);
1295 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001296
1297 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001298 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001299 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001300 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001301 view_info.offset = 0;
1302 view_info.range = sizeof(data);
1303
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001304 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001305 assert(!err);
1306
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001307 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001308}
1309
Chia-I Wuf8385062015-01-04 16:27:24 +08001310static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001311{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001312 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001313 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001314 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001315 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001316 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001317 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001318 },
1319 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001320 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001321 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001322 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001323 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001324 },
1325 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001326 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001327 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001328 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001329 .count = 2,
1330 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001331 };
Tony Barbour22a30862015-04-22 09:02:32 -06001332 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001333
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001334 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001335 &descriptor_layout, &demo->desc_layout);
1336 assert(!err);
1337
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001338 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1339 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1340 .pNext = NULL,
1341 .descriptorSetCount = 1,
1342 .pSetLayouts = &demo->desc_layout,
1343 };
1344
1345 err = vkCreatePipelineLayout(demo->device,
1346 &pPipelineLayoutCreateInfo,
1347 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001348 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001349}
1350
Chia-I Wu76cd4222015-07-08 13:34:24 +08001351static void demo_prepare_render_pass(struct demo *demo)
1352{
Chia-I Wuc278df82015-07-07 11:50:03 +08001353 const VkAttachmentDescription attachments[2] = {
1354 [0] = {
1355 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1356 .pNext = NULL,
1357 .format = demo->format,
1358 .samples = 1,
1359 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1360 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1361 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1362 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1363 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1364 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1365 },
1366 [1] = {
1367 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1368 .pNext = NULL,
1369 .format = demo->depth.format,
1370 .samples = 1,
1371 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1372 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1373 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1374 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1375 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1376 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1377 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001378 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001379 const VkAttachmentReference color_reference = {
1380 .attachment = 0,
1381 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1382 };
1383 const VkSubpassDescription subpass = {
1384 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1385 .pNext = NULL,
1386 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1387 .flags = 0,
1388 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001389 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001390 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001391 .pColorAttachments = &color_reference,
1392 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001393 .depthStencilAttachment = {
1394 .attachment = 1,
1395 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1396 },
1397 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001398 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001399 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001400 const VkRenderPassCreateInfo rp_info = {
1401 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1402 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001403 .attachmentCount = 2,
1404 .pAttachments = attachments,
1405 .subpassCount = 1,
1406 .pSubpasses = &subpass,
1407 .dependencyCount = 0,
1408 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001409 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001410 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001411
1412 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1413 assert(!err);
1414}
1415
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001416static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001417 VkShaderStage stage,
Tony Barbour9400f092015-07-21 09:04:41 -06001418 VkShaderModule* pShaderModule,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001419 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001420 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001421{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001422 VkShaderModuleCreateInfo moduleCreateInfo;
1423 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001424 VkShader shader;
1425 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001426
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001427
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001428 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1429 moduleCreateInfo.pNext = NULL;
1430
1431 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1432 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001433 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001434
Cody Northrop75db0322015-05-28 11:27:16 -06001435 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001436 moduleCreateInfo.codeSize = size;
1437 moduleCreateInfo.pCode = code;
1438 moduleCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001439 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001440 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001441 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001442 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001443
1444 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001445 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001446 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001447 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001448 } else {
1449 // Create fake SPV structure to feed GLSL
1450 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001451 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1452 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1453 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001454
1455 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001456 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1457 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1458 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1459 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001460
Tony Barbour9400f092015-07-21 09:04:41 -06001461 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001462 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001463 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001464 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001465
1466 shaderCreateInfo.flags = 0;
Tony Barbour9400f092015-07-21 09:04:41 -06001467 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001468 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001469 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001470 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001471 return shader;
1472}
1473
Cody Northropacfb0492015-03-17 15:55:58 -06001474char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001475{
1476 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001477 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001478 void *shader_code;
1479
1480 FILE *fp = fopen(filename, "rb");
1481 if (!fp) return NULL;
1482
1483 fseek(fp, 0L, SEEK_END);
1484 size = ftell(fp);
1485
1486 fseek(fp, 0L, SEEK_SET);
1487
1488 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001489 retval = fread(shader_code, size, 1, fp);
1490 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001491
1492 *psize = size;
1493
1494 return shader_code;
1495}
1496
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001497static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001498{
Cody Northrop75db0322015-05-28 11:27:16 -06001499 if (!demo->use_glsl) {
1500 void *vertShaderCode;
1501 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001502
Cody Northrop75db0322015-05-28 11:27:16 -06001503 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001504
Tony Barbour9400f092015-07-21 09:04:41 -06001505 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001506 vertShaderCode, size);
1507 } else {
1508 static const char *vertShaderText =
1509 "#version 140\n"
1510 "#extension GL_ARB_separate_shader_objects : enable\n"
1511 "#extension GL_ARB_shading_language_420pack : enable\n"
1512 "\n"
1513 "layout(binding = 0) uniform buf {\n"
1514 " mat4 MVP;\n"
1515 " vec4 position[12*3];\n"
1516 " vec4 attr[12*3];\n"
1517 "} ubuf;\n"
1518 "\n"
1519 "layout (location = 0) out vec4 texcoord;\n"
1520 "\n"
1521 "void main() \n"
1522 "{\n"
1523 " texcoord = ubuf.attr[gl_VertexID];\n"
1524 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1525 "\n"
1526 " // GL->VK conventions\n"
1527 " gl_Position.y = -gl_Position.y;\n"
1528 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1529 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001530
Tony Barbour9400f092015-07-21 09:04:41 -06001531 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001532 (const void *) vertShaderText,
1533 strlen(vertShaderText));
1534 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001535}
1536
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001537static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001538{
Cody Northrop75db0322015-05-28 11:27:16 -06001539 if (!demo->use_glsl) {
1540 void *fragShaderCode;
1541 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001542
Cody Northrop75db0322015-05-28 11:27:16 -06001543 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001544
Tony Barbour9400f092015-07-21 09:04:41 -06001545 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001546 fragShaderCode, size);
1547 } else {
1548 static const char *fragShaderText =
1549 "#version 140\n"
1550 "#extension GL_ARB_separate_shader_objects : enable\n"
1551 "#extension GL_ARB_shading_language_420pack : enable\n"
1552 "layout (binding = 1) uniform sampler2D tex;\n"
1553 "\n"
1554 "layout (location = 0) in vec4 texcoord;\n"
1555 "layout (location = 0) out vec4 uFragColor;\n"
1556 "void main() {\n"
1557 " uFragColor = texture(tex, texcoord.xy);\n"
1558 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001559
Tony Barbour9400f092015-07-21 09:04:41 -06001560 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001561 (const void *) fragShaderText,
1562 strlen(fragShaderText));
1563 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001564}
1565
1566static void demo_prepare_pipeline(struct demo *demo)
1567{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001568 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001569 VkPipelineCacheCreateInfo pipelineCache;
Tony Barboure307f582015-07-10 15:29:03 -06001570 VkPipelineInputAssemblyStateCreateInfo ia;
1571 VkPipelineRasterStateCreateInfo rs;
1572 VkPipelineColorBlendStateCreateInfo cb;
1573 VkPipelineDepthStencilStateCreateInfo ds;
1574 VkPipelineViewportStateCreateInfo vp;
1575 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001576 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001577
1578 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001579 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001580 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001581
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001582 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001583 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001584 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001585
1586 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001587 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001588 rs.fillMode = VK_FILL_MODE_SOLID;
1589 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001590 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001591 rs.depthClipEnable = VK_TRUE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001592 rs.rasterizerDiscardEnable = VK_FALSE;
1593 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001594
1595 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001596 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1597 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001598 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001599 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001600 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001601 cb.attachmentCount = 1;
1602 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001603
Tony Barbourfa6cac72015-01-16 14:27:35 -07001604 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001605 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001606 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001607
1608 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001609 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001610 ds.depthTestEnable = VK_TRUE;
1611 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001612 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001613 ds.depthBoundsEnable = VK_FALSE;
1614 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1615 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001616 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001617 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001618 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001619
Tony Barbourfa6cac72015-01-16 14:27:35 -07001620 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001621 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001622 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001623 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001624
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001625 // Two stages: vs and fs
1626 pipeline.stageCount = 2;
1627 VkPipelineShaderStageCreateInfo shaderStages[2];
1628 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1629
1630 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1631 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1632 shaderStages[0].shader = demo_prepare_vs(demo);
1633
1634 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1635 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1636 shaderStages[1].shader = demo_prepare_fs(demo);
1637
Jon Ashburn0d60d272015-07-09 15:02:25 -06001638 memset(&pipelineCache, 0, sizeof(pipelineCache));
1639 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001640
Jon Ashburn0d60d272015-07-09 15:02:25 -06001641 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1642 assert(!err);
Tony Barboure307f582015-07-10 15:29:03 -06001643
1644 pipeline.pVertexInputState = NULL;
1645 pipeline.pInputAssemblyState = &ia;
1646 pipeline.pRasterState = &rs;
1647 pipeline.pColorBlendState = &cb;
1648 pipeline.pMultisampleState = &ms;
1649 pipeline.pViewportState = &vp;
1650 pipeline.pDepthStencilState = &ds;
1651 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001652 pipeline.renderPass = demo->render_pass;
Tony Barboure307f582015-07-10 15:29:03 -06001653
Courtney Goeltzenleuchter14eb5ea2015-08-13 16:55:04 -06001654 pipeline.renderPass = demo->render_pass;
1655
Jon Ashburn0d60d272015-07-09 15:02:25 -06001656 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001657 assert(!err);
1658
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001659 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001660 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001661 }
Tony Barbour9400f092015-07-21 09:04:41 -06001662 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1663 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001664}
1665
1666static void demo_prepare_dynamic_states(struct demo *demo)
1667{
Tony Barbourde4124d2015-07-03 10:33:54 -06001668 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northropf5bd2252015-08-17 11:10:49 -06001669 VkDynamicRasterLineStateCreateInfo raster_line;
1670 VkDynamicRasterDepthBiasStateCreateInfo raster_depth_bias;
Tony Barbourde4124d2015-07-03 10:33:54 -06001671 VkDynamicColorBlendStateCreateInfo color_blend;
1672 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001673 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001674
Tony Barbourfa6cac72015-01-16 14:27:35 -07001675 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001676 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001677 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001678 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001679 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001680 viewport.height = (float) demo->height;
1681 viewport.width = (float) demo->width;
1682 viewport.minDepth = (float) 0.0f;
1683 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001684 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001685 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001686 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001687 scissor.extent.width = demo->width;
1688 scissor.extent.height = demo->height;
1689 scissor.offset.x = 0;
1690 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001691 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001692
Cody Northropf5bd2252015-08-17 11:10:49 -06001693 memset(&raster_line, 0, sizeof(raster_line));
1694 raster_line.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_LINE_STATE_CREATE_INFO;
1695 raster_line.lineWidth = 1.0;
1696
1697 memset(&raster_depth_bias, 0, sizeof(raster_depth_bias));
1698 raster_depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_DEPTH_BIAS_STATE_CREATE_INFO;
1699 raster_depth_bias.depthBias = 0.0f;
1700 raster_depth_bias.depthBiasClamp = 0.0f;
1701 raster_depth_bias.slopeScaledDepthBias = 0.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001702
1703 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourde4124d2015-07-03 10:33:54 -06001704 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001705 color_blend.blendConst[0] = 1.0f;
1706 color_blend.blendConst[1] = 1.0f;
1707 color_blend.blendConst[2] = 1.0f;
1708 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001709
1710 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourde4124d2015-07-03 10:33:54 -06001711 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001712 depth_stencil.minDepthBounds = 0.0f;
1713 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001714 depth_stencil.stencilBackRef = 0;
1715 depth_stencil.stencilFrontRef = 0;
1716 depth_stencil.stencilReadMask = 0xff;
1717 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001718
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001719 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001720 assert(!err);
1721
Cody Northropf5bd2252015-08-17 11:10:49 -06001722 err = vkCreateDynamicRasterLineState(demo->device, &raster_line, &demo->raster_line);
1723 assert(!err);
1724
1725 err = vkCreateDynamicRasterDepthBiasState(demo->device, &raster_depth_bias, &demo->raster_depth_bias);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001726 assert(!err);
1727
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001728 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001729 &color_blend, &demo->color_blend);
1730 assert(!err);
1731
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001732 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001733 &depth_stencil, &demo->depth_stencil);
1734 assert(!err);
1735}
1736
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001737static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001738{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001739 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001740 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001741 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001742 .count = 1,
1743 },
1744 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001745 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001746 .count = DEMO_TEXTURE_COUNT,
1747 },
1748 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001749 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001750 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001751 .pNext = NULL,
1752 .count = 2,
1753 .pTypeCount = type_counts,
1754 };
Tony Barbour22a30862015-04-22 09:02:32 -06001755 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001756
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001757 err = vkCreateDescriptorPool(demo->device,
1758 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001759 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001760 assert(!err);
1761}
1762
1763static void demo_prepare_descriptor_set(struct demo *demo)
1764{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001765 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1766 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001767 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001768 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001769
Mike Stroyan230e6252015-04-17 12:36:38 -06001770 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001771 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001772 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001773 &demo->desc_set);
1774 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001775
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001776 memset(&tex_descs, 0, sizeof(tex_descs));
1777 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1778 tex_descs[i].sampler = demo->textures[i].sampler;
1779 tex_descs[i].imageView = demo->textures[i].view;
1780 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1781 }
1782
1783 memset(&writes, 0, sizeof(writes));
1784
1785 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1786 writes[0].destSet = demo->desc_set;
1787 writes[0].count = 1;
1788 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1789 writes[0].pDescriptors = &demo->uniform_data.desc;
1790
1791 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1792 writes[1].destSet = demo->desc_set;
1793 writes[1].destBinding = 1;
1794 writes[1].count = DEMO_TEXTURE_COUNT;
1795 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1796 writes[1].pDescriptors = tex_descs;
1797
1798 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1799 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001800}
1801
Chia-I Wu76cd4222015-07-08 13:34:24 +08001802static void demo_prepare_framebuffers(struct demo *demo)
1803{
Cody Northropf110c6e2015-08-04 10:47:08 -06001804 VkAttachmentView attachments[2];
1805 attachments[1] = demo->depth.view;
1806
Chia-I Wu76cd4222015-07-08 13:34:24 +08001807 const VkFramebufferCreateInfo fb_info = {
1808 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1809 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001810 .renderPass = demo->render_pass,
1811 .attachmentCount = 2,
1812 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001813 .width = demo->width,
1814 .height = demo->height,
1815 .layers = 1,
1816 };
1817 VkResult U_ASSERT_ONLY err;
1818 uint32_t i;
1819
1820 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001821 attachments[0] = demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001822 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1823 assert(!err);
1824 }
1825}
1826
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001827static void demo_prepare(struct demo *demo)
1828{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001829 VkResult U_ASSERT_ONLY err;
1830
1831 const VkCmdPoolCreateInfo cmd_pool_info = {
1832 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1833 .pNext = NULL,
1834 .queueFamilyIndex = demo->graphics_queue_node_index,
1835 .flags = 0,
1836 };
1837 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1838 assert(!err);
1839
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001840 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001841 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001842 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001843 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001844 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001845 .flags = 0,
1846 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001847
1848 demo_prepare_buffers(demo);
1849 demo_prepare_depth(demo);
1850 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001851 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001852
Chia-I Wuf8385062015-01-04 16:27:24 +08001853 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001854 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001855 demo_prepare_pipeline(demo);
1856 demo_prepare_dynamic_states(demo);
1857
Ian Elliotte36b2082015-07-06 14:27:58 -06001858 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001859 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001860 assert(!err);
1861 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001862
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001863 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001864 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001865
Chia-I Wu76cd4222015-07-08 13:34:24 +08001866 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001867
Ian Elliotte36b2082015-07-06 14:27:58 -06001868 for (uint32_t i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001869 demo->current_buffer = i;
1870 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1871 }
1872
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001873 /*
1874 * Prepare functions above may generate pipeline commands
1875 * that need to be flushed before beginning the render loop.
1876 */
1877 demo_flush_init_cmd(demo);
1878
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001879 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001880 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001881}
1882
David Pinedoeeca2a22015-06-18 17:03:14 -06001883static void demo_cleanup(struct demo *demo)
1884{
1885 uint32_t i;
1886
1887 demo->prepared = false;
1888
Tony Barbourde4124d2015-07-03 10:33:54 -06001889 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1890 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1891 }
Tony Barbourb857d312015-07-10 10:50:45 -06001892 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06001893 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001894
Tony Barbourde4124d2015-07-03 10:33:54 -06001895 vkDestroyDynamicViewportState(demo->device, demo->viewport);
Cody Northropf5bd2252015-08-17 11:10:49 -06001896 vkDestroyDynamicRasterLineState(demo->device, demo->raster_line);
1897 vkDestroyDynamicRasterDepthBiasState(demo->device, demo->raster_depth_bias);
Tony Barbourde4124d2015-07-03 10:33:54 -06001898 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1899 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedoeeca2a22015-06-18 17:03:14 -06001900
Tony Barbourde4124d2015-07-03 10:33:54 -06001901 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001902 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbourde4124d2015-07-03 10:33:54 -06001903 vkDestroyRenderPass(demo->device, demo->render_pass);
1904 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1905 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -06001906
1907 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001908 vkDestroyImageView(demo->device, demo->textures[i].view);
1909 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001910 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001911 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedoeeca2a22015-06-18 17:03:14 -06001912 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001913 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
David Pinedoeeca2a22015-06-18 17:03:14 -06001914
Tony Barbourde4124d2015-07-03 10:33:54 -06001915 vkDestroyAttachmentView(demo->device, demo->depth.view);
1916 vkDestroyImage(demo->device, demo->depth.image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001917 vkFreeMemory(demo->device, demo->depth.mem);
1918
Tony Barbourde4124d2015-07-03 10:33:54 -06001919 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1920 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedoeeca2a22015-06-18 17:03:14 -06001921 vkFreeMemory(demo->device, demo->uniform_data.mem);
1922
Ian Elliotte36b2082015-07-06 14:27:58 -06001923 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001924 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1925 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001926 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001927 free(demo->buffers);
David Pinedoeeca2a22015-06-18 17:03:14 -06001928
Cody Northrop18ea11b2015-07-09 18:08:32 -06001929 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedoeeca2a22015-06-18 17:03:14 -06001930 vkDestroyDevice(demo->device);
Tony Barboura65ecc22015-06-30 14:14:19 -06001931 if (demo->validate) {
1932 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1933 }
David Pinedoeeca2a22015-06-18 17:03:14 -06001934 vkDestroyInstance(demo->inst);
1935
1936#ifndef _WIN32
1937 xcb_destroy_window(demo->connection, demo->window);
1938 xcb_disconnect(demo->connection);
1939#endif // _WIN32
1940}
1941
1942// On MS-Windows, make this a global, so it's available to WndProc()
1943struct demo demo;
1944
Ian Elliotte14e9f92015-04-16 15:23:05 -06001945#ifdef _WIN32
1946static void demo_run(struct demo *demo)
1947{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001948 if (!demo->prepared)
1949 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001950 // Wait for work to finish before updating MVP.
1951 vkDeviceWaitIdle(demo->device);
1952 demo_update_data_buffer(demo);
1953
1954 demo_draw(demo);
1955
1956 // Wait for work to finish before updating MVP.
1957 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001958
David Pinedoeeca2a22015-06-18 17:03:14 -06001959 demo->curFrame++;
1960
1961 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1962 {
1963 demo->quit=true;
1964 demo_cleanup(demo);
1965 ExitProcess(0);
1966 }
1967
1968}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001969
1970// MS-Windows event handling function:
1971LRESULT CALLBACK WndProc(HWND hWnd,
1972 UINT uMsg,
1973 WPARAM wParam,
1974 LPARAM lParam)
1975{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001976 switch(uMsg)
1977 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001978 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001979 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001980 break;
Ian Elliotte36b2082015-07-06 14:27:58 -06001981 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001982 demo_run(&demo);
1983 return 0;
1984 default:
1985 break;
1986 }
1987 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1988}
1989
1990static void demo_create_window(struct demo *demo)
1991{
1992 WNDCLASSEX win_class;
1993
1994 // Initialize the window class structure:
1995 win_class.cbSize = sizeof(WNDCLASSEX);
1996 win_class.style = CS_HREDRAW | CS_VREDRAW;
1997 win_class.lpfnWndProc = WndProc;
1998 win_class.cbClsExtra = 0;
1999 win_class.cbWndExtra = 0;
2000 win_class.hInstance = demo->connection; // hInstance
2001 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2002 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2003 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2004 win_class.lpszMenuName = NULL;
2005 win_class.lpszClassName = demo->name;
2006 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2007 // Register window class:
2008 if (!RegisterClassEx(&win_class)) {
2009 // It didn't work, so try to give a useful error:
2010 printf("Unexpected error trying to start the application!\n");
2011 fflush(stdout);
2012 exit(1);
2013 }
2014 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06002015 RECT wr = { 0, 0, demo->width, demo->height };
2016 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002017 demo->window = CreateWindowEx(0,
2018 demo->name, // class name
2019 demo->name, // app name
2020 WS_OVERLAPPEDWINDOW | // window style
2021 WS_VISIBLE |
2022 WS_SYSMENU,
2023 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06002024 wr.right-wr.left, // width
2025 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06002026 NULL, // handle to parent
2027 NULL, // handle to menu
2028 demo->connection, // hInstance
2029 NULL); // no extra parameters
2030 if (!demo->window) {
2031 // It didn't work, so try to give a useful error:
2032 printf("Cannot create a window in which to draw!\n");
2033 fflush(stdout);
2034 exit(1);
2035 }
2036}
2037#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002038static void demo_handle_event(struct demo *demo,
2039 const xcb_generic_event_t *event)
2040{
Piers Daniell886be472015-02-23 16:23:13 -07002041 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002042 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002043 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002044 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002045 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002046 case XCB_CLIENT_MESSAGE:
2047 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2048 (*demo->atom_wm_delete_window).atom) {
2049 demo->quit = true;
2050 }
2051 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002052 case XCB_KEY_RELEASE:
2053 {
2054 const xcb_key_release_event_t *key =
2055 (const xcb_key_release_event_t *) event;
2056
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002057 switch (key->detail) {
2058 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002059 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002060 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002061 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002062 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06002063 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002064 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002065 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002066 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002067 case 0x41:
2068 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07002069 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06002070 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002071 }
2072 break;
2073 default:
2074 break;
2075 }
2076}
2077
2078static void demo_run(struct demo *demo)
2079{
2080 xcb_flush(demo->connection);
2081
2082 while (!demo->quit) {
2083 xcb_generic_event_t *event;
2084
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002085 if (demo->pause) {
2086 event = xcb_wait_for_event(demo->connection);
2087 } else {
2088 event = xcb_poll_for_event(demo->connection);
2089 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002090 if (event) {
2091 demo_handle_event(demo, event);
2092 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002093 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002094
2095 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002096 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07002097 demo_update_data_buffer(demo);
2098
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002099 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07002100
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07002101 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002102 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06002103 demo->curFrame++;
2104 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
2105 demo->quit = true;
2106
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002107 }
2108}
2109
2110static void demo_create_window(struct demo *demo)
2111{
2112 uint32_t value_mask, value_list[32];
2113
2114 demo->window = xcb_generate_id(demo->connection);
2115
2116 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2117 value_list[0] = demo->screen->black_pixel;
2118 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
2119 XCB_EVENT_MASK_EXPOSURE;
2120
2121 xcb_create_window(demo->connection,
2122 XCB_COPY_FROM_PARENT,
2123 demo->window, demo->screen->root,
2124 0, 0, demo->width, demo->height, 0,
2125 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2126 demo->screen->root_visual,
2127 value_mask, value_list);
2128
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07002129 /* Magic code that will send notification when window is destroyed */
2130 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2131 "WM_PROTOCOLS");
2132 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2133
2134 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2135 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2136
2137 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2138 demo->window, (*reply).atom, 4, 32, 1,
2139 &(*demo->atom_wm_delete_window).atom);
2140 free(reply);
2141
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002142 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06002143
2144 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2145 const uint32_t coords[] = {100, 100};
2146 xcb_configure_window(demo->connection, demo->window,
2147 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002148}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002149#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002150
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002151/*
2152 * Return 1 (true) if all layer names specified in check_names
2153 * can be found in given layer properties.
2154 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002155static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002156 uint32_t layer_count, VkLayerProperties *layers)
2157{
2158 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002159 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002160 for (uint32_t j = 0; j < layer_count; j++) {
2161 if (!strcmp(check_names[i], layers[j].layerName)) {
2162 found = 1;
2163 }
2164 }
2165 if (!found) {
2166 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2167 return 0;
2168 }
2169 }
2170 return 1;
2171}
2172
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002173static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002174{
Tobin Ehlis3536b442015-04-16 18:04:57 -06002175 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002176 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002177 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002178 VkLayerProperties *instance_layers;
2179 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002180 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002181 uint32_t instance_layer_count = 0;
2182 uint32_t enabled_extension_count = 0;
2183 uint32_t enabled_layer_count = 0;
2184
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002185 char *instance_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002186 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002187 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002188 "ObjectTracker",
2189 "DrawState",
2190 "ParamChecker",
2191 "ShaderChecker",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002192 };
2193
2194 char *device_validation_layers[] = {
Tony Barbour9400f092015-07-21 09:04:41 -06002195 "Threading",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002196 "MemTracker",
Tony Barbour9400f092015-07-21 09:04:41 -06002197 "ObjectTracker",
2198 "DrawState",
2199 "ParamChecker",
2200 "ShaderChecker",
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002201 };
2202
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002203 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002204 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002205 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002206 assert(!err);
2207
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002208 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2209 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2210 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002211
2212 if (demo->validate) {
2213 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2214 instance_layer_count, instance_layers);
2215 if (!validation_found) {
2216 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2217 "required validation layer.\n\n"
2218 "Please look at the Getting Started guide for additional "
2219 "information.\n",
2220 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002221 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002222 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002223 }
2224
2225 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2226 assert(!err);
2227
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002228 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002229 memset(extension_names, 0, sizeof(extension_names));
2230 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2231 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2232 assert(!err);
2233 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002234 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002235 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06002236 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002237 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002238 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002239 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06002240 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002241 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002242 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002243 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002244 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002245 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06002246 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliotte36b2082015-07-06 14:27:58 -06002247 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002248 "Vulkan installable client driver (ICD) installed?\nPlease "
2249 "look at the Getting Started guide for additional "
2250 "information.\n",
2251 "vkCreateInstance Failure");
2252 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002253 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002254 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002255 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002256 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002257 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002258 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002259 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002260 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002261 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002262 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002263 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002264 .pNext = NULL,
2265 .pAppInfo = &app,
2266 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002267 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002268 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002269 .extensionCount = enabled_extension_count,
2270 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002271 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06002272 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12002273 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002274 .queueCount = 1,
2275 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002276
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002277 uint32_t gpu_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002278
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002279 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002280 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2281 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002282 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002283 "additional information.\n",
2284 "vkCreateInstance Failure");
Tony Barbour5685ad72015-04-29 16:19:20 -06002285 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2286 ERR_EXIT("Cannot find a specified extension library"
2287 ".\nMake sure your layers path is set appropriately\n",
2288 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002289 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002290 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2291 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002292 "the Getting Started guide for additional information.\n",
2293 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002294 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002295
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002296 free(instance_layers);
2297 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002298
Jon Ashburn07b309a2015-04-15 11:31:12 -06002299 gpu_count = 1;
2300 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002301 assert(!err && gpu_count == 1);
2302
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002303 /* Look for validation layers */
2304 validation_found = 0;
2305 enabled_layer_count = 0;
2306 uint32_t device_layer_count = 0;
2307 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2308 assert(!err);
2309
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002310 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2311 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2312 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002313
2314 if (demo->validate) {
2315 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2316 device_layer_count, device_layers);
2317 if (!validation_found) {
2318 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2319 "a required validation layer.\n\n"
2320 "Please look at the Getting Started guide for additional "
2321 "information.\n",
2322 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002323 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002324 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002325 }
2326
2327 uint32_t device_extension_count = 0;
2328 VkExtensionProperties *device_extensions = NULL;
2329 err = vkGetPhysicalDeviceExtensionProperties(
2330 demo->gpu, NULL, &device_extension_count, NULL);
2331 assert(!err);
2332
2333 WSIextFound = 0;
2334 enabled_extension_count = 0;
2335 memset(extension_names, 0, sizeof(extension_names));
2336 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2337 err = vkGetPhysicalDeviceExtensionProperties(
2338 demo->gpu, NULL, &device_extension_count, device_extensions);
2339 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06002340
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002341 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002342 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002343 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06002344 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002345 }
2346 assert(enabled_extension_count < 64);
2347 }
2348 if (!WSIextFound) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002349 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
2350 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002351 "Vulkan installable client driver (ICD) installed?\nPlease "
2352 "look at the Getting Started guide for additional "
2353 "information.\n",
2354 "vkCreateInstance Failure");
2355 }
2356
2357 VkDeviceCreateInfo device = {
2358 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2359 .pNext = NULL,
2360 .queueRecordCount = 1,
2361 .pRequestedQueues = &queue,
2362 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002363 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002364 .extensionCount = enabled_extension_count,
2365 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002366 };
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002367
Tony Barbour5685ad72015-04-29 16:19:20 -06002368 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06002369 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2370 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002371 if (!demo->dbgCreateMsgCallback) {
2372 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2373 "vkGetProcAddr Failure");
2374 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002375 if (!demo->dbgDestroyMsgCallback) {
2376 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2377 "vkGetProcAddr Failure");
2378 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002379 demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback");
2380 if (!demo->dbgBreakCallback) {
2381 ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n",
2382 "vkGetProcAddr Failure");
2383 }
2384
2385 PFN_vkDbgMsgCallback callback;
2386
2387 if (!demo->use_break) {
2388 callback = dbgFunc;
2389 } else {
2390 callback = demo->dbgBreakCallback;
2391 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002392 err = demo->dbgCreateMsgCallback(
2393 demo->inst,
2394 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002395 callback, NULL,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002396 &demo->msg_callback);
2397 switch (err) {
2398 case VK_SUCCESS:
2399 break;
2400 case VK_ERROR_INVALID_POINTER:
2401 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2402 "dbgCreateMsgCallback Failure");
2403 break;
2404 case VK_ERROR_OUT_OF_HOST_MEMORY:
2405 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2406 "dbgCreateMsgCallback Failure");
2407 break;
2408 default:
2409 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2410 "dbgCreateMsgCallback Failure");
2411 break;
2412 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002413 }
2414
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002415 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002416 assert(!err);
2417
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002418 free(device_layers);
2419
Ian Elliotte36b2082015-07-06 14:27:58 -06002420 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002421 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
2422 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
2423 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06002424 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2425 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2426 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002427 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliotte36b2082015-07-06 14:27:58 -06002428 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06002429 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburncedc15f2015-05-21 18:13:33 -06002430
Tony Barbour426b9052015-06-24 16:06:58 -06002431 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002432 assert(!err);
2433
Cody Northropef72e2a2015-08-03 17:04:53 -06002434 /* Call with NULL data to get count */
2435 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002436 assert(!err);
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002437 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002438
Cody Northropef72e2a2015-08-03 17:04:53 -06002439 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
2440 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002441 assert(!err);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002442 assert(demo->queue_count >= 1);
2443}
2444
2445static void demo_init_vk_wsi(struct demo *demo)
2446{
2447 VkResult err;
2448 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002449
Ian Elliotte36b2082015-07-06 14:27:58 -06002450 // Construct the WSI surface description:
2451 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
2452 demo->surface_description.pNext = NULL;
2453#ifdef _WIN32
2454 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
2455 demo->surface_description.pPlatformHandle = demo->connection;
2456 demo->surface_description.pPlatformWindow = demo->window;
2457#else // _WIN32
2458 demo->platform_handle_xcb.connection = demo->connection;
2459 demo->platform_handle_xcb.root = demo->screen->root;
2460 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
2461 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2462 demo->surface_description.pPlatformWindow = &demo->window;
2463#endif // _WIN32
2464
2465 // Iterate over each queue to learn whether it supports presenting to WSI:
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002466 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2467 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002468 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
2469 (VkSurfaceDescriptionWSI *) &demo->surface_description,
2470 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002471 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002472
2473 // Search for a graphics and a present queue in the array of queue
2474 // families, try to find one that supports both
2475 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2476 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002477 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002478 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2479 if (graphicsQueueNodeIndex == UINT32_MAX) {
2480 graphicsQueueNodeIndex = i;
2481 }
2482
2483 if (supportsPresent[i] == VK_TRUE) {
2484 graphicsQueueNodeIndex = i;
2485 presentQueueNodeIndex = i;
2486 break;
2487 }
2488 }
2489 }
2490 if (presentQueueNodeIndex == UINT32_MAX) {
2491 // If didn't find a queue that supports both graphics and present, then
2492 // find a separate present queue.
Courtney Goeltzenleuchterb787a8e2015-07-15 17:40:20 -06002493 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002494 if (supportsPresent[i] == VK_TRUE) {
2495 presentQueueNodeIndex = i;
2496 break;
2497 }
2498 }
2499 }
2500 free(supportsPresent);
2501
2502 // Generate error if could not find both a graphics and a present queue
2503 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2504 ERR_EXIT("Could not find a graphics and a present queue\n",
2505 "WSI Initialization Failure");
2506 }
2507
2508 // TODO: Add support for separate queues, including presentation,
2509 // synchronization, and appropriate tracking for QueueSubmit
2510 // While it is possible for an application to use a separate graphics and a
2511 // present queues, this demo program assumes it is only using one:
2512 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2513 ERR_EXIT("Could not find a common graphics and a present queue\n",
2514 "WSI Initialization Failure");
2515 }
2516
2517 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002518
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002519 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002520 0, &demo->queue);
2521 assert(!err);
Tony Barbour5685ad72015-04-29 16:19:20 -06002522
Ian Elliotte36b2082015-07-06 14:27:58 -06002523 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002524 uint32_t formatCount;
2525 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002526 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002527 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002528 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002529 VkSurfaceFormatWSI *surfFormats =
2530 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
2531 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002532 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002533 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002534 assert(!err);
2535 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2536 // the surface has no preferred format. Otherwise, at least one
2537 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002538 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2539 {
2540 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2541 }
2542 else
2543 {
2544 assert(formatCount >= 1);
2545 demo->format = surfFormats[0].format;
2546 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002547 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002548
David Pinedoeeca2a22015-06-18 17:03:14 -06002549 demo->quit = false;
2550 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002551
2552 // Get Memory information and properties
2553 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2554 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002555}
2556
2557static void demo_init_connection(struct demo *demo)
2558{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002559#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002560 const xcb_setup_t *setup;
2561 xcb_screen_iterator_t iter;
2562 int scr;
2563
2564 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002565 if (demo->connection == NULL) {
2566 printf("Cannot find a compatible Vulkan installable client driver "
2567 "(ICD).\nExiting ...\n");
2568 fflush(stdout);
2569 exit(1);
2570 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002571
2572 setup = xcb_get_setup(demo->connection);
2573 iter = xcb_setup_roots_iterator(setup);
2574 while (scr-- > 0)
2575 xcb_screen_next(&iter);
2576
2577 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002578#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002579}
2580
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002581static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002582{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002583 vec3 eye = {0.0f, 3.0f, 5.0f};
2584 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002585 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002586
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002587 memset(demo, 0, sizeof(*demo));
David Pinedoeeca2a22015-06-18 17:03:14 -06002588 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002589
Piers Daniell886be472015-02-23 16:23:13 -07002590 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002591 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002592 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002593 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002594 }
Cody Northrop75db0322015-05-28 11:27:16 -06002595 if (strcmp(argv[i], "--use_glsl") == 0) {
2596 demo->use_glsl = true;
2597 continue;
2598 }
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002599 if (strcmp(argv[i], "--break") == 0) {
2600 demo->use_break = true;
2601 continue;
2602 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002603 if (strcmp(argv[i], "--validate") == 0) {
2604 demo->validate = true;
2605 continue;
2606 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002607 if (strcmp(argv[i], "--c") == 0 &&
2608 demo->frameCount == INT_MAX &&
2609 i < argc-1 &&
2610 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2611 demo->frameCount >= 0)
2612 {
2613 i++;
2614 continue;
2615 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002616
Courtney Goeltzenleuchter3230e582015-07-22 11:03:51 -06002617 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002618 fflush(stderr);
2619 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002620 }
2621
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002622 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002623 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002624
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002625 demo->width = 500;
2626 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002627
2628 demo->spin_angle = 0.01f;
2629 demo->spin_increment = 0.01f;
2630 demo->pause = false;
2631
Piers Daniell886be472015-02-23 16:23:13 -07002632 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002633 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2634 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002635}
2636
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002637
Ian Elliotte14e9f92015-04-16 15:23:05 -06002638#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002639extern int __getmainargs(
2640 int * _Argc,
2641 char *** _Argv,
2642 char *** _Env,
2643 int _DoWildCard,
2644 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002645
Ian Elliott421107f2015-04-28 15:50:36 -06002646int WINAPI WinMain(HINSTANCE hInstance,
2647 HINSTANCE hPrevInstance,
2648 LPSTR pCmdLine,
2649 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002650{
2651 MSG msg; // message
2652 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002653 int argc;
2654 char** argv;
2655 char** env;
2656 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002657
Tony Barbour5685ad72015-04-29 16:19:20 -06002658 __getmainargs(&argc,&argv,&env,0,&new_mode);
2659
2660 demo_init(&demo, argc, argv);
2661 demo.connection = hInstance;
2662 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002663 demo_create_window(&demo);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002664 demo_init_vk_wsi(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002665
2666 demo_prepare(&demo);
2667
2668 done = false; //initialize loop condition variable
2669 /* main message loop*/
2670 while(!done)
2671 {
Ian Elliott421107f2015-04-28 15:50:36 -06002672 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002673 if (msg.message == WM_QUIT) //check for a quit message
2674 {
2675 done = true; //if found, quit app
2676 }
2677 else
2678 {
2679 /* Translate and dispatch to event queue*/
2680 TranslateMessage(&msg);
2681 DispatchMessage(&msg);
2682 }
2683 }
2684
2685 demo_cleanup(&demo);
2686
Tony Barboura938abb2015-04-22 11:36:22 -06002687 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002688}
2689#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002690int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002691{
2692 struct demo demo;
2693
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002694 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002695 demo_create_window(&demo);
Courtney Goeltzenleuchter89147b42015-07-15 17:45:38 -06002696 demo_init_vk_wsi(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002697
2698 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002699 demo_run(&demo);
2700
2701 demo_cleanup(&demo);
2702
2703 return 0;
2704}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002705#endif // _WIN32