blob: fa649ca2393c9f652d98a281f0795701260375d5 [file] [log] [blame]
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001/*
Karl Schultz481756e2016-02-02 15:37:51 -07002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
Ian Elliott7595eee2015-04-28 10:33:11 -06005 *
Karl Schultz481756e2016-02-02 15:37:51 -07006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
Ian Elliott7595eee2015-04-28 10:33:11 -060012 *
Karl Schultz481756e2016-02-02 15:37:51 -070013 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
Ian Elliott7595eee2015-04-28 10:33:11 -060015 *
Karl Schultz481756e2016-02-02 15:37:51 -070016 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Ian Elliott7595eee2015-04-28 10:33:11 -060017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultz481756e2016-02-02 15:37:51 -070018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
Courtney Goeltzenleuchter96cd7952015-10-30 11:14:30 -060024 *
25 * Author: Chia-I Wu <olvaffe@gmail.com>
26 * Author: Cody Northrop <cody@lunarg.com>
27 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
28 * Author: Ian Elliott <ian@LunarG.com>
29 * Author: Jon Ashburn <jon@lunarg.com>
30 * Author: Piers Daniell <pdaniell@nvidia.com>
Ian Elliott7595eee2015-04-28 10:33:11 -060031 */
32/*
Chia-I Wuf5caeb02014-10-25 12:11:27 +080033 * Draw a textured triangle with depth testing. This is written against Intel
34 * ICD. It does not do state transition nor object memory binding like it
35 * should. It also does no error checking.
36 */
37
Mark Youngb8b6f0c2016-01-26 15:09:10 -070038#ifndef _MSC_VER
39#define _ISOC11_SOURCE /* for aligned_alloc() */
40#endif
41
Chia-I Wuc19795a2014-09-13 11:12:55 +080042#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <stdbool.h>
46#include <assert.h>
Karl Schultz8b100ca2016-03-25 14:31:16 -060047#include <signal.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080048
Ian Elliotte14e9f92015-04-16 15:23:05 -060049#ifdef _WIN32
50#pragma comment(linker, "/subsystem:windows")
Ian Elliotte14e9f92015-04-16 15:23:05 -060051#define APP_NAME_STR_LEN 80
Ian Elliotte14e9f92015-04-16 15:23:05 -060052#endif // _WIN32
53
David Pinedo329ca9e2015-11-06 12:54:48 -070054#include <vulkan/vulkan.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080055
Chia-I Wub043fe32014-10-06 15:30:33 +080056#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060057#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060058#define APP_SHORT_NAME "tri"
59#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080060
Ian Elliotte36b2082015-07-06 14:27:58 -060061#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
62
Tony Barbour22a30862015-04-22 09:02:32 -060063#if defined(NDEBUG) && defined(__GNUC__)
64#define U_ASSERT_ONLY __attribute__((unused))
65#else
66#define U_ASSERT_ONLY
67#endif
68
Ian Elliottcaa9f272015-04-28 11:35:02 -060069#ifdef _WIN32
Karl Schultz481756e2016-02-02 15:37:51 -070070#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 MessageBox(NULL, err_msg, err_class, MB_OK); \
73 exit(1); \
74 } while (0)
75#else // _WIN32
Ian Elliottcaa9f272015-04-28 11:35:02 -060076
Karl Schultz481756e2016-02-02 15:37:51 -070077#define ERR_EXIT(err_msg, err_class) \
78 do { \
79 printf(err_msg); \
80 fflush(stdout); \
81 exit(1); \
82 } while (0)
Ian Elliottcaa9f272015-04-28 11:35:02 -060083#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060084
Karl Schultz481756e2016-02-02 15:37:51 -070085#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
86 { \
87 demo->fp##entrypoint = \
88 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
89 if (demo->fp##entrypoint == NULL) { \
90 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
91 "vkGetInstanceProcAddr Failure"); \
92 } \
93 }
Ian Elliotte36b2082015-07-06 14:27:58 -060094
Karl Schultz481756e2016-02-02 15:37:51 -070095#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
96 { \
97 demo->fp##entrypoint = \
98 (PFN_vk##entrypoint)vkGetDeviceProcAddr(dev, "vk" #entrypoint); \
99 if (demo->fp##entrypoint == NULL) { \
100 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
101 "vkGetDeviceProcAddr Failure"); \
102 } \
103 }
Ian Elliott1b6de092015-06-22 15:07:49 -0600104
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600105struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600106 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700107
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600108 VkImage image;
109 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600110
Mark Lobodzinski23182612015-05-29 09:32:35 -0500111 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600112 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700113 int32_t tex_width, tex_height;
114};
115
Karl Schultz501fdc22016-03-24 18:43:41 -0600116static int validation_error = 0;
117
Karl Schultz481756e2016-02-02 15:37:51 -0700118VKAPI_ATTR VkBool32 VKAPI_CALL
119dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
120 uint64_t srcObject, size_t location, int32_t msgCode,
121 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
122 char *message = (char *)malloc(strlen(pMsg) + 100);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600123
Karl Schultz481756e2016-02-02 15:37:51 -0700124 assert(message);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600125
Karl Schultz501fdc22016-03-24 18:43:41 -0600126 validation_error = 1;
127
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700128 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultz481756e2016-02-02 15:37:51 -0700129 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
130 pMsg);
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -0700131 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Karl Schultz481756e2016-02-02 15:37:51 -0700132 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
133 pMsg);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600134 } else {
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600135 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600136 }
137
138#ifdef _WIN32
139 MessageBox(NULL, message, "Alert", MB_OK);
140#else
Karl Schultz481756e2016-02-02 15:37:51 -0700141 printf("%s\n", message);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600142 fflush(stdout);
143#endif
144 free(message);
Courtney Goeltzenleuchtere4c43132015-09-08 16:57:22 -0600145
146 /*
147 * false indicates that layer should not bail-out of an
148 * API call that had validation failures. This may mean that the
149 * app dies inside the driver due to invalid parameter(s).
150 * That's what would happen without validation layers, so we'll
151 * keep that behavior here.
152 */
153 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600154}
155
Karl Schultz5b10b812016-03-25 15:35:18 -0600156VKAPI_ATTR VkBool32 VKAPI_CALL
157BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
158 uint64_t srcObject, size_t location, int32_t msgCode,
159 const char *pLayerPrefix, const char *pMsg,
160 void *pUserData) {
Karl Schultz7c5f63e2016-03-25 14:25:16 -0600161#ifndef WIN32
162 raise(SIGTRAP);
163#else
164 DebugBreak();
165#endif
166
167 return false;
168}
169
Ian Elliott338dedb2015-08-21 15:09:33 -0600170typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600171 VkImage image;
Chia-I Wu1f851912015-10-27 18:04:07 +0800172 VkCommandBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600173 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600174} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600175
Chia-I Wuc19795a2014-09-13 11:12:55 +0800176struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600177#ifdef _WIN32
178#define APP_NAME_STR_LEN 80
179 HINSTANCE connection; // hInstance - Windows Instance
180 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultz481756e2016-02-02 15:37:51 -0700181 HWND window; // hWnd - window handle
182#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800183 xcb_connection_t *connection;
184 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800185 xcb_window_t window;
186 xcb_intern_atom_reply_t *atom_wm_delete_window;
Karl Schultz481756e2016-02-02 15:37:51 -0700187#endif // _WIN32
Ian Elliottb5fad792015-11-20 11:55:46 -0700188 VkSurfaceKHR surface;
189 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600190 bool use_staging_buffer;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800191
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600192 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600193 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600194 VkDevice device;
195 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600196 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600197 VkQueueFamilyProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700198 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800199
Tony Barbour3d03a4a2016-01-22 14:36:40 -0700200 uint32_t enabled_extension_count;
201 uint32_t enabled_layer_count;
202 char *extension_names[64];
203 char *device_validation_layers[64];
204
Chia-I Wuc19795a2014-09-13 11:12:55 +0800205 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600206 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600207 VkColorSpaceKHR color_space;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800208
Karl Schultz481756e2016-02-02 15:37:51 -0700209 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
210 fpGetPhysicalDeviceSurfaceSupportKHR;
211 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
212 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
213 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
214 fpGetPhysicalDeviceSurfaceFormatsKHR;
215 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
216 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliott338dedb2015-08-21 15:09:33 -0600217 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
218 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
219 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
220 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
221 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliott338dedb2015-08-21 15:09:33 -0600222 uint32_t swapchainImageCount;
Ian Elliotte2688a52015-10-16 18:02:43 -0600223 VkSwapchainKHR swapchain;
Ian Elliott338dedb2015-08-21 15:09:33 -0600224 SwapchainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800225
Chia-I Wu1f851912015-10-27 18:04:07 +0800226 VkCommandPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800227
Chia-I Wub043fe32014-10-06 15:30:33 +0800228 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600229 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800230
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600231 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500232 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600233 VkImageView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800234 } depth;
235
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600236 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800237
Chia-I Wu99621bc2014-10-08 11:52:22 +0800238 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600239 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500240 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800241
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600242 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600243 VkVertexInputBindingDescription vi_bindings[1];
244 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800245 } vertices;
246
Karl Schultz481756e2016-02-02 15:37:51 -0700247 VkCommandBuffer setup_cmd; // Command Buffer for initialization commands
Chia-I Wu1f851912015-10-27 18:04:07 +0800248 VkCommandBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500249 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600250 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600251 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800252 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600253 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800254
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -0600255 VkShaderModule vert_shader_module;
256 VkShaderModule frag_shader_module;
257
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600258 VkDescriptorPool desc_pool;
259 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800260
Tony Barbour5aabff52015-10-08 14:26:24 -0600261 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800262
Mark Lobodzinski72346292015-07-02 16:49:40 -0600263 VkPhysicalDeviceMemoryProperties memory_properties;
264
Karl Schultz501fdc22016-03-24 18:43:41 -0600265 int32_t curFrame;
266 int32_t frameCount;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600267 bool validate;
Karl Schultz501fdc22016-03-24 18:43:41 -0600268 bool use_break;
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -0700269 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
270 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
271 VkDebugReportCallbackEXT msg_callback;
Mark Lobodzinski96b82412016-02-22 09:32:55 -0700272 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600273
Tony Barbour4a6692d2015-10-08 13:45:45 -0600274 float depthStencil;
275 float depthIncrement;
276
Chia-I Wuc19795a2014-09-13 11:12:55 +0800277 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600278 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600279 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800280};
281
Ian Elliotte2688a52015-10-16 18:02:43 -0600282// Forward declaration:
283static void demo_resize(struct demo *demo);
284
Karl Schultz481756e2016-02-02 15:37:51 -0700285static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
286 VkFlags requirements_mask,
287 uint32_t *typeIndex) {
288 // Search memtypes to find first index with those properties
289 for (uint32_t i = 0; i < 32; i++) {
290 if ((typeBits & 1) == 1) {
291 // Type is available, does it match user properties?
292 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
293 requirements_mask) == requirements_mask) {
294 *typeIndex = i;
295 return true;
296 }
297 }
298 typeBits >>= 1;
299 }
300 // No memory types matched, return failure
301 return false;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600302}
303
Karl Schultz481756e2016-02-02 15:37:51 -0700304static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbour22a30862015-04-22 09:02:32 -0600305 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600306
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600307 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600308 return;
309
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600310 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600311 assert(!err);
312
Karl Schultz481756e2016-02-02 15:37:51 -0700313 const VkCommandBuffer cmd_bufs[] = {demo->setup_cmd};
Tony Barbourde4124d2015-07-03 10:33:54 -0600314 VkFence nullFence = {VK_NULL_HANDLE};
Karl Schultz481756e2016-02-02 15:37:51 -0700315 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
316 .pNext = NULL,
317 .waitSemaphoreCount = 0,
318 .pWaitSemaphores = NULL,
319 .pWaitDstStageMask = NULL,
320 .commandBufferCount = 1,
321 .pCommandBuffers = cmd_bufs,
322 .signalSemaphoreCount = 0,
323 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600324
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600325 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600326 assert(!err);
327
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600328 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600329 assert(!err);
330
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600331 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600332 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600333}
334
Karl Schultz481756e2016-02-02 15:37:51 -0700335static void demo_set_image_layout(struct demo *demo, VkImage image,
336 VkImageAspectFlags aspectMask,
337 VkImageLayout old_image_layout,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700338 VkImageLayout new_image_layout,
339 VkAccessFlagBits srcAccessMask) {
340
Tony Barbour22a30862015-04-22 09:02:32 -0600341 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600342
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600343 if (demo->setup_cmd == VK_NULL_HANDLE) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800344 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuc1f5e402015-11-10 16:21:09 +0800345 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600346 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +0800347 .commandPool = demo->cmd_pool,
348 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburna4ae48b2016-01-11 13:12:43 -0700349 .commandBufferCount = 1,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600350 };
351
Chia-I Wu1f851912015-10-27 18:04:07 +0800352 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600353 assert(!err);
354
Jon Ashburna4ae48b2016-01-11 13:12:43 -0700355 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
356 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600357 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +0800358 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600359 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800360 .framebuffer = VK_NULL_HANDLE,
Chia-I Wufdc1cd02015-11-11 10:18:12 +0800361 .occlusionQueryEnable = VK_FALSE,
362 .queryFlags = 0,
363 .pipelineStatistics = 0,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600364 };
Jon Ashburna4ae48b2016-01-11 13:12:43 -0700365 VkCommandBufferBeginInfo cmd_buf_info = {
366 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
367 .pNext = NULL,
368 .flags = 0,
369 .pInheritanceInfo = &cmd_buf_hinfo,
370 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600371 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Tony Barbourc1098272015-10-23 10:53:30 -0600372 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600373 }
374
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600375 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600376 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600377 .pNext = NULL,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700378 .srcAccessMask = srcAccessMask,
Chia-I Wu989de842015-10-27 19:54:37 +0800379 .dstAccessMask = 0,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600380 .oldLayout = old_image_layout,
381 .newLayout = new_image_layout,
382 .image = image,
Karl Schultz481756e2016-02-02 15:37:51 -0700383 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600384
Chia-I Wu1f851912015-10-27 18:04:07 +0800385 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600386 /* Make sure anything that was copying from this image has completed */
Chia-I Wu989de842015-10-27 19:54:37 +0800387 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600388 }
389
Tony Barbourb7c243a2015-11-11 16:15:54 -0700390 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultz481756e2016-02-02 15:37:51 -0700391 image_memory_barrier.dstAccessMask =
392 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
393 }
Tony Barbourb7c243a2015-11-11 16:15:54 -0700394
395 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultz481756e2016-02-02 15:37:51 -0700396 image_memory_barrier.dstAccessMask =
397 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barbourb7c243a2015-11-11 16:15:54 -0700398 }
399
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600400 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600401 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultz481756e2016-02-02 15:37:51 -0700402 image_memory_barrier.dstAccessMask =
403 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600404 }
405
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600406 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600407
Tony Barbourc2e987e2015-06-29 16:20:35 -0600408 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
409 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600410
Karl Schultz481756e2016-02-02 15:37:51 -0700411 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, 0, 0, NULL,
412 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600413}
414
Karl Schultz481756e2016-02-02 15:37:51 -0700415static void demo_draw_build_cmd(struct demo *demo) {
416 const VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
417 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
418 .pNext = NULL,
419 .renderPass = VK_NULL_HANDLE,
420 .subpass = 0,
421 .framebuffer = VK_NULL_HANDLE,
422 .occlusionQueryEnable = VK_FALSE,
423 .queryFlags = 0,
424 .pipelineStatistics = 0,
425 };
Chia-I Wu1f851912015-10-27 18:04:07 +0800426 const VkCommandBufferBeginInfo cmd_buf_info = {
427 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600428 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600429 .flags = 0,
Jon Ashburna4ae48b2016-01-11 13:12:43 -0700430 .pInheritanceInfo = &cmd_buf_hinfo,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700431 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800432 const VkClearValue clear_values[2] = {
Karl Schultz481756e2016-02-02 15:37:51 -0700433 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
434 [1] = {.depthStencil = {demo->depthStencil, 0}},
Chia-I Wuc278df82015-07-07 11:50:03 +0800435 };
436 const VkRenderPassBeginInfo rp_begin = {
437 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
438 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800439 .renderPass = demo->render_pass,
440 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800441 .renderArea.offset.x = 0,
442 .renderArea.offset.y = 0,
443 .renderArea.extent.width = demo->width,
444 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600445 .clearValueCount = 2,
446 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700447 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800448 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800449
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600450 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800451 assert(!err);
452
Chia-I Wuc51b1212015-10-27 19:25:11 +0800453 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600454 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Karl Schultz481756e2016-02-02 15:37:51 -0700455 demo->pipeline);
456 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
457 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
458 NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800459
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600460 VkViewport viewport;
461 memset(&viewport, 0, sizeof(viewport));
Karl Schultz481756e2016-02-02 15:37:51 -0700462 viewport.height = (float)demo->height;
463 viewport.width = (float)demo->width;
464 viewport.minDepth = (float)0.0f;
465 viewport.maxDepth = (float)1.0f;
Jon Ashburnf2516522015-12-30 14:06:55 -0700466 vkCmdSetViewport(demo->draw_cmd, 0, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600467
468 VkRect2D scissor;
469 memset(&scissor, 0, sizeof(scissor));
470 scissor.extent.width = demo->width;
471 scissor.extent.height = demo->height;
472 scissor.offset.x = 0;
473 scissor.offset.y = 0;
Jon Ashburnf2516522015-12-30 14:06:55 -0700474 vkCmdSetScissor(demo->draw_cmd, 0, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600475
Tony Barbour8205d902015-04-16 15:59:00 -0600476 VkDeviceSize offsets[1] = {0};
Karl Schultz481756e2016-02-02 15:37:51 -0700477 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1,
478 &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800479
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600480 vkCmdDraw(demo->draw_cmd, 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800481 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800482
Tony Barbour40292ae2015-10-21 10:14:48 -0600483 VkImageMemoryBarrier prePresentBarrier = {
484 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
485 .pNext = NULL,
Chia-I Wu989de842015-10-27 19:54:37 +0800486 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barbour0313c5b2016-01-05 09:59:05 -0700487 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour40292ae2015-10-21 10:14:48 -0600488 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliott64070a82015-11-17 17:29:40 -0700489 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour40292ae2015-10-21 10:14:48 -0600490 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wu1f851912015-10-27 18:04:07 +0800491 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultz481756e2016-02-02 15:37:51 -0700492 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour40292ae2015-10-21 10:14:48 -0600493
494 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
495 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultz481756e2016-02-02 15:37:51 -0700496 vkCmdPipelineBarrier(demo->draw_cmd, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
497 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
498 NULL, 1, pmemory_barrier);
Tony Barbour40292ae2015-10-21 10:14:48 -0600499
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600500 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800501 assert(!err);
502}
503
Karl Schultz481756e2016-02-02 15:37:51 -0700504static void demo_draw(struct demo *demo) {
Tony Barbour22a30862015-04-22 09:02:32 -0600505 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600506 VkSemaphore presentCompleteSemaphore;
507 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
508 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
509 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600510 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600511 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800512
Karl Schultz481756e2016-02-02 15:37:51 -0700513 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
514 NULL, &presentCompleteSemaphore);
Ian Elliotte36b2082015-07-06 14:27:58 -0600515 assert(!err);
516
517 // Get the index of the next available swapchain image:
Karl Schultz481756e2016-02-02 15:37:51 -0700518 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliotte36b2082015-07-06 14:27:58 -0600519 presentCompleteSemaphore,
Karl Schultz481756e2016-02-02 15:37:51 -0700520 (VkFence)0, // TODO: Show use of fence
Ian Elliotte36b2082015-07-06 14:27:58 -0600521 &demo->current_buffer);
Ian Elliotte2688a52015-10-16 18:02:43 -0600522 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
523 // demo->swapchain is out of date (e.g. the window was resized) and
524 // must be recreated:
525 demo_resize(demo);
526 demo_draw(demo);
Chia-I Wu69f40122015-10-26 21:10:41 +0800527 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -0600528 return;
529 } else if (err == VK_SUBOPTIMAL_KHR) {
530 // demo->swapchain is not as optimal as it could be, but the platform's
531 // presentation engine will still present the image correctly.
532 } else {
533 assert(!err);
534 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600535
Tony Barbour40292ae2015-10-21 10:14:48 -0600536 // Assume the command buffer has been run on current_buffer before so
537 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
538 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
Karl Schultz481756e2016-02-02 15:37:51 -0700539 VK_IMAGE_ASPECT_COLOR_BIT,
540 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700541 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
542 0);
Tony Barbour40292ae2015-10-21 10:14:48 -0600543 demo_flush_init_cmd(demo);
544
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600545 // Wait for the present complete semaphore to be signaled to ensure
546 // that the image won't be rendered to until the presentation
547 // engine has fully released ownership to the application, and it is
548 // okay to render to the image.
549
Karl Schultz481756e2016-02-02 15:37:51 -0700550 // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Chia-I Wuc19795a2014-09-13 11:12:55 +0800551 demo_draw_build_cmd(demo);
Chia-I Wue420a332015-10-26 20:04:44 +0800552 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultz481756e2016-02-02 15:37:51 -0700553 VkPipelineStageFlags pipe_stage_flags =
554 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
555 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
556 .pNext = NULL,
557 .waitSemaphoreCount = 1,
558 .pWaitSemaphores = &presentCompleteSemaphore,
559 .pWaitDstStageMask = &pipe_stage_flags,
560 .commandBufferCount = 1,
561 .pCommandBuffers = &demo->draw_cmd,
562 .signalSemaphoreCount = 0,
563 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600564
565 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800566 assert(!err);
567
Ian Elliott338dedb2015-08-21 15:09:33 -0600568 VkPresentInfoKHR present = {
569 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600570 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600571 .swapchainCount = 1,
Ian Elliottb5fad792015-11-20 11:55:46 -0700572 .pSwapchains = &demo->swapchain,
573 .pImageIndices = &demo->current_buffer,
Ian Elliotte36b2082015-07-06 14:27:58 -0600574 };
575
Karl Schultz481756e2016-02-02 15:37:51 -0700576 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600577 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliotte2688a52015-10-16 18:02:43 -0600578 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
579 // demo->swapchain is out of date (e.g. the window was resized) and
580 // must be recreated:
581 demo_resize(demo);
582 } else if (err == VK_SUBOPTIMAL_KHR) {
583 // demo->swapchain is not as optimal as it could be, but the platform's
584 // presentation engine will still present the image correctly.
585 } else {
586 assert(!err);
587 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800588
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800589 err = vkQueueWaitIdle(demo->queue);
590 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600591
Chia-I Wu69f40122015-10-26 21:10:41 +0800592 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800593}
594
Karl Schultz481756e2016-02-02 15:37:51 -0700595static void demo_prepare_buffers(struct demo *demo) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600596 VkResult U_ASSERT_ONLY err;
Ian Elliotte2688a52015-10-16 18:02:43 -0600597 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600598
Ian Elliottb5fad792015-11-20 11:55:46 -0700599 // Check the surface capabilities and formats
600 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultz481756e2016-02-02 15:37:51 -0700601 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
602 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliotte36b2082015-07-06 14:27:58 -0600603 assert(!err);
604
Ian Elliott7fe115d2015-08-07 15:56:59 -0600605 uint32_t presentModeCount;
Karl Schultz481756e2016-02-02 15:37:51 -0700606 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
607 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600608 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600609 VkPresentModeKHR *presentModes =
610 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600611 assert(presentModes);
Karl Schultz481756e2016-02-02 15:37:51 -0700612 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
613 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600614 assert(!err);
615
Ian Elliott338dedb2015-08-21 15:09:33 -0600616 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600617 // width and height are either both -1, or both not -1.
Karl Schultz481756e2016-02-02 15:37:51 -0700618 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600619 // If the surface size is undefined, the size is set to
620 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600621 swapchainExtent.width = demo->width;
622 swapchainExtent.height = demo->height;
Karl Schultz481756e2016-02-02 15:37:51 -0700623 } else {
Ian Elliotte36b2082015-07-06 14:27:58 -0600624 // If the surface size is defined, the swap chain size must match
Ian Elliottb5fad792015-11-20 11:55:46 -0700625 swapchainExtent = surfCapabilities.currentExtent;
626 demo->width = surfCapabilities.currentExtent.width;
627 demo->height = surfCapabilities.currentExtent.height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600628 }
629
Ian Elliott338dedb2015-08-21 15:09:33 -0600630 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600631
632 // Determine the number of VkImage's to use in the swap chain (we desire to
633 // own only 1 image at a time, besides the images being displayed and
634 // queued for display):
Karl Schultz481756e2016-02-02 15:37:51 -0700635 uint32_t desiredNumberOfSwapchainImages =
636 surfCapabilities.minImageCount + 1;
Ian Elliottb5fad792015-11-20 11:55:46 -0700637 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultz481756e2016-02-02 15:37:51 -0700638 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600639 // Application must settle for fewer images than desired:
Ian Elliottb5fad792015-11-20 11:55:46 -0700640 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600641 }
642
Ian Elliottb5fad792015-11-20 11:55:46 -0700643 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultz481756e2016-02-02 15:37:51 -0700644 if (surfCapabilities.supportedTransforms &
645 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliottda44dfe2015-12-11 15:52:12 -0700646 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600647 } else {
Ian Elliottb5fad792015-11-20 11:55:46 -0700648 preTransform = surfCapabilities.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600649 }
650
Ian Elliotte2688a52015-10-16 18:02:43 -0600651 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600652 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800653 .pNext = NULL,
Ian Elliottb5fad792015-11-20 11:55:46 -0700654 .surface = demo->surface,
Ian Elliott338dedb2015-08-21 15:09:33 -0600655 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800656 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600657 .imageColorSpace = demo->color_space,
Karl Schultz481756e2016-02-02 15:37:51 -0700658 .imageExtent =
659 {
660 .width = swapchainExtent.width, .height = swapchainExtent.height,
661 },
Ian Elliottb5fad792015-11-20 11:55:46 -0700662 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600663 .preTransform = preTransform,
Ian Elliott3f07ab62015-12-28 15:25:33 -0700664 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb5fad792015-11-20 11:55:46 -0700665 .imageArrayLayers = 1,
666 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
667 .queueFamilyIndexCount = 0,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600668 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600669 .presentMode = swapchainPresentMode,
Ian Elliotte2688a52015-10-16 18:02:43 -0600670 .oldSwapchain = oldSwapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600671 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800672 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600673 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800674
Karl Schultz481756e2016-02-02 15:37:51 -0700675 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
676 &demo->swapchain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800677 assert(!err);
678
Ian Elliotte2688a52015-10-16 18:02:43 -0600679 // If we just re-created an existing swapchain, we should destroy the old
680 // swapchain at this point.
681 // Note: destroying the swapchain also cleans up all its associated
682 // presentable images once the platform is done with them.
Chia-I Wue420a332015-10-26 20:04:44 +0800683 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb5fad792015-11-20 11:55:46 -0700684 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -0600685 }
686
687 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600688 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600689 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800690
Karl Schultz481756e2016-02-02 15:37:51 -0700691 VkImage *swapchainImages =
692 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliott338dedb2015-08-21 15:09:33 -0600693 assert(swapchainImages);
Ian Elliotte2688a52015-10-16 18:02:43 -0600694 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600695 &demo->swapchainImageCount,
696 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600697 assert(!err);
698
Karl Schultz481756e2016-02-02 15:37:51 -0700699 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
700 demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600701 assert(demo->buffers);
702
Ian Elliott338dedb2015-08-21 15:09:33 -0600703 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600704 VkImageViewCreateInfo color_attachment_view = {
705 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800706 .pNext = NULL,
707 .format = demo->format,
Karl Schultz481756e2016-02-02 15:37:51 -0700708 .components =
709 {
710 .r = VK_COMPONENT_SWIZZLE_R,
711 .g = VK_COMPONENT_SWIZZLE_G,
712 .b = VK_COMPONENT_SWIZZLE_B,
713 .a = VK_COMPONENT_SWIZZLE_A,
714 },
715 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
716 .baseMipLevel = 0,
717 .levelCount = 1,
718 .baseArrayLayer = 0,
719 .layerCount = 1},
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600720 .viewType = VK_IMAGE_VIEW_TYPE_2D,
721 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800722 };
723
Ian Elliott338dedb2015-08-21 15:09:33 -0600724 demo->buffers[i].image = swapchainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500725
Karl Schultz481756e2016-02-02 15:37:51 -0700726 // Render loop will expect image to have been used before and in
727 // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
728 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image
729 // to that state
730 demo_set_image_layout(
731 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700732 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
733 0);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600734
Chia-I Wuc19795a2014-09-13 11:12:55 +0800735 color_attachment_view.image = demo->buffers[i].image;
736
Karl Schultz481756e2016-02-02 15:37:51 -0700737 err = vkCreateImageView(demo->device, &color_attachment_view, NULL,
738 &demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800739 assert(!err);
740 }
Piers Daniell886be472015-02-23 16:23:13 -0700741
742 demo->current_buffer = 0;
Karl Schultz481756e2016-02-02 15:37:51 -0700743
Mark Young842f9412016-01-25 14:43:50 -0700744 if (NULL != presentModes) {
745 free(presentModes);
746 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800747}
748
Karl Schultz481756e2016-02-02 15:37:51 -0700749static void demo_prepare_depth(struct demo *demo) {
Tony Barbour8205d902015-04-16 15:59:00 -0600750 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600751 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600752 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800753 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600754 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800755 .format = depth_format,
Karl Schultz481756e2016-02-02 15:37:51 -0700756 .extent = {demo->width, demo->height, 1},
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800757 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600758 .arrayLayers = 1,
Chia-I Wu3138d6a2015-10-31 00:31:16 +0800759 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour8205d902015-04-16 15:59:00 -0600760 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600761 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800762 .flags = 0,
763 };
Chia-I Wu1f851912015-10-27 18:04:07 +0800764 VkMemoryAllocateInfo mem_alloc = {
Chia-I Wuc1f5e402015-11-10 16:21:09 +0800765 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500766 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800767 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600768 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800769 };
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600770 VkImageViewCreateInfo view = {
771 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800772 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +0800773 .image = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600774 .format = depth_format,
Karl Schultz481756e2016-02-02 15:37:51 -0700775 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
776 .baseMipLevel = 0,
777 .levelCount = 1,
778 .baseArrayLayer = 0,
779 .layerCount = 1},
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800780 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600781 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800782 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700783
Mark Lobodzinski23182612015-05-29 09:32:35 -0500784 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600785 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600786 bool U_ASSERT_ONLY pass;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800787
788 demo->depth.format = depth_format;
789
790 /* create image */
Karl Schultz481756e2016-02-02 15:37:51 -0700791 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800792 assert(!err);
793
Mark Lobodzinski72346292015-07-02 16:49:40 -0600794 /* get memory requirements for this object */
Karl Schultz481756e2016-02-02 15:37:51 -0700795 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600796
797 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500798 mem_alloc.allocationSize = mem_reqs.size;
Karl Schultz481756e2016-02-02 15:37:51 -0700799 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
800 0, /* No requirements */
801 &mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600802 assert(pass);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800803
Mark Lobodzinski23182612015-05-29 09:32:35 -0500804 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +0800805 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500806 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800807
Mark Lobodzinski23182612015-05-29 09:32:35 -0500808 /* bind memory */
Karl Schultz481756e2016-02-02 15:37:51 -0700809 err =
810 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500811 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800812
Karl Schultz481756e2016-02-02 15:37:51 -0700813 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
814 VK_IMAGE_LAYOUT_UNDEFINED,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700815 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
816 0);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600817
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800818 /* create image view */
819 view.image = demo->depth.image;
Chia-I Wu69f40122015-10-26 21:10:41 +0800820 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800821 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800822}
823
Karl Schultz481756e2016-02-02 15:37:51 -0700824static void
825demo_prepare_texture_image(struct demo *demo, const uint32_t *tex_colors,
826 struct texture_object *tex_obj, VkImageTiling tiling,
827 VkImageUsageFlags usage, VkFlags required_props) {
Tony Barbour8205d902015-04-16 15:59:00 -0600828 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600829 const int32_t tex_width = 2;
830 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600831 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600832 bool U_ASSERT_ONLY pass;
Chia-I Wub043fe32014-10-06 15:30:33 +0800833
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600834 tex_obj->tex_width = tex_width;
835 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700836
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600837 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600838 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700839 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600840 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700841 .format = tex_format,
Karl Schultz481756e2016-02-02 15:37:51 -0700842 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700843 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600844 .arrayLayers = 1,
Chia-I Wu3138d6a2015-10-31 00:31:16 +0800845 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700846 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600847 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700848 .flags = 0,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700849 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700850 };
Chia-I Wu1f851912015-10-27 18:04:07 +0800851 VkMemoryAllocateInfo mem_alloc = {
Chia-I Wuc1f5e402015-11-10 16:21:09 +0800852 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500853 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700854 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600855 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700856 };
857
Mark Lobodzinski23182612015-05-29 09:32:35 -0500858 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700859
Karl Schultz481756e2016-02-02 15:37:51 -0700860 err =
861 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700862 assert(!err);
863
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600864 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600865
Karl Schultz481756e2016-02-02 15:37:51 -0700866 mem_alloc.allocationSize = mem_reqs.size;
867 pass =
868 memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
869 required_props, &mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600870 assert(pass);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700871
Mark Lobodzinski23182612015-05-29 09:32:35 -0500872 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +0800873 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &tex_obj->mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500874 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700875
Mark Lobodzinski23182612015-05-29 09:32:35 -0500876 /* bind memory */
Karl Schultz481756e2016-02-02 15:37:51 -0700877 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500878 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700879
Tony Barbourf1eceb92015-10-15 12:42:56 -0600880 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600881 const VkImageSubresource subres = {
Chia-I Wu195c9e12015-10-27 19:55:05 +0800882 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700883 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600884 .arrayLayer = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700885 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600886 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700887 void *data;
888 int32_t x, y;
889
Karl Schultz481756e2016-02-02 15:37:51 -0700890 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
891 &layout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700892
Karl Schultz481756e2016-02-02 15:37:51 -0700893 err = vkMapMemory(demo->device, tex_obj->mem, 0,
894 mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700895 assert(!err);
896
897 for (y = 0; y < tex_height; y++) {
Karl Schultz481756e2016-02-02 15:37:51 -0700898 uint32_t *row = (uint32_t *)((char *)data + layout.rowPitch * y);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700899 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700900 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700901 }
902
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600903 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700904 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600905
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600906 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultz481756e2016-02-02 15:37:51 -0700907 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700908 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
909 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultz481756e2016-02-02 15:37:51 -0700910 /* setting the image layout does not reference the actual memory so no need
911 * to add a mem ref */
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700912}
913
Karl Schultz481756e2016-02-02 15:37:51 -0700914static void demo_destroy_texture_image(struct demo *demo,
915 struct texture_object *tex_obj) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700916 /* clean up staging resources */
Chia-I Wu69f40122015-10-26 21:10:41 +0800917 vkDestroyImage(demo->device, tex_obj->image, NULL);
918 vkFreeMemory(demo->device, tex_obj->mem, NULL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700919}
920
Karl Schultz481756e2016-02-02 15:37:51 -0700921static void demo_prepare_textures(struct demo *demo) {
Tony Barbour8205d902015-04-16 15:59:00 -0600922 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600923 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700924 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
Karl Schultz481756e2016-02-02 15:37:51 -0700925 {0xffff0000, 0xff00ff00},
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700926 };
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700927 uint32_t i;
Courtney Goeltzenleuchtere92af7e2015-12-17 09:53:29 -0700928 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700929
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600930 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700931
932 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Karl Schultz481756e2016-02-02 15:37:51 -0700933 if ((props.linearTilingFeatures &
934 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
935 !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700936 /* Device can texture using linear textures */
937 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Karl Schultz481756e2016-02-02 15:37:51 -0700938 VK_IMAGE_TILING_LINEAR,
939 VK_IMAGE_USAGE_SAMPLED_BIT,
940 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
941 } else if (props.optimalTilingFeatures &
942 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700943 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600944 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700945
946 memset(&staging_texture, 0, sizeof(staging_texture));
947 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Karl Schultz481756e2016-02-02 15:37:51 -0700948 VK_IMAGE_TILING_LINEAR,
949 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
950 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700951
Karl Schultz481756e2016-02-02 15:37:51 -0700952 demo_prepare_texture_image(
953 demo, tex_colors[i], &demo->textures[i],
954 VK_IMAGE_TILING_OPTIMAL,
955 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
956 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700957
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600958 demo_set_image_layout(demo, staging_texture.image,
Karl Schultz481756e2016-02-02 15:37:51 -0700959 VK_IMAGE_ASPECT_COLOR_BIT,
960 staging_texture.imageLayout,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700961 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
962 0);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700963
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600964 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultz481756e2016-02-02 15:37:51 -0700965 VK_IMAGE_ASPECT_COLOR_BIT,
966 demo->textures[i].imageLayout,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700967 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
968 0);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700969
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600970 VkImageCopy copy_region = {
Karl Schultz481756e2016-02-02 15:37:51 -0700971 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
972 .srcOffset = {0, 0, 0},
973 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
974 .dstOffset = {0, 0, 0},
975 .extent = {staging_texture.tex_width,
976 staging_texture.tex_height, 1},
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700977 };
Karl Schultz481756e2016-02-02 15:37:51 -0700978 vkCmdCopyImage(
979 demo->setup_cmd, staging_texture.image,
980 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
981 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700982
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600983 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultz481756e2016-02-02 15:37:51 -0700984 VK_IMAGE_ASPECT_COLOR_BIT,
985 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Mark Lobodzinskiacae0532016-02-22 08:57:55 -0700986 demo->textures[i].imageLayout,
987 0);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700988
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600989 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700990
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600991 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700992 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600993 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700994 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700995 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700996
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600997 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600998 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800999 .pNext = NULL,
Chia-I Wu3603b082015-10-26 16:49:32 +08001000 .magFilter = VK_FILTER_NEAREST,
1001 .minFilter = VK_FILTER_NEAREST,
Jon Ashburna4ae48b2016-01-11 13:12:43 -07001002 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wuce532f72015-10-26 17:32:47 +08001003 .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
1004 .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
1005 .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
Chia-I Wub043fe32014-10-06 15:30:33 +08001006 .mipLodBias = 0.0f,
Jon Ashburn0717ed52015-12-14 14:59:20 -07001007 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -07001008 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001009 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +08001010 .minLod = 0.0f,
1011 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001012 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -06001013 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +08001014 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001015 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001016 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +08001017 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +08001018 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001019 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -07001020 .format = tex_format,
Karl Schultz481756e2016-02-02 15:37:51 -07001021 .components =
1022 {
1023 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1024 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1025 },
1026 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001027 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +08001028 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001029
Chia-I Wub043fe32014-10-06 15:30:33 +08001030 /* create sampler */
Chia-I Wu69f40122015-10-26 21:10:41 +08001031 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultz481756e2016-02-02 15:37:51 -07001032 &demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08001033 assert(!err);
1034
Chia-I Wub043fe32014-10-06 15:30:33 +08001035 /* create image view */
1036 view.image = demo->textures[i].image;
Chia-I Wu69f40122015-10-26 21:10:41 +08001037 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultz481756e2016-02-02 15:37:51 -07001038 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +08001039 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001040 }
1041}
1042
Karl Schultz481756e2016-02-02 15:37:51 -07001043static void demo_prepare_vertices(struct demo *demo) {
1044 // clang-format off
Chia-I Wu99621bc2014-10-08 11:52:22 +08001045 const float vb[3][5] = {
1046 /* position texcoord */
Cody Northropa9efa052015-10-13 11:28:23 -06001047 { -1.0f, -1.0f, 0.25f, 0.0f, 0.0f },
Chia-I Wue2504cb2015-04-22 14:20:52 +08001048 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +08001049 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
1050 };
Karl Schultz481756e2016-02-02 15:37:51 -07001051 // clang-format on
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001052 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001053 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +08001054 .pNext = NULL,
1055 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001056 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +08001057 .flags = 0,
1058 };
Chia-I Wu1f851912015-10-27 18:04:07 +08001059 VkMemoryAllocateInfo mem_alloc = {
Chia-I Wuc1f5e402015-11-10 16:21:09 +08001060 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001061 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +08001062 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001063 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +08001064 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001065 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -06001066 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001067 bool U_ASSERT_ONLY pass;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001068 void *data;
1069
1070 memset(&demo->vertices, 0, sizeof(demo->vertices));
1071
Chia-I Wu69f40122015-10-26 21:10:41 +08001072 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001073 assert(!err);
1074
Karl Schultz481756e2016-02-02 15:37:51 -07001075 vkGetBufferMemoryRequirements(demo->device, demo->vertices.buf, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -06001076 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001077
Karl Schultz481756e2016-02-02 15:37:51 -07001078 mem_alloc.allocationSize = mem_reqs.size;
1079 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1080 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1081 &mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001082 assert(pass);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001083
Chia-I Wu1f851912015-10-27 18:04:07 +08001084 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001085 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001086
Karl Schultz481756e2016-02-02 15:37:51 -07001087 err = vkMapMemory(demo->device, demo->vertices.mem, 0,
1088 mem_alloc.allocationSize, 0, &data);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001089 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001090
Mark Lobodzinski23182612015-05-29 09:32:35 -05001091 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +08001092
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001093 vkUnmapMemory(demo->device, demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001094
Tony Barbourde4124d2015-07-03 10:33:54 -06001095 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Karl Schultz481756e2016-02-02 15:37:51 -07001096 demo->vertices.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001097 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001098
Karl Schultz481756e2016-02-02 15:37:51 -07001099 demo->vertices.vi.sType =
1100 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001101 demo->vertices.vi.pNext = NULL;
Chia-I Wu763a7492015-10-26 20:48:51 +08001102 demo->vertices.vi.vertexBindingDescriptionCount = 1;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001103 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
Chia-I Wu763a7492015-10-26 20:48:51 +08001104 demo->vertices.vi.vertexAttributeDescriptionCount = 2;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001105 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
1106
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001107 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu7e470702015-10-26 17:24:52 +08001108 demo->vertices.vi_bindings[0].stride = sizeof(vb[0]);
Chia-I Wuc51b1212015-10-27 19:25:11 +08001109 demo->vertices.vi_bindings[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001110
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001111 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
1112 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -06001113 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu7e470702015-10-26 17:24:52 +08001114 demo->vertices.vi_attrs[0].offset = 0;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001115
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001116 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
1117 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -06001118 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu7e470702015-10-26 17:24:52 +08001119 demo->vertices.vi_attrs[1].offset = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001120}
1121
Karl Schultz481756e2016-02-02 15:37:51 -07001122static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001123 const VkDescriptorSetLayoutBinding layout_binding = {
Chia-I Wub5689ee2015-10-31 00:31:16 +08001124 .binding = 0,
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001125 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu045654f2015-11-06 06:42:02 +08001126 .descriptorCount = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001127 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001128 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +08001129 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001130 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001131 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001132 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001133 .bindingCount = 1,
Jon Ashburnadb61352015-12-30 18:01:16 -07001134 .pBindings = &layout_binding,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001135 };
Tony Barbour22a30862015-04-22 09:02:32 -06001136 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +08001137
Karl Schultz481756e2016-02-02 15:37:51 -07001138 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1139 &demo->desc_layout);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001140 assert(!err);
1141
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001142 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultz481756e2016-02-02 15:37:51 -07001143 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1144 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001145 .setLayoutCount = 1,
Karl Schultz481756e2016-02-02 15:37:51 -07001146 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001147 };
1148
Karl Schultz481756e2016-02-02 15:37:51 -07001149 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001150 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001151 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001152}
1153
Karl Schultz481756e2016-02-02 15:37:51 -07001154static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wuc278df82015-07-07 11:50:03 +08001155 const VkAttachmentDescription attachments[2] = {
Karl Schultz481756e2016-02-02 15:37:51 -07001156 [0] =
1157 {
1158 .format = demo->format,
1159 .samples = VK_SAMPLE_COUNT_1_BIT,
1160 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1161 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1162 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1163 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1164 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1165 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1166 },
1167 [1] =
1168 {
1169 .format = demo->depth.format,
1170 .samples = VK_SAMPLE_COUNT_1_BIT,
1171 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1172 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1173 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1174 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1175 .initialLayout =
1176 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1177 .finalLayout =
1178 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1179 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001180 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001181 const VkAttachmentReference color_reference = {
Karl Schultz481756e2016-02-02 15:37:51 -07001182 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001183 };
Chia-I Wuce532f72015-10-26 17:32:47 +08001184 const VkAttachmentReference depth_reference = {
1185 .attachment = 1,
1186 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1187 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001188 const VkSubpassDescription subpass = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001189 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1190 .flags = 0,
Chia-I Wu763a7492015-10-26 20:48:51 +08001191 .inputAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001192 .pInputAttachments = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001193 .colorAttachmentCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001194 .pColorAttachments = &color_reference,
1195 .pResolveAttachments = NULL,
Chia-I Wuce532f72015-10-26 17:32:47 +08001196 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu763a7492015-10-26 20:48:51 +08001197 .preserveAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001198 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001199 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001200 const VkRenderPassCreateInfo rp_info = {
1201 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1202 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001203 .attachmentCount = 2,
1204 .pAttachments = attachments,
1205 .subpassCount = 1,
1206 .pSubpasses = &subpass,
1207 .dependencyCount = 0,
1208 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001209 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001210 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001211
Chia-I Wu69f40122015-10-26 21:10:41 +08001212 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001213 assert(!err);
1214}
1215
Karl Schultz481756e2016-02-02 15:37:51 -07001216static VkShaderModule
1217demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001218 VkShaderModuleCreateInfo moduleCreateInfo;
Chia-I Wu062ad152015-10-31 00:31:16 +08001219 VkShaderModule module;
Tony Barbour4a6692d2015-10-08 13:45:45 -06001220 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001221
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001222 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1223 moduleCreateInfo.pNext = NULL;
1224
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001225 moduleCreateInfo.codeSize = size;
1226 moduleCreateInfo.pCode = code;
1227 moduleCreateInfo.flags = 0;
1228 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1229 assert(!err);
Chia-I Wu062ad152015-10-31 00:31:16 +08001230
1231 return module;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001232}
1233
Karl Schultz481756e2016-02-02 15:37:51 -07001234char *demo_read_spv(const char *filename, size_t *psize) {
Cody Northrop75db0322015-05-28 11:27:16 -06001235 long int size;
1236 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001237 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001238
1239 FILE *fp = fopen(filename, "rb");
Karl Schultz481756e2016-02-02 15:37:51 -07001240 if (!fp)
1241 return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001242
1243 fseek(fp, 0L, SEEK_END);
1244 size = ftell(fp);
1245
1246 fseek(fp, 0L, SEEK_SET);
1247
1248 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001249 retVal = fread(shader_code, size, 1, fp);
Karl Schultz481756e2016-02-02 15:37:51 -07001250 if (!retVal)
1251 return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001252
1253 *psize = size;
1254
Mike Stroyan85180252015-10-28 11:15:46 -06001255 fclose(fp);
Cody Northrop75db0322015-05-28 11:27:16 -06001256 return shader_code;
1257}
1258
Karl Schultz481756e2016-02-02 15:37:51 -07001259static VkShaderModule demo_prepare_vs(struct demo *demo) {
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001260 void *vertShaderCode;
Tobin Ehlisb08d4872016-04-13 12:59:08 -06001261 size_t size = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001262
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001263 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001264
Karl Schultz481756e2016-02-02 15:37:51 -07001265 demo->vert_shader_module =
1266 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001267
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001268 free(vertShaderCode);
Chia-I Wu062ad152015-10-31 00:31:16 +08001269
1270 return demo->vert_shader_module;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001271}
1272
Karl Schultz481756e2016-02-02 15:37:51 -07001273static VkShaderModule demo_prepare_fs(struct demo *demo) {
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001274 void *fragShaderCode;
1275 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001276
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001277 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001278
Karl Schultz481756e2016-02-02 15:37:51 -07001279 demo->frag_shader_module =
1280 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001281
Courtney Goeltzenleuchter8961c982015-10-30 15:19:27 -06001282 free(fragShaderCode);
Chia-I Wu062ad152015-10-31 00:31:16 +08001283
1284 return demo->frag_shader_module;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001285}
1286
Karl Schultz481756e2016-02-02 15:37:51 -07001287static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001288 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001289 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001290
Karl Schultz481756e2016-02-02 15:37:51 -07001291 VkPipelineVertexInputStateCreateInfo vi;
Tony Barboure307f582015-07-10 15:29:03 -06001292 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultz481756e2016-02-02 15:37:51 -07001293 VkPipelineRasterizationStateCreateInfo rs;
1294 VkPipelineColorBlendStateCreateInfo cb;
1295 VkPipelineDepthStencilStateCreateInfo ds;
1296 VkPipelineViewportStateCreateInfo vp;
1297 VkPipelineMultisampleStateCreateInfo ms;
1298 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1299 VkPipelineDynamicStateCreateInfo dynamicState;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001300
Tony Barbour22a30862015-04-22 09:02:32 -06001301 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001302
Piers Daniell811eb742015-09-29 13:01:09 -06001303 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1304 memset(&dynamicState, 0, sizeof dynamicState);
1305 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1306 dynamicState.pDynamicStates = dynamicStateEnables;
1307
Chia-I Wuc19795a2014-09-13 11:12:55 +08001308 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001309 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001310 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001311
Chia-I Wu8d29d022014-10-08 12:14:39 +08001312 vi = demo->vertices.vi;
1313
Chia-I Wuc19795a2014-09-13 11:12:55 +08001314 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001315 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001316 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001317
1318 memset(&rs, 0, sizeof(rs));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001319 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1320 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wuce532f72015-10-26 17:32:47 +08001321 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wu1f851912015-10-27 18:04:07 +08001322 rs.frontFace = VK_FRONT_FACE_CLOCKWISE;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001323 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001324 rs.rasterizerDiscardEnable = VK_FALSE;
1325 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001326
1327 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001328 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1329 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001330 memset(att_state, 0, sizeof(att_state));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001331 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001332 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001333 cb.attachmentCount = 1;
1334 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001335
Tony Barbourfa6cac72015-01-16 14:27:35 -07001336 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001337 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001338 vp.viewportCount = 1;
Karl Schultz481756e2016-02-02 15:37:51 -07001339 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1340 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniell811eb742015-09-29 13:01:09 -06001341 vp.scissorCount = 1;
Karl Schultz481756e2016-02-02 15:37:51 -07001342 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1343 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001344
1345 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001346 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001347 ds.depthTestEnable = VK_TRUE;
1348 ds.depthWriteEnable = VK_TRUE;
Chia-I Wu1f851912015-10-27 18:04:07 +08001349 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001350 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuc51b1212015-10-27 19:25:11 +08001351 ds.back.failOp = VK_STENCIL_OP_KEEP;
1352 ds.back.passOp = VK_STENCIL_OP_KEEP;
1353 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001354 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001355 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001356
Tony Barbourfa6cac72015-01-16 14:27:35 -07001357 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001358 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001359 ms.pSampleMask = NULL;
Chia-I Wu3138d6a2015-10-31 00:31:16 +08001360 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Chia-I Wub043fe32014-10-06 15:30:33 +08001361
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001362 // Two stages: vs and fs
1363 pipeline.stageCount = 2;
1364 VkPipelineShaderStageCreateInfo shaderStages[2];
1365 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1366
Karl Schultz481756e2016-02-02 15:37:51 -07001367 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1368 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu062ad152015-10-31 00:31:16 +08001369 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultz481756e2016-02-02 15:37:51 -07001370 shaderStages[0].pName = "main";
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001371
Karl Schultz481756e2016-02-02 15:37:51 -07001372 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1373 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu062ad152015-10-31 00:31:16 +08001374 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultz481756e2016-02-02 15:37:51 -07001375 shaderStages[1].pName = "main";
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001376
Karl Schultz481756e2016-02-02 15:37:51 -07001377 pipeline.pVertexInputState = &vi;
Tony Barboure307f582015-07-10 15:29:03 -06001378 pipeline.pInputAssemblyState = &ia;
Karl Schultz481756e2016-02-02 15:37:51 -07001379 pipeline.pRasterizationState = &rs;
1380 pipeline.pColorBlendState = &cb;
1381 pipeline.pMultisampleState = &ms;
1382 pipeline.pViewportState = &vp;
1383 pipeline.pDepthStencilState = &ds;
1384 pipeline.pStages = shaderStages;
1385 pipeline.renderPass = demo->render_pass;
1386 pipeline.pDynamicState = &dynamicState;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001387
Jon Ashburn0d60d272015-07-09 15:02:25 -06001388 memset(&pipelineCache, 0, sizeof(pipelineCache));
1389 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1390
Karl Schultz481756e2016-02-02 15:37:51 -07001391 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1392 &demo->pipelineCache);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001393 assert(!err);
Karl Schultz481756e2016-02-02 15:37:51 -07001394 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1395 &pipeline, NULL, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001396 assert(!err);
1397
Chia-I Wu69f40122015-10-26 21:10:41 +08001398 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001399
Chia-I Wu69f40122015-10-26 21:10:41 +08001400 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1401 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001402}
1403
Karl Schultz481756e2016-02-02 15:37:51 -07001404static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuc51b1212015-10-27 19:25:11 +08001405 const VkDescriptorPoolSize type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001406 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001407 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wuf8385062015-01-04 16:27:24 +08001408 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001409 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001410 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001411 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001412 .maxSets = 1,
Chia-I Wuc51b1212015-10-27 19:25:11 +08001413 .poolSizeCount = 1,
1414 .pPoolSizes = &type_count,
Chia-I Wuf8385062015-01-04 16:27:24 +08001415 };
Tony Barbour22a30862015-04-22 09:02:32 -06001416 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001417
Karl Schultz481756e2016-02-02 15:37:51 -07001418 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1419 &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001420 assert(!err);
1421}
1422
Karl Schultz481756e2016-02-02 15:37:51 -07001423static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001424 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001425 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001426 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001427 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001428
Chia-I Wu1f851912015-10-27 18:04:07 +08001429 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuc1f5e402015-11-10 16:21:09 +08001430 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001431 .pNext = NULL,
1432 .descriptorPool = demo->desc_pool,
Jon Ashburna4ae48b2016-01-11 13:12:43 -07001433 .descriptorSetCount = 1,
Karl Schultz481756e2016-02-02 15:37:51 -07001434 .pSetLayouts = &demo->desc_layout};
Chia-I Wu1f851912015-10-27 18:04:07 +08001435 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northropc8aa4a52015-08-03 12:47:29 -06001436 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001437
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001438 memset(&tex_descs, 0, sizeof(tex_descs));
1439 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001440 tex_descs[i].sampler = demo->textures[i].sampler;
1441 tex_descs[i].imageView = demo->textures[i].view;
1442 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001443 }
1444
1445 memset(&write, 0, sizeof(write));
1446 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu1f851912015-10-27 18:04:07 +08001447 write.dstSet = demo->desc_set;
Chia-I Wu763a7492015-10-26 20:48:51 +08001448 write.descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001449 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001450 write.pImageInfo = tex_descs;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001451
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001452 vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001453}
1454
Karl Schultz481756e2016-02-02 15:37:51 -07001455static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001456 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001457 attachments[1] = demo->depth.view;
1458
Chia-I Wu76cd4222015-07-08 13:34:24 +08001459 const VkFramebufferCreateInfo fb_info = {
Karl Schultz481756e2016-02-02 15:37:51 -07001460 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1461 .pNext = NULL,
1462 .renderPass = demo->render_pass,
1463 .attachmentCount = 2,
1464 .pAttachments = attachments,
1465 .width = demo->width,
1466 .height = demo->height,
1467 .layers = 1,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001468 };
1469 VkResult U_ASSERT_ONLY err;
1470 uint32_t i;
1471
Karl Schultz481756e2016-02-02 15:37:51 -07001472 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1473 sizeof(VkFramebuffer));
Tony Barbour5aabff52015-10-08 14:26:24 -06001474 assert(demo->framebuffers);
1475
1476 for (i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultz481756e2016-02-02 15:37:51 -07001477 attachments[0] = demo->buffers[i].view;
1478 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1479 &demo->framebuffers[i]);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001480 assert(!err);
1481 }
1482}
1483
Karl Schultz481756e2016-02-02 15:37:51 -07001484static void demo_prepare(struct demo *demo) {
Cody Northrop18ea11b2015-07-09 18:08:32 -06001485 VkResult U_ASSERT_ONLY err;
1486
Chia-I Wu1f851912015-10-27 18:04:07 +08001487 const VkCommandPoolCreateInfo cmd_pool_info = {
1488 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001489 .pNext = NULL,
1490 .queueFamilyIndex = demo->graphics_queue_node_index,
Mark Lobodzinskiac6b2c72015-12-14 15:47:36 -07001491 .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001492 };
Karl Schultz481756e2016-02-02 15:37:51 -07001493 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1494 &demo->cmd_pool);
Cody Northrop18ea11b2015-07-09 18:08:32 -06001495 assert(!err);
1496
Chia-I Wu1f851912015-10-27 18:04:07 +08001497 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuc1f5e402015-11-10 16:21:09 +08001498 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001499 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001500 .commandPool = demo->cmd_pool,
1501 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburna4ae48b2016-01-11 13:12:43 -07001502 .commandBufferCount = 1,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001503 };
Chia-I Wu1f851912015-10-27 18:04:07 +08001504 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->draw_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001505 assert(!err);
1506
Chia-I Wuc19795a2014-09-13 11:12:55 +08001507 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001508 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001509 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001510 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001511 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001512 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001513 demo_prepare_pipeline(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001514
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001515 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001516 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001517
1518 demo_prepare_framebuffers(demo);
1519
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001520 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001521}
1522
Ian Elliotte14e9f92015-04-16 15:23:05 -06001523#ifdef _WIN32
Karl Schultz481756e2016-02-02 15:37:51 -07001524static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001525 if (!demo->prepared)
1526 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001527 demo_draw(demo);
Tony Barbour4a6692d2015-10-08 13:45:45 -06001528
1529 if (demo->depthStencil > 0.99f)
1530 demo->depthIncrement = -0.001f;
1531 if (demo->depthStencil < 0.8f)
1532 demo->depthIncrement = 0.001f;
1533
1534 demo->depthStencil += demo->depthIncrement;
Karl Schultz501fdc22016-03-24 18:43:41 -06001535
1536 demo->curFrame++;
1537 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
1538 PostQuitMessage(validation_error);
1539 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001540}
1541
1542// On MS-Windows, make this a global, so it's available to WndProc()
1543struct demo demo;
1544
1545// MS-Windows event handling function:
Karl Schultz481756e2016-02-02 15:37:51 -07001546LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001547 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001548
Karl Schultz481756e2016-02-02 15:37:51 -07001549 switch (uMsg) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001550 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001551 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001552 case WM_CLOSE:
Karl Schultz501fdc22016-03-24 18:43:41 -06001553 PostQuitMessage(validation_error);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001554 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001555 case WM_PAINT:
Cody Northrop67ff0a42015-09-09 10:21:49 -06001556 if (demo.prepared) {
1557 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001558 break;
Cody Northrop67ff0a42015-09-09 10:21:49 -06001559 }
Ian Elliottf3f80822015-10-21 15:14:07 -06001560 case WM_SIZE:
Piers Daniellfdd5af62016-02-18 10:54:15 -07001561 // Resize the application to the new window size, except when
1562 // it was minimized. Vulkan doesn't support images or swapchains
1563 // with width=0 and height=0.
1564 if (wParam != SIZE_MINIMIZED) {
1565 demo.width = lParam & 0xffff;
1566 demo.height = lParam & 0xffff0000 >> 16;
1567 demo_resize(&demo);
1568 }
Ian Elliottf3f80822015-10-21 15:14:07 -06001569 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001570 default:
1571 break;
1572 }
1573 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1574}
1575
Karl Schultz481756e2016-02-02 15:37:51 -07001576static void demo_create_window(struct demo *demo) {
1577 WNDCLASSEX win_class;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001578
1579 // Initialize the window class structure:
1580 win_class.cbSize = sizeof(WNDCLASSEX);
1581 win_class.style = CS_HREDRAW | CS_VREDRAW;
1582 win_class.lpfnWndProc = WndProc;
1583 win_class.cbClsExtra = 0;
1584 win_class.cbWndExtra = 0;
1585 win_class.hInstance = demo->connection; // hInstance
1586 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1587 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1588 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1589 win_class.lpszMenuName = NULL;
1590 win_class.lpszClassName = demo->name;
1591 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1592 // Register window class:
1593 if (!RegisterClassEx(&win_class)) {
1594 // It didn't work, so try to give a useful error:
1595 printf("Unexpected error trying to start the application!\n");
1596 fflush(stdout);
1597 exit(1);
1598 }
1599 // Create window with the registered class:
Karl Schultz481756e2016-02-02 15:37:51 -07001600 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyan7eef5742015-06-15 14:19:19 -06001601 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001602 demo->window = CreateWindowEx(0,
1603 demo->name, // class name
1604 demo->name, // app name
1605 WS_OVERLAPPEDWINDOW | // window style
Karl Schultz481756e2016-02-02 15:37:51 -07001606 WS_VISIBLE | WS_SYSMENU,
1607 100, 100, // x/y coords
1608 wr.right - wr.left, // width
1609 wr.bottom - wr.top, // height
1610 NULL, // handle to parent
1611 NULL, // handle to menu
1612 demo->connection, // hInstance
1613 NULL); // no extra parameters
Ian Elliotte14e9f92015-04-16 15:23:05 -06001614 if (!demo->window) {
1615 // It didn't work, so try to give a useful error:
1616 printf("Cannot create a window in which to draw!\n");
1617 fflush(stdout);
1618 exit(1);
1619 }
1620}
1621#else // _WIN32
1622
Chia-I Wuc19795a2014-09-13 11:12:55 +08001623static void demo_handle_event(struct demo *demo,
Karl Schultz481756e2016-02-02 15:37:51 -07001624 const xcb_generic_event_t *event) {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001625 switch (event->response_type & 0x7f) {
1626 case XCB_EXPOSE:
1627 demo_draw(demo);
1628 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001629 case XCB_CLIENT_MESSAGE:
Karl Schultz481756e2016-02-02 15:37:51 -07001630 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
1631 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001632 demo->quit = true;
1633 }
1634 break;
Karl Schultz481756e2016-02-02 15:37:51 -07001635 case XCB_KEY_RELEASE: {
1636 const xcb_key_release_event_t *key =
1637 (const xcb_key_release_event_t *)event;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001638
Karl Schultz481756e2016-02-02 15:37:51 -07001639 if (key->detail == 0x9)
1640 demo->quit = true;
1641 } break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001642 case XCB_DESTROY_NOTIFY:
1643 demo->quit = true;
1644 break;
Karl Schultz481756e2016-02-02 15:37:51 -07001645 case XCB_CONFIGURE_NOTIFY: {
1646 const xcb_configure_notify_event_t *cfg =
1647 (const xcb_configure_notify_event_t *)event;
1648 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone3d77c302016-01-26 14:34:12 -08001649 demo->width = cfg->width;
1650 demo->height = cfg->height;
Karl Schultz481756e2016-02-02 15:37:51 -07001651 demo_resize(demo);
Ian Elliotte2688a52015-10-16 18:02:43 -06001652 }
Karl Schultz481756e2016-02-02 15:37:51 -07001653 } break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001654 default:
1655 break;
1656 }
1657}
1658
Karl Schultz481756e2016-02-02 15:37:51 -07001659static void demo_run(struct demo *demo) {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001660 xcb_flush(demo->connection);
1661
1662 while (!demo->quit) {
1663 xcb_generic_event_t *event;
1664
Tony Barbour4a6692d2015-10-08 13:45:45 -06001665 event = xcb_poll_for_event(demo->connection);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001666 if (event) {
1667 demo_handle_event(demo, event);
1668 free(event);
1669 }
Tony Barbour4a6692d2015-10-08 13:45:45 -06001670
1671 demo_draw(demo);
1672
1673 if (demo->depthStencil > 0.99f)
1674 demo->depthIncrement = -0.001f;
1675 if (demo->depthStencil < 0.8f)
1676 demo->depthIncrement = 0.001f;
1677
1678 demo->depthStencil += demo->depthIncrement;
1679
1680 // Wait for work to finish before updating MVP.
1681 vkDeviceWaitIdle(demo->device);
Karl Schultz501fdc22016-03-24 18:43:41 -06001682 demo->curFrame++;
1683 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
1684 demo->quit = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001685 }
1686}
1687
Karl Schultz481756e2016-02-02 15:37:51 -07001688static void demo_create_window(struct demo *demo) {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001689 uint32_t value_mask, value_list[32];
1690
1691 demo->window = xcb_generate_id(demo->connection);
1692
1693 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1694 value_list[0] = demo->screen->black_pixel;
Karl Schultz481756e2016-02-02 15:37:51 -07001695 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001696 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001697
Karl Schultz481756e2016-02-02 15:37:51 -07001698 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window,
1699 demo->screen->root, 0, 0, demo->width, demo->height, 0,
1700 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
1701 value_mask, value_list);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001702
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001703 /* Magic code that will send notification when window is destroyed */
Karl Schultz481756e2016-02-02 15:37:51 -07001704 xcb_intern_atom_cookie_t cookie =
1705 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
1706 xcb_intern_atom_reply_t *reply =
1707 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001708
Karl Schultz481756e2016-02-02 15:37:51 -07001709 xcb_intern_atom_cookie_t cookie2 =
1710 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1711 demo->atom_wm_delete_window =
1712 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001713
Karl Schultz481756e2016-02-02 15:37:51 -07001714 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window,
1715 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001716 &(*demo->atom_wm_delete_window).atom);
1717 free(reply);
1718
Chia-I Wuc19795a2014-09-13 11:12:55 +08001719 xcb_map_window(demo->connection, demo->window);
1720}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001721#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001722
Ian Elliotte36b2082015-07-06 14:27:58 -06001723/*
1724 * Return 1 (true) if all layer names specified in check_names
1725 * can be found in given layer properties.
1726 */
1727static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultz481756e2016-02-02 15:37:51 -07001728 uint32_t layer_count,
1729 VkLayerProperties *layers) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001730 for (uint32_t i = 0; i < check_count; i++) {
1731 VkBool32 found = 0;
1732 for (uint32_t j = 0; j < layer_count; j++) {
1733 if (!strcmp(check_names[i], layers[j].layerName)) {
1734 found = 1;
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001735 break;
Ian Elliotte36b2082015-07-06 14:27:58 -06001736 }
1737 }
1738 if (!found) {
1739 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1740 return 0;
1741 }
1742 }
1743 return 1;
1744}
Derrick Owensf83966f2016-01-21 14:46:16 -05001745
Karl Schultz481756e2016-02-02 15:37:51 -07001746static void demo_init_vk(struct demo *demo) {
Tobin Ehlis3536b442015-04-16 18:04:57 -06001747 VkResult err;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001748 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001749 uint32_t instance_layer_count = 0;
Tony Barbour3d03a4a2016-01-22 14:36:40 -07001750 uint32_t device_validation_layer_count = 0;
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001751 char **instance_validation_layers = NULL;
Tony Barbour3d03a4a2016-01-22 14:36:40 -07001752 demo->enabled_extension_count = 0;
1753 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001754
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001755 char *instance_validation_layers_alt1[] = {
1756 "VK_LAYER_LUNARG_standard_validation"
Ian Elliotte36b2082015-07-06 14:27:58 -06001757 };
1758
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001759 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski1c333572016-03-17 15:08:18 -06001760 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001761 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis5b5e7bc2016-03-09 16:12:48 -07001762 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
1763 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001764 };
Ian Elliotte36b2082015-07-06 14:27:58 -06001765
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001766 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001767 VkBool32 validation_found = 0;
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001768 if (demo->validate) {
Tobin Ehlis3536b442015-04-16 18:04:57 -06001769
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001770 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001771 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001772
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001773 instance_validation_layers = instance_validation_layers_alt1;
1774 if (instance_layer_count > 0) {
1775 VkLayerProperties *instance_layers =
1776 malloc(sizeof (VkLayerProperties) * instance_layer_count);
1777 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
1778 instance_layers);
1779 assert(!err);
1780
1781
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001782 validation_found = demo_check_layers(
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001783 ARRAY_SIZE(instance_validation_layers_alt1),
1784 instance_validation_layers, instance_layer_count,
1785 instance_layers);
1786 if (validation_found) {
1787 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
1788 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation";
1789 device_validation_layer_count = 1;
1790 } else {
1791 // use alternative set of validation layers
1792 instance_validation_layers = instance_validation_layers_alt2;
1793 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
1794 validation_found = demo_check_layers(
1795 ARRAY_SIZE(instance_validation_layers_alt2),
1796 instance_validation_layers, instance_layer_count,
1797 instance_layers);
1798 device_validation_layer_count =
1799 ARRAY_SIZE(instance_validation_layers_alt2);
1800 for (uint32_t i = 0; i < device_validation_layer_count; i++) {
1801 demo->device_validation_layers[i] =
1802 instance_validation_layers[i];
1803 }
1804 }
1805 free(instance_layers);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001806 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001807
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001808 if (!validation_found) {
Karl Schultz2a5eaf02016-04-07 09:40:30 -06001809 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001810 "required validation layer.\n\n"
1811 "Please look at the Getting Started guide for additional "
1812 "information.\n",
1813 "vkCreateInstance Failure");
1814 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001815 }
1816
1817 /* Look for instance extensions */
1818 VkBool32 surfaceExtFound = 0;
1819 VkBool32 platformSurfaceExtFound = 0;
1820 memset(demo->extension_names, 0, sizeof(demo->extension_names));
1821
Karl Schultz481756e2016-02-02 15:37:51 -07001822 err = vkEnumerateInstanceExtensionProperties(
1823 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001824 assert(!err);
1825
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001826 if (instance_extension_count > 0) {
1827 VkExtensionProperties *instance_extensions =
1828 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1829 err = vkEnumerateInstanceExtensionProperties(
1830 NULL, &instance_extension_count, instance_extensions);
1831 assert(!err);
1832 for (uint32_t i = 0; i < instance_extension_count; i++) {
1833 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
1834 instance_extensions[i].extensionName)) {
1835 surfaceExtFound = 1;
Karl Schultz481756e2016-02-02 15:37:51 -07001836 demo->extension_names[demo->enabled_extension_count++] =
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001837 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001838 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001839#ifdef _WIN32
1840 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
1841 instance_extensions[i].extensionName)) {
1842 platformSurfaceExtFound = 1;
1843 demo->extension_names[demo->enabled_extension_count++] =
1844 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
1845 }
1846#else // _WIN32
1847 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
1848 instance_extensions[i].extensionName)) {
1849 platformSurfaceExtFound = 1;
1850 demo->extension_names[demo->enabled_extension_count++] =
1851 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
1852 }
1853#endif // _WIN32
1854 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
1855 instance_extensions[i].extensionName)) {
1856 if (demo->validate) {
1857 demo->extension_names[demo->enabled_extension_count++] =
1858 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
1859 }
1860 }
1861 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001862 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001863
1864 free(instance_extensions);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001865 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001866
Ian Elliottf1d90132015-11-20 13:08:34 -07001867 if (!surfaceExtFound) {
Karl Schultz481756e2016-02-02 15:37:51 -07001868 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
1869 "the " VK_KHR_SURFACE_EXTENSION_NAME
1870 " extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001871 "Vulkan installable client driver (ICD) installed?\nPlease "
1872 "look at the Getting Started guide for additional "
1873 "information.\n",
1874 "vkCreateInstance Failure");
1875 }
Ian Elliottf1d90132015-11-20 13:08:34 -07001876 if (!platformSurfaceExtFound) {
Ian Elliottf1d90132015-11-20 13:08:34 -07001877#ifdef _WIN32
Karl Schultz481756e2016-02-02 15:37:51 -07001878 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
1879 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
1880 " extension.\n\nDo you have a compatible "
1881 "Vulkan installable client driver (ICD) installed?\nPlease "
1882 "look at the Getting Started guide for additional "
1883 "information.\n",
1884 "vkCreateInstance Failure");
Ian Elliottf1d90132015-11-20 13:08:34 -07001885#else // _WIN32
Karl Schultz481756e2016-02-02 15:37:51 -07001886 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
1887 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
1888 " extension.\n\nDo you have a compatible "
1889 "Vulkan installable client driver (ICD) installed?\nPlease "
1890 "look at the Getting Started guide for additional "
1891 "information.\n",
1892 "vkCreateInstance Failure");
Ian Elliottf1d90132015-11-20 13:08:34 -07001893#endif // _WIN32
Ian Elliottf1d90132015-11-20 13:08:34 -07001894 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001895 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001896 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001897 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001898 .pApplicationName = APP_SHORT_NAME,
1899 .applicationVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001900 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001901 .engineVersion = 0,
Jon Ashburnd3995c92016-03-22 13:57:46 -06001902 .apiVersion = VK_API_VERSION_1_0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001903 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001904 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001905 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001906 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001907 .pApplicationInfo = &app,
Tony Barbour3d03a4a2016-01-22 14:36:40 -07001908 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultz481756e2016-02-02 15:37:51 -07001909 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
Tony Barbour3d03a4a2016-01-22 14:36:40 -07001910 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultz481756e2016-02-02 15:37:51 -07001911 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001912 };
Tony Barbour3d03a4a2016-01-22 14:36:40 -07001913
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001914 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001915
Tony Barbour3ec16a52016-03-07 15:48:06 -07001916 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001917 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1918 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001919 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001920 "additional information.\n",
1921 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06001922 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001923 ERR_EXIT("Cannot find a specified extension library"
1924 ".\nMake sure your layers path is set appropriately\n",
1925 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001926 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001927 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1928 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001929 "the Getting Started guide for additional information.\n",
1930 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001931 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001932
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001933 /* Make initial call to query gpu_count, then second call for gpu info*/
1934 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
1935 assert(!err && gpu_count > 0);
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001936
1937 if (gpu_count > 0) {
1938 VkPhysicalDevice *physical_devices =
1939 malloc(sizeof(VkPhysicalDevice) * gpu_count);
1940 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count,
1941 physical_devices);
1942 assert(!err);
1943 /* For tri demo we just grab the first physical device */
1944 demo->gpu = physical_devices[0];
1945 free(physical_devices);
1946 } else {
1947 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices."
1948 "\n\nDo you have a compatible Vulkan installable client"
1949 " driver (ICD) installed?\nPlease look at the Getting Started"
1950 " guide for additional information.\n",
1951 "vkEnumeratePhysicalDevices Failure");
1952 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001953
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001954 /* Look for validation layers */
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001955 if (demo->validate) {
1956 validation_found = 0;
1957 demo->enabled_layer_count = 0;
1958 uint32_t device_layer_count = 0;
1959 err =
1960 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001961 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001962
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001963 if (device_layer_count > 0) {
1964 VkLayerProperties *device_layers =
1965 malloc(sizeof (VkLayerProperties) * device_layer_count);
1966 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
1967 device_layers);
1968 assert(!err);
1969
1970
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001971 validation_found = demo_check_layers(device_validation_layer_count,
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001972 demo->device_validation_layers,
1973 device_layer_count,
1974 device_layers);
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001975 demo->enabled_layer_count = device_validation_layer_count;
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001976
1977 free(device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001978 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001979
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001980 if (!validation_found) {
1981 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find "
1982 "a required validation layer.\n\n"
1983 "Please look at the Getting Started guide for additional "
1984 "information.\n",
1985 "vkCreateDevice Failure");
1986 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001987 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001988
Jon Ashburn99ea74d2016-02-19 15:23:41 -07001989 /* Look for device extensions */
Ian Elliotte36b2082015-07-06 14:27:58 -06001990 uint32_t device_extension_count = 0;
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001991 VkBool32 swapchainExtFound = 0;
1992 demo->enabled_extension_count = 0;
1993 memset(demo->extension_names, 0, sizeof(demo->extension_names));
1994
Karl Schultz481756e2016-02-02 15:37:51 -07001995 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
1996 &device_extension_count, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06001997 assert(!err);
1998
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07001999 if (device_extension_count > 0) {
2000 VkExtensionProperties *device_extensions =
2001 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2002 err = vkEnumerateDeviceExtensionProperties(
2003 demo->gpu, NULL, &device_extension_count, device_extensions);
2004 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06002005
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07002006 for (uint32_t i = 0; i < device_extension_count; i++) {
2007 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2008 device_extensions[i].extensionName)) {
2009 swapchainExtFound = 1;
2010 demo->extension_names[demo->enabled_extension_count++] =
2011 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2012 }
2013 assert(demo->enabled_extension_count < 64);
Ian Elliotte36b2082015-07-06 14:27:58 -06002014 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07002015
2016 free(device_extensions);
Ian Elliotte36b2082015-07-06 14:27:58 -06002017 }
Dustin Gravesbeebf7d2016-01-27 13:48:06 -07002018
Tony Barbourd9955e42015-10-08 13:59:42 -06002019 if (!swapchainExtFound) {
Karl Schultz481756e2016-02-02 15:37:51 -07002020 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2021 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2022 " extension.\n\nDo you have a compatible "
Ian Elliotte36b2082015-07-06 14:27:58 -06002023 "Vulkan installable client driver (ICD) installed?\nPlease "
2024 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002025 "information.\n",
2026 "vkCreateInstance Failure");
2027 }
2028
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002029 if (demo->validate) {
Karl Schultz481756e2016-02-02 15:37:51 -07002030 demo->CreateDebugReportCallback =
2031 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2032 demo->inst, "vkCreateDebugReportCallbackEXT");
Mark Lobodzinski96b82412016-02-22 09:32:55 -07002033 demo->DestroyDebugReportCallback =
2034 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2035 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -07002036 if (!demo->CreateDebugReportCallback) {
Karl Schultz481756e2016-02-02 15:37:51 -07002037 ERR_EXIT(
2038 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2039 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002040 }
Mark Lobodzinski96b82412016-02-22 09:32:55 -07002041 if (!demo->DestroyDebugReportCallback) {
2042 ERR_EXIT(
2043 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2044 "vkGetProcAddr Failure");
2045 }
2046 demo->DebugReportMessage =
2047 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2048 demo->inst, "vkDebugReportMessageEXT");
2049 if (!demo->DebugReportMessage) {
2050 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
2051 "vkGetProcAddr Failure");
2052 }
2053
Courtney Goeltzenleuchteracb13592015-12-09 15:48:16 -07002054 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
2055 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Karl Schultz481756e2016-02-02 15:37:51 -07002056 dbgCreateInfo.flags =
Mark Lobodzinski5c13d4d2016-02-11 09:26:16 -07002057 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultz7c5f63e2016-03-25 14:25:16 -06002058 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -07002059 dbgCreateInfo.pUserData = NULL;
2060 dbgCreateInfo.pNext = NULL;
Karl Schultz481756e2016-02-02 15:37:51 -07002061 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2062 &demo->msg_callback);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002063 switch (err) {
2064 case VK_SUCCESS:
2065 break;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002066 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -07002067 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2068 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002069 break;
2070 default:
Courtney Goeltzenleuchterf6a6e222015-11-30 12:13:14 -07002071 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2072 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002073 break;
2074 }
2075 }
2076
Jon Ashburn250984c2015-11-17 15:22:07 -07002077 // Having these GIPA queries of device extension entry points both
2078 // BEFORE and AFTER vkCreateDevice is a good test for the loader
Ian Elliott64070a82015-11-17 17:29:40 -07002079 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2080 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2081 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
Ian Elliott338dedb2015-08-21 15:09:33 -06002082 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
Jon Ashburn250984c2015-11-17 15:22:07 -07002083 GET_INSTANCE_PROC_ADDR(demo->inst, CreateSwapchainKHR);
2084 GET_INSTANCE_PROC_ADDR(demo->inst, DestroySwapchainKHR);
2085 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2086 GET_INSTANCE_PROC_ADDR(demo->inst, AcquireNextImageKHR);
2087 GET_INSTANCE_PROC_ADDR(demo->inst, QueuePresentKHR);
Ian Elliott1b6de092015-06-22 15:07:49 -06002088
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002089 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002090
Cody Northropef72e2a2015-08-03 17:04:53 -06002091 // Query with NULL data to get count
Karl Schultz481756e2016-02-02 15:37:51 -07002092 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2093 NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002094
Karl Schultz481756e2016-02-02 15:37:51 -07002095 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2096 demo->queue_count * sizeof(VkQueueFamilyProperties));
2097 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2098 demo->queue_props);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002099 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002100
Chris Forbesa3c20a22016-03-15 10:12:48 +13002101 VkPhysicalDeviceFeatures features;
2102 vkGetPhysicalDeviceFeatures(demo->gpu, &features);
2103
2104 if (!features.shaderClipDistance) {
2105 ERR_EXIT("Required device feature `shaderClipDistance` not supported\n",
2106 "GetPhysicalDeviceFeatures failure");
2107 }
2108
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002109 // Graphics queue and MemMgr queue can be separate.
2110 // TODO: Add support for separate queues, including synchronization,
2111 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002112}
2113
Karl Schultz481756e2016-02-02 15:37:51 -07002114static void demo_init_device(struct demo *demo) {
Tony Barbour3d03a4a2016-01-22 14:36:40 -07002115 VkResult U_ASSERT_ONLY err;
2116
Karl Schultz481756e2016-02-02 15:37:51 -07002117 float queue_priorities[1] = {0.0};
Tony Barbour3d03a4a2016-01-22 14:36:40 -07002118 const VkDeviceQueueCreateInfo queue = {
2119 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2120 .pNext = NULL,
2121 .queueFamilyIndex = demo->graphics_queue_node_index,
2122 .queueCount = 1,
Karl Schultz481756e2016-02-02 15:37:51 -07002123 .pQueuePriorities = queue_priorities};
Tony Barbour3d03a4a2016-01-22 14:36:40 -07002124
Chris Forbesa3c20a22016-03-15 10:12:48 +13002125 VkPhysicalDeviceFeatures features = {
2126 .shaderClipDistance = VK_TRUE,
2127 };
2128
Tony Barbour3d03a4a2016-01-22 14:36:40 -07002129 VkDeviceCreateInfo device = {
2130 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2131 .pNext = NULL,
2132 .queueCreateInfoCount = 1,
2133 .pQueueCreateInfos = &queue,
2134 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultz481756e2016-02-02 15:37:51 -07002135 .ppEnabledLayerNames =
2136 (const char *const *)((demo->validate)
2137 ? demo->device_validation_layers
2138 : NULL),
Tony Barbour3d03a4a2016-01-22 14:36:40 -07002139 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultz481756e2016-02-02 15:37:51 -07002140 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Chris Forbesa3c20a22016-03-15 10:12:48 +13002141 .pEnabledFeatures = &features,
Tony Barbour3d03a4a2016-01-22 14:36:40 -07002142 };
2143
2144 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
2145 assert(!err);
2146}
2147
Karl Schultz481756e2016-02-02 15:37:51 -07002148static void demo_init_vk_swapchain(struct demo *demo) {
Tony Barbour4a6692d2015-10-08 13:45:45 -06002149 VkResult U_ASSERT_ONLY err;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002150 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06002151
Karl Schultz481756e2016-02-02 15:37:51 -07002152// Create a WSI surface for the window:
Ian Elliotte36b2082015-07-06 14:27:58 -06002153#ifdef _WIN32
Ian Elliottda44dfe2015-12-11 15:52:12 -07002154 VkWin32SurfaceCreateInfoKHR createInfo;
2155 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2156 createInfo.pNext = NULL;
2157 createInfo.flags = 0;
Jon Ashburn09338b52015-12-24 17:08:01 -07002158 createInfo.hinstance = demo->connection;
2159 createInfo.hwnd = demo->window;
Ian Elliottda44dfe2015-12-11 15:52:12 -07002160
Karl Schultz481756e2016-02-02 15:37:51 -07002161 err =
2162 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottb5fad792015-11-20 11:55:46 -07002163
Ian Elliotte36b2082015-07-06 14:27:58 -06002164#else // _WIN32
Ian Elliottda44dfe2015-12-11 15:52:12 -07002165 VkXcbSurfaceCreateInfoKHR createInfo;
2166 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2167 createInfo.pNext = NULL;
2168 createInfo.flags = 0;
2169 createInfo.connection = demo->connection;
2170 createInfo.window = demo->window;
2171
Karl Schultz481756e2016-02-02 15:37:51 -07002172 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliotte36b2082015-07-06 14:27:58 -06002173#endif // _WIN32
2174
Tony Barbourd9955e42015-10-08 13:59:42 -06002175 // Iterate over each queue to learn whether it supports presenting:
Karl Schultz481756e2016-02-02 15:37:51 -07002176 VkBool32 *supportsPresent =
2177 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002178 for (i = 0; i < demo->queue_count; i++) {
Karl Schultz481756e2016-02-02 15:37:51 -07002179 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliotte36b2082015-07-06 14:27:58 -06002180 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002181 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002182
2183 // Search for a graphics and a present queue in the array of queue
2184 // families, try to find one that supports both
2185 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultz481756e2016-02-02 15:37:51 -07002186 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002187 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002188 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2189 if (graphicsQueueNodeIndex == UINT32_MAX) {
2190 graphicsQueueNodeIndex = i;
2191 }
Karl Schultz481756e2016-02-02 15:37:51 -07002192
Ian Elliotte36b2082015-07-06 14:27:58 -06002193 if (supportsPresent[i] == VK_TRUE) {
2194 graphicsQueueNodeIndex = i;
2195 presentQueueNodeIndex = i;
2196 break;
2197 }
2198 }
2199 }
2200 if (presentQueueNodeIndex == UINT32_MAX) {
2201 // If didn't find a queue that supports both graphics and present, then
2202 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002203 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002204 if (supportsPresent[i] == VK_TRUE) {
2205 presentQueueNodeIndex = i;
2206 break;
2207 }
2208 }
2209 }
2210 free(supportsPresent);
2211
2212 // Generate error if could not find both a graphics and a present queue
Karl Schultz481756e2016-02-02 15:37:51 -07002213 if (graphicsQueueNodeIndex == UINT32_MAX ||
2214 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002215 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002216 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002217 }
2218
2219 // TODO: Add support for separate queues, including presentation,
Ian Elliott8d2a2eb2015-11-23 12:48:15 -07002220 // synchronization, and appropriate tracking for QueueSubmit.
2221 // NOTE: While it is possible for an application to use a separate graphics
2222 // and a present queues, this demo program assumes it is only using
2223 // one:
Ian Elliotte36b2082015-07-06 14:27:58 -06002224 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2225 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002226 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002227 }
2228
2229 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002230
Tony Barbour3d03a4a2016-01-22 14:36:40 -07002231 demo_init_device(demo);
2232
Karl Schultz481756e2016-02-02 15:37:51 -07002233 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2234 &demo->queue);
Ian Elliott32536f92015-04-21 16:41:02 -06002235
Ian Elliotte36b2082015-07-06 14:27:58 -06002236 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002237 uint32_t formatCount;
Karl Schultz481756e2016-02-02 15:37:51 -07002238 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliott8d2a2eb2015-11-23 12:48:15 -07002239 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002240 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002241 VkSurfaceFormatKHR *surfFormats =
2242 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultz481756e2016-02-02 15:37:51 -07002243 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliott8d2a2eb2015-11-23 12:48:15 -07002244 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002245 assert(!err);
2246 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2247 // the surface has no preferred format. Otherwise, at least one
2248 // supported format will be returned.
Karl Schultz481756e2016-02-02 15:37:51 -07002249 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002250 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultz481756e2016-02-02 15:37:51 -07002251 } else {
Ian Elliotte36b2082015-07-06 14:27:58 -06002252 assert(formatCount >= 1);
2253 demo->format = surfFormats[0].format;
2254 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002255 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002256
Karl Schultz501fdc22016-03-24 18:43:41 -06002257 demo->quit = false;
2258 demo->curFrame = 0;
2259
Mark Lobodzinski72346292015-07-02 16:49:40 -06002260 // Get Memory information and properties
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002261 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002262}
2263
Karl Schultz481756e2016-02-02 15:37:51 -07002264static void demo_init_connection(struct demo *demo) {
Ian Elliotte14e9f92015-04-16 15:23:05 -06002265#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002266 const xcb_setup_t *setup;
2267 xcb_screen_iterator_t iter;
2268 int scr;
2269
2270 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002271 if (demo->connection == NULL) {
2272 printf("Cannot find a compatible Vulkan installable client driver "
2273 "(ICD).\nExiting ...\n");
2274 fflush(stdout);
2275 exit(1);
2276 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002277
2278 setup = xcb_get_setup(demo->connection);
2279 iter = xcb_setup_roots_iterator(setup);
2280 while (scr-- > 0)
2281 xcb_screen_next(&iter);
2282
2283 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002284#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002285}
2286
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002287static void demo_init(struct demo *demo, const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002288{
2289 memset(demo, 0, sizeof(*demo));
Karl Schultz501fdc22016-03-24 18:43:41 -06002290 demo->frameCount = INT32_MAX;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002291
Karl Schultz501fdc22016-03-24 18:43:41 -06002292 for (int i = 1; i < argc; i++) {
2293 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002294 demo->use_staging_buffer = true;
Karl Schultz501fdc22016-03-24 18:43:41 -06002295 continue;
2296 }
2297 if (strcmp(argv[i], "--break") == 0) {
2298 demo->use_break = true;
2299 continue;
2300 }
2301 if (strcmp(argv[i], "--validate") == 0) {
Jon Ashburn99ea74d2016-02-19 15:23:41 -07002302 demo->validate = true;
Karl Schultz501fdc22016-03-24 18:43:41 -06002303 continue;
2304 }
2305 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2306 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2307 demo->frameCount >= 0) {
2308 i++;
2309 continue;
2310 }
2311
2312 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2313 "[--c <framecount>]\n",
2314 APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002315 fflush(stderr);
2316 exit(1);
2317 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002318
Chia-I Wuc19795a2014-09-13 11:12:55 +08002319 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002320 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002321
2322 demo->width = 300;
2323 demo->height = 300;
Tony Barbour4a6692d2015-10-08 13:45:45 -06002324 demo->depthStencil = 1.0;
2325 demo->depthIncrement = -0.01f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002326}
2327
Karl Schultz481756e2016-02-02 15:37:51 -07002328static void demo_cleanup(struct demo *demo) {
Mark Lobodzinski23182612015-05-29 09:32:35 -05002329 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002330
Cody Northrop67ff0a42015-09-09 10:21:49 -06002331 demo->prepared = false;
2332
Tony Barbour5aabff52015-10-08 14:26:24 -06002333 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002334 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbourde4124d2015-07-03 10:33:54 -06002335 }
Tony Barbour5aabff52015-10-08 14:26:24 -06002336 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08002337 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08002338
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002339 if (demo->setup_cmd) {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06002340 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002341 }
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06002342 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->draw_cmd);
Chia-I Wu69f40122015-10-26 21:10:41 +08002343 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002344
Chia-I Wu69f40122015-10-26 21:10:41 +08002345 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2346 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2347 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2348 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Chia-I Wub043fe32014-10-06 15:30:33 +08002349
Chia-I Wu69f40122015-10-26 21:10:41 +08002350 vkDestroyBuffer(demo->device, demo->vertices.buf, NULL);
2351 vkFreeMemory(demo->device, demo->vertices.mem, NULL);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002352
Chia-I Wub043fe32014-10-06 15:30:33 +08002353 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002354 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2355 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2356 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2357 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Chia-I Wub043fe32014-10-06 15:30:33 +08002358 }
2359
Ian Elliott338dedb2015-08-21 15:09:33 -06002360 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002361 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002362 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002363
Chia-I Wu69f40122015-10-26 21:10:41 +08002364 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2365 vkDestroyImage(demo->device, demo->depth.image, NULL);
2366 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Tony Barbourde4124d2015-07-03 10:33:54 -06002367
Ian Elliottb5fad792015-11-20 11:55:46 -07002368 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002369 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002370
Chia-I Wu69f40122015-10-26 21:10:41 +08002371 vkDestroyDevice(demo->device, NULL);
Mark Lobodzinski96b82412016-02-22 09:32:55 -07002372 if (demo->validate) {
2373 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
2374 }
Ian Elliottb5fad792015-11-20 11:55:46 -07002375 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Tony Barbour3ec16a52016-03-07 15:48:06 -07002376 vkDestroyInstance(demo->inst, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002377
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002378 free(demo->queue_props);
2379
Ian Elliotte14e9f92015-04-16 15:23:05 -06002380#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002381 xcb_destroy_window(demo->connection, demo->window);
2382 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002383 free(demo->atom_wm_delete_window);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002384#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002385}
2386
Karl Schultz481756e2016-02-02 15:37:51 -07002387static void demo_resize(struct demo *demo) {
Ian Elliotte2688a52015-10-16 18:02:43 -06002388 uint32_t i;
2389
Mike Stroyanb194be62015-10-28 09:46:57 -06002390 // Don't react to resize until after first initialization.
2391 if (!demo->prepared) {
2392 return;
2393 }
Ian Elliotte2688a52015-10-16 18:02:43 -06002394 // In order to properly resize the window, we must re-create the swapchain
2395 // AND redo the command buffers, etc.
2396 //
2397 // First, perform part of the demo_cleanup() function:
2398 demo->prepared = false;
2399
2400 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002401 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002402 }
2403 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08002404 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002405
2406 if (demo->setup_cmd) {
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002407 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->setup_cmd);
Ian Elliotte2688a52015-10-16 18:02:43 -06002408 }
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002409 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->draw_cmd);
Chia-I Wu69f40122015-10-26 21:10:41 +08002410 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002411
Chia-I Wu69f40122015-10-26 21:10:41 +08002412 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2413 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2414 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2415 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002416
Chia-I Wu69f40122015-10-26 21:10:41 +08002417 vkDestroyBuffer(demo->device, demo->vertices.buf, NULL);
2418 vkFreeMemory(demo->device, demo->vertices.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002419
2420 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002421 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2422 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2423 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2424 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002425 }
2426
2427 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002428 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002429 }
2430
Chia-I Wu69f40122015-10-26 21:10:41 +08002431 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2432 vkDestroyImage(demo->device, demo->depth.image, NULL);
2433 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002434
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002435 free(demo->buffers);
2436
Ian Elliotte2688a52015-10-16 18:02:43 -06002437 // Second, re-perform the demo_prepare() function, which will re-create the
2438 // swapchain:
2439 demo_prepare(demo);
2440}
2441
Ian Elliotte14e9f92015-04-16 15:23:05 -06002442#ifdef _WIN32
Karl Schultz501fdc22016-03-24 18:43:41 -06002443// Include header required for parsing the command line options.
2444#include <shellapi.h>
2445
2446int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
Karl Schultz481756e2016-02-02 15:37:51 -07002447 LPSTR pCmdLine, int nCmdShow) {
2448 MSG msg; // message
2449 bool done; // flag saying when app is complete
Karl Schultz501fdc22016-03-24 18:43:41 -06002450 int argc;
2451 char **argv;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002452
Karl Schultz501fdc22016-03-24 18:43:41 -06002453 // Use the CommandLine functions to get the command line arguments.
2454 // Unfortunately, Microsoft outputs
2455 // this information as wide characters for Unicode, and we simply want the
2456 // Ascii version to be compatible
2457 // with the non-Windows side. So, we have to convert the information to
2458 // Ascii character strings.
2459 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
2460 if (NULL == commandLineArgs) {
2461 argc = 0;
2462 }
2463
2464 if (argc > 0) {
2465 argv = (char **)malloc(sizeof(char *) * argc);
2466 if (argv == NULL) {
2467 argc = 0;
2468 } else {
2469 for (int iii = 0; iii < argc; iii++) {
2470 size_t wideCharLen = wcslen(commandLineArgs[iii]);
2471 size_t numConverted = 0;
2472
2473 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
2474 if (argv[iii] != NULL) {
2475 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
2476 commandLineArgs[iii], wideCharLen + 1);
2477 }
2478 }
2479 }
2480 } else {
2481 argv = NULL;
2482 }
2483
2484 demo_init(&demo, argc, argv);
2485
2486 // Free up the items we had to allocate for the command line arguments.
2487 if (argc > 0 && argv != NULL) {
2488 for (int iii = 0; iii < argc; iii++) {
2489 if (argv[iii] != NULL) {
2490 free(argv[iii]);
2491 }
2492 }
2493 free(argv);
2494 }
2495
2496 demo.connection = hInstance;
2497 strncpy(demo.name, "tri", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002498 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002499 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002500
2501 demo_prepare(&demo);
2502
Karl Schultz481756e2016-02-02 15:37:51 -07002503 done = false; // initialize loop condition variable
Ian Elliotte14e9f92015-04-16 15:23:05 -06002504 /* main message loop*/
Karl Schultz481756e2016-02-02 15:37:51 -07002505 while (!done) {
Ian Elliott421107f2015-04-28 15:50:36 -06002506 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultz481756e2016-02-02 15:37:51 -07002507 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliotte14e9f92015-04-16 15:23:05 -06002508 {
Karl Schultz481756e2016-02-02 15:37:51 -07002509 done = true; // if found, quit app
2510 } else {
Ian Elliotte14e9f92015-04-16 15:23:05 -06002511 /* Translate and dispatch to event queue*/
Karl Schultz481756e2016-02-02 15:37:51 -07002512 TranslateMessage(&msg);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002513 DispatchMessage(&msg);
2514 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002515 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002516 }
2517
2518 demo_cleanup(&demo);
2519
Karl Schultz481756e2016-02-02 15:37:51 -07002520 return (int)msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002521}
2522#else // _WIN32
Karl Schultz481756e2016-02-02 15:37:51 -07002523int main(const int argc, const char *argv[]) {
Chia-I Wuc19795a2014-09-13 11:12:55 +08002524 struct demo demo;
2525
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002526 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002527 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002528 demo_init_vk_swapchain(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002529
2530 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002531 demo_run(&demo);
2532
2533 demo_cleanup(&demo);
2534
Karl Schultz501fdc22016-03-24 18:43:41 -06002535 return validation_error;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002536}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002537#endif // _WIN32