blob: 9b276e9186cdf2b00a18403d0687d244efbc2384 [file] [log] [blame]
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001/*
Ian Elliott7595eee2015-04-28 10:33:11 -06002 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24/*
Chia-I Wuf5caeb02014-10-25 12:11:27 +080025 * Draw a textured triangle with depth testing. This is written against Intel
26 * ICD. It does not do state transition nor object memory binding like it
27 * should. It also does no error checking.
28 */
29
Chia-I Wuc19795a2014-09-13 11:12:55 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
35
Ian Elliotte14e9f92015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
38#include <windows.h>
39#define APP_NAME_STR_LEN 80
40#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +080041#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060042#endif // _WIN32
43
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060044#include <vulkan.h>
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060045#include "vk_debug_report_lunarg.h"
Ian Elliott338dedb2015-08-21 15:09:33 -060046#include <vk_ext_khr_swapchain.h>
47#include <vk_ext_khr_device_swapchain.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080048
Cody Northropd4e020a2015-03-17 14:54:35 -060049#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060050
Chia-I Wub043fe32014-10-06 15:30:33 +080051#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060052#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060053#define APP_SHORT_NAME "tri"
54#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080055
Ian Elliotte36b2082015-07-06 14:27:58 -060056#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
57
Tony Barbour22a30862015-04-22 09:02:32 -060058#if defined(NDEBUG) && defined(__GNUC__)
59#define U_ASSERT_ONLY __attribute__((unused))
60#else
61#define U_ASSERT_ONLY
62#endif
63
Ian Elliottcaa9f272015-04-28 11:35:02 -060064#ifdef _WIN32
65#define ERR_EXIT(err_msg, err_class) \
66 do { \
67 MessageBox(NULL, err_msg, err_class, MB_OK); \
68 exit(1); \
69 } while (0)
Ian Elliottcaa9f272015-04-28 11:35:02 -060070#else // _WIN32
71
72#define ERR_EXIT(err_msg, err_class) \
73 do { \
74 printf(err_msg); \
75 fflush(stdout); \
76 exit(1); \
77 } while (0)
78#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060079
Ian Elliotte36b2082015-07-06 14:27:58 -060080#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
81{ \
82 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
83 if (demo->fp##entrypoint == NULL) { \
84 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
85 "vkGetInstanceProcAddr Failure"); \
86 } \
87}
88
Ian Elliott1b6de092015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060091 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060092 if (demo->fp##entrypoint == NULL) { \
93 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
94 "vkGetDeviceProcAddr Failure"); \
95 } \
96}
97
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060098struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060099 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700100
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600101 VkImage image;
102 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600103
Mark Lobodzinski23182612015-05-29 09:32:35 -0500104 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600105 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700106 int32_t tex_width, tex_height;
107};
108
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600109VkBool32 dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600110 VkFlags msgFlags,
111 VkDbgObjectType objType,
112 uint64_t srcObject,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600113 size_t location,
114 int32_t msgCode,
115 const char* pLayerPrefix,
116 const char* pMsg,
117 void* pUserData)
118{
119 char *message = (char *) malloc(strlen(pMsg)+100);
120
121 assert (message);
122
123 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
124 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
125 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
126 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
127 } else {
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600128 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600129 }
130
131#ifdef _WIN32
132 MessageBox(NULL, message, "Alert", MB_OK);
133#else
134 printf("%s\n",message);
135 fflush(stdout);
136#endif
137 free(message);
Courtney Goeltzenleuchtere4c43132015-09-08 16:57:22 -0600138
139 /*
140 * false indicates that layer should not bail-out of an
141 * API call that had validation failures. This may mean that the
142 * app dies inside the driver due to invalid parameter(s).
143 * That's what would happen without validation layers, so we'll
144 * keep that behavior here.
145 */
146 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600147}
148
Ian Elliott338dedb2015-08-21 15:09:33 -0600149typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600150 VkImage image;
151 VkCmdBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600152 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600153} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600154
Chia-I Wuc19795a2014-09-13 11:12:55 +0800155struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600156#ifdef _WIN32
157#define APP_NAME_STR_LEN 80
158 HINSTANCE connection; // hInstance - Windows Instance
159 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
160 HWND window; // hWnd - window handle
161#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800162 xcb_connection_t *connection;
163 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800164 xcb_window_t window;
165 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott338dedb2015-08-21 15:09:33 -0600166 VkPlatformHandleXcbKHR platform_handle_xcb;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600167#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700168 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600169 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600170 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800171
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600172 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600173 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600174 VkDevice device;
175 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600176 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600177 VkQueueFamilyProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700178 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800179
180 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600181 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600182 VkColorSpaceKHR color_space;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800183
Ian Elliott338dedb2015-08-21 15:09:33 -0600184 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
185 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
186 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
187 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
188 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
189 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
190 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
191 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
192 PFN_vkQueuePresentKHR fpQueuePresentKHR;
193 VkSurfaceDescriptionWindowKHR surface_description;
194 uint32_t swapchainImageCount;
195 VkSwapchainKHR swap_chain;
196 SwapchainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800197
Ian Elliotte36b2082015-07-06 14:27:58 -0600198 VkCmdPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800199
Chia-I Wub043fe32014-10-06 15:30:33 +0800200 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600201 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800202
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500204 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600205 VkImageView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800206 } depth;
207
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600208 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800209
Chia-I Wu99621bc2014-10-08 11:52:22 +0800210 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600211 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500212 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800213
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600214 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600215 VkVertexInputBindingDescription vi_bindings[1];
216 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800217 } vertices;
218
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600219 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
220 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500221 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600222 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600223 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800224 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600225 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800226
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -0600227 VkShaderModule vert_shader_module;
228 VkShaderModule frag_shader_module;
229
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600230 VkDescriptorPool desc_pool;
231 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800232
Tony Barbour5aabff52015-10-08 14:26:24 -0600233 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800234
Mark Lobodzinski72346292015-07-02 16:49:40 -0600235 VkPhysicalDeviceMemoryProperties memory_properties;
236
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600237 bool validate;
238 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Ian Elliotte36b2082015-07-06 14:27:58 -0600239 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600240 VkDbgMsgCallback msg_callback;
241
Tony Barbour4a6692d2015-10-08 13:45:45 -0600242 float depthStencil;
243 float depthIncrement;
244
Chia-I Wuc19795a2014-09-13 11:12:55 +0800245 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600246 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600247 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800248};
249
Tony Barbourf1eceb92015-10-15 12:42:56 -0600250static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinski72346292015-07-02 16:49:40 -0600251{
252 // Search memtypes to find first index with those properties
253 for (uint32_t i = 0; i < 32; i++) {
254 if ((typeBits & 1) == 1) {
255 // Type is available, does it match user properties?
Tony Barbourf1eceb92015-10-15 12:42:56 -0600256 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinski72346292015-07-02 16:49:40 -0600257 *typeIndex = i;
258 return VK_SUCCESS;
259 }
260 }
261 typeBits >>= 1;
262 }
263 // No memory types matched, return failure
264 return VK_UNSUPPORTED;
265}
266
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600267static void demo_flush_init_cmd(struct demo *demo)
268{
Tony Barbour22a30862015-04-22 09:02:32 -0600269 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600270
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600271 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600272 return;
273
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600274 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275 assert(!err);
276
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600277 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600278 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600279
Tony Barbourde4124d2015-07-03 10:33:54 -0600280 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600281 assert(!err);
282
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600283 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600284 assert(!err);
285
Tony Barbourde4124d2015-07-03 10:33:54 -0600286 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600287 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600288}
289
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600290static void demo_set_image_layout(
291 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600292 VkImage image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600293 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600294 VkImageLayout old_image_layout,
295 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600296{
Tony Barbour22a30862015-04-22 09:02:32 -0600297 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600298
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600299 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600300 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600301 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600302 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600303 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800304 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600305 .flags = 0,
306 };
307
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600308 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309 assert(!err);
310
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600311 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600312 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600313 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600314 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600315 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600316 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600317 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600318 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600319 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600320 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Tony Barbourc1098272015-10-23 10:53:30 -0600321 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600322 }
323
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600324 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600325 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600326 .pNext = NULL,
327 .outputMask = 0,
328 .inputMask = 0,
329 .oldLayout = old_image_layout,
330 .newLayout = new_image_layout,
331 .image = image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600332 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600333 };
334
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600335 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600336 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600337 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600338 }
339
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600340 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600341 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600342 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600343 }
344
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600345 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600346
Tony Barbourc2e987e2015-06-29 16:20:35 -0600347 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
348 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600349
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600350 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600351}
352
Chia-I Wuc19795a2014-09-13 11:12:55 +0800353static void demo_draw_build_cmd(struct demo *demo)
354{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800355 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600356 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600357 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600358 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600359 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600360 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600361 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600362 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn53d27af2014-12-31 17:08:35 -0700363 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800364 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600365 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
Tony Barbour4a6692d2015-10-08 13:45:45 -0600366 [1] = { .depthStencil = { demo->depthStencil, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800367 };
368 const VkRenderPassBeginInfo rp_begin = {
369 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
370 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800371 .renderPass = demo->render_pass,
372 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800373 .renderArea.offset.x = 0,
374 .renderArea.offset.y = 0,
375 .renderArea.extent.width = demo->width,
376 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600377 .clearValueCount = 2,
378 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700379 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800380 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800381
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600382 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800383 assert(!err);
384
Chia-I Wuc278df82015-07-07 11:50:03 +0800385 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600386 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800387 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600388 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600389 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800390
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600391 VkViewport viewport;
392 memset(&viewport, 0, sizeof(viewport));
393 viewport.height = (float) demo->height;
394 viewport.width = (float) demo->width;
395 viewport.minDepth = (float) 0.0f;
396 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600397 vkCmdSetViewport(demo->draw_cmd, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600398
399 VkRect2D scissor;
400 memset(&scissor, 0, sizeof(scissor));
401 scissor.extent.width = demo->width;
402 scissor.extent.height = demo->height;
403 scissor.offset.x = 0;
404 scissor.offset.y = 0;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600405 vkCmdSetScissor(demo->draw_cmd, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600406
Tony Barbour8205d902015-04-16 15:59:00 -0600407 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600408 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800409
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600410 vkCmdDraw(demo->draw_cmd, 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800411 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800412
Tony Barbour40292ae2015-10-21 10:14:48 -0600413 VkImageMemoryBarrier prePresentBarrier = {
414 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
415 .pNext = NULL,
416 .outputMask = VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT,
417 .inputMask = 0,
418 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
419 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
420 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
421 .destQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
422 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
423 };
424
425 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
426 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
427 vkCmdPipelineBarrier(demo->draw_cmd, VK_PIPELINE_STAGE_ALL_GPU_COMMANDS, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
428 VK_FALSE, 1, (const void * const*)&pmemory_barrier);
429
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600430 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800431 assert(!err);
432}
433
434static void demo_draw(struct demo *demo)
435{
Tony Barbour22a30862015-04-22 09:02:32 -0600436 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600437 VkSemaphore presentCompleteSemaphore;
438 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
439 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
440 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600441 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600442 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800443
Ian Elliotte36b2082015-07-06 14:27:58 -0600444 err = vkCreateSemaphore(demo->device,
445 &presentCompleteSemaphoreCreateInfo,
446 &presentCompleteSemaphore);
447 assert(!err);
448
449 // Get the index of the next available swapchain image:
Ian Elliott338dedb2015-08-21 15:09:33 -0600450 err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600451 UINT64_MAX,
452 presentCompleteSemaphore,
453 &demo->current_buffer);
Ian Elliott338dedb2015-08-21 15:09:33 -0600454 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliotte36b2082015-07-06 14:27:58 -0600455 // return codes
456 assert(!err);
457
458 // Wait for the present complete semaphore to be signaled to ensure
459 // that the image won't be rendered to until the presentation
460 // engine has fully released ownership to the application, and it is
461 // okay to render to the image.
Tony Barbourede36362015-10-20 13:01:07 -0600462 err = vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
463 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -0600464
Tony Barbour40292ae2015-10-21 10:14:48 -0600465 // Assume the command buffer has been run on current_buffer before so
466 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
467 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
468 VK_IMAGE_ASPECT_COLOR_BIT,
469 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
470 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
471 demo_flush_init_cmd(demo);
472
Ian Elliott338dedb2015-08-21 15:09:33 -0600473// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Chia-I Wuc19795a2014-09-13 11:12:55 +0800474 demo_draw_build_cmd(demo);
Tony Barbourde4124d2015-07-03 10:33:54 -0600475 VkFence nullFence = { VK_NULL_HANDLE };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800476
Tony Barbourde4124d2015-07-03 10:33:54 -0600477 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800478 assert(!err);
479
Ian Elliott338dedb2015-08-21 15:09:33 -0600480 VkPresentInfoKHR present = {
481 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600482 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600483 .swapchainCount = 1,
484 .swapchains = &demo->swap_chain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600485 .imageIndices = &demo->current_buffer,
486 };
487
488// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600489 err = demo->fpQueuePresentKHR(demo->queue, &present);
490 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliotte36b2082015-07-06 14:27:58 -0600491 // return codes
Chia-I Wuc19795a2014-09-13 11:12:55 +0800492 assert(!err);
493
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800494 err = vkQueueWaitIdle(demo->queue);
495 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600496
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600497 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800498}
499
500static void demo_prepare_buffers(struct demo *demo)
501{
Ian Elliotte36b2082015-07-06 14:27:58 -0600502 VkResult U_ASSERT_ONLY err;
503
504 // Check the surface proprties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600505 VkSurfacePropertiesKHR surfProperties;
506 err = demo->fpGetSurfacePropertiesKHR(demo->device,
507 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600508 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600509 assert(!err);
510
Ian Elliott7fe115d2015-08-07 15:56:59 -0600511 uint32_t presentModeCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600512 err = demo->fpGetSurfacePresentModesKHR(demo->device,
513 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600514 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600515 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600516 VkPresentModeKHR *presentModes =
517 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600518 assert(presentModes);
Ian Elliott338dedb2015-08-21 15:09:33 -0600519 err = demo->fpGetSurfacePresentModesKHR(demo->device,
520 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600521 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600522 assert(!err);
523
Ian Elliott338dedb2015-08-21 15:09:33 -0600524 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600525 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600526 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600527 {
528 // If the surface size is undefined, the size is set to
529 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600530 swapchainExtent.width = demo->width;
531 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600532 }
533 else
534 {
535 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -0600536 swapchainExtent = surfProperties.currentExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600537 }
538
Ian Elliott338dedb2015-08-21 15:09:33 -0600539 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600540
541 // Determine the number of VkImage's to use in the swap chain (we desire to
542 // own only 1 image at a time, besides the images being displayed and
543 // queued for display):
Ian Elliott338dedb2015-08-21 15:09:33 -0600544 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600545 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600546 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600547 {
548 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600549 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600550 }
551
Ian Elliott338dedb2015-08-21 15:09:33 -0600552 VkSurfaceTransformKHR preTransform;
553 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
554 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600555 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600556 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600557 }
558
Ian Elliott338dedb2015-08-21 15:09:33 -0600559 const VkSwapchainCreateInfoKHR swap_chain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600560 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800561 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600562 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
563 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800564 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600565 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800566 .imageExtent = {
Ian Elliott338dedb2015-08-21 15:09:33 -0600567 .width = swapchainExtent.width,
568 .height = swapchainExtent.height,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800569 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600570 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600571 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800572 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600573 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
574 .queueFamilyCount = 0,
575 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600576 .presentMode = swapchainPresentMode,
577 .oldSwapchain.handle = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600578 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800579 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600580 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800581
Ian Elliott338dedb2015-08-21 15:09:33 -0600582 err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800583 assert(!err);
584
Ian Elliott338dedb2015-08-21 15:09:33 -0600585 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
586 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600587 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800588
Ian Elliott338dedb2015-08-21 15:09:33 -0600589 VkImage* swapchainImages =
590 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
591 assert(swapchainImages);
592 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
593 &demo->swapchainImageCount,
594 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600595 assert(!err);
596
Ian Elliott338dedb2015-08-21 15:09:33 -0600597 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600598 assert(demo->buffers);
599
Ian Elliott338dedb2015-08-21 15:09:33 -0600600 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600601 VkImageViewCreateInfo color_attachment_view = {
602 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800603 .pNext = NULL,
604 .format = demo->format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600605 .channels = {
606 .r = VK_CHANNEL_SWIZZLE_R,
607 .g = VK_CHANNEL_SWIZZLE_G,
608 .b = VK_CHANNEL_SWIZZLE_B,
609 .a = VK_CHANNEL_SWIZZLE_A,
610 },
611 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600612 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600613 .baseMipLevel = 0,
614 .mipLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600615 .baseArrayLayer = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600616 .arraySize = 1
617 },
618 .viewType = VK_IMAGE_VIEW_TYPE_2D,
619 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800620 };
621
Ian Elliott338dedb2015-08-21 15:09:33 -0600622 demo->buffers[i].image = swapchainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500623
Tony Barbour40292ae2015-10-21 10:14:48 -0600624 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
625 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600626 demo_set_image_layout(demo, demo->buffers[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600627 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600628 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600629 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600630
Chia-I Wuc19795a2014-09-13 11:12:55 +0800631 color_attachment_view.image = demo->buffers[i].image;
632
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600633 err = vkCreateImageView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800634 &color_attachment_view, &demo->buffers[i].view);
635 assert(!err);
636 }
Piers Daniell886be472015-02-23 16:23:13 -0700637
638 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800639}
640
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800641static void demo_prepare_depth(struct demo *demo)
642{
Tony Barbour8205d902015-04-16 15:59:00 -0600643 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600644 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600645 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800646 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600647 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800648 .format = depth_format,
649 .extent = { demo->width, demo->height, 1 },
650 .mipLevels = 1,
651 .arraySize = 1,
652 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600653 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600654 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800655 .flags = 0,
656 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600657 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600658 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500659 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800660 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600661 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800662 };
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600663 VkImageViewCreateInfo view = {
664 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800665 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600666 .image.handle = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600667 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600668 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600669 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600670 .baseMipLevel = 0,
671 .mipLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600672 .baseArrayLayer = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600673 .arraySize = 1
674 },
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800675 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600676 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800677 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700678
Mark Lobodzinski23182612015-05-29 09:32:35 -0500679 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600680 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800681
682 demo->depth.format = depth_format;
683
684 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600685 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800686 &demo->depth.image);
687 assert(!err);
688
Mark Lobodzinski72346292015-07-02 16:49:40 -0600689 /* get memory requirements for this object */
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600690 vkGetImageMemoryRequirements(demo->device, demo->depth.image,
Tony Barbourde4124d2015-07-03 10:33:54 -0600691 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600692
693 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500694 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600695 err = memory_type_from_properties(demo,
696 mem_reqs.memoryTypeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600697 0, /* No requirements */
Mark Lobodzinski72346292015-07-02 16:49:40 -0600698 &mem_alloc.memoryTypeIndex);
699 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800700
Mark Lobodzinski23182612015-05-29 09:32:35 -0500701 /* allocate memory */
702 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
703 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800704
Mark Lobodzinski23182612015-05-29 09:32:35 -0500705 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600706 err = vkBindImageMemory(demo->device, demo->depth.image,
707 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500708 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800709
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600710 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600711 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600712 VK_IMAGE_LAYOUT_UNDEFINED,
713 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600714
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800715 /* create image view */
716 view.image = demo->depth.image;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600717 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800718 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800719}
720
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700721static void demo_prepare_texture_image(struct demo *demo,
722 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600723 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600724 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600725 VkImageUsageFlags usage,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600726 VkFlags required_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800727{
Tony Barbour8205d902015-04-16 15:59:00 -0600728 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600729 const int32_t tex_width = 2;
730 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600731 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800732
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600733 tex_obj->tex_width = tex_width;
734 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700735
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600736 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600737 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700738 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600739 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700740 .format = tex_format,
741 .extent = { tex_width, tex_height, 1 },
742 .mipLevels = 1,
743 .arraySize = 1,
744 .samples = 1,
745 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600746 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700747 .flags = 0,
748 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600749 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600750 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500751 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700752 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600753 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700754 };
755
Mark Lobodzinski23182612015-05-29 09:32:35 -0500756 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700757
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600758 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600759 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700760 assert(!err);
761
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600762 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600763
764 mem_alloc.allocationSize = mem_reqs.size;
Tony Barbourf1eceb92015-10-15 12:42:56 -0600765 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &mem_alloc.memoryTypeIndex);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600766 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700767
Mark Lobodzinski23182612015-05-29 09:32:35 -0500768 /* allocate memory */
769 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
770 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700771
Mark Lobodzinski23182612015-05-29 09:32:35 -0500772 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600773 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500774 tex_obj->mem, 0);
775 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700776
Tony Barbourf1eceb92015-10-15 12:42:56 -0600777 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600778 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600779 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700780 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600781 .arrayLayer = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700782 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600783 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700784 void *data;
785 int32_t x, y;
786
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600787 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700788
Dominik Witczake82d5b62015-09-22 18:25:33 +0200789 err = vkMapMemory(demo->device, tex_obj->mem, 0, mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700790 assert(!err);
791
792 for (y = 0; y < tex_height; y++) {
793 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
794 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700795 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700796 }
797
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600798 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700799 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600800
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600801 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600802 demo_set_image_layout(demo, tex_obj->image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600803 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600804 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600805 tex_obj->imageLayout);
806 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700807}
808
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500809static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700810{
811 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600812 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600813 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700814}
815
816static void demo_prepare_textures(struct demo *demo)
817{
Tony Barbour8205d902015-04-16 15:59:00 -0600818 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600819 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700820 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
821 { 0xffff0000, 0xff00ff00 },
822 };
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700823 uint32_t i;
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600824 VkResult err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700825
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600826 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700827
828 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600829 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700830 /* Device can texture using linear textures */
831 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600832 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600833 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700834 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600835 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700836
837 memset(&staging_texture, 0, sizeof(staging_texture));
838 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600839 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700840
841 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600842 VK_IMAGE_TILING_OPTIMAL,
843 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
844 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700845
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600846 demo_set_image_layout(demo, staging_texture.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600847 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600848 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600849 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700850
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600851 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600852 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600853 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600854 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700855
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600856 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600857 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700858 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600859 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700860 .destOffset = { 0, 0, 0 },
861 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
862 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600863 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600864 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
865 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600866 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700867
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600868 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600869 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600870 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600871 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700872
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600873 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700874
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600875 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700876 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600877 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700878 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700879 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700880
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600881 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600882 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800883 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600884 .magFilter = VK_TEX_FILTER_NEAREST,
885 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600886 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter51624412015-09-10 14:08:50 -0600887 .addressModeU = VK_TEX_ADDRESS_MODE_WRAP,
888 .addressModeV = VK_TEX_ADDRESS_MODE_WRAP,
889 .addressModeW = VK_TEX_ADDRESS_MODE_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800890 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700891 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600892 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800893 .minLod = 0.0f,
894 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600895 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600896 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800897 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600898 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600899 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800900 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600901 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600902 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700903 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600904 .channels = { VK_CHANNEL_SWIZZLE_R,
905 VK_CHANNEL_SWIZZLE_G,
906 VK_CHANNEL_SWIZZLE_B,
907 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -0600908 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600909 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +0800910 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700911
Chia-I Wub043fe32014-10-06 15:30:33 +0800912 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600913 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800914 &demo->textures[i].sampler);
915 assert(!err);
916
Chia-I Wub043fe32014-10-06 15:30:33 +0800917 /* create image view */
918 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600919 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700920 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800921 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800922 }
923}
924
Chia-I Wu99621bc2014-10-08 11:52:22 +0800925static void demo_prepare_vertices(struct demo *demo)
926{
927 const float vb[3][5] = {
928 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800929 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
930 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800931 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
932 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600933 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600934 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800935 .pNext = NULL,
936 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600937 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800938 .flags = 0,
939 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600940 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600941 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500942 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800943 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600944 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800945 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500946 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600947 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800948 void *data;
949
950 memset(&demo->vertices, 0, sizeof(demo->vertices));
951
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600952 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800953 assert(!err);
954
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600955 vkGetBufferMemoryRequirements(demo->device,
Tony Barbourde4124d2015-07-03 10:33:54 -0600956 demo->vertices.buf, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -0600957 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800958
Mark Lobodzinski72346292015-07-02 16:49:40 -0600959 mem_alloc.allocationSize = mem_reqs.size;
960 err = memory_type_from_properties(demo,
961 mem_reqs.memoryTypeBits,
962 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
963 &mem_alloc.memoryTypeIndex);
964 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800965
Mark Lobodzinski23182612015-05-29 09:32:35 -0500966 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
967 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800968
Dominik Witczake82d5b62015-09-22 18:25:33 +0200969 err = vkMapMemory(demo->device, demo->vertices.mem, 0, mem_alloc.allocationSize, 0, &data);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500970 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800971
Mark Lobodzinski23182612015-05-29 09:32:35 -0500972 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800973
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600974 vkUnmapMemory(demo->device, demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500975
Tony Barbourde4124d2015-07-03 10:33:54 -0600976 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500977 demo->vertices.mem, 0);
978 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800979
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600980 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800981 demo->vertices.vi.pNext = NULL;
982 demo->vertices.vi.bindingCount = 1;
983 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
984 demo->vertices.vi.attributeCount = 2;
985 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
986
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600987 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800988 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600989 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800990
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600991 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
992 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600993 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800994 demo->vertices.vi_attrs[0].offsetInBytes = 0;
995
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600996 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
997 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600998 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800999 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001000}
1001
Chia-I Wuf8385062015-01-04 16:27:24 +08001002static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +08001003{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001004 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001005 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001006 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001007 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001008 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +08001009 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001010 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001011 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001012 .pNext = NULL,
1013 .count = 1,
1014 .pBinding = &layout_binding,
1015 };
Tony Barbour22a30862015-04-22 09:02:32 -06001016 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +08001017
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001018 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001019 &descriptor_layout, &demo->desc_layout);
1020 assert(!err);
1021
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001022 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1023 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1024 .pNext = NULL,
1025 .descriptorSetCount = 1,
1026 .pSetLayouts = &demo->desc_layout,
1027 };
1028
1029 err = vkCreatePipelineLayout(demo->device,
1030 &pPipelineLayoutCreateInfo,
1031 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001032 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001033}
1034
Chia-I Wu76cd4222015-07-08 13:34:24 +08001035static void demo_prepare_render_pass(struct demo *demo)
1036{
Chia-I Wuc278df82015-07-07 11:50:03 +08001037 const VkAttachmentDescription attachments[2] = {
1038 [0] = {
1039 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1040 .pNext = NULL,
1041 .format = demo->format,
1042 .samples = 1,
1043 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1044 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1045 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1046 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1047 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1048 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1049 },
1050 [1] = {
1051 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1052 .pNext = NULL,
1053 .format = demo->depth.format,
1054 .samples = 1,
1055 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1056 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1057 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1058 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1059 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1060 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1061 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001062 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001063 const VkAttachmentReference color_reference = {
1064 .attachment = 0,
1065 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1066 };
1067 const VkSubpassDescription subpass = {
1068 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1069 .pNext = NULL,
1070 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1071 .flags = 0,
1072 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001073 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001074 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001075 .pColorAttachments = &color_reference,
1076 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001077 .depthStencilAttachment = {
1078 .attachment = 1,
1079 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1080 },
1081 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001082 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001083 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001084 const VkRenderPassCreateInfo rp_info = {
1085 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1086 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001087 .attachmentCount = 2,
1088 .pAttachments = attachments,
1089 .subpassCount = 1,
1090 .pSubpasses = &subpass,
1091 .dependencyCount = 0,
1092 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001093 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001094 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001095
1096 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1097 assert(!err);
1098}
1099
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001100static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001101 VkShaderStage stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001102 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001103 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001104 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001105{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001106 VkShaderModuleCreateInfo moduleCreateInfo;
1107 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001108 VkShader shader;
Tony Barbour4a6692d2015-10-08 13:45:45 -06001109 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001110
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001111
1112 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1113 moduleCreateInfo.pNext = NULL;
1114
1115 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1116 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001117
Cody Northrop75db0322015-05-28 11:27:16 -06001118 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001119 moduleCreateInfo.codeSize = size;
1120 moduleCreateInfo.pCode = code;
1121 moduleCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001122 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001123 assert(!err);
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001124
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001125 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001126 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001127 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001128 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001129 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001130 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001131 } else {
1132 // Create fake SPV structure to feed GLSL
1133 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001134 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1135 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1136 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001137
Cody Northrop75db0322015-05-28 11:27:16 -06001138 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001139 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1140 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1141 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1142 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001143
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001144 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001145 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001146
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001147 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001148 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001149 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001150 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001151 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001152 assert(!err);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001153 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001154 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001155 return shader;
1156}
1157
Cody Northrop75db0322015-05-28 11:27:16 -06001158char *demo_read_spv(const char *filename, size_t *psize)
1159{
1160 long int size;
1161 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001162 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001163
1164 FILE *fp = fopen(filename, "rb");
1165 if (!fp) return NULL;
1166
1167 fseek(fp, 0L, SEEK_END);
1168 size = ftell(fp);
1169
1170 fseek(fp, 0L, SEEK_SET);
1171
1172 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001173 retVal = fread(shader_code, size, 1, fp);
1174 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001175
1176 *psize = size;
1177
1178 return shader_code;
1179}
1180
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001181static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001182{
Cody Northrop75db0322015-05-28 11:27:16 -06001183 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001184 VkShader shader;
1185 void *vertShaderCode;
1186 size_t size;
Cody Northrop75db0322015-05-28 11:27:16 -06001187
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001188 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001189
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001190 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1191 &demo->vert_shader_module,
1192 vertShaderCode, size);
1193 free(vertShaderCode);
1194 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001195 } else {
1196 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001197 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001198 "#extension GL_ARB_separate_shader_objects : enable\n"
1199 "#extension GL_ARB_shading_language_420pack : enable\n"
1200 "layout (location = 0) in vec4 pos;\n"
1201 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001202 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001203 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001204 " texcoord = attr;\n"
1205 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001206 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001207
Cody Northrop75db0322015-05-28 11:27:16 -06001208 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001209 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001210 (const void *) vertShaderText,
1211 strlen(vertShaderText));
1212 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001213}
1214
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001215static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001216{
Cody Northrop75db0322015-05-28 11:27:16 -06001217 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001218 VkShader shader;
1219 void *fragShaderCode;
1220 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001221
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001222 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001223
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001224 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1225 &demo->frag_shader_module,
1226 fragShaderCode, size);
1227
1228 free(fragShaderCode);
1229 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001230 } else {
1231 static const char *fragShaderText =
1232 "#version 140\n"
1233 "#extension GL_ARB_separate_shader_objects : enable\n"
1234 "#extension GL_ARB_shading_language_420pack : enable\n"
1235 "layout (binding = 0) uniform sampler2D tex;\n"
1236 "layout (location = 0) in vec2 texcoord;\n"
1237 "layout (location = 0) out vec4 uFragColor;\n"
1238 "void main() {\n"
1239 " uFragColor = texture(tex, texcoord);\n"
1240 "}\n";
1241
1242 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001243 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001244 (const void *) fragShaderText,
1245 strlen(fragShaderText));
1246 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001247}
1248
1249static void demo_prepare_pipeline(struct demo *demo)
1250{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001251 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001252 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001253
Tony Barboure307f582015-07-10 15:29:03 -06001254 VkPipelineVertexInputStateCreateInfo vi;
1255 VkPipelineInputAssemblyStateCreateInfo ia;
1256 VkPipelineRasterStateCreateInfo rs;
1257 VkPipelineColorBlendStateCreateInfo cb;
1258 VkPipelineDepthStencilStateCreateInfo ds;
1259 VkPipelineViewportStateCreateInfo vp;
1260 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniell811eb742015-09-29 13:01:09 -06001261 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1262 VkPipelineDynamicStateCreateInfo dynamicState;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001263
Tony Barbour22a30862015-04-22 09:02:32 -06001264 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001265
Piers Daniell811eb742015-09-29 13:01:09 -06001266 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1267 memset(&dynamicState, 0, sizeof dynamicState);
1268 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1269 dynamicState.pDynamicStates = dynamicStateEnables;
1270
Chia-I Wuc19795a2014-09-13 11:12:55 +08001271 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001272 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001273 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001274
Chia-I Wu8d29d022014-10-08 12:14:39 +08001275 vi = demo->vertices.vi;
1276
Chia-I Wuc19795a2014-09-13 11:12:55 +08001277 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001278 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001279 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001280
1281 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001282 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001283 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001284 rs.cullMode = VK_CULL_MODE_BACK;
1285 rs.frontFace = VK_FRONT_FACE_CW;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001286 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001287 rs.rasterizerDiscardEnable = VK_FALSE;
1288 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001289
1290 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001291 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1292 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001293 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001294 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001295 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001296 cb.attachmentCount = 1;
1297 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001298
Tony Barbourfa6cac72015-01-16 14:27:35 -07001299 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001300 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001301 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001302 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1303 vp.scissorCount = 1;
1304 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001305
1306 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001307 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001308 ds.depthTestEnable = VK_TRUE;
1309 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001310 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001311 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001312 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1313 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001314 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001315 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001316 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001317
Tony Barbourfa6cac72015-01-16 14:27:35 -07001318 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001319 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001320 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001321 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001322
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001323 // Two stages: vs and fs
1324 pipeline.stageCount = 2;
1325 VkPipelineShaderStageCreateInfo shaderStages[2];
1326 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1327
1328 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1329 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1330 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001331
1332 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1333 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1334 shaderStages[1].shader = demo_prepare_fs(demo);
1335
Tony Barboure307f582015-07-10 15:29:03 -06001336 pipeline.pVertexInputState = &vi;
1337 pipeline.pInputAssemblyState = &ia;
1338 pipeline.pRasterState = &rs;
1339 pipeline.pColorBlendState = &cb;
1340 pipeline.pMultisampleState = &ms;
1341 pipeline.pViewportState = &vp;
1342 pipeline.pDepthStencilState = &ds;
1343 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001344 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001345 pipeline.pDynamicState = &dynamicState;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001346
Jon Ashburn0d60d272015-07-09 15:02:25 -06001347 memset(&pipelineCache, 0, sizeof(pipelineCache));
1348 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1349
1350 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1351 assert(!err);
1352 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001353 assert(!err);
1354
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001355 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001356
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001357 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001358 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001359 }
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001360 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1361 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001362}
1363
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001364static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001365{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001366 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001367 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001368 .count = DEMO_TEXTURE_COUNT,
1369 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001370 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001371 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001372 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001373 .poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT,
1374 .maxSets = 1,
Chia-I Wuf8385062015-01-04 16:27:24 +08001375 .count = 1,
1376 .pTypeCount = &type_count,
1377 };
Tony Barbour22a30862015-04-22 09:02:32 -06001378 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001379
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001380 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001381 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001382 assert(!err);
1383}
1384
1385static void demo_prepare_descriptor_set(struct demo *demo)
1386{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001387 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1388 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001389 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001390 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001391
Mike Stroyan230e6252015-04-17 12:36:38 -06001392 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001393 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001394 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001395 &demo->desc_set);
1396 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001397
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001398 memset(&tex_descs, 0, sizeof(tex_descs));
1399 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1400 tex_descs[i].sampler = demo->textures[i].sampler;
1401 tex_descs[i].imageView = demo->textures[i].view;
1402 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1403 }
1404
1405 memset(&write, 0, sizeof(write));
1406 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1407 write.destSet = demo->desc_set;
1408 write.count = DEMO_TEXTURE_COUNT;
1409 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1410 write.pDescriptors = tex_descs;
1411
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001412 vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001413}
1414
Chia-I Wu76cd4222015-07-08 13:34:24 +08001415static void demo_prepare_framebuffers(struct demo *demo)
1416{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001417 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001418 attachments[1] = demo->depth.view;
1419
Chia-I Wu76cd4222015-07-08 13:34:24 +08001420 const VkFramebufferCreateInfo fb_info = {
1421 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1422 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001423 .renderPass = demo->render_pass,
1424 .attachmentCount = 2,
1425 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001426 .width = demo->width,
1427 .height = demo->height,
1428 .layers = 1,
1429 };
1430 VkResult U_ASSERT_ONLY err;
1431 uint32_t i;
1432
Tony Barbour5aabff52015-10-08 14:26:24 -06001433 demo->framebuffers = (VkFramebuffer *) malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1434 assert(demo->framebuffers);
1435
1436 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001437 attachments[0]= demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001438 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1439 assert(!err);
1440 }
1441}
1442
Chia-I Wuc19795a2014-09-13 11:12:55 +08001443static void demo_prepare(struct demo *demo)
1444{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001445 VkResult U_ASSERT_ONLY err;
1446
1447 const VkCmdPoolCreateInfo cmd_pool_info = {
1448 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1449 .pNext = NULL,
1450 .queueFamilyIndex = demo->graphics_queue_node_index,
1451 .flags = 0,
1452 };
1453 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1454 assert(!err);
1455
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001456 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001457 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001458 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001459 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001460 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001461 .flags = 0,
1462 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001463 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1464 assert(!err);
1465
Chia-I Wuc19795a2014-09-13 11:12:55 +08001466 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001467 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001468 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001469 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001470 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001471 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001472 demo_prepare_pipeline(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001473
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001474 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001475 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001476
1477 demo_prepare_framebuffers(demo);
1478
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001479 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001480}
1481
Ian Elliotte14e9f92015-04-16 15:23:05 -06001482#ifdef _WIN32
1483static void demo_run(struct demo *demo)
1484{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001485 if (!demo->prepared)
1486 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001487 demo_draw(demo);
Tony Barbour4a6692d2015-10-08 13:45:45 -06001488
1489 if (demo->depthStencil > 0.99f)
1490 demo->depthIncrement = -0.001f;
1491 if (demo->depthStencil < 0.8f)
1492 demo->depthIncrement = 0.001f;
1493
1494 demo->depthStencil += demo->depthIncrement;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001495}
1496
1497// On MS-Windows, make this a global, so it's available to WndProc()
1498struct demo demo;
1499
1500// MS-Windows event handling function:
1501LRESULT CALLBACK WndProc(HWND hWnd,
1502 UINT uMsg,
1503 WPARAM wParam,
1504 LPARAM lParam)
1505{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001506 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001507
1508 switch(uMsg)
1509 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001510 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001511 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001512 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001513 PostQuitMessage(0);
1514 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001515 case WM_PAINT:
Cody Northrop67ff0a42015-09-09 10:21:49 -06001516 if (demo.prepared) {
1517 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001518 break;
Cody Northrop67ff0a42015-09-09 10:21:49 -06001519 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001520 default:
1521 break;
1522 }
1523 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1524}
1525
1526static void demo_create_window(struct demo *demo)
1527{
1528 WNDCLASSEX win_class;
1529
1530 // Initialize the window class structure:
1531 win_class.cbSize = sizeof(WNDCLASSEX);
1532 win_class.style = CS_HREDRAW | CS_VREDRAW;
1533 win_class.lpfnWndProc = WndProc;
1534 win_class.cbClsExtra = 0;
1535 win_class.cbWndExtra = 0;
1536 win_class.hInstance = demo->connection; // hInstance
1537 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1538 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1539 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1540 win_class.lpszMenuName = NULL;
1541 win_class.lpszClassName = demo->name;
1542 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1543 // Register window class:
1544 if (!RegisterClassEx(&win_class)) {
1545 // It didn't work, so try to give a useful error:
1546 printf("Unexpected error trying to start the application!\n");
1547 fflush(stdout);
1548 exit(1);
1549 }
1550 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001551 RECT wr = { 0, 0, demo->width, demo->height };
1552 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001553 demo->window = CreateWindowEx(0,
1554 demo->name, // class name
1555 demo->name, // app name
1556 WS_OVERLAPPEDWINDOW | // window style
1557 WS_VISIBLE |
1558 WS_SYSMENU,
1559 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001560 wr.right-wr.left, // width
1561 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001562 NULL, // handle to parent
1563 NULL, // handle to menu
1564 demo->connection, // hInstance
1565 NULL); // no extra parameters
1566 if (!demo->window) {
1567 // It didn't work, so try to give a useful error:
1568 printf("Cannot create a window in which to draw!\n");
1569 fflush(stdout);
1570 exit(1);
1571 }
1572}
1573#else // _WIN32
1574
Chia-I Wuc19795a2014-09-13 11:12:55 +08001575static void demo_handle_event(struct demo *demo,
1576 const xcb_generic_event_t *event)
1577{
1578 switch (event->response_type & 0x7f) {
1579 case XCB_EXPOSE:
1580 demo_draw(demo);
1581 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001582 case XCB_CLIENT_MESSAGE:
1583 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1584 (*demo->atom_wm_delete_window).atom) {
1585 demo->quit = true;
1586 }
1587 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001588 case XCB_KEY_RELEASE:
1589 {
1590 const xcb_key_release_event_t *key =
1591 (const xcb_key_release_event_t *) event;
1592
1593 if (key->detail == 0x9)
1594 demo->quit = true;
1595 }
1596 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001597 case XCB_DESTROY_NOTIFY:
1598 demo->quit = true;
1599 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001600 default:
1601 break;
1602 }
1603}
1604
1605static void demo_run(struct demo *demo)
1606{
1607 xcb_flush(demo->connection);
1608
1609 while (!demo->quit) {
1610 xcb_generic_event_t *event;
1611
Tony Barbour4a6692d2015-10-08 13:45:45 -06001612 event = xcb_poll_for_event(demo->connection);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001613 if (event) {
1614 demo_handle_event(demo, event);
1615 free(event);
1616 }
Tony Barbour4a6692d2015-10-08 13:45:45 -06001617
1618 demo_draw(demo);
1619
1620 if (demo->depthStencil > 0.99f)
1621 demo->depthIncrement = -0.001f;
1622 if (demo->depthStencil < 0.8f)
1623 demo->depthIncrement = 0.001f;
1624
1625 demo->depthStencil += demo->depthIncrement;
1626
1627 // Wait for work to finish before updating MVP.
1628 vkDeviceWaitIdle(demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001629 }
1630}
1631
1632static void demo_create_window(struct demo *demo)
1633{
1634 uint32_t value_mask, value_list[32];
1635
1636 demo->window = xcb_generate_id(demo->connection);
1637
1638 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1639 value_list[0] = demo->screen->black_pixel;
1640 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001641 XCB_EVENT_MASK_EXPOSURE |
1642 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001643
1644 xcb_create_window(demo->connection,
1645 XCB_COPY_FROM_PARENT,
1646 demo->window, demo->screen->root,
1647 0, 0, demo->width, demo->height, 0,
1648 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1649 demo->screen->root_visual,
1650 value_mask, value_list);
1651
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001652 /* Magic code that will send notification when window is destroyed */
1653 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1654 "WM_PROTOCOLS");
1655 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1656
1657 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1658 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1659
1660 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1661 demo->window, (*reply).atom, 4, 32, 1,
1662 &(*demo->atom_wm_delete_window).atom);
1663 free(reply);
1664
Chia-I Wuc19795a2014-09-13 11:12:55 +08001665 xcb_map_window(demo->connection, demo->window);
1666}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001667#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001668
Ian Elliotte36b2082015-07-06 14:27:58 -06001669/*
1670 * Return 1 (true) if all layer names specified in check_names
1671 * can be found in given layer properties.
1672 */
1673static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1674 uint32_t layer_count, VkLayerProperties *layers)
1675{
1676 for (uint32_t i = 0; i < check_count; i++) {
1677 VkBool32 found = 0;
1678 for (uint32_t j = 0; j < layer_count; j++) {
1679 if (!strcmp(check_names[i], layers[j].layerName)) {
1680 found = 1;
1681 }
1682 }
1683 if (!found) {
1684 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1685 return 0;
1686 }
1687 }
1688 return 1;
1689}
1690
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001691void* VKAPI myalloc(
1692 void* pUserData,
1693 size_t size,
1694 size_t alignment,
1695 VkSystemAllocType allocType)
1696{
1697 return malloc(size);
1698}
1699void VKAPI myfree(
1700 void* pUserData,
1701 void* pMem)
1702{
Piers Daniell811eb742015-09-29 13:01:09 -06001703 free(pMem);
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001704}
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001705static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001706{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001707 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001708 char *extension_names[64];
1709 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001710 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001711 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001712 VkLayerProperties *instance_layers;
1713 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001714 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001715 uint32_t instance_layer_count = 0;
1716 uint32_t enabled_extension_count = 0;
1717 uint32_t enabled_layer_count = 0;
1718
Ian Elliotte36b2082015-07-06 14:27:58 -06001719 char *instance_validation_layers[] = {
1720 "MemTracker",
1721 };
1722
1723 char *device_validation_layers[] = {
1724 "MemTracker",
1725 };
1726
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001727 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001728 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001729 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001730 assert(!err);
1731
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001732 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001733 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001734 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001735
1736 if (demo->validate) {
1737 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1738 instance_layer_count, instance_layers);
1739 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001740 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001741 "required validation layer.\n\n"
1742 "Please look at the Getting Started guide for additional "
1743 "information.\n",
1744 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001745 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001746 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001747 }
1748
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001749 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001750 assert(!err);
1751
Tony Barbourd9955e42015-10-08 13:59:42 -06001752 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001753 memset(extension_names, 0, sizeof(extension_names));
1754 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001755 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001756 assert(!err);
1757 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001758 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001759 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001760 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001761 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001762 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001763 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001764 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001765 }
1766 }
1767 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001768 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001769 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001770 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001771 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001772 "Vulkan installable client driver (ICD) installed?\nPlease "
1773 "look at the Getting Started guide for additional "
1774 "information.\n",
1775 "vkCreateInstance Failure");
1776 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001777 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001778 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001779 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001780 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001781 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001782 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001783 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001784 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001785 };
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001786 VkAllocCallbacks cb = {
1787 .pUserData = NULL,
1788 .pfnAlloc = myalloc,
1789 .pfnFree = myfree,
1790 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001791 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001792 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001793 .pNext = NULL,
1794 .pAppInfo = &app,
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001795 .pAllocCb = &cb,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001796 .layerCount = enabled_layer_count,
1797 .ppEnabledLayerNames = (const char *const*) layer_names,
1798 .extensionCount = enabled_extension_count,
1799 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001800 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001801 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchterea975642015-09-16 16:23:55 -06001802 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1803 .pNext = NULL,
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001804 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001805 .queueCount = 1,
1806 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001807 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001808
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001809 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001810 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1811 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001812 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001813 "additional information.\n",
1814 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06001815 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001816 ERR_EXIT("Cannot find a specified extension library"
1817 ".\nMake sure your layers path is set appropriately\n",
1818 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001819 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001820 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1821 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001822 "the Getting Started guide for additional information.\n",
1823 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001824 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001825
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001826 free(instance_layers);
1827 free(instance_extensions);
1828
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001829 /* Make initial call to query gpu_count, then second call for gpu info*/
1830 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
1831 assert(!err && gpu_count > 0);
1832 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
1833 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
1834 assert(!err);
1835 /* For tri demo we just grab the first physical device */
1836 demo->gpu = physical_devices[0];
1837 free(physical_devices);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001838
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001839 /* Look for validation layers */
1840 validation_found = 0;
1841 enabled_layer_count = 0;
1842 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001843 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001844 assert(!err);
1845
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001846 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001847 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001848 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001849
1850 if (demo->validate) {
1851 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1852 device_layer_count, device_layers);
1853 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001854 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001855 "a required validation layer.\n\n"
1856 "Please look at the Getting Started guide for additional "
1857 "information.\n",
1858 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001859 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001860 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001861 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001862
1863 uint32_t device_extension_count = 0;
1864 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001865 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001866 demo->gpu, NULL, &device_extension_count, NULL);
1867 assert(!err);
1868
Tony Barbourd9955e42015-10-08 13:59:42 -06001869 swapchainExtFound = 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001870 enabled_extension_count = 0;
1871 memset(extension_names, 0, sizeof(extension_names));
1872 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001873 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001874 demo->gpu, NULL, &device_extension_count, device_extensions);
1875 assert(!err);
1876
1877 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001878 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001879 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001880 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Ian Elliotte36b2082015-07-06 14:27:58 -06001881 }
1882 assert(enabled_extension_count < 64);
1883 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001884 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001885 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001886 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliotte36b2082015-07-06 14:27:58 -06001887 "Vulkan installable client driver (ICD) installed?\nPlease "
1888 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001889 "information.\n",
1890 "vkCreateInstance Failure");
1891 }
1892
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001893 VkDeviceCreateInfo device = {
1894 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1895 .pNext = NULL,
Courtney Goeltzenleuchterdfd53f52015-10-15 16:58:44 -06001896 .requestedQueueCount = 1,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001897 .pRequestedQueues = &queue,
1898 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001899 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1900 .extensionCount = enabled_extension_count,
1901 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001902 };
1903
1904 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001905 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001906 if (!demo->dbgCreateMsgCallback) {
1907 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1908 "vkGetProcAddr Failure");
1909 }
1910 err = demo->dbgCreateMsgCallback(
1911 demo->inst,
1912 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1913 dbgFunc, NULL,
1914 &demo->msg_callback);
1915 switch (err) {
1916 case VK_SUCCESS:
1917 break;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001918 case VK_ERROR_OUT_OF_HOST_MEMORY:
1919 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1920 "dbgCreateMsgCallback Failure");
1921 break;
1922 default:
1923 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1924 "dbgCreateMsgCallback Failure");
1925 break;
1926 }
1927 }
1928
Ian Elliotte36b2082015-07-06 14:27:58 -06001929
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001930 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001931 assert(!err);
1932
Ian Elliott338dedb2015-08-21 15:09:33 -06001933 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
1934 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
1935 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
1936 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
1937 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1938 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1939 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
1940 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
1941 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
1942 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliott1b6de092015-06-22 15:07:49 -06001943
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001944 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001945
Cody Northropef72e2a2015-08-03 17:04:53 -06001946 // Query with NULL data to get count
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001947 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001948
Cody Northropef72e2a2015-08-03 17:04:53 -06001949 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001950 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001951 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001952
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001953 // Graphics queue and MemMgr queue can be separate.
1954 // TODO: Add support for separate queues, including synchronization,
1955 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001956}
1957
Tony Barbourd9955e42015-10-08 13:59:42 -06001958static void demo_init_vk_swapchain(struct demo *demo)
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001959{
Tony Barbour4a6692d2015-10-08 13:45:45 -06001960 VkResult U_ASSERT_ONLY err;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001961 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06001962
Tony Barbourd9955e42015-10-08 13:59:42 -06001963 // Construct the surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06001964 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001965 demo->surface_description.pNext = NULL;
1966#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06001967 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001968 demo->surface_description.pPlatformHandle = demo->connection;
1969 demo->surface_description.pPlatformWindow = demo->window;
1970#else // _WIN32
1971 demo->platform_handle_xcb.connection = demo->connection;
1972 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06001973 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001974 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
1975 demo->surface_description.pPlatformWindow = &demo->window;
1976#endif // _WIN32
1977
Tony Barbourd9955e42015-10-08 13:59:42 -06001978 // Iterate over each queue to learn whether it supports presenting:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001979 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
1980 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001981 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
1982 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06001983 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001984 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001985
1986 // Search for a graphics and a present queue in the array of queue
1987 // families, try to find one that supports both
1988 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
1989 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001990 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001991 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
1992 if (graphicsQueueNodeIndex == UINT32_MAX) {
1993 graphicsQueueNodeIndex = i;
1994 }
1995
1996 if (supportsPresent[i] == VK_TRUE) {
1997 graphicsQueueNodeIndex = i;
1998 presentQueueNodeIndex = i;
1999 break;
2000 }
2001 }
2002 }
2003 if (presentQueueNodeIndex == UINT32_MAX) {
2004 // If didn't find a queue that supports both graphics and present, then
2005 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002006 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002007 if (supportsPresent[i] == VK_TRUE) {
2008 presentQueueNodeIndex = i;
2009 break;
2010 }
2011 }
2012 }
2013 free(supportsPresent);
2014
2015 // Generate error if could not find both a graphics and a present queue
2016 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2017 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002018 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002019 }
2020
2021 // TODO: Add support for separate queues, including presentation,
2022 // synchronization, and appropriate tracking for QueueSubmit
2023 // While it is possible for an application to use a separate graphics and a
2024 // present queues, this demo program assumes it is only using one:
2025 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2026 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002027 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002028 }
2029
2030 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002031
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002032 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08002033 0, &demo->queue);
Ian Elliott32536f92015-04-21 16:41:02 -06002034
Ian Elliotte36b2082015-07-06 14:27:58 -06002035 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002036 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002037 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2038 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002039 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002040 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002041 VkSurfaceFormatKHR *surfFormats =
2042 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2043 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2044 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002045 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002046 assert(!err);
2047 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2048 // the surface has no preferred format. Otherwise, at least one
2049 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002050 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2051 {
2052 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2053 }
2054 else
2055 {
2056 assert(formatCount >= 1);
2057 demo->format = surfFormats[0].format;
2058 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002059 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002060
Mark Lobodzinski72346292015-07-02 16:49:40 -06002061 // Get Memory information and properties
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002062 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002063}
2064
2065static void demo_init_connection(struct demo *demo)
2066{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002067#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002068 const xcb_setup_t *setup;
2069 xcb_screen_iterator_t iter;
2070 int scr;
2071
2072 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002073 if (demo->connection == NULL) {
2074 printf("Cannot find a compatible Vulkan installable client driver "
2075 "(ICD).\nExiting ...\n");
2076 fflush(stdout);
2077 exit(1);
2078 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002079
2080 setup = xcb_get_setup(demo->connection);
2081 iter = xcb_setup_roots_iterator(setup);
2082 while (scr-- > 0)
2083 xcb_screen_next(&iter);
2084
2085 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002086#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002087}
2088
Ian Elliotte14e9f92015-04-16 15:23:05 -06002089#ifdef _WIN32
2090static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2091#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002092static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002093#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002094{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002095 bool argv_error = false;
2096
Chia-I Wuc19795a2014-09-13 11:12:55 +08002097 memset(demo, 0, sizeof(*demo));
2098
Ian Elliotte14e9f92015-04-16 15:23:05 -06002099#ifdef _WIN32
2100 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002101 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002102
2103 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2104 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002105 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2106 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002107 else if (strlen(pCmdLine) != 0) {
2108 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2109 argv_error = true;
2110 }
2111#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002112 for (int i = 0; i < argc; i++) {
2113 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2114 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002115 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2116 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002117 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002118#endif // _WIN32
2119 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002120 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002121 fflush(stderr);
2122 exit(1);
2123 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002124
Chia-I Wuc19795a2014-09-13 11:12:55 +08002125 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002126 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002127
2128 demo->width = 300;
2129 demo->height = 300;
Tony Barbour4a6692d2015-10-08 13:45:45 -06002130 demo->depthStencil = 1.0;
2131 demo->depthIncrement = -0.01f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002132}
2133
2134static void demo_cleanup(struct demo *demo)
2135{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002136 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002137
Cody Northrop67ff0a42015-09-09 10:21:49 -06002138 demo->prepared = false;
2139
Tony Barbour5aabff52015-10-08 14:26:24 -06002140 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002141 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2142 }
Tony Barbour5aabff52015-10-08 14:26:24 -06002143 free(demo->framebuffers);
Tony Barbourde4124d2015-07-03 10:33:54 -06002144 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002145
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002146 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002147 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002148 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002149 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002150 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002151
Tony Barbourde4124d2015-07-03 10:33:54 -06002152 vkDestroyPipeline(demo->device, demo->pipeline);
2153 vkDestroyRenderPass(demo->device, demo->render_pass);
2154 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2155 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002156
Tony Barbourde4124d2015-07-03 10:33:54 -06002157 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002158 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002159
Chia-I Wub043fe32014-10-06 15:30:33 +08002160 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002161 vkDestroyImageView(demo->device, demo->textures[i].view);
2162 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002163 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002164 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08002165 }
2166
Ian Elliott338dedb2015-08-21 15:09:33 -06002167 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002168 vkDestroyImageView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002169 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002170
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002171 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbourde4124d2015-07-03 10:33:54 -06002172 vkDestroyImage(demo->device, demo->depth.image);
2173 vkFreeMemory(demo->device, demo->depth.mem);
2174
Ian Elliott338dedb2015-08-21 15:09:33 -06002175 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
Ian Elliotte36b2082015-07-06 14:27:58 -06002176 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002177
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002178 vkDestroyDevice(demo->device);
2179 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002180
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002181 free(demo->queue_props);
2182
Ian Elliotte14e9f92015-04-16 15:23:05 -06002183#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002184 xcb_destroy_window(demo->connection, demo->window);
2185 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002186 free(demo->atom_wm_delete_window);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002187#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002188}
2189
Ian Elliotte14e9f92015-04-16 15:23:05 -06002190#ifdef _WIN32
2191int APIENTRY WinMain(HINSTANCE hInstance,
2192 HINSTANCE hPrevInstance,
2193 LPSTR pCmdLine,
2194 int nCmdShow)
2195{
2196 MSG msg; // message
2197 bool done; // flag saying when app is complete
2198
2199 demo_init(&demo, hInstance, pCmdLine);
2200 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002201 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002202
2203 demo_prepare(&demo);
2204
2205 done = false; //initialize loop condition variable
2206 /* main message loop*/
2207 while(!done)
2208 {
Ian Elliott421107f2015-04-28 15:50:36 -06002209 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002210 if (msg.message == WM_QUIT) //check for a quit message
2211 {
2212 done = true; //if found, quit app
2213 }
2214 else
2215 {
2216 /* Translate and dispatch to event queue*/
2217 TranslateMessage(&msg);
2218 DispatchMessage(&msg);
2219 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002220 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002221 }
2222
2223 demo_cleanup(&demo);
2224
Tony Barboura938abb2015-04-22 11:36:22 -06002225 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002226}
2227#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002228int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002229{
2230 struct demo demo;
2231
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002232 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002233 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002234 demo_init_vk_swapchain(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002235
2236 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002237 demo_run(&demo);
2238
2239 demo_cleanup(&demo);
2240
Chia-I Wuc19795a2014-09-13 11:12:55 +08002241 return 0;
2242}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002243#endif // _WIN32