blob: 174f877d25f8a7a0cb77d9f8bd7b53dad9ca6e18 [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 */
Tony Barbourde4124d2015-07-03 10:33:54 -0600690 err = vkGetImageMemoryRequirements(demo->device, demo->depth.image,
691 &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
Tony Barbourde4124d2015-07-03 10:33:54 -0600762 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -0600763 assert(!err);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600764
765 mem_alloc.allocationSize = mem_reqs.size;
Tony Barbourf1eceb92015-10-15 12:42:56 -0600766 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &mem_alloc.memoryTypeIndex);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600767 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700768
Mark Lobodzinski23182612015-05-29 09:32:35 -0500769 /* allocate memory */
770 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
771 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700772
Mark Lobodzinski23182612015-05-29 09:32:35 -0500773 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600774 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500775 tex_obj->mem, 0);
776 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700777
Tony Barbourf1eceb92015-10-15 12:42:56 -0600778 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600779 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600780 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700781 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600782 .arrayLayer = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700783 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600784 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700785 void *data;
786 int32_t x, y;
787
Tony Barbour426b9052015-06-24 16:06:58 -0600788 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
789 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700790
Dominik Witczake82d5b62015-09-22 18:25:33 +0200791 err = vkMapMemory(demo->device, tex_obj->mem, 0, mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700792 assert(!err);
793
794 for (y = 0; y < tex_height; y++) {
795 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
796 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700797 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700798 }
799
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600800 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700801 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600802
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600803 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600804 demo_set_image_layout(demo, tex_obj->image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600805 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600806 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600807 tex_obj->imageLayout);
808 /* 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 -0700809}
810
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500811static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700812{
813 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600814 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600815 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700816}
817
818static void demo_prepare_textures(struct demo *demo)
819{
Tony Barbour8205d902015-04-16 15:59:00 -0600820 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600821 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700822 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
823 { 0xffff0000, 0xff00ff00 },
824 };
Tony Barbour22a30862015-04-22 09:02:32 -0600825 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700826 uint32_t i;
827
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600828 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700829 assert(!err);
830
831 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600832 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700833 /* Device can texture using linear textures */
834 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600835 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600836 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700837 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600838 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700839
840 memset(&staging_texture, 0, sizeof(staging_texture));
841 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600842 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700843
844 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600845 VK_IMAGE_TILING_OPTIMAL,
846 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
847 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700848
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600849 demo_set_image_layout(demo, staging_texture.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600850 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600851 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600852 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700853
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600854 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600855 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600856 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600857 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700858
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600859 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600860 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700861 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600862 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700863 .destOffset = { 0, 0, 0 },
864 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
865 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600866 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600867 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
868 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600869 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700870
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600871 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600872 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600873 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600874 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700875
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600876 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700877
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600878 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700879 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600880 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700881 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700882 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700883
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600884 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600885 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800886 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600887 .magFilter = VK_TEX_FILTER_NEAREST,
888 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600889 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter51624412015-09-10 14:08:50 -0600890 .addressModeU = VK_TEX_ADDRESS_MODE_WRAP,
891 .addressModeV = VK_TEX_ADDRESS_MODE_WRAP,
892 .addressModeW = VK_TEX_ADDRESS_MODE_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800893 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700894 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600895 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800896 .minLod = 0.0f,
897 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600898 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600899 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800900 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600901 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600902 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800903 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600904 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600905 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700906 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600907 .channels = { VK_CHANNEL_SWIZZLE_R,
908 VK_CHANNEL_SWIZZLE_G,
909 VK_CHANNEL_SWIZZLE_B,
910 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -0600911 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600912 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +0800913 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700914
Chia-I Wub043fe32014-10-06 15:30:33 +0800915 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600916 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800917 &demo->textures[i].sampler);
918 assert(!err);
919
Chia-I Wub043fe32014-10-06 15:30:33 +0800920 /* create image view */
921 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600922 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700923 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800924 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800925 }
926}
927
Chia-I Wu99621bc2014-10-08 11:52:22 +0800928static void demo_prepare_vertices(struct demo *demo)
929{
930 const float vb[3][5] = {
931 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800932 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
933 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800934 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
935 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600936 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600937 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800938 .pNext = NULL,
939 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600940 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800941 .flags = 0,
942 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600943 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600944 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500945 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800946 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600947 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800948 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500949 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600950 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800951 void *data;
952
953 memset(&demo->vertices, 0, sizeof(demo->vertices));
954
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600955 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800956 assert(!err);
957
Tony Barbourde4124d2015-07-03 10:33:54 -0600958 err = vkGetBufferMemoryRequirements(demo->device,
959 demo->vertices.buf, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -0600960 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800961
Mark Lobodzinski72346292015-07-02 16:49:40 -0600962 mem_alloc.allocationSize = mem_reqs.size;
963 err = memory_type_from_properties(demo,
964 mem_reqs.memoryTypeBits,
965 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
966 &mem_alloc.memoryTypeIndex);
967 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800968
Mark Lobodzinski23182612015-05-29 09:32:35 -0500969 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
970 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800971
Dominik Witczake82d5b62015-09-22 18:25:33 +0200972 err = vkMapMemory(demo->device, demo->vertices.mem, 0, mem_alloc.allocationSize, 0, &data);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500973 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800974
Mark Lobodzinski23182612015-05-29 09:32:35 -0500975 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800976
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600977 vkUnmapMemory(demo->device, demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500978
Tony Barbourde4124d2015-07-03 10:33:54 -0600979 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500980 demo->vertices.mem, 0);
981 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800982
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600983 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800984 demo->vertices.vi.pNext = NULL;
985 demo->vertices.vi.bindingCount = 1;
986 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
987 demo->vertices.vi.attributeCount = 2;
988 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
989
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600990 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800991 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600992 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800993
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600994 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
995 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600996 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800997 demo->vertices.vi_attrs[0].offsetInBytes = 0;
998
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600999 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
1000 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -06001001 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001002 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001003}
1004
Chia-I Wuf8385062015-01-04 16:27:24 +08001005static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +08001006{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001007 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001008 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001009 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001010 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001011 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +08001012 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001013 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001014 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001015 .pNext = NULL,
1016 .count = 1,
1017 .pBinding = &layout_binding,
1018 };
Tony Barbour22a30862015-04-22 09:02:32 -06001019 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +08001020
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001021 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001022 &descriptor_layout, &demo->desc_layout);
1023 assert(!err);
1024
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001025 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1026 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1027 .pNext = NULL,
1028 .descriptorSetCount = 1,
1029 .pSetLayouts = &demo->desc_layout,
1030 };
1031
1032 err = vkCreatePipelineLayout(demo->device,
1033 &pPipelineLayoutCreateInfo,
1034 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001035 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001036}
1037
Chia-I Wu76cd4222015-07-08 13:34:24 +08001038static void demo_prepare_render_pass(struct demo *demo)
1039{
Chia-I Wuc278df82015-07-07 11:50:03 +08001040 const VkAttachmentDescription attachments[2] = {
1041 [0] = {
1042 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1043 .pNext = NULL,
1044 .format = demo->format,
1045 .samples = 1,
1046 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1047 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1048 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1049 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1050 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1051 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1052 },
1053 [1] = {
1054 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1055 .pNext = NULL,
1056 .format = demo->depth.format,
1057 .samples = 1,
1058 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1059 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1060 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1061 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1062 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1063 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1064 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001065 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001066 const VkAttachmentReference color_reference = {
1067 .attachment = 0,
1068 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1069 };
1070 const VkSubpassDescription subpass = {
1071 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1072 .pNext = NULL,
1073 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1074 .flags = 0,
1075 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001076 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001077 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001078 .pColorAttachments = &color_reference,
1079 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001080 .depthStencilAttachment = {
1081 .attachment = 1,
1082 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1083 },
1084 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001085 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001086 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001087 const VkRenderPassCreateInfo rp_info = {
1088 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1089 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001090 .attachmentCount = 2,
1091 .pAttachments = attachments,
1092 .subpassCount = 1,
1093 .pSubpasses = &subpass,
1094 .dependencyCount = 0,
1095 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001096 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001097 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001098
1099 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1100 assert(!err);
1101}
1102
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001103static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001104 VkShaderStage stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001105 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001106 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001107 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001108{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001109 VkShaderModuleCreateInfo moduleCreateInfo;
1110 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001111 VkShader shader;
Tony Barbour4a6692d2015-10-08 13:45:45 -06001112 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001113
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001114
1115 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1116 moduleCreateInfo.pNext = NULL;
1117
1118 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1119 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001120
Cody Northrop75db0322015-05-28 11:27:16 -06001121 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001122 moduleCreateInfo.codeSize = size;
1123 moduleCreateInfo.pCode = code;
1124 moduleCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001125 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001126 assert(!err);
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001127
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001128 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001129 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001130 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001131 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001132 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001133 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001134 } else {
1135 // Create fake SPV structure to feed GLSL
1136 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001137 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1138 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1139 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001140
Cody Northrop75db0322015-05-28 11:27:16 -06001141 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001142 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1143 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1144 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1145 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001146
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001147 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001148 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001149
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001150 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001151 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001152 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001153 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001154 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001155 assert(!err);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001156 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001157 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001158 return shader;
1159}
1160
Cody Northrop75db0322015-05-28 11:27:16 -06001161char *demo_read_spv(const char *filename, size_t *psize)
1162{
1163 long int size;
1164 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001165 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001166
1167 FILE *fp = fopen(filename, "rb");
1168 if (!fp) return NULL;
1169
1170 fseek(fp, 0L, SEEK_END);
1171 size = ftell(fp);
1172
1173 fseek(fp, 0L, SEEK_SET);
1174
1175 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001176 retVal = fread(shader_code, size, 1, fp);
1177 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001178
1179 *psize = size;
1180
1181 return shader_code;
1182}
1183
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001184static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001185{
Cody Northrop75db0322015-05-28 11:27:16 -06001186 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001187 VkShader shader;
1188 void *vertShaderCode;
1189 size_t size;
Cody Northrop75db0322015-05-28 11:27:16 -06001190
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001191 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001192
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001193 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1194 &demo->vert_shader_module,
1195 vertShaderCode, size);
1196 free(vertShaderCode);
1197 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001198 } else {
1199 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001200 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001201 "#extension GL_ARB_separate_shader_objects : enable\n"
1202 "#extension GL_ARB_shading_language_420pack : enable\n"
1203 "layout (location = 0) in vec4 pos;\n"
1204 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001205 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001206 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001207 " texcoord = attr;\n"
1208 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001209 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001210
Cody Northrop75db0322015-05-28 11:27:16 -06001211 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001212 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001213 (const void *) vertShaderText,
1214 strlen(vertShaderText));
1215 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001216}
1217
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001218static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001219{
Cody Northrop75db0322015-05-28 11:27:16 -06001220 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001221 VkShader shader;
1222 void *fragShaderCode;
1223 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001224
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001225 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001226
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001227 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1228 &demo->frag_shader_module,
1229 fragShaderCode, size);
1230
1231 free(fragShaderCode);
1232 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001233 } else {
1234 static const char *fragShaderText =
1235 "#version 140\n"
1236 "#extension GL_ARB_separate_shader_objects : enable\n"
1237 "#extension GL_ARB_shading_language_420pack : enable\n"
1238 "layout (binding = 0) uniform sampler2D tex;\n"
1239 "layout (location = 0) in vec2 texcoord;\n"
1240 "layout (location = 0) out vec4 uFragColor;\n"
1241 "void main() {\n"
1242 " uFragColor = texture(tex, texcoord);\n"
1243 "}\n";
1244
1245 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001246 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001247 (const void *) fragShaderText,
1248 strlen(fragShaderText));
1249 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001250}
1251
1252static void demo_prepare_pipeline(struct demo *demo)
1253{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001254 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001255 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001256
Tony Barboure307f582015-07-10 15:29:03 -06001257 VkPipelineVertexInputStateCreateInfo vi;
1258 VkPipelineInputAssemblyStateCreateInfo ia;
1259 VkPipelineRasterStateCreateInfo rs;
1260 VkPipelineColorBlendStateCreateInfo cb;
1261 VkPipelineDepthStencilStateCreateInfo ds;
1262 VkPipelineViewportStateCreateInfo vp;
1263 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniell811eb742015-09-29 13:01:09 -06001264 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1265 VkPipelineDynamicStateCreateInfo dynamicState;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001266
Tony Barbour22a30862015-04-22 09:02:32 -06001267 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001268
Piers Daniell811eb742015-09-29 13:01:09 -06001269 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1270 memset(&dynamicState, 0, sizeof dynamicState);
1271 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1272 dynamicState.pDynamicStates = dynamicStateEnables;
1273
Chia-I Wuc19795a2014-09-13 11:12:55 +08001274 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001275 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001276 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001277
Chia-I Wu8d29d022014-10-08 12:14:39 +08001278 vi = demo->vertices.vi;
1279
Chia-I Wuc19795a2014-09-13 11:12:55 +08001280 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001281 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001282 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001283
1284 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001285 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001286 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001287 rs.cullMode = VK_CULL_MODE_BACK;
1288 rs.frontFace = VK_FRONT_FACE_CW;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001289 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001290 rs.rasterizerDiscardEnable = VK_FALSE;
1291 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001292
1293 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001294 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1295 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001296 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001297 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001298 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001299 cb.attachmentCount = 1;
1300 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001301
Tony Barbourfa6cac72015-01-16 14:27:35 -07001302 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001303 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001304 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001305 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1306 vp.scissorCount = 1;
1307 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001308
1309 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001310 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001311 ds.depthTestEnable = VK_TRUE;
1312 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001313 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001314 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001315 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1316 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001317 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001318 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001319 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001320
Tony Barbourfa6cac72015-01-16 14:27:35 -07001321 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001322 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001323 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001324 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001325
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001326 // Two stages: vs and fs
1327 pipeline.stageCount = 2;
1328 VkPipelineShaderStageCreateInfo shaderStages[2];
1329 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1330
1331 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1332 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1333 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001334
1335 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1336 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1337 shaderStages[1].shader = demo_prepare_fs(demo);
1338
Tony Barboure307f582015-07-10 15:29:03 -06001339 pipeline.pVertexInputState = &vi;
1340 pipeline.pInputAssemblyState = &ia;
1341 pipeline.pRasterState = &rs;
1342 pipeline.pColorBlendState = &cb;
1343 pipeline.pMultisampleState = &ms;
1344 pipeline.pViewportState = &vp;
1345 pipeline.pDepthStencilState = &ds;
1346 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001347 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001348 pipeline.pDynamicState = &dynamicState;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001349
Jon Ashburn0d60d272015-07-09 15:02:25 -06001350 memset(&pipelineCache, 0, sizeof(pipelineCache));
1351 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1352
1353 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1354 assert(!err);
1355 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001356 assert(!err);
1357
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001358 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001359
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001360 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001361 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001362 }
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001363 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1364 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001365}
1366
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001367static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001368{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001369 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001370 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001371 .count = DEMO_TEXTURE_COUNT,
1372 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001373 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001374 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001375 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001376 .poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT,
1377 .maxSets = 1,
Chia-I Wuf8385062015-01-04 16:27:24 +08001378 .count = 1,
1379 .pTypeCount = &type_count,
1380 };
Tony Barbour22a30862015-04-22 09:02:32 -06001381 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001382
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001383 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001384 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001385 assert(!err);
1386}
1387
1388static void demo_prepare_descriptor_set(struct demo *demo)
1389{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001390 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1391 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001392 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001393 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001394
Mike Stroyan230e6252015-04-17 12:36:38 -06001395 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001396 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001397 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001398 &demo->desc_set);
1399 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001400
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001401 memset(&tex_descs, 0, sizeof(tex_descs));
1402 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1403 tex_descs[i].sampler = demo->textures[i].sampler;
1404 tex_descs[i].imageView = demo->textures[i].view;
1405 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1406 }
1407
1408 memset(&write, 0, sizeof(write));
1409 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1410 write.destSet = demo->desc_set;
1411 write.count = DEMO_TEXTURE_COUNT;
1412 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1413 write.pDescriptors = tex_descs;
1414
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001415 vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001416}
1417
Chia-I Wu76cd4222015-07-08 13:34:24 +08001418static void demo_prepare_framebuffers(struct demo *demo)
1419{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001420 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001421 attachments[1] = demo->depth.view;
1422
Chia-I Wu76cd4222015-07-08 13:34:24 +08001423 const VkFramebufferCreateInfo fb_info = {
1424 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1425 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001426 .renderPass = demo->render_pass,
1427 .attachmentCount = 2,
1428 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001429 .width = demo->width,
1430 .height = demo->height,
1431 .layers = 1,
1432 };
1433 VkResult U_ASSERT_ONLY err;
1434 uint32_t i;
1435
Tony Barbour5aabff52015-10-08 14:26:24 -06001436 demo->framebuffers = (VkFramebuffer *) malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1437 assert(demo->framebuffers);
1438
1439 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001440 attachments[0]= demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001441 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1442 assert(!err);
1443 }
1444}
1445
Chia-I Wuc19795a2014-09-13 11:12:55 +08001446static void demo_prepare(struct demo *demo)
1447{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001448 VkResult U_ASSERT_ONLY err;
1449
1450 const VkCmdPoolCreateInfo cmd_pool_info = {
1451 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1452 .pNext = NULL,
1453 .queueFamilyIndex = demo->graphics_queue_node_index,
1454 .flags = 0,
1455 };
1456 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1457 assert(!err);
1458
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001459 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001460 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001461 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001462 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001463 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001464 .flags = 0,
1465 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001466 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1467 assert(!err);
1468
Chia-I Wuc19795a2014-09-13 11:12:55 +08001469 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001470 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001471 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001472 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001473 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001474 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001475 demo_prepare_pipeline(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001476
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001477 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001478 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001479
1480 demo_prepare_framebuffers(demo);
1481
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001482 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001483}
1484
Ian Elliotte14e9f92015-04-16 15:23:05 -06001485#ifdef _WIN32
1486static void demo_run(struct demo *demo)
1487{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001488 if (!demo->prepared)
1489 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001490 demo_draw(demo);
Tony Barbour4a6692d2015-10-08 13:45:45 -06001491
1492 if (demo->depthStencil > 0.99f)
1493 demo->depthIncrement = -0.001f;
1494 if (demo->depthStencil < 0.8f)
1495 demo->depthIncrement = 0.001f;
1496
1497 demo->depthStencil += demo->depthIncrement;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001498}
1499
1500// On MS-Windows, make this a global, so it's available to WndProc()
1501struct demo demo;
1502
1503// MS-Windows event handling function:
1504LRESULT CALLBACK WndProc(HWND hWnd,
1505 UINT uMsg,
1506 WPARAM wParam,
1507 LPARAM lParam)
1508{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001509 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001510
1511 switch(uMsg)
1512 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001513 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001514 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001515 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001516 PostQuitMessage(0);
1517 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001518 case WM_PAINT:
Cody Northrop67ff0a42015-09-09 10:21:49 -06001519 if (demo.prepared) {
1520 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001521 break;
Cody Northrop67ff0a42015-09-09 10:21:49 -06001522 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001523 default:
1524 break;
1525 }
1526 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1527}
1528
1529static void demo_create_window(struct demo *demo)
1530{
1531 WNDCLASSEX win_class;
1532
1533 // Initialize the window class structure:
1534 win_class.cbSize = sizeof(WNDCLASSEX);
1535 win_class.style = CS_HREDRAW | CS_VREDRAW;
1536 win_class.lpfnWndProc = WndProc;
1537 win_class.cbClsExtra = 0;
1538 win_class.cbWndExtra = 0;
1539 win_class.hInstance = demo->connection; // hInstance
1540 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1541 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1542 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1543 win_class.lpszMenuName = NULL;
1544 win_class.lpszClassName = demo->name;
1545 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1546 // Register window class:
1547 if (!RegisterClassEx(&win_class)) {
1548 // It didn't work, so try to give a useful error:
1549 printf("Unexpected error trying to start the application!\n");
1550 fflush(stdout);
1551 exit(1);
1552 }
1553 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001554 RECT wr = { 0, 0, demo->width, demo->height };
1555 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001556 demo->window = CreateWindowEx(0,
1557 demo->name, // class name
1558 demo->name, // app name
1559 WS_OVERLAPPEDWINDOW | // window style
1560 WS_VISIBLE |
1561 WS_SYSMENU,
1562 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001563 wr.right-wr.left, // width
1564 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001565 NULL, // handle to parent
1566 NULL, // handle to menu
1567 demo->connection, // hInstance
1568 NULL); // no extra parameters
1569 if (!demo->window) {
1570 // It didn't work, so try to give a useful error:
1571 printf("Cannot create a window in which to draw!\n");
1572 fflush(stdout);
1573 exit(1);
1574 }
1575}
1576#else // _WIN32
1577
Chia-I Wuc19795a2014-09-13 11:12:55 +08001578static void demo_handle_event(struct demo *demo,
1579 const xcb_generic_event_t *event)
1580{
1581 switch (event->response_type & 0x7f) {
1582 case XCB_EXPOSE:
1583 demo_draw(demo);
1584 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001585 case XCB_CLIENT_MESSAGE:
1586 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1587 (*demo->atom_wm_delete_window).atom) {
1588 demo->quit = true;
1589 }
1590 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001591 case XCB_KEY_RELEASE:
1592 {
1593 const xcb_key_release_event_t *key =
1594 (const xcb_key_release_event_t *) event;
1595
1596 if (key->detail == 0x9)
1597 demo->quit = true;
1598 }
1599 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001600 case XCB_DESTROY_NOTIFY:
1601 demo->quit = true;
1602 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001603 default:
1604 break;
1605 }
1606}
1607
1608static void demo_run(struct demo *demo)
1609{
1610 xcb_flush(demo->connection);
1611
1612 while (!demo->quit) {
1613 xcb_generic_event_t *event;
1614
Tony Barbour4a6692d2015-10-08 13:45:45 -06001615 event = xcb_poll_for_event(demo->connection);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001616 if (event) {
1617 demo_handle_event(demo, event);
1618 free(event);
1619 }
Tony Barbour4a6692d2015-10-08 13:45:45 -06001620
1621 demo_draw(demo);
1622
1623 if (demo->depthStencil > 0.99f)
1624 demo->depthIncrement = -0.001f;
1625 if (demo->depthStencil < 0.8f)
1626 demo->depthIncrement = 0.001f;
1627
1628 demo->depthStencil += demo->depthIncrement;
1629
1630 // Wait for work to finish before updating MVP.
1631 vkDeviceWaitIdle(demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001632 }
1633}
1634
1635static void demo_create_window(struct demo *demo)
1636{
1637 uint32_t value_mask, value_list[32];
1638
1639 demo->window = xcb_generate_id(demo->connection);
1640
1641 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1642 value_list[0] = demo->screen->black_pixel;
1643 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001644 XCB_EVENT_MASK_EXPOSURE |
1645 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001646
1647 xcb_create_window(demo->connection,
1648 XCB_COPY_FROM_PARENT,
1649 demo->window, demo->screen->root,
1650 0, 0, demo->width, demo->height, 0,
1651 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1652 demo->screen->root_visual,
1653 value_mask, value_list);
1654
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001655 /* Magic code that will send notification when window is destroyed */
1656 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1657 "WM_PROTOCOLS");
1658 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1659
1660 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1661 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1662
1663 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1664 demo->window, (*reply).atom, 4, 32, 1,
1665 &(*demo->atom_wm_delete_window).atom);
1666 free(reply);
1667
Chia-I Wuc19795a2014-09-13 11:12:55 +08001668 xcb_map_window(demo->connection, demo->window);
1669}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001670#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001671
Ian Elliotte36b2082015-07-06 14:27:58 -06001672/*
1673 * Return 1 (true) if all layer names specified in check_names
1674 * can be found in given layer properties.
1675 */
1676static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1677 uint32_t layer_count, VkLayerProperties *layers)
1678{
1679 for (uint32_t i = 0; i < check_count; i++) {
1680 VkBool32 found = 0;
1681 for (uint32_t j = 0; j < layer_count; j++) {
1682 if (!strcmp(check_names[i], layers[j].layerName)) {
1683 found = 1;
1684 }
1685 }
1686 if (!found) {
1687 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1688 return 0;
1689 }
1690 }
1691 return 1;
1692}
1693
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001694void* VKAPI myalloc(
1695 void* pUserData,
1696 size_t size,
1697 size_t alignment,
1698 VkSystemAllocType allocType)
1699{
1700 return malloc(size);
1701}
1702void VKAPI myfree(
1703 void* pUserData,
1704 void* pMem)
1705{
Piers Daniell811eb742015-09-29 13:01:09 -06001706 free(pMem);
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001707}
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001708static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001709{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001710 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001711 char *extension_names[64];
1712 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001713 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001714 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001715 VkLayerProperties *instance_layers;
1716 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001717 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001718 uint32_t instance_layer_count = 0;
1719 uint32_t enabled_extension_count = 0;
1720 uint32_t enabled_layer_count = 0;
1721
Ian Elliotte36b2082015-07-06 14:27:58 -06001722 char *instance_validation_layers[] = {
1723 "MemTracker",
1724 };
1725
1726 char *device_validation_layers[] = {
1727 "MemTracker",
1728 };
1729
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001730 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001731 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001732 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001733 assert(!err);
1734
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001735 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001736 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001737 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001738
1739 if (demo->validate) {
1740 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1741 instance_layer_count, instance_layers);
1742 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001743 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001744 "required validation layer.\n\n"
1745 "Please look at the Getting Started guide for additional "
1746 "information.\n",
1747 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001748 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001749 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001750 }
1751
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001752 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001753 assert(!err);
1754
Tony Barbourd9955e42015-10-08 13:59:42 -06001755 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001756 memset(extension_names, 0, sizeof(extension_names));
1757 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001758 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001759 assert(!err);
1760 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001761 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001762 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001763 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001764 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001765 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001766 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001767 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001768 }
1769 }
1770 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001771 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001772 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001773 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001774 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001775 "Vulkan installable client driver (ICD) installed?\nPlease "
1776 "look at the Getting Started guide for additional "
1777 "information.\n",
1778 "vkCreateInstance Failure");
1779 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001780 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001781 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001782 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001783 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001784 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001785 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001786 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001787 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001788 };
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001789 VkAllocCallbacks cb = {
1790 .pUserData = NULL,
1791 .pfnAlloc = myalloc,
1792 .pfnFree = myfree,
1793 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001794 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001795 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001796 .pNext = NULL,
1797 .pAppInfo = &app,
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001798 .pAllocCb = &cb,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001799 .layerCount = enabled_layer_count,
1800 .ppEnabledLayerNames = (const char *const*) layer_names,
1801 .extensionCount = enabled_extension_count,
1802 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001803 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001804 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchterea975642015-09-16 16:23:55 -06001805 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1806 .pNext = NULL,
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001807 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001808 .queueCount = 1,
1809 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001810 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001811
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001812 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001813 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1814 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001815 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001816 "additional information.\n",
1817 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06001818 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001819 ERR_EXIT("Cannot find a specified extension library"
1820 ".\nMake sure your layers path is set appropriately\n",
1821 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001822 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001823 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1824 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001825 "the Getting Started guide for additional information.\n",
1826 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001827 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001828
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001829 free(instance_layers);
1830 free(instance_extensions);
1831
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001832 /* Make initial call to query gpu_count, then second call for gpu info*/
1833 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
1834 assert(!err && gpu_count > 0);
1835 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
1836 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
1837 assert(!err);
1838 /* For tri demo we just grab the first physical device */
1839 demo->gpu = physical_devices[0];
1840 free(physical_devices);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001841
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001842 /* Look for validation layers */
1843 validation_found = 0;
1844 enabled_layer_count = 0;
1845 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001846 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001847 assert(!err);
1848
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001849 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001850 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001851 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001852
1853 if (demo->validate) {
1854 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1855 device_layer_count, device_layers);
1856 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001857 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001858 "a required validation layer.\n\n"
1859 "Please look at the Getting Started guide for additional "
1860 "information.\n",
1861 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001862 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001863 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001864 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001865
1866 uint32_t device_extension_count = 0;
1867 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001868 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001869 demo->gpu, NULL, &device_extension_count, NULL);
1870 assert(!err);
1871
Tony Barbourd9955e42015-10-08 13:59:42 -06001872 swapchainExtFound = 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001873 enabled_extension_count = 0;
1874 memset(extension_names, 0, sizeof(extension_names));
1875 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001876 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001877 demo->gpu, NULL, &device_extension_count, device_extensions);
1878 assert(!err);
1879
1880 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001881 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001882 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001883 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Ian Elliotte36b2082015-07-06 14:27:58 -06001884 }
1885 assert(enabled_extension_count < 64);
1886 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001887 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001888 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001889 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliotte36b2082015-07-06 14:27:58 -06001890 "Vulkan installable client driver (ICD) installed?\nPlease "
1891 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001892 "information.\n",
1893 "vkCreateInstance Failure");
1894 }
1895
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001896 VkDeviceCreateInfo device = {
1897 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1898 .pNext = NULL,
1899 .queueRecordCount = 1,
1900 .pRequestedQueues = &queue,
1901 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001902 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1903 .extensionCount = enabled_extension_count,
1904 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001905 };
1906
1907 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001908 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001909 if (!demo->dbgCreateMsgCallback) {
1910 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1911 "vkGetProcAddr Failure");
1912 }
1913 err = demo->dbgCreateMsgCallback(
1914 demo->inst,
1915 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1916 dbgFunc, NULL,
1917 &demo->msg_callback);
1918 switch (err) {
1919 case VK_SUCCESS:
1920 break;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001921 case VK_ERROR_OUT_OF_HOST_MEMORY:
1922 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1923 "dbgCreateMsgCallback Failure");
1924 break;
1925 default:
1926 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1927 "dbgCreateMsgCallback Failure");
1928 break;
1929 }
1930 }
1931
Ian Elliotte36b2082015-07-06 14:27:58 -06001932
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001933 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001934 assert(!err);
1935
Ian Elliott338dedb2015-08-21 15:09:33 -06001936 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
1937 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
1938 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
1939 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
1940 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1941 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1942 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
1943 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
1944 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
1945 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliott1b6de092015-06-22 15:07:49 -06001946
Tony Barbour426b9052015-06-24 16:06:58 -06001947 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tony Barbourc1098272015-10-23 10:53:30 -06001948 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001949
Cody Northropef72e2a2015-08-03 17:04:53 -06001950 // Query with NULL data to get count
1951 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001952 assert(!err);
1953
Cody Northropef72e2a2015-08-03 17:04:53 -06001954 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
1955 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001956 assert(!err);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001957 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001958
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001959 // Graphics queue and MemMgr queue can be separate.
1960 // TODO: Add support for separate queues, including synchronization,
1961 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001962}
1963
Tony Barbourd9955e42015-10-08 13:59:42 -06001964static void demo_init_vk_swapchain(struct demo *demo)
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001965{
Tony Barbour4a6692d2015-10-08 13:45:45 -06001966 VkResult U_ASSERT_ONLY err;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001967 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06001968
Tony Barbourd9955e42015-10-08 13:59:42 -06001969 // Construct the surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06001970 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001971 demo->surface_description.pNext = NULL;
1972#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06001973 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001974 demo->surface_description.pPlatformHandle = demo->connection;
1975 demo->surface_description.pPlatformWindow = demo->window;
1976#else // _WIN32
1977 demo->platform_handle_xcb.connection = demo->connection;
1978 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06001979 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001980 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
1981 demo->surface_description.pPlatformWindow = &demo->window;
1982#endif // _WIN32
1983
Tony Barbourd9955e42015-10-08 13:59:42 -06001984 // Iterate over each queue to learn whether it supports presenting:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001985 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
1986 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001987 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
1988 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06001989 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001990 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001991
1992 // Search for a graphics and a present queue in the array of queue
1993 // families, try to find one that supports both
1994 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
1995 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001996 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001997 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
1998 if (graphicsQueueNodeIndex == UINT32_MAX) {
1999 graphicsQueueNodeIndex = i;
2000 }
2001
2002 if (supportsPresent[i] == VK_TRUE) {
2003 graphicsQueueNodeIndex = i;
2004 presentQueueNodeIndex = i;
2005 break;
2006 }
2007 }
2008 }
2009 if (presentQueueNodeIndex == UINT32_MAX) {
2010 // If didn't find a queue that supports both graphics and present, then
2011 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002012 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002013 if (supportsPresent[i] == VK_TRUE) {
2014 presentQueueNodeIndex = i;
2015 break;
2016 }
2017 }
2018 }
2019 free(supportsPresent);
2020
2021 // Generate error if could not find both a graphics and a present queue
2022 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2023 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002024 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002025 }
2026
2027 // TODO: Add support for separate queues, including presentation,
2028 // synchronization, and appropriate tracking for QueueSubmit
2029 // While it is possible for an application to use a separate graphics and a
2030 // present queues, this demo program assumes it is only using one:
2031 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2032 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002033 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002034 }
2035
2036 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002037
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002038 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08002039 0, &demo->queue);
2040 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06002041
Ian Elliotte36b2082015-07-06 14:27:58 -06002042 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002043 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002044 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2045 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002046 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002047 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002048 VkSurfaceFormatKHR *surfFormats =
2049 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2050 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2051 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002052 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002053 assert(!err);
2054 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2055 // the surface has no preferred format. Otherwise, at least one
2056 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002057 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2058 {
2059 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2060 }
2061 else
2062 {
2063 assert(formatCount >= 1);
2064 demo->format = surfFormats[0].format;
2065 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002066 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002067
Mark Lobodzinski72346292015-07-02 16:49:40 -06002068 // Get Memory information and properties
2069 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2070 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002071}
2072
2073static void demo_init_connection(struct demo *demo)
2074{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002075#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002076 const xcb_setup_t *setup;
2077 xcb_screen_iterator_t iter;
2078 int scr;
2079
2080 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002081 if (demo->connection == NULL) {
2082 printf("Cannot find a compatible Vulkan installable client driver "
2083 "(ICD).\nExiting ...\n");
2084 fflush(stdout);
2085 exit(1);
2086 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002087
2088 setup = xcb_get_setup(demo->connection);
2089 iter = xcb_setup_roots_iterator(setup);
2090 while (scr-- > 0)
2091 xcb_screen_next(&iter);
2092
2093 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002094#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002095}
2096
Ian Elliotte14e9f92015-04-16 15:23:05 -06002097#ifdef _WIN32
2098static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2099#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002100static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002101#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002102{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002103 bool argv_error = false;
2104
Chia-I Wuc19795a2014-09-13 11:12:55 +08002105 memset(demo, 0, sizeof(*demo));
2106
Ian Elliotte14e9f92015-04-16 15:23:05 -06002107#ifdef _WIN32
2108 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002109 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002110
2111 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2112 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002113 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2114 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002115 else if (strlen(pCmdLine) != 0) {
2116 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2117 argv_error = true;
2118 }
2119#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002120 for (int i = 0; i < argc; i++) {
2121 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2122 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002123 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2124 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002125 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002126#endif // _WIN32
2127 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002128 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002129 fflush(stderr);
2130 exit(1);
2131 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002132
Chia-I Wuc19795a2014-09-13 11:12:55 +08002133 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002134 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002135
2136 demo->width = 300;
2137 demo->height = 300;
Tony Barbour4a6692d2015-10-08 13:45:45 -06002138 demo->depthStencil = 1.0;
2139 demo->depthIncrement = -0.01f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002140}
2141
2142static void demo_cleanup(struct demo *demo)
2143{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002144 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002145
Cody Northrop67ff0a42015-09-09 10:21:49 -06002146 demo->prepared = false;
2147
Tony Barbour5aabff52015-10-08 14:26:24 -06002148 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002149 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2150 }
Tony Barbour5aabff52015-10-08 14:26:24 -06002151 free(demo->framebuffers);
Tony Barbourde4124d2015-07-03 10:33:54 -06002152 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002153
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002154 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002155 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002156 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002157 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002158 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002159
Tony Barbourde4124d2015-07-03 10:33:54 -06002160 vkDestroyPipeline(demo->device, demo->pipeline);
2161 vkDestroyRenderPass(demo->device, demo->render_pass);
2162 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2163 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002164
Tony Barbourde4124d2015-07-03 10:33:54 -06002165 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002166 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002167
Chia-I Wub043fe32014-10-06 15:30:33 +08002168 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002169 vkDestroyImageView(demo->device, demo->textures[i].view);
2170 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002171 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002172 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08002173 }
2174
Ian Elliott338dedb2015-08-21 15:09:33 -06002175 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002176 vkDestroyImageView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002177 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002178
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002179 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbourde4124d2015-07-03 10:33:54 -06002180 vkDestroyImage(demo->device, demo->depth.image);
2181 vkFreeMemory(demo->device, demo->depth.mem);
2182
Ian Elliott338dedb2015-08-21 15:09:33 -06002183 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
Ian Elliotte36b2082015-07-06 14:27:58 -06002184 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002185
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002186 vkDestroyDevice(demo->device);
2187 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002188
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002189 free(demo->queue_props);
2190
Ian Elliotte14e9f92015-04-16 15:23:05 -06002191#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002192 xcb_destroy_window(demo->connection, demo->window);
2193 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002194 free(demo->atom_wm_delete_window);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002195#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002196}
2197
Ian Elliotte14e9f92015-04-16 15:23:05 -06002198#ifdef _WIN32
2199int APIENTRY WinMain(HINSTANCE hInstance,
2200 HINSTANCE hPrevInstance,
2201 LPSTR pCmdLine,
2202 int nCmdShow)
2203{
2204 MSG msg; // message
2205 bool done; // flag saying when app is complete
2206
2207 demo_init(&demo, hInstance, pCmdLine);
2208 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002209 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002210
2211 demo_prepare(&demo);
2212
2213 done = false; //initialize loop condition variable
2214 /* main message loop*/
2215 while(!done)
2216 {
Ian Elliott421107f2015-04-28 15:50:36 -06002217 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002218 if (msg.message == WM_QUIT) //check for a quit message
2219 {
2220 done = true; //if found, quit app
2221 }
2222 else
2223 {
2224 /* Translate and dispatch to event queue*/
2225 TranslateMessage(&msg);
2226 DispatchMessage(&msg);
2227 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002228 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002229 }
2230
2231 demo_cleanup(&demo);
2232
Tony Barboura938abb2015-04-22 11:36:22 -06002233 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002234}
2235#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002236int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002237{
2238 struct demo demo;
2239
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002240 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002241 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002242 demo_init_vk_swapchain(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002243
2244 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002245 demo_run(&demo);
2246
2247 demo_cleanup(&demo);
2248
Chia-I Wuc19795a2014-09-13 11:12:55 +08002249 return 0;
2250}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002251#endif // _WIN32