blob: e1035fb3254acc758a144e7ff096374dbfd36d20 [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;
Chia-I Wu1f851912015-10-27 18:04:07 +0800151 VkCommandBuffer 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
Chia-I Wu1f851912015-10-27 18:04:07 +0800172 VkAllocationCallbacks allocator;
Chia-I Wu69f40122015-10-26 21:10:41 +0800173
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600174 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600175 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600176 VkDevice device;
177 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600178 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600179 VkQueueFamilyProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700180 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800181
182 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600183 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600184 VkColorSpaceKHR color_space;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800185
Ian Elliott338dedb2015-08-21 15:09:33 -0600186 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
187 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
188 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
189 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
190 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
191 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
192 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
193 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
194 PFN_vkQueuePresentKHR fpQueuePresentKHR;
195 VkSurfaceDescriptionWindowKHR surface_description;
196 uint32_t swapchainImageCount;
Ian Elliotte2688a52015-10-16 18:02:43 -0600197 VkSwapchainKHR swapchain;
Ian Elliott338dedb2015-08-21 15:09:33 -0600198 SwapchainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800199
Chia-I Wu1f851912015-10-27 18:04:07 +0800200 VkCommandPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800201
Chia-I Wub043fe32014-10-06 15:30:33 +0800202 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800204
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600205 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500206 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600207 VkImageView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800208 } depth;
209
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600210 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800211
Chia-I Wu99621bc2014-10-08 11:52:22 +0800212 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600213 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500214 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800215
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600216 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600217 VkVertexInputBindingDescription vi_bindings[1];
218 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800219 } vertices;
220
Chia-I Wu1f851912015-10-27 18:04:07 +0800221 VkCommandBuffer setup_cmd; // Command Buffer for initialization commands
222 VkCommandBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500223 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600224 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600225 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800226 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600227 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800228
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -0600229 VkShaderModule vert_shader_module;
230 VkShaderModule frag_shader_module;
231
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600232 VkDescriptorPool desc_pool;
233 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800234
Tony Barbour5aabff52015-10-08 14:26:24 -0600235 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800236
Mark Lobodzinski72346292015-07-02 16:49:40 -0600237 VkPhysicalDeviceMemoryProperties memory_properties;
238
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600239 bool validate;
240 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Ian Elliotte36b2082015-07-06 14:27:58 -0600241 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600242 VkDbgMsgCallback msg_callback;
243
Tony Barbour4a6692d2015-10-08 13:45:45 -0600244 float depthStencil;
245 float depthIncrement;
246
Chia-I Wuc19795a2014-09-13 11:12:55 +0800247 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600248 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600249 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800250};
251
Ian Elliotte2688a52015-10-16 18:02:43 -0600252// Forward declaration:
253static void demo_resize(struct demo *demo);
254
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600255static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinski72346292015-07-02 16:49:40 -0600256{
257 // Search memtypes to find first index with those properties
258 for (uint32_t i = 0; i < 32; i++) {
259 if ((typeBits & 1) == 1) {
260 // Type is available, does it match user properties?
Tony Barbourf1eceb92015-10-15 12:42:56 -0600261 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinski72346292015-07-02 16:49:40 -0600262 *typeIndex = i;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600263 return true;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600264 }
265 }
266 typeBits >>= 1;
267 }
268 // No memory types matched, return failure
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600269 return false;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600270}
271
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600272static void demo_flush_init_cmd(struct demo *demo)
273{
Tony Barbour22a30862015-04-22 09:02:32 -0600274 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600276 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600277 return;
278
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600279 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600280 assert(!err);
281
Chia-I Wu1f851912015-10-27 18:04:07 +0800282 const VkCommandBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600283 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600284 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800285 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
286 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800287 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600288 .pWaitSemaphores = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800289 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600290 .pCommandBuffers = cmd_bufs,
Chia-I Wu763a7492015-10-26 20:48:51 +0800291 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600292 .pSignalSemaphores = NULL
293 };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600294
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600295 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600296 assert(!err);
297
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600298 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600299 assert(!err);
300
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600301 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600302 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600303}
304
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600305static void demo_set_image_layout(
306 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600307 VkImage image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600308 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600309 VkImageLayout old_image_layout,
310 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600311{
Tony Barbour22a30862015-04-22 09:02:32 -0600312 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600313
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600314 if (demo->setup_cmd == VK_NULL_HANDLE) {
Chia-I Wu1f851912015-10-27 18:04:07 +0800315 const VkCommandBufferAllocateInfo cmd = {
316 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600317 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +0800318 .commandPool = demo->cmd_pool,
319 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +0800320 .bufferCount = 1,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600321 };
322
Chia-I Wu1f851912015-10-27 18:04:07 +0800323 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600324 assert(!err);
325
Chia-I Wu1f851912015-10-27 18:04:07 +0800326 VkCommandBufferBeginInfo cmd_buf_info = {
327 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600328 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600329 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800330 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600331 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800332 .framebuffer = VK_NULL_HANDLE,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600333 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600334 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Tony Barbourc1098272015-10-23 10:53:30 -0600335 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600336 }
337
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600338 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600339 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600340 .pNext = NULL,
341 .outputMask = 0,
342 .inputMask = 0,
343 .oldLayout = old_image_layout,
344 .newLayout = new_image_layout,
345 .image = image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600346 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600347 };
348
Chia-I Wu1f851912015-10-27 18:04:07 +0800349 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600350 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600351 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600352 }
353
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600354 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600355 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600356 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600357 }
358
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600359 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600360
Tony Barbourc2e987e2015-06-29 16:20:35 -0600361 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
362 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600363
Chia-I Wu3ffcd732015-10-26 17:08:33 +0800364 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600365}
366
Chia-I Wuc19795a2014-09-13 11:12:55 +0800367static void demo_draw_build_cmd(struct demo *demo)
368{
Chia-I Wu1f851912015-10-27 18:04:07 +0800369 const VkCommandBufferBeginInfo cmd_buf_info = {
370 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600371 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600372 .flags = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800373 .renderPass = VK_NULL_HANDLE,
Cody Northrop16898b02015-08-11 11:35:58 -0600374 .subpass = 0,
Chia-I Wue420a332015-10-26 20:04:44 +0800375 .framebuffer = VK_NULL_HANDLE,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700376 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800377 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600378 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
Tony Barbour4a6692d2015-10-08 13:45:45 -0600379 [1] = { .depthStencil = { demo->depthStencil, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800380 };
381 const VkRenderPassBeginInfo rp_begin = {
382 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
383 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800384 .renderPass = demo->render_pass,
385 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800386 .renderArea.offset.x = 0,
387 .renderArea.offset.y = 0,
388 .renderArea.extent.width = demo->width,
389 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600390 .clearValueCount = 2,
391 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700392 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800393 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800394
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600395 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800396 assert(!err);
397
Chia-I Wuc51b1212015-10-27 19:25:11 +0800398 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600399 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800400 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600401 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600402 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800403
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600404 VkViewport viewport;
405 memset(&viewport, 0, sizeof(viewport));
406 viewport.height = (float) demo->height;
407 viewport.width = (float) demo->width;
408 viewport.minDepth = (float) 0.0f;
409 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600410 vkCmdSetViewport(demo->draw_cmd, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600411
412 VkRect2D scissor;
413 memset(&scissor, 0, sizeof(scissor));
414 scissor.extent.width = demo->width;
415 scissor.extent.height = demo->height;
416 scissor.offset.x = 0;
417 scissor.offset.y = 0;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600418 vkCmdSetScissor(demo->draw_cmd, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600419
Tony Barbour8205d902015-04-16 15:59:00 -0600420 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600421 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800422
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600423 vkCmdDraw(demo->draw_cmd, 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800424 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800425
Tony Barbour40292ae2015-10-21 10:14:48 -0600426 VkImageMemoryBarrier prePresentBarrier = {
427 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
428 .pNext = NULL,
429 .outputMask = VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT,
430 .inputMask = 0,
431 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
432 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
433 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wu1f851912015-10-27 18:04:07 +0800434 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600435 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
436 };
437
438 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
439 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
440 vkCmdPipelineBarrier(demo->draw_cmd, VK_PIPELINE_STAGE_ALL_GPU_COMMANDS, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu3ffcd732015-10-26 17:08:33 +0800441 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour40292ae2015-10-21 10:14:48 -0600442
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600443 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800444 assert(!err);
445}
446
447static void demo_draw(struct demo *demo)
448{
Tony Barbour22a30862015-04-22 09:02:32 -0600449 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600450 VkSemaphore presentCompleteSemaphore;
451 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
452 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
453 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600454 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600455 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800456
Ian Elliotte36b2082015-07-06 14:27:58 -0600457 err = vkCreateSemaphore(demo->device,
458 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +0800459 NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600460 &presentCompleteSemaphore);
461 assert(!err);
462
463 // Get the index of the next available swapchain image:
Ian Elliotte2688a52015-10-16 18:02:43 -0600464 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600465 UINT64_MAX,
466 presentCompleteSemaphore,
467 &demo->current_buffer);
Ian Elliotte2688a52015-10-16 18:02:43 -0600468 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
469 // demo->swapchain is out of date (e.g. the window was resized) and
470 // must be recreated:
471 demo_resize(demo);
472 demo_draw(demo);
Chia-I Wu69f40122015-10-26 21:10:41 +0800473 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -0600474 return;
475 } else if (err == VK_SUBOPTIMAL_KHR) {
476 // demo->swapchain is not as optimal as it could be, but the platform's
477 // presentation engine will still present the image correctly.
478 } else {
479 assert(!err);
480 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600481
Tony Barbour40292ae2015-10-21 10:14:48 -0600482 // Assume the command buffer has been run on current_buffer before so
483 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
484 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
485 VK_IMAGE_ASPECT_COLOR_BIT,
486 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
487 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
488 demo_flush_init_cmd(demo);
489
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600490 // Wait for the present complete semaphore to be signaled to ensure
491 // that the image won't be rendered to until the presentation
492 // engine has fully released ownership to the application, and it is
493 // okay to render to the image.
494
Ian Elliott338dedb2015-08-21 15:09:33 -0600495// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Chia-I Wuc19795a2014-09-13 11:12:55 +0800496 demo_draw_build_cmd(demo);
Chia-I Wue420a332015-10-26 20:04:44 +0800497 VkFence nullFence = VK_NULL_HANDLE;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800498
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600499 VkSubmitInfo submit_info = {
Chia-I Wu6ec33a02015-10-26 20:37:06 +0800500 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
501 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +0800502 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600503 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu763a7492015-10-26 20:48:51 +0800504 .commandBufferCount = 1,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600505 .pCommandBuffers = &demo->draw_cmd,
Chia-I Wu763a7492015-10-26 20:48:51 +0800506 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600507 .pSignalSemaphores = NULL
508 };
509
510 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800511 assert(!err);
512
Ian Elliott338dedb2015-08-21 15:09:33 -0600513 VkPresentInfoKHR present = {
514 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600515 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600516 .swapchainCount = 1,
Ian Elliotte2688a52015-10-16 18:02:43 -0600517 .swapchains = &demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600518 .imageIndices = &demo->current_buffer,
519 };
520
521// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600522 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliotte2688a52015-10-16 18:02:43 -0600523 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
524 // demo->swapchain is out of date (e.g. the window was resized) and
525 // must be recreated:
526 demo_resize(demo);
527 } else if (err == VK_SUBOPTIMAL_KHR) {
528 // demo->swapchain is not as optimal as it could be, but the platform's
529 // presentation engine will still present the image correctly.
530 } else {
531 assert(!err);
532 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800533
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800534 err = vkQueueWaitIdle(demo->queue);
535 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600536
Chia-I Wu69f40122015-10-26 21:10:41 +0800537 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800538}
539
540static void demo_prepare_buffers(struct demo *demo)
541{
Ian Elliotte36b2082015-07-06 14:27:58 -0600542 VkResult U_ASSERT_ONLY err;
Ian Elliotte2688a52015-10-16 18:02:43 -0600543 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600544
545 // Check the surface proprties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600546 VkSurfacePropertiesKHR surfProperties;
547 err = demo->fpGetSurfacePropertiesKHR(demo->device,
548 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600549 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600550 assert(!err);
551
Ian Elliott7fe115d2015-08-07 15:56:59 -0600552 uint32_t presentModeCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600553 err = demo->fpGetSurfacePresentModesKHR(demo->device,
554 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600555 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600556 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600557 VkPresentModeKHR *presentModes =
558 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600559 assert(presentModes);
Ian Elliott338dedb2015-08-21 15:09:33 -0600560 err = demo->fpGetSurfacePresentModesKHR(demo->device,
561 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600562 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600563 assert(!err);
564
Ian Elliott338dedb2015-08-21 15:09:33 -0600565 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600566 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600567 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600568 {
569 // If the surface size is undefined, the size is set to
570 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600571 swapchainExtent.width = demo->width;
572 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600573 }
574 else
575 {
576 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -0600577 swapchainExtent = surfProperties.currentExtent;
Ian Elliotte2688a52015-10-16 18:02:43 -0600578 demo->width = surfProperties.currentExtent.width;
579 demo->height = surfProperties.currentExtent.height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600580 }
581
Ian Elliott338dedb2015-08-21 15:09:33 -0600582 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600583
584 // Determine the number of VkImage's to use in the swap chain (we desire to
585 // own only 1 image at a time, besides the images being displayed and
586 // queued for display):
Ian Elliott338dedb2015-08-21 15:09:33 -0600587 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600588 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600589 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600590 {
591 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600592 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600593 }
594
Ian Elliott338dedb2015-08-21 15:09:33 -0600595 VkSurfaceTransformKHR preTransform;
596 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
597 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600598 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600599 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600600 }
601
Ian Elliotte2688a52015-10-16 18:02:43 -0600602 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600603 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800604 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600605 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
606 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800607 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600608 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800609 .imageExtent = {
Ian Elliott338dedb2015-08-21 15:09:33 -0600610 .width = swapchainExtent.width,
611 .height = swapchainExtent.height,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800612 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600613 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600614 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800615 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600616 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
617 .queueFamilyCount = 0,
618 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600619 .presentMode = swapchainPresentMode,
Ian Elliotte2688a52015-10-16 18:02:43 -0600620 .oldSwapchain = oldSwapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600621 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800622 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600623 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800624
Ian Elliotte2688a52015-10-16 18:02:43 -0600625 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800626 assert(!err);
627
Ian Elliotte2688a52015-10-16 18:02:43 -0600628 // If we just re-created an existing swapchain, we should destroy the old
629 // swapchain at this point.
630 // Note: destroying the swapchain also cleans up all its associated
631 // presentable images once the platform is done with them.
Chia-I Wue420a332015-10-26 20:04:44 +0800632 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliotte2688a52015-10-16 18:02:43 -0600633 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
634 }
635
636 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600637 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600638 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800639
Ian Elliott338dedb2015-08-21 15:09:33 -0600640 VkImage* swapchainImages =
641 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
642 assert(swapchainImages);
Ian Elliotte2688a52015-10-16 18:02:43 -0600643 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600644 &demo->swapchainImageCount,
645 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600646 assert(!err);
647
Ian Elliott338dedb2015-08-21 15:09:33 -0600648 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600649 assert(demo->buffers);
650
Ian Elliott338dedb2015-08-21 15:09:33 -0600651 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600652 VkImageViewCreateInfo color_attachment_view = {
653 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800654 .pNext = NULL,
655 .format = demo->format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600656 .channels = {
Chia-I Wuc51b1212015-10-27 19:25:11 +0800657 .r = VK_COMPONENT_SWIZZLE_R,
658 .g = VK_COMPONENT_SWIZZLE_G,
659 .b = VK_COMPONENT_SWIZZLE_B,
660 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600661 },
662 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600663 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600664 .baseMipLevel = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800665 .levelCount = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600666 .baseArrayLayer = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800667 .layerCount = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600668 },
669 .viewType = VK_IMAGE_VIEW_TYPE_2D,
670 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800671 };
672
Ian Elliott338dedb2015-08-21 15:09:33 -0600673 demo->buffers[i].image = swapchainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500674
Tony Barbour40292ae2015-10-21 10:14:48 -0600675 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
676 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600677 demo_set_image_layout(demo, demo->buffers[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600678 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600679 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600680 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600681
Chia-I Wuc19795a2014-09-13 11:12:55 +0800682 color_attachment_view.image = demo->buffers[i].image;
683
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600684 err = vkCreateImageView(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +0800685 &color_attachment_view, NULL, &demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800686 assert(!err);
687 }
Piers Daniell886be472015-02-23 16:23:13 -0700688
689 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800690}
691
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800692static void demo_prepare_depth(struct demo *demo)
693{
Tony Barbour8205d902015-04-16 15:59:00 -0600694 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600695 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600696 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800697 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600698 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800699 .format = depth_format,
700 .extent = { demo->width, demo->height, 1 },
701 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600702 .arrayLayers = 1,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800703 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600704 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600705 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800706 .flags = 0,
707 };
Chia-I Wu1f851912015-10-27 18:04:07 +0800708 VkMemoryAllocateInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600709 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500710 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800711 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600712 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800713 };
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600714 VkImageViewCreateInfo view = {
715 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800716 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +0800717 .image = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600718 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600719 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600720 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600721 .baseMipLevel = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800722 .levelCount = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600723 .baseArrayLayer = 0,
Chia-I Wu1f851912015-10-27 18:04:07 +0800724 .layerCount = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600725 },
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800726 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600727 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800728 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700729
Mark Lobodzinski23182612015-05-29 09:32:35 -0500730 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600731 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600732 bool U_ASSERT_ONLY pass;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800733
734 demo->depth.format = depth_format;
735
736 /* create image */
Chia-I Wu69f40122015-10-26 21:10:41 +0800737 err = vkCreateImage(demo->device, &image, NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800738 &demo->depth.image);
739 assert(!err);
740
Mark Lobodzinski72346292015-07-02 16:49:40 -0600741 /* get memory requirements for this object */
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600742 vkGetImageMemoryRequirements(demo->device, demo->depth.image,
Tony Barbourde4124d2015-07-03 10:33:54 -0600743 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600744
745 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500746 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600747 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600748 mem_reqs.memoryTypeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600749 0, /* No requirements */
Mark Lobodzinski72346292015-07-02 16:49:40 -0600750 &mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600751 assert(pass);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800752
Mark Lobodzinski23182612015-05-29 09:32:35 -0500753 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +0800754 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500755 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800756
Mark Lobodzinski23182612015-05-29 09:32:35 -0500757 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600758 err = vkBindImageMemory(demo->device, demo->depth.image,
759 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500760 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800761
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600762 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600763 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600764 VK_IMAGE_LAYOUT_UNDEFINED,
765 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600766
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800767 /* create image view */
768 view.image = demo->depth.image;
Chia-I Wu69f40122015-10-26 21:10:41 +0800769 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800770 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800771}
772
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700773static void demo_prepare_texture_image(struct demo *demo,
774 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600775 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600776 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600777 VkImageUsageFlags usage,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600778 VkFlags required_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800779{
Tony Barbour8205d902015-04-16 15:59:00 -0600780 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600781 const int32_t tex_width = 2;
782 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600783 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600784 bool U_ASSERT_ONLY pass;
Chia-I Wub043fe32014-10-06 15:30:33 +0800785
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600786 tex_obj->tex_width = tex_width;
787 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700788
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600789 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600790 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700791 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600792 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700793 .format = tex_format,
794 .extent = { tex_width, tex_height, 1 },
795 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600796 .arrayLayers = 1,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700797 .samples = 1,
798 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600799 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700800 .flags = 0,
801 };
Chia-I Wu1f851912015-10-27 18:04:07 +0800802 VkMemoryAllocateInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600803 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500804 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700805 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600806 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700807 };
808
Mark Lobodzinski23182612015-05-29 09:32:35 -0500809 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700810
Chia-I Wu69f40122015-10-26 21:10:41 +0800811 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600812 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700813 assert(!err);
814
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600815 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600816
817 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600818 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &mem_alloc.memoryTypeIndex);
819 assert(pass);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700820
Mark Lobodzinski23182612015-05-29 09:32:35 -0500821 /* allocate memory */
Chia-I Wu1f851912015-10-27 18:04:07 +0800822 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &tex_obj->mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500823 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700824
Mark Lobodzinski23182612015-05-29 09:32:35 -0500825 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600826 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500827 tex_obj->mem, 0);
828 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700829
Tony Barbourf1eceb92015-10-15 12:42:56 -0600830 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600831 const VkImageSubresource subres = {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600832 .aspect = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700833 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600834 .arrayLayer = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700835 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600836 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700837 void *data;
838 int32_t x, y;
839
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600840 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700841
Dominik Witczake82d5b62015-09-22 18:25:33 +0200842 err = vkMapMemory(demo->device, tex_obj->mem, 0, mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700843 assert(!err);
844
845 for (y = 0; y < tex_height; y++) {
846 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
847 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700848 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700849 }
850
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600851 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700852 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600853
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600854 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600855 demo_set_image_layout(demo, tex_obj->image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600856 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600857 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600858 tex_obj->imageLayout);
859 /* 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 -0700860}
861
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500862static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700863{
864 /* clean up staging resources */
Chia-I Wu69f40122015-10-26 21:10:41 +0800865 vkDestroyImage(demo->device, tex_obj->image, NULL);
866 vkFreeMemory(demo->device, tex_obj->mem, NULL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700867}
868
869static void demo_prepare_textures(struct demo *demo)
870{
Tony Barbour8205d902015-04-16 15:59:00 -0600871 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600872 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700873 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
874 { 0xffff0000, 0xff00ff00 },
875 };
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700876 uint32_t i;
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600877 VkResult err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700878
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600879 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700880
881 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600882 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700883 /* Device can texture using linear textures */
884 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600885 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600886 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700887 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600888 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700889
890 memset(&staging_texture, 0, sizeof(staging_texture));
891 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Chia-I Wu1f851912015-10-27 18:04:07 +0800892 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700893
894 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600895 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wu1f851912015-10-27 18:04:07 +0800896 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wu27d8ed92015-10-27 18:53:22 +0800897 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700898
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600899 demo_set_image_layout(demo, staging_texture.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600900 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600901 staging_texture.imageLayout,
Chia-I Wu1f851912015-10-27 18:04:07 +0800902 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700903
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600904 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600905 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600906 demo->textures[i].imageLayout,
Chia-I Wu1f851912015-10-27 18:04:07 +0800907 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700908
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600909 VkImageCopy copy_region = {
Tony Barbourca5c4eb2015-11-02 11:47:51 -0700910 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700911 .srcOffset = { 0, 0, 0 },
Chia-I Wu1f851912015-10-27 18:04:07 +0800912 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
913 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700914 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
915 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600916 vkCmdCopyImage(demo->setup_cmd,
Chia-I Wu1f851912015-10-27 18:04:07 +0800917 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
918 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600919 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700920
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600921 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600922 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wu1f851912015-10-27 18:04:07 +0800923 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600924 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700925
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600926 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700927
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600928 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700929 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600930 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700931 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700932 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700933
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600934 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600935 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800936 .pNext = NULL,
Chia-I Wu3603b082015-10-26 16:49:32 +0800937 .magFilter = VK_FILTER_NEAREST,
938 .minFilter = VK_FILTER_NEAREST,
939 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wuce532f72015-10-26 17:32:47 +0800940 .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
941 .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
942 .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
Chia-I Wub043fe32014-10-06 15:30:33 +0800943 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700944 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600945 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800946 .minLod = 0.0f,
947 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600948 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600949 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800950 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600951 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600952 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800953 .pNext = NULL,
Chia-I Wue420a332015-10-26 20:04:44 +0800954 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600955 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700956 .format = tex_format,
Chia-I Wuc51b1212015-10-27 19:25:11 +0800957 .channels = { VK_COMPONENT_SWIZZLE_R,
958 VK_COMPONENT_SWIZZLE_G,
959 VK_COMPONENT_SWIZZLE_B,
960 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -0600961 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600962 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +0800963 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700964
Chia-I Wub043fe32014-10-06 15:30:33 +0800965 /* create sampler */
Chia-I Wu69f40122015-10-26 21:10:41 +0800966 err = vkCreateSampler(demo->device, &sampler, NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800967 &demo->textures[i].sampler);
968 assert(!err);
969
Chia-I Wub043fe32014-10-06 15:30:33 +0800970 /* create image view */
971 view.image = demo->textures[i].image;
Chia-I Wu69f40122015-10-26 21:10:41 +0800972 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700973 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800974 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800975 }
976}
977
Chia-I Wu99621bc2014-10-08 11:52:22 +0800978static void demo_prepare_vertices(struct demo *demo)
979{
980 const float vb[3][5] = {
981 /* position texcoord */
Cody Northropa9efa052015-10-13 11:28:23 -0600982 { -1.0f, -1.0f, 0.25f, 0.0f, 0.0f },
Chia-I Wue2504cb2015-04-22 14:20:52 +0800983 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800984 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
985 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600986 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600987 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800988 .pNext = NULL,
989 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600990 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800991 .flags = 0,
992 };
Chia-I Wu1f851912015-10-27 18:04:07 +0800993 VkMemoryAllocateInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600994 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500995 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800996 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600997 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800998 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500999 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -06001000 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001001 bool U_ASSERT_ONLY pass;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001002 void *data;
1003
1004 memset(&demo->vertices, 0, sizeof(demo->vertices));
1005
Chia-I Wu69f40122015-10-26 21:10:41 +08001006 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001007 assert(!err);
1008
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001009 vkGetBufferMemoryRequirements(demo->device,
Tony Barbourde4124d2015-07-03 10:33:54 -06001010 demo->vertices.buf, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -06001011 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001012
Mark Lobodzinski72346292015-07-02 16:49:40 -06001013 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001014 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001015 mem_reqs.memoryTypeBits,
1016 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1017 &mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001018 assert(pass);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001019
Chia-I Wu1f851912015-10-27 18:04:07 +08001020 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001021 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001022
Dominik Witczake82d5b62015-09-22 18:25:33 +02001023 err = vkMapMemory(demo->device, demo->vertices.mem, 0, mem_alloc.allocationSize, 0, &data);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001024 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001025
Mark Lobodzinski23182612015-05-29 09:32:35 -05001026 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +08001027
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001028 vkUnmapMemory(demo->device, demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001029
Tony Barbourde4124d2015-07-03 10:33:54 -06001030 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001031 demo->vertices.mem, 0);
1032 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001033
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001034 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001035 demo->vertices.vi.pNext = NULL;
Chia-I Wu763a7492015-10-26 20:48:51 +08001036 demo->vertices.vi.vertexBindingDescriptionCount = 1;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001037 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
Chia-I Wu763a7492015-10-26 20:48:51 +08001038 demo->vertices.vi.vertexAttributeDescriptionCount = 2;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001039 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
1040
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001041 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu7e470702015-10-26 17:24:52 +08001042 demo->vertices.vi_bindings[0].stride = sizeof(vb[0]);
Chia-I Wuc51b1212015-10-27 19:25:11 +08001043 demo->vertices.vi_bindings[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001044
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001045 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
1046 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -06001047 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu7e470702015-10-26 17:24:52 +08001048 demo->vertices.vi_attrs[0].offset = 0;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001049
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001050 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
1051 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -06001052 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu7e470702015-10-26 17:24:52 +08001053 demo->vertices.vi_attrs[1].offset = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001054}
1055
Chia-I Wuf8385062015-01-04 16:27:24 +08001056static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +08001057{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001058 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001059 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001060 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001061 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001062 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +08001063 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001064 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001065 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001066 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001067 .bindingCount = 1,
1068 .pBindings = &layout_binding,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001069 };
Tony Barbour22a30862015-04-22 09:02:32 -06001070 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +08001071
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001072 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001073 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001074 assert(!err);
1075
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001076 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1077 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1078 .pNext = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001079 .setLayoutCount = 1,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001080 .pSetLayouts = &demo->desc_layout,
1081 };
1082
1083 err = vkCreatePipelineLayout(demo->device,
1084 &pPipelineLayoutCreateInfo,
Chia-I Wu69f40122015-10-26 21:10:41 +08001085 NULL,
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001086 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001087 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001088}
1089
Chia-I Wu76cd4222015-07-08 13:34:24 +08001090static void demo_prepare_render_pass(struct demo *demo)
1091{
Chia-I Wuc278df82015-07-07 11:50:03 +08001092 const VkAttachmentDescription attachments[2] = {
1093 [0] = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001094 .format = demo->format,
1095 .samples = 1,
1096 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1097 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1098 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1099 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1100 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1101 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1102 },
1103 [1] = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001104 .format = demo->depth.format,
1105 .samples = 1,
1106 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1107 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1108 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1109 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1110 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1111 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1112 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001113 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001114 const VkAttachmentReference color_reference = {
1115 .attachment = 0,
1116 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1117 };
Chia-I Wuce532f72015-10-26 17:32:47 +08001118 const VkAttachmentReference depth_reference = {
1119 .attachment = 1,
1120 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1121 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001122 const VkSubpassDescription subpass = {
Chia-I Wuc278df82015-07-07 11:50:03 +08001123 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1124 .flags = 0,
Chia-I Wu763a7492015-10-26 20:48:51 +08001125 .inputAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001126 .pInputAttachments = NULL,
Chia-I Wu763a7492015-10-26 20:48:51 +08001127 .colorAttachmentCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001128 .pColorAttachments = &color_reference,
1129 .pResolveAttachments = NULL,
Chia-I Wuce532f72015-10-26 17:32:47 +08001130 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu763a7492015-10-26 20:48:51 +08001131 .preserveAttachmentCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001132 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001133 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001134 const VkRenderPassCreateInfo rp_info = {
1135 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1136 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001137 .attachmentCount = 2,
1138 .pAttachments = attachments,
1139 .subpassCount = 1,
1140 .pSubpasses = &subpass,
1141 .dependencyCount = 0,
1142 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001143 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001144 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001145
Chia-I Wu69f40122015-10-26 21:10:41 +08001146 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001147 assert(!err);
1148}
1149
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001150static VkShader demo_prepare_shader(struct demo *demo,
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001151 VkShaderStageFlagBits stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001152 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001153 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001154 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001155{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001156 VkShaderModuleCreateInfo moduleCreateInfo;
1157 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001158 VkShader shader;
Tony Barbour4a6692d2015-10-08 13:45:45 -06001159 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001160
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001161
1162 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1163 moduleCreateInfo.pNext = NULL;
1164
1165 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1166 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001167
Cody Northrop75db0322015-05-28 11:27:16 -06001168 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001169 moduleCreateInfo.codeSize = size;
1170 moduleCreateInfo.pCode = code;
1171 moduleCreateInfo.flags = 0;
Chia-I Wu69f40122015-10-26 21:10:41 +08001172 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001173 assert(!err);
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001174
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001175 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001176 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001177 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001178 shaderCreateInfo.stage = stage;
Chia-I Wu69f40122015-10-26 21:10:41 +08001179 err = vkCreateShader(demo->device, &shaderCreateInfo, NULL, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001180 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001181 } else {
1182 // Create fake SPV structure to feed GLSL
1183 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001184 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1185 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1186 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001187
Cody Northrop75db0322015-05-28 11:27:16 -06001188 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001189 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1190 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1191 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1192 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001193
Chia-I Wu69f40122015-10-26 21:10:41 +08001194 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001195 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001196
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001197 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001198 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001199 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001200 shaderCreateInfo.stage = stage;
Chia-I Wu69f40122015-10-26 21:10:41 +08001201 err = vkCreateShader(demo->device, &shaderCreateInfo, NULL, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001202 assert(!err);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001203 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001204 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001205 return shader;
1206}
1207
Cody Northrop75db0322015-05-28 11:27:16 -06001208char *demo_read_spv(const char *filename, size_t *psize)
1209{
1210 long int size;
1211 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001212 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001213
1214 FILE *fp = fopen(filename, "rb");
1215 if (!fp) return NULL;
1216
1217 fseek(fp, 0L, SEEK_END);
1218 size = ftell(fp);
1219
1220 fseek(fp, 0L, SEEK_SET);
1221
1222 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001223 retVal = fread(shader_code, size, 1, fp);
1224 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001225
1226 *psize = size;
1227
Mike Stroyan85180252015-10-28 11:15:46 -06001228 fclose(fp);
Cody Northrop75db0322015-05-28 11:27:16 -06001229 return shader_code;
1230}
1231
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001232static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001233{
Cody Northrop75db0322015-05-28 11:27:16 -06001234 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001235 VkShader shader;
1236 void *vertShaderCode;
1237 size_t size;
Cody Northrop75db0322015-05-28 11:27:16 -06001238
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001239 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001240
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001241 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT,
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001242 &demo->vert_shader_module,
1243 vertShaderCode, size);
1244 free(vertShaderCode);
1245 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001246 } else {
1247 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001248 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001249 "#extension GL_ARB_separate_shader_objects : enable\n"
1250 "#extension GL_ARB_shading_language_420pack : enable\n"
1251 "layout (location = 0) in vec4 pos;\n"
1252 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001253 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001254 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001255 " texcoord = attr;\n"
1256 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001257 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001258
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001259 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001260 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001261 (const void *) vertShaderText,
1262 strlen(vertShaderText));
1263 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001264}
1265
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001266static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001267{
Cody Northrop75db0322015-05-28 11:27:16 -06001268 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001269 VkShader shader;
1270 void *fragShaderCode;
1271 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001272
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001273 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001274
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001275 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT,
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001276 &demo->frag_shader_module,
1277 fragShaderCode, size);
1278
1279 free(fragShaderCode);
1280 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001281 } else {
1282 static const char *fragShaderText =
1283 "#version 140\n"
1284 "#extension GL_ARB_separate_shader_objects : enable\n"
1285 "#extension GL_ARB_shading_language_420pack : enable\n"
1286 "layout (binding = 0) uniform sampler2D tex;\n"
1287 "layout (location = 0) in vec2 texcoord;\n"
1288 "layout (location = 0) out vec4 uFragColor;\n"
1289 "void main() {\n"
1290 " uFragColor = texture(tex, texcoord);\n"
1291 "}\n";
1292
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001293 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001294 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001295 (const void *) fragShaderText,
1296 strlen(fragShaderText));
1297 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001298}
1299
1300static void demo_prepare_pipeline(struct demo *demo)
1301{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001302 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001303 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001304
Tony Barboure307f582015-07-10 15:29:03 -06001305 VkPipelineVertexInputStateCreateInfo vi;
1306 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wu1f851912015-10-27 18:04:07 +08001307 VkPipelineRasterizationStateCreateInfo rs;
Tony Barboure307f582015-07-10 15:29:03 -06001308 VkPipelineColorBlendStateCreateInfo cb;
1309 VkPipelineDepthStencilStateCreateInfo ds;
1310 VkPipelineViewportStateCreateInfo vp;
1311 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wu1f851912015-10-27 18:04:07 +08001312 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniell811eb742015-09-29 13:01:09 -06001313 VkPipelineDynamicStateCreateInfo dynamicState;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001314
Tony Barbour22a30862015-04-22 09:02:32 -06001315 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001316
Piers Daniell811eb742015-09-29 13:01:09 -06001317 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1318 memset(&dynamicState, 0, sizeof dynamicState);
1319 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1320 dynamicState.pDynamicStates = dynamicStateEnables;
1321
Chia-I Wuc19795a2014-09-13 11:12:55 +08001322 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001323 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001324 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001325
Chia-I Wu8d29d022014-10-08 12:14:39 +08001326 vi = demo->vertices.vi;
1327
Chia-I Wuc19795a2014-09-13 11:12:55 +08001328 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001329 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001330 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001331
1332 memset(&rs, 0, sizeof(rs));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001333 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1334 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wuce532f72015-10-26 17:32:47 +08001335 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wu1f851912015-10-27 18:04:07 +08001336 rs.frontFace = VK_FRONT_FACE_CLOCKWISE;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001337 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001338 rs.rasterizerDiscardEnable = VK_FALSE;
1339 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001340
1341 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001342 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1343 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001344 memset(att_state, 0, sizeof(att_state));
Chia-I Wuc51b1212015-10-27 19:25:11 +08001345 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001346 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001347 cb.attachmentCount = 1;
1348 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001349
Tony Barbourfa6cac72015-01-16 14:27:35 -07001350 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001351 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001352 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001353 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1354 vp.scissorCount = 1;
1355 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001356
1357 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001358 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001359 ds.depthTestEnable = VK_TRUE;
1360 ds.depthWriteEnable = VK_TRUE;
Chia-I Wu1f851912015-10-27 18:04:07 +08001361 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001362 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuc51b1212015-10-27 19:25:11 +08001363 ds.back.failOp = VK_STENCIL_OP_KEEP;
1364 ds.back.passOp = VK_STENCIL_OP_KEEP;
1365 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001366 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001367 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001368
Tony Barbourfa6cac72015-01-16 14:27:35 -07001369 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001370 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001371 ms.pSampleMask = NULL;
Chia-I Wu1f851912015-10-27 18:04:07 +08001372 ms.rasterizationSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001373
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001374 // Two stages: vs and fs
1375 pipeline.stageCount = 2;
1376 VkPipelineShaderStageCreateInfo shaderStages[2];
1377 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1378
1379 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001380 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001381
1382 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001383 shaderStages[1].shader = demo_prepare_fs(demo);
1384
Tony Barboure307f582015-07-10 15:29:03 -06001385 pipeline.pVertexInputState = &vi;
1386 pipeline.pInputAssemblyState = &ia;
Chia-I Wu1f851912015-10-27 18:04:07 +08001387 pipeline.pRasterizationState = &rs;
Tony Barboure307f582015-07-10 15:29:03 -06001388 pipeline.pColorBlendState = &cb;
1389 pipeline.pMultisampleState = &ms;
1390 pipeline.pViewportState = &vp;
1391 pipeline.pDepthStencilState = &ds;
1392 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001393 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001394 pipeline.pDynamicState = &dynamicState;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001395
Jon Ashburn0d60d272015-07-09 15:02:25 -06001396 memset(&pipelineCache, 0, sizeof(pipelineCache));
1397 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1398
Chia-I Wu69f40122015-10-26 21:10:41 +08001399 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001400 assert(!err);
Chia-I Wu69f40122015-10-26 21:10:41 +08001401 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1402 1, &pipeline, NULL, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001403 assert(!err);
1404
Chia-I Wu69f40122015-10-26 21:10:41 +08001405 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001406
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001407 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08001408 vkDestroyShader(demo->device, shaderStages[i].shader, NULL);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001409 }
Chia-I Wu69f40122015-10-26 21:10:41 +08001410 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1411 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001412}
1413
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001414static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001415{
Chia-I Wuc51b1212015-10-27 19:25:11 +08001416 const VkDescriptorPoolSize type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001417 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu763a7492015-10-26 20:48:51 +08001418 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wuf8385062015-01-04 16:27:24 +08001419 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001420 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001421 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001422 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001423 .maxSets = 1,
Chia-I Wuc51b1212015-10-27 19:25:11 +08001424 .poolSizeCount = 1,
1425 .pPoolSizes = &type_count,
Chia-I Wuf8385062015-01-04 16:27:24 +08001426 };
Tony Barbour22a30862015-04-22 09:02:32 -06001427 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001428
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001429 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu69f40122015-10-26 21:10:41 +08001430 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001431 assert(!err);
1432}
1433
1434static void demo_prepare_descriptor_set(struct demo *demo)
1435{
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001436 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001437 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001438 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001439 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001440
Chia-I Wu1f851912015-10-27 18:04:07 +08001441 VkDescriptorSetAllocateInfo alloc_info = {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001442 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO,
1443 .pNext = NULL,
1444 .descriptorPool = demo->desc_pool,
Chia-I Wu763a7492015-10-26 20:48:51 +08001445 .setLayoutCount = 1,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001446 .pSetLayouts = &demo->desc_layout
1447 };
Chia-I Wu1f851912015-10-27 18:04:07 +08001448 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northropc8aa4a52015-08-03 12:47:29 -06001449 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001450
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001451 memset(&tex_descs, 0, sizeof(tex_descs));
1452 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001453 tex_descs[i].sampler = demo->textures[i].sampler;
1454 tex_descs[i].imageView = demo->textures[i].view;
1455 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001456 }
1457
1458 memset(&write, 0, sizeof(write));
1459 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu1f851912015-10-27 18:04:07 +08001460 write.dstSet = demo->desc_set;
Chia-I Wu763a7492015-10-26 20:48:51 +08001461 write.descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001462 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001463 write.pImageInfo = tex_descs;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001464
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001465 vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001466}
1467
Chia-I Wu76cd4222015-07-08 13:34:24 +08001468static void demo_prepare_framebuffers(struct demo *demo)
1469{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001470 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001471 attachments[1] = demo->depth.view;
1472
Chia-I Wu76cd4222015-07-08 13:34:24 +08001473 const VkFramebufferCreateInfo fb_info = {
1474 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1475 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001476 .renderPass = demo->render_pass,
1477 .attachmentCount = 2,
1478 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001479 .width = demo->width,
1480 .height = demo->height,
1481 .layers = 1,
1482 };
1483 VkResult U_ASSERT_ONLY err;
1484 uint32_t i;
1485
Tony Barbour5aabff52015-10-08 14:26:24 -06001486 demo->framebuffers = (VkFramebuffer *) malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1487 assert(demo->framebuffers);
1488
1489 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001490 attachments[0]= demo->buffers[i].view;
Chia-I Wu69f40122015-10-26 21:10:41 +08001491 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001492 assert(!err);
1493 }
1494}
1495
Chia-I Wuc19795a2014-09-13 11:12:55 +08001496static void demo_prepare(struct demo *demo)
1497{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001498 VkResult U_ASSERT_ONLY err;
1499
Chia-I Wu1f851912015-10-27 18:04:07 +08001500 const VkCommandPoolCreateInfo cmd_pool_info = {
1501 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001502 .pNext = NULL,
1503 .queueFamilyIndex = demo->graphics_queue_node_index,
1504 .flags = 0,
1505 };
Chia-I Wu69f40122015-10-26 21:10:41 +08001506 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop18ea11b2015-07-09 18:08:32 -06001507 assert(!err);
1508
Chia-I Wu1f851912015-10-27 18:04:07 +08001509 const VkCommandBufferAllocateInfo cmd = {
1510 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001511 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001512 .commandPool = demo->cmd_pool,
1513 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu763a7492015-10-26 20:48:51 +08001514 .bufferCount = 1,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001515 };
Chia-I Wu1f851912015-10-27 18:04:07 +08001516 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->draw_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001517 assert(!err);
1518
Chia-I Wuc19795a2014-09-13 11:12:55 +08001519 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001520 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001521 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001522 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001523 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001524 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001525 demo_prepare_pipeline(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001526
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001527 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001528 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001529
1530 demo_prepare_framebuffers(demo);
1531
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001532 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001533}
1534
Ian Elliotte14e9f92015-04-16 15:23:05 -06001535#ifdef _WIN32
1536static void demo_run(struct demo *demo)
1537{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001538 if (!demo->prepared)
1539 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001540 demo_draw(demo);
Tony Barbour4a6692d2015-10-08 13:45:45 -06001541
1542 if (demo->depthStencil > 0.99f)
1543 demo->depthIncrement = -0.001f;
1544 if (demo->depthStencil < 0.8f)
1545 demo->depthIncrement = 0.001f;
1546
1547 demo->depthStencil += demo->depthIncrement;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001548}
1549
1550// On MS-Windows, make this a global, so it's available to WndProc()
1551struct demo demo;
1552
1553// MS-Windows event handling function:
1554LRESULT CALLBACK WndProc(HWND hWnd,
1555 UINT uMsg,
1556 WPARAM wParam,
1557 LPARAM lParam)
1558{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001559 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001560
1561 switch(uMsg)
1562 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001563 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001564 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001565 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001566 PostQuitMessage(0);
1567 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001568 case WM_PAINT:
Cody Northrop67ff0a42015-09-09 10:21:49 -06001569 if (demo.prepared) {
1570 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001571 break;
Cody Northrop67ff0a42015-09-09 10:21:49 -06001572 }
Ian Elliottf3f80822015-10-21 15:14:07 -06001573 case WM_SIZE:
Mike Stroyanb194be62015-10-28 09:46:57 -06001574 demo.width = lParam & 0xffff;
1575 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliottf3f80822015-10-21 15:14:07 -06001576 demo_resize(&demo);
1577 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001578 default:
1579 break;
1580 }
1581 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1582}
1583
1584static void demo_create_window(struct demo *demo)
1585{
1586 WNDCLASSEX win_class;
1587
1588 // Initialize the window class structure:
1589 win_class.cbSize = sizeof(WNDCLASSEX);
1590 win_class.style = CS_HREDRAW | CS_VREDRAW;
1591 win_class.lpfnWndProc = WndProc;
1592 win_class.cbClsExtra = 0;
1593 win_class.cbWndExtra = 0;
1594 win_class.hInstance = demo->connection; // hInstance
1595 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1596 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1597 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1598 win_class.lpszMenuName = NULL;
1599 win_class.lpszClassName = demo->name;
1600 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1601 // Register window class:
1602 if (!RegisterClassEx(&win_class)) {
1603 // It didn't work, so try to give a useful error:
1604 printf("Unexpected error trying to start the application!\n");
1605 fflush(stdout);
1606 exit(1);
1607 }
1608 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001609 RECT wr = { 0, 0, demo->width, demo->height };
1610 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001611 demo->window = CreateWindowEx(0,
1612 demo->name, // class name
1613 demo->name, // app name
1614 WS_OVERLAPPEDWINDOW | // window style
1615 WS_VISIBLE |
1616 WS_SYSMENU,
1617 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001618 wr.right-wr.left, // width
1619 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001620 NULL, // handle to parent
1621 NULL, // handle to menu
1622 demo->connection, // hInstance
1623 NULL); // no extra parameters
1624 if (!demo->window) {
1625 // It didn't work, so try to give a useful error:
1626 printf("Cannot create a window in which to draw!\n");
1627 fflush(stdout);
1628 exit(1);
1629 }
1630}
1631#else // _WIN32
1632
Chia-I Wuc19795a2014-09-13 11:12:55 +08001633static void demo_handle_event(struct demo *demo,
1634 const xcb_generic_event_t *event)
1635{
1636 switch (event->response_type & 0x7f) {
1637 case XCB_EXPOSE:
1638 demo_draw(demo);
1639 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001640 case XCB_CLIENT_MESSAGE:
1641 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1642 (*demo->atom_wm_delete_window).atom) {
1643 demo->quit = true;
1644 }
1645 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001646 case XCB_KEY_RELEASE:
1647 {
1648 const xcb_key_release_event_t *key =
1649 (const xcb_key_release_event_t *) event;
1650
1651 if (key->detail == 0x9)
1652 demo->quit = true;
1653 }
1654 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001655 case XCB_DESTROY_NOTIFY:
1656 demo->quit = true;
1657 break;
Ian Elliotte2688a52015-10-16 18:02:43 -06001658 case XCB_CONFIGURE_NOTIFY:
1659 {
1660 const xcb_configure_notify_event_t *cfg =
1661 (const xcb_configure_notify_event_t *) event;
1662 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
1663 demo_resize(demo);
1664 }
1665 }
1666 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001667 default:
1668 break;
1669 }
1670}
1671
1672static void demo_run(struct demo *demo)
1673{
1674 xcb_flush(demo->connection);
1675
1676 while (!demo->quit) {
1677 xcb_generic_event_t *event;
1678
Tony Barbour4a6692d2015-10-08 13:45:45 -06001679 event = xcb_poll_for_event(demo->connection);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001680 if (event) {
1681 demo_handle_event(demo, event);
1682 free(event);
1683 }
Tony Barbour4a6692d2015-10-08 13:45:45 -06001684
1685 demo_draw(demo);
1686
1687 if (demo->depthStencil > 0.99f)
1688 demo->depthIncrement = -0.001f;
1689 if (demo->depthStencil < 0.8f)
1690 demo->depthIncrement = 0.001f;
1691
1692 demo->depthStencil += demo->depthIncrement;
1693
1694 // Wait for work to finish before updating MVP.
1695 vkDeviceWaitIdle(demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001696 }
1697}
1698
1699static void demo_create_window(struct demo *demo)
1700{
1701 uint32_t value_mask, value_list[32];
1702
1703 demo->window = xcb_generate_id(demo->connection);
1704
1705 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1706 value_list[0] = demo->screen->black_pixel;
1707 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001708 XCB_EVENT_MASK_EXPOSURE |
1709 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001710
1711 xcb_create_window(demo->connection,
1712 XCB_COPY_FROM_PARENT,
1713 demo->window, demo->screen->root,
1714 0, 0, demo->width, demo->height, 0,
1715 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1716 demo->screen->root_visual,
1717 value_mask, value_list);
1718
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001719 /* Magic code that will send notification when window is destroyed */
1720 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1721 "WM_PROTOCOLS");
1722 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1723
1724 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1725 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1726
1727 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1728 demo->window, (*reply).atom, 4, 32, 1,
1729 &(*demo->atom_wm_delete_window).atom);
1730 free(reply);
1731
Chia-I Wuc19795a2014-09-13 11:12:55 +08001732 xcb_map_window(demo->connection, demo->window);
1733}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001734#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001735
Ian Elliotte36b2082015-07-06 14:27:58 -06001736/*
1737 * Return 1 (true) if all layer names specified in check_names
1738 * can be found in given layer properties.
1739 */
1740static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1741 uint32_t layer_count, VkLayerProperties *layers)
1742{
1743 for (uint32_t i = 0; i < check_count; i++) {
1744 VkBool32 found = 0;
1745 for (uint32_t j = 0; j < layer_count; j++) {
1746 if (!strcmp(check_names[i], layers[j].layerName)) {
1747 found = 1;
1748 }
1749 }
1750 if (!found) {
1751 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1752 return 0;
1753 }
1754 }
1755 return 1;
1756}
1757
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001758void* VKAPI myalloc(
1759 void* pUserData,
1760 size_t size,
1761 size_t alignment,
Chia-I Wu1f851912015-10-27 18:04:07 +08001762 VkSystemAllocationScope allocationScope)
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001763{
1764 return malloc(size);
1765}
1766void VKAPI myfree(
1767 void* pUserData,
Chia-I Wu1f851912015-10-27 18:04:07 +08001768 void* pMemory)
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001769{
Chia-I Wu1f851912015-10-27 18:04:07 +08001770 free(pMemory);
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001771}
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001772static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001773{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001774 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001775 char *extension_names[64];
1776 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001777 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001778 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001779 VkLayerProperties *instance_layers;
1780 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001781 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001782 uint32_t instance_layer_count = 0;
1783 uint32_t enabled_extension_count = 0;
1784 uint32_t enabled_layer_count = 0;
1785
Ian Elliotte36b2082015-07-06 14:27:58 -06001786 char *instance_validation_layers[] = {
1787 "MemTracker",
1788 };
1789
1790 char *device_validation_layers[] = {
1791 "MemTracker",
1792 };
1793
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001794 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001795 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001796 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001797 assert(!err);
1798
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001799 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001800 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001801 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001802
1803 if (demo->validate) {
1804 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1805 instance_layer_count, instance_layers);
1806 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001807 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001808 "required validation layer.\n\n"
1809 "Please look at the Getting Started guide for additional "
1810 "information.\n",
1811 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001812 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001813 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001814 }
1815
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001816 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001817 assert(!err);
1818
Tony Barbourd9955e42015-10-08 13:59:42 -06001819 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001820 memset(extension_names, 0, sizeof(extension_names));
1821 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001822 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001823 assert(!err);
1824 for (uint32_t i = 0; i < instance_extension_count; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08001825 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extensionName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001826 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001827 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001828 }
Chia-I Wu1f851912015-10-27 18:04:07 +08001829 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001830 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001831 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001832 }
1833 }
1834 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001835 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001836 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001837 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001838 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001839 "Vulkan installable client driver (ICD) installed?\nPlease "
1840 "look at the Getting Started guide for additional "
1841 "information.\n",
1842 "vkCreateInstance Failure");
1843 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001844 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001845 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001846 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001847 .pApplicationName = APP_SHORT_NAME,
1848 .applicationVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001849 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001850 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001851 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001852 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001853 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001854 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001855 .pNext = NULL,
Chia-I Wu1f851912015-10-27 18:04:07 +08001856 .pApplicationInfo = &app,
Chia-I Wu763a7492015-10-26 20:48:51 +08001857 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001858 .ppEnabledLayerNames = (const char *const*) layer_names,
Chia-I Wu763a7492015-10-26 20:48:51 +08001859 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001860 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001861 };
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06001862 float queue_priorities[1] = { 0.0 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001863 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchterea975642015-09-16 16:23:55 -06001864 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1865 .pNext = NULL,
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001866 .queueFamilyIndex = 0,
Chia-I Wu763a7492015-10-26 20:48:51 +08001867 .queuePriorityCount = 1,
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06001868 .pQueuePriorities = queue_priorities
Chia-I Wuc19795a2014-09-13 11:12:55 +08001869 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001870 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001871
Chia-I Wu1f851912015-10-27 18:04:07 +08001872 demo->allocator.pfnAllocation = myalloc;
Chia-I Wu69f40122015-10-26 21:10:41 +08001873 demo->allocator.pfnFree = myfree;
1874
1875 err = vkCreateInstance(&inst_info, &demo->allocator, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001876 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1877 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001878 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001879 "additional information.\n",
1880 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06001881 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001882 ERR_EXIT("Cannot find a specified extension library"
1883 ".\nMake sure your layers path is set appropriately\n",
1884 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001885 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001886 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1887 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001888 "the Getting Started guide for additional information.\n",
1889 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001890 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001891
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001892 free(instance_layers);
1893 free(instance_extensions);
1894
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001895 /* Make initial call to query gpu_count, then second call for gpu info*/
1896 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
1897 assert(!err && gpu_count > 0);
1898 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
1899 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
1900 assert(!err);
1901 /* For tri demo we just grab the first physical device */
1902 demo->gpu = physical_devices[0];
1903 free(physical_devices);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001904
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001905 /* Look for validation layers */
1906 validation_found = 0;
1907 enabled_layer_count = 0;
1908 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001909 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001910 assert(!err);
1911
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001912 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001913 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001914 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001915
1916 if (demo->validate) {
1917 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1918 device_layer_count, device_layers);
1919 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001920 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001921 "a required validation layer.\n\n"
1922 "Please look at the Getting Started guide for additional "
1923 "information.\n",
1924 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001925 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001926 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001927 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001928
1929 uint32_t device_extension_count = 0;
1930 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001931 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001932 demo->gpu, NULL, &device_extension_count, NULL);
1933 assert(!err);
1934
Tony Barbourd9955e42015-10-08 13:59:42 -06001935 swapchainExtFound = 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001936 enabled_extension_count = 0;
1937 memset(extension_names, 0, sizeof(extension_names));
1938 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001939 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001940 demo->gpu, NULL, &device_extension_count, device_extensions);
1941 assert(!err);
1942
1943 for (uint32_t i = 0; i < device_extension_count; i++) {
Chia-I Wu1f851912015-10-27 18:04:07 +08001944 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extensionName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001945 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001946 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Ian Elliotte36b2082015-07-06 14:27:58 -06001947 }
1948 assert(enabled_extension_count < 64);
1949 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001950 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001951 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001952 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliotte36b2082015-07-06 14:27:58 -06001953 "Vulkan installable client driver (ICD) installed?\nPlease "
1954 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001955 "information.\n",
1956 "vkCreateInstance Failure");
1957 }
1958
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001959 VkDeviceCreateInfo device = {
1960 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1961 .pNext = NULL,
Courtney Goeltzenleuchterdfd53f52015-10-15 16:58:44 -06001962 .requestedQueueCount = 1,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001963 .pRequestedQueues = &queue,
Chia-I Wu763a7492015-10-26 20:48:51 +08001964 .enabledLayerNameCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001965 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu763a7492015-10-26 20:48:51 +08001966 .enabledExtensionNameCount = enabled_extension_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001967 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001968 };
1969
1970 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001971 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001972 if (!demo->dbgCreateMsgCallback) {
1973 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1974 "vkGetProcAddr Failure");
1975 }
1976 err = demo->dbgCreateMsgCallback(
1977 demo->inst,
1978 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1979 dbgFunc, NULL,
1980 &demo->msg_callback);
1981 switch (err) {
1982 case VK_SUCCESS:
1983 break;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001984 case VK_ERROR_OUT_OF_HOST_MEMORY:
1985 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1986 "dbgCreateMsgCallback Failure");
1987 break;
1988 default:
1989 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1990 "dbgCreateMsgCallback Failure");
1991 break;
1992 }
1993 }
1994
Ian Elliotte36b2082015-07-06 14:27:58 -06001995
Chia-I Wu69f40122015-10-26 21:10:41 +08001996 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001997 assert(!err);
1998
Ian Elliott338dedb2015-08-21 15:09:33 -06001999 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2000 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
2001 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
2002 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
2003 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2004 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2005 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2006 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2007 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2008 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliott1b6de092015-06-22 15:07:49 -06002009
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002010 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002011
Cody Northropef72e2a2015-08-03 17:04:53 -06002012 // Query with NULL data to get count
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002013 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002014
Cody Northropef72e2a2015-08-03 17:04:53 -06002015 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002016 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002017 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002018
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002019 // Graphics queue and MemMgr queue can be separate.
2020 // TODO: Add support for separate queues, including synchronization,
2021 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002022}
2023
Tony Barbourd9955e42015-10-08 13:59:42 -06002024static void demo_init_vk_swapchain(struct demo *demo)
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002025{
Tony Barbour4a6692d2015-10-08 13:45:45 -06002026 VkResult U_ASSERT_ONLY err;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002027 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06002028
Tony Barbourd9955e42015-10-08 13:59:42 -06002029 // Construct the surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06002030 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002031 demo->surface_description.pNext = NULL;
2032#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06002033 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002034 demo->surface_description.pPlatformHandle = demo->connection;
2035 demo->surface_description.pPlatformWindow = demo->window;
2036#else // _WIN32
2037 demo->platform_handle_xcb.connection = demo->connection;
2038 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06002039 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06002040 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
2041 demo->surface_description.pPlatformWindow = &demo->window;
2042#endif // _WIN32
2043
Tony Barbourd9955e42015-10-08 13:59:42 -06002044 // Iterate over each queue to learn whether it supports presenting:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002045 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2046 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06002047 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2048 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06002049 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002050 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002051
2052 // Search for a graphics and a present queue in the array of queue
2053 // families, try to find one that supports both
2054 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2055 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002056 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002057 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2058 if (graphicsQueueNodeIndex == UINT32_MAX) {
2059 graphicsQueueNodeIndex = i;
2060 }
2061
2062 if (supportsPresent[i] == VK_TRUE) {
2063 graphicsQueueNodeIndex = i;
2064 presentQueueNodeIndex = i;
2065 break;
2066 }
2067 }
2068 }
2069 if (presentQueueNodeIndex == UINT32_MAX) {
2070 // If didn't find a queue that supports both graphics and present, then
2071 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002072 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002073 if (supportsPresent[i] == VK_TRUE) {
2074 presentQueueNodeIndex = i;
2075 break;
2076 }
2077 }
2078 }
2079 free(supportsPresent);
2080
2081 // Generate error if could not find both a graphics and a present queue
2082 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2083 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002084 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002085 }
2086
2087 // TODO: Add support for separate queues, including presentation,
2088 // synchronization, and appropriate tracking for QueueSubmit
2089 // While it is possible for an application to use a separate graphics and a
2090 // present queues, this demo program assumes it is only using one:
2091 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2092 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002093 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002094 }
2095
2096 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002097
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002098 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08002099 0, &demo->queue);
Ian Elliott32536f92015-04-21 16:41:02 -06002100
Ian Elliotte36b2082015-07-06 14:27:58 -06002101 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002102 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002103 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2104 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002105 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002106 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002107 VkSurfaceFormatKHR *surfFormats =
2108 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2109 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2110 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002111 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002112 assert(!err);
2113 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2114 // the surface has no preferred format. Otherwise, at least one
2115 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002116 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2117 {
2118 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2119 }
2120 else
2121 {
2122 assert(formatCount >= 1);
2123 demo->format = surfFormats[0].format;
2124 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002125 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002126
Mark Lobodzinski72346292015-07-02 16:49:40 -06002127 // Get Memory information and properties
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06002128 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002129}
2130
2131static void demo_init_connection(struct demo *demo)
2132{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002133#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002134 const xcb_setup_t *setup;
2135 xcb_screen_iterator_t iter;
2136 int scr;
2137
2138 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002139 if (demo->connection == NULL) {
2140 printf("Cannot find a compatible Vulkan installable client driver "
2141 "(ICD).\nExiting ...\n");
2142 fflush(stdout);
2143 exit(1);
2144 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002145
2146 setup = xcb_get_setup(demo->connection);
2147 iter = xcb_setup_roots_iterator(setup);
2148 while (scr-- > 0)
2149 xcb_screen_next(&iter);
2150
2151 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002152#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002153}
2154
Ian Elliotte14e9f92015-04-16 15:23:05 -06002155#ifdef _WIN32
2156static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2157#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002158static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002159#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002160{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002161 bool argv_error = false;
2162
Chia-I Wuc19795a2014-09-13 11:12:55 +08002163 memset(demo, 0, sizeof(*demo));
2164
Ian Elliotte14e9f92015-04-16 15:23:05 -06002165#ifdef _WIN32
2166 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002167 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002168
2169 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2170 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002171 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2172 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002173 else if (strlen(pCmdLine) != 0) {
2174 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2175 argv_error = true;
2176 }
2177#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002178 for (int i = 0; i < argc; i++) {
2179 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2180 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002181 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2182 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002183 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002184#endif // _WIN32
2185 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002186 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002187 fflush(stderr);
2188 exit(1);
2189 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002190
Chia-I Wuc19795a2014-09-13 11:12:55 +08002191 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002192 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002193
2194 demo->width = 300;
2195 demo->height = 300;
Tony Barbour4a6692d2015-10-08 13:45:45 -06002196 demo->depthStencil = 1.0;
2197 demo->depthIncrement = -0.01f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002198}
2199
2200static void demo_cleanup(struct demo *demo)
2201{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002202 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002203
Cody Northrop67ff0a42015-09-09 10:21:49 -06002204 demo->prepared = false;
2205
Tony Barbour5aabff52015-10-08 14:26:24 -06002206 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002207 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbourde4124d2015-07-03 10:33:54 -06002208 }
Tony Barbour5aabff52015-10-08 14:26:24 -06002209 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08002210 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08002211
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002212 if (demo->setup_cmd) {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06002213 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002214 }
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06002215 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->draw_cmd);
Chia-I Wu69f40122015-10-26 21:10:41 +08002216 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002217
Chia-I Wu69f40122015-10-26 21:10:41 +08002218 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2219 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2220 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2221 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Chia-I Wub043fe32014-10-06 15:30:33 +08002222
Chia-I Wu69f40122015-10-26 21:10:41 +08002223 vkDestroyBuffer(demo->device, demo->vertices.buf, NULL);
2224 vkFreeMemory(demo->device, demo->vertices.mem, NULL);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002225
Chia-I Wub043fe32014-10-06 15:30:33 +08002226 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002227 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2228 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2229 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2230 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Chia-I Wub043fe32014-10-06 15:30:33 +08002231 }
2232
Ian Elliott338dedb2015-08-21 15:09:33 -06002233 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002234 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002235 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002236
Chia-I Wu69f40122015-10-26 21:10:41 +08002237 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2238 vkDestroyImage(demo->device, demo->depth.image, NULL);
2239 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Tony Barbourde4124d2015-07-03 10:33:54 -06002240
Ian Elliotte2688a52015-10-16 18:02:43 -06002241 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain);
Ian Elliotte36b2082015-07-06 14:27:58 -06002242 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002243
Chia-I Wu69f40122015-10-26 21:10:41 +08002244 vkDestroyDevice(demo->device, NULL);
2245 vkDestroyInstance(demo->inst, &demo->allocator);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002246
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002247 free(demo->queue_props);
2248
Ian Elliotte14e9f92015-04-16 15:23:05 -06002249#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002250 xcb_destroy_window(demo->connection, demo->window);
2251 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002252 free(demo->atom_wm_delete_window);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002253#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002254}
2255
Ian Elliotte2688a52015-10-16 18:02:43 -06002256static void demo_resize(struct demo *demo)
2257{
2258 uint32_t i;
2259
Mike Stroyanb194be62015-10-28 09:46:57 -06002260 // Don't react to resize until after first initialization.
2261 if (!demo->prepared) {
2262 return;
2263 }
Ian Elliotte2688a52015-10-16 18:02:43 -06002264 // In order to properly resize the window, we must re-create the swapchain
2265 // AND redo the command buffers, etc.
2266 //
2267 // First, perform part of the demo_cleanup() function:
2268 demo->prepared = false;
2269
2270 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002271 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002272 }
2273 free(demo->framebuffers);
Chia-I Wu69f40122015-10-26 21:10:41 +08002274 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002275
2276 if (demo->setup_cmd) {
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002277 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->setup_cmd);
Ian Elliotte2688a52015-10-16 18:02:43 -06002278 }
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002279 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->draw_cmd);
Chia-I Wu69f40122015-10-26 21:10:41 +08002280 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002281
Chia-I Wu69f40122015-10-26 21:10:41 +08002282 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2283 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2284 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2285 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002286
Chia-I Wu69f40122015-10-26 21:10:41 +08002287 vkDestroyBuffer(demo->device, demo->vertices.buf, NULL);
2288 vkFreeMemory(demo->device, demo->vertices.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002289
2290 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002291 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2292 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2293 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2294 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002295 }
2296
2297 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu69f40122015-10-26 21:10:41 +08002298 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002299 }
2300
Chia-I Wu69f40122015-10-26 21:10:41 +08002301 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2302 vkDestroyImage(demo->device, demo->depth.image, NULL);
2303 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliotte2688a52015-10-16 18:02:43 -06002304
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002305 free(demo->buffers);
2306
Ian Elliotte2688a52015-10-16 18:02:43 -06002307 // Second, re-perform the demo_prepare() function, which will re-create the
2308 // swapchain:
2309 demo_prepare(demo);
2310}
2311
Ian Elliotte14e9f92015-04-16 15:23:05 -06002312#ifdef _WIN32
2313int APIENTRY WinMain(HINSTANCE hInstance,
2314 HINSTANCE hPrevInstance,
2315 LPSTR pCmdLine,
2316 int nCmdShow)
2317{
2318 MSG msg; // message
2319 bool done; // flag saying when app is complete
2320
2321 demo_init(&demo, hInstance, pCmdLine);
2322 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002323 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002324
2325 demo_prepare(&demo);
2326
2327 done = false; //initialize loop condition variable
2328 /* main message loop*/
2329 while(!done)
2330 {
Ian Elliott421107f2015-04-28 15:50:36 -06002331 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002332 if (msg.message == WM_QUIT) //check for a quit message
2333 {
2334 done = true; //if found, quit app
2335 }
2336 else
2337 {
2338 /* Translate and dispatch to event queue*/
2339 TranslateMessage(&msg);
2340 DispatchMessage(&msg);
2341 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002342 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002343 }
2344
2345 demo_cleanup(&demo);
2346
Tony Barboura938abb2015-04-22 11:36:22 -06002347 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002348}
2349#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002350int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002351{
2352 struct demo demo;
2353
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002354 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002355 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002356 demo_init_vk_swapchain(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002357
2358 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002359 demo_run(&demo);
2360
2361 demo_cleanup(&demo);
2362
Chia-I Wuc19795a2014-09-13 11:12:55 +08002363 return 0;
2364}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002365#endif // _WIN32