blob: 9bd83d8a79f64517d69c0caae88ed9af84ae9bbd [file] [log] [blame]
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001/*
Ian Elliott7595eee2015-04-28 10:33:11 -06002 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24/*
Chia-I Wuf5caeb02014-10-25 12:11:27 +080025 * Draw a textured triangle with depth testing. This is written against Intel
26 * ICD. It does not do state transition nor object memory binding like it
27 * should. It also does no error checking.
28 */
29
Chia-I Wuc19795a2014-09-13 11:12:55 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
35
Ian Elliotte14e9f92015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
38#include <windows.h>
39#define APP_NAME_STR_LEN 80
40#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +080041#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060042#endif // _WIN32
43
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060044#include <vulkan.h>
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060045#include "vk_debug_report_lunarg.h"
Ian Elliott338dedb2015-08-21 15:09:33 -060046#include <vk_ext_khr_swapchain.h>
47#include <vk_ext_khr_device_swapchain.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080048
Cody Northropd4e020a2015-03-17 14:54:35 -060049#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060050
Chia-I Wub043fe32014-10-06 15:30:33 +080051#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060052#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060053#define APP_SHORT_NAME "tri"
54#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080055
Ian Elliotte36b2082015-07-06 14:27:58 -060056#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
57
Tony Barbour22a30862015-04-22 09:02:32 -060058#if defined(NDEBUG) && defined(__GNUC__)
59#define U_ASSERT_ONLY __attribute__((unused))
60#else
61#define U_ASSERT_ONLY
62#endif
63
Ian Elliottcaa9f272015-04-28 11:35:02 -060064#ifdef _WIN32
65#define ERR_EXIT(err_msg, err_class) \
66 do { \
67 MessageBox(NULL, err_msg, err_class, MB_OK); \
68 exit(1); \
69 } while (0)
Ian Elliottcaa9f272015-04-28 11:35:02 -060070#else // _WIN32
71
72#define ERR_EXIT(err_msg, err_class) \
73 do { \
74 printf(err_msg); \
75 fflush(stdout); \
76 exit(1); \
77 } while (0)
78#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060079
Ian Elliotte36b2082015-07-06 14:27:58 -060080#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
81{ \
82 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
83 if (demo->fp##entrypoint == NULL) { \
84 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
85 "vkGetInstanceProcAddr Failure"); \
86 } \
87}
88
Ian Elliott1b6de092015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060091 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060092 if (demo->fp##entrypoint == NULL) { \
93 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
94 "vkGetDeviceProcAddr Failure"); \
95 } \
96}
97
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060098struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060099 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700100
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600101 VkImage image;
102 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600103
Mark Lobodzinski23182612015-05-29 09:32:35 -0500104 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600105 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700106 int32_t tex_width, tex_height;
107};
108
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600109VkBool32 dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600110 VkFlags msgFlags,
111 VkDbgObjectType objType,
112 uint64_t srcObject,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600113 size_t location,
114 int32_t msgCode,
115 const char* pLayerPrefix,
116 const char* pMsg,
117 void* pUserData)
118{
119 char *message = (char *) malloc(strlen(pMsg)+100);
120
121 assert (message);
122
123 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
124 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
125 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
126 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
127 } else {
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600128 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600129 }
130
131#ifdef _WIN32
132 MessageBox(NULL, message, "Alert", MB_OK);
133#else
134 printf("%s\n",message);
135 fflush(stdout);
136#endif
137 free(message);
Courtney Goeltzenleuchtere4c43132015-09-08 16:57:22 -0600138
139 /*
140 * false indicates that layer should not bail-out of an
141 * API call that had validation failures. This may mean that the
142 * app dies inside the driver due to invalid parameter(s).
143 * That's what would happen without validation layers, so we'll
144 * keep that behavior here.
145 */
146 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600147}
148
Ian Elliott338dedb2015-08-21 15:09:33 -0600149typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600150 VkImage image;
151 VkCmdBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600152 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600153} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600154
Chia-I Wuc19795a2014-09-13 11:12:55 +0800155struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600156#ifdef _WIN32
157#define APP_NAME_STR_LEN 80
158 HINSTANCE connection; // hInstance - Windows Instance
159 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
160 HWND window; // hWnd - window handle
161#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800162 xcb_connection_t *connection;
163 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800164 xcb_window_t window;
165 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott338dedb2015-08-21 15:09:33 -0600166 VkPlatformHandleXcbKHR platform_handle_xcb;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600167#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700168 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600169 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600170 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800171
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600172 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600173 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600174 VkDevice device;
175 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600176 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600177 VkQueueFamilyProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700178 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800179
180 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600181 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600182 VkColorSpaceKHR color_space;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800183
Ian Elliott338dedb2015-08-21 15:09:33 -0600184 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
185 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
186 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
187 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
188 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
189 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
190 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
191 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
192 PFN_vkQueuePresentKHR fpQueuePresentKHR;
193 VkSurfaceDescriptionWindowKHR surface_description;
194 uint32_t swapchainImageCount;
Ian Elliotte2688a52015-10-16 18:02:43 -0600195 VkSwapchainKHR swapchain;
Ian Elliott338dedb2015-08-21 15:09:33 -0600196 SwapchainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800197
Ian Elliotte36b2082015-07-06 14:27:58 -0600198 VkCmdPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800199
Chia-I Wub043fe32014-10-06 15:30:33 +0800200 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600201 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800202
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500204 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600205 VkImageView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800206 } depth;
207
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600208 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800209
Chia-I Wu99621bc2014-10-08 11:52:22 +0800210 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600211 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500212 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800213
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600214 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600215 VkVertexInputBindingDescription vi_bindings[1];
216 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800217 } vertices;
218
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600219 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
220 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500221 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600222 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600223 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800224 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600225 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800226
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -0600227 VkShaderModule vert_shader_module;
228 VkShaderModule frag_shader_module;
229
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600230 VkDescriptorPool desc_pool;
231 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800232
Tony Barbour5aabff52015-10-08 14:26:24 -0600233 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800234
Mark Lobodzinski72346292015-07-02 16:49:40 -0600235 VkPhysicalDeviceMemoryProperties memory_properties;
236
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600237 bool validate;
238 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Ian Elliotte36b2082015-07-06 14:27:58 -0600239 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600240 VkDbgMsgCallback msg_callback;
241
Tony Barbour4a6692d2015-10-08 13:45:45 -0600242 float depthStencil;
243 float depthIncrement;
244
Chia-I Wuc19795a2014-09-13 11:12:55 +0800245 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600246 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600247 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800248};
249
Ian Elliotte2688a52015-10-16 18:02:43 -0600250// Forward declaration:
251static void demo_resize(struct demo *demo);
252
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600253static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinski72346292015-07-02 16:49:40 -0600254{
255 // Search memtypes to find first index with those properties
256 for (uint32_t i = 0; i < 32; i++) {
257 if ((typeBits & 1) == 1) {
258 // Type is available, does it match user properties?
Tony Barbourf1eceb92015-10-15 12:42:56 -0600259 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinski72346292015-07-02 16:49:40 -0600260 *typeIndex = i;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600261 return true;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600262 }
263 }
264 typeBits >>= 1;
265 }
266 // No memory types matched, return failure
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600267 return false;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600268}
269
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600270static void demo_flush_init_cmd(struct demo *demo)
271{
Tony Barbour22a30862015-04-22 09:02:32 -0600272 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600273
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600274 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275 return;
276
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600277 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600278 assert(!err);
279
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600280 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600281 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600282 VkSubmitInfo submit_info = {
283 .waitSemCount = 0,
284 .pWaitSemaphores = NULL,
285 .cmdBufferCount = 1,
286 .pCommandBuffers = cmd_bufs,
287 .signalSemCount = 0,
288 .pSignalSemaphores = NULL
289 };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600290
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600291 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600292 assert(!err);
293
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600294 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600295 assert(!err);
296
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600297 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600298 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600299}
300
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600301static void demo_set_image_layout(
302 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600303 VkImage image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600304 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600305 VkImageLayout old_image_layout,
306 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600307{
Tony Barbour22a30862015-04-22 09:02:32 -0600308 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600310 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600311 const VkCmdBufferAllocInfo cmd = {
312 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600313 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600314 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800315 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600316 .count = 1,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600317 };
318
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600319 err = vkAllocCommandBuffers(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600320 assert(!err);
321
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600322 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600323 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600324 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600325 .flags = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600326 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600327 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600328 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600329 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600330 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Tony Barbourc1098272015-10-23 10:53:30 -0600331 assert(!err);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600332 }
333
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600334 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600335 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600336 .pNext = NULL,
337 .outputMask = 0,
338 .inputMask = 0,
339 .oldLayout = old_image_layout,
340 .newLayout = new_image_layout,
341 .image = image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600342 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600343 };
344
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600345 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600346 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600347 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600348 }
349
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600350 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600351 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600352 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600353 }
354
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600355 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600356
Tony Barbourc2e987e2015-06-29 16:20:35 -0600357 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
358 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600359
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600360 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600361}
362
Chia-I Wuc19795a2014-09-13 11:12:55 +0800363static void demo_draw_build_cmd(struct demo *demo)
364{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800365 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600366 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600367 .pNext = NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600368 .flags = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600369 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600370 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600371 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn53d27af2014-12-31 17:08:35 -0700372 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800373 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600374 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
Tony Barbour4a6692d2015-10-08 13:45:45 -0600375 [1] = { .depthStencil = { demo->depthStencil, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800376 };
377 const VkRenderPassBeginInfo rp_begin = {
378 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
379 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800380 .renderPass = demo->render_pass,
381 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800382 .renderArea.offset.x = 0,
383 .renderArea.offset.y = 0,
384 .renderArea.extent.width = demo->width,
385 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600386 .clearValueCount = 2,
387 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700388 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800389 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800390
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600391 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800392 assert(!err);
393
Chia-I Wuc278df82015-07-07 11:50:03 +0800394 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600395 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800396 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600397 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600398 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800399
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600400 VkViewport viewport;
401 memset(&viewport, 0, sizeof(viewport));
402 viewport.height = (float) demo->height;
403 viewport.width = (float) demo->width;
404 viewport.minDepth = (float) 0.0f;
405 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600406 vkCmdSetViewport(demo->draw_cmd, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600407
408 VkRect2D scissor;
409 memset(&scissor, 0, sizeof(scissor));
410 scissor.extent.width = demo->width;
411 scissor.extent.height = demo->height;
412 scissor.offset.x = 0;
413 scissor.offset.y = 0;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600414 vkCmdSetScissor(demo->draw_cmd, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600415
Tony Barbour8205d902015-04-16 15:59:00 -0600416 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600417 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800418
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600419 vkCmdDraw(demo->draw_cmd, 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800420 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800421
Tony Barbour40292ae2015-10-21 10:14:48 -0600422 VkImageMemoryBarrier prePresentBarrier = {
423 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
424 .pNext = NULL,
425 .outputMask = VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT,
426 .inputMask = 0,
427 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
428 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
429 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
430 .destQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
431 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
432 };
433
434 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
435 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
436 vkCmdPipelineBarrier(demo->draw_cmd, VK_PIPELINE_STAGE_ALL_GPU_COMMANDS, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
437 VK_FALSE, 1, (const void * const*)&pmemory_barrier);
438
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600439 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800440 assert(!err);
441}
442
443static void demo_draw(struct demo *demo)
444{
Tony Barbour22a30862015-04-22 09:02:32 -0600445 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600446 VkSemaphore presentCompleteSemaphore;
447 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
448 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
449 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600450 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600451 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800452
Ian Elliotte36b2082015-07-06 14:27:58 -0600453 err = vkCreateSemaphore(demo->device,
454 &presentCompleteSemaphoreCreateInfo,
455 &presentCompleteSemaphore);
456 assert(!err);
457
458 // Get the index of the next available swapchain image:
Ian Elliotte2688a52015-10-16 18:02:43 -0600459 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600460 UINT64_MAX,
461 presentCompleteSemaphore,
462 &demo->current_buffer);
Ian Elliotte2688a52015-10-16 18:02:43 -0600463 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
464 // demo->swapchain is out of date (e.g. the window was resized) and
465 // must be recreated:
466 demo_resize(demo);
467 demo_draw(demo);
468 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
469 return;
470 } else if (err == VK_SUBOPTIMAL_KHR) {
471 // demo->swapchain is not as optimal as it could be, but the platform's
472 // presentation engine will still present the image correctly.
473 } else {
474 assert(!err);
475 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600476
Tony Barbour40292ae2015-10-21 10:14:48 -0600477 // Assume the command buffer has been run on current_buffer before so
478 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
479 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
480 VK_IMAGE_ASPECT_COLOR_BIT,
481 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR,
482 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
483 demo_flush_init_cmd(demo);
484
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600485 // Wait for the present complete semaphore to be signaled to ensure
486 // that the image won't be rendered to until the presentation
487 // engine has fully released ownership to the application, and it is
488 // okay to render to the image.
489
Ian Elliott338dedb2015-08-21 15:09:33 -0600490// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Chia-I Wuc19795a2014-09-13 11:12:55 +0800491 demo_draw_build_cmd(demo);
Tony Barbourde4124d2015-07-03 10:33:54 -0600492 VkFence nullFence = { VK_NULL_HANDLE };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800493
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600494 VkSubmitInfo submit_info = {
495 .waitSemCount = 1,
496 .pWaitSemaphores = &presentCompleteSemaphore,
497 .cmdBufferCount = 1,
498 .pCommandBuffers = &demo->draw_cmd,
499 .signalSemCount = 0,
500 .pSignalSemaphores = NULL
501 };
502
503 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800504 assert(!err);
505
Ian Elliott338dedb2015-08-21 15:09:33 -0600506 VkPresentInfoKHR present = {
507 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600508 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600509 .swapchainCount = 1,
Ian Elliotte2688a52015-10-16 18:02:43 -0600510 .swapchains = &demo->swapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600511 .imageIndices = &demo->current_buffer,
512 };
513
514// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600515 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliotte2688a52015-10-16 18:02:43 -0600516 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
517 // demo->swapchain is out of date (e.g. the window was resized) and
518 // must be recreated:
519 demo_resize(demo);
520 } else if (err == VK_SUBOPTIMAL_KHR) {
521 // demo->swapchain is not as optimal as it could be, but the platform's
522 // presentation engine will still present the image correctly.
523 } else {
524 assert(!err);
525 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800526
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800527 err = vkQueueWaitIdle(demo->queue);
528 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600529
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600530 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800531}
532
533static void demo_prepare_buffers(struct demo *demo)
534{
Ian Elliotte36b2082015-07-06 14:27:58 -0600535 VkResult U_ASSERT_ONLY err;
Ian Elliotte2688a52015-10-16 18:02:43 -0600536 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600537
538 // Check the surface proprties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600539 VkSurfacePropertiesKHR surfProperties;
540 err = demo->fpGetSurfacePropertiesKHR(demo->device,
541 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600542 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600543 assert(!err);
544
Ian Elliott7fe115d2015-08-07 15:56:59 -0600545 uint32_t presentModeCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600546 err = demo->fpGetSurfacePresentModesKHR(demo->device,
547 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600548 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600549 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600550 VkPresentModeKHR *presentModes =
551 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600552 assert(presentModes);
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, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600556 assert(!err);
557
Ian Elliott338dedb2015-08-21 15:09:33 -0600558 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600559 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600560 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600561 {
562 // If the surface size is undefined, the size is set to
563 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600564 swapchainExtent.width = demo->width;
565 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600566 }
567 else
568 {
569 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -0600570 swapchainExtent = surfProperties.currentExtent;
Ian Elliotte2688a52015-10-16 18:02:43 -0600571 demo->width = surfProperties.currentExtent.width;
572 demo->height = surfProperties.currentExtent.height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600573 }
574
Ian Elliott338dedb2015-08-21 15:09:33 -0600575 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600576
577 // Determine the number of VkImage's to use in the swap chain (we desire to
578 // own only 1 image at a time, besides the images being displayed and
579 // queued for display):
Ian Elliott338dedb2015-08-21 15:09:33 -0600580 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600581 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600582 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600583 {
584 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600585 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600586 }
587
Ian Elliott338dedb2015-08-21 15:09:33 -0600588 VkSurfaceTransformKHR preTransform;
589 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
590 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600591 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600592 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600593 }
594
Ian Elliotte2688a52015-10-16 18:02:43 -0600595 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600596 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800597 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600598 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
599 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800600 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600601 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800602 .imageExtent = {
Ian Elliott338dedb2015-08-21 15:09:33 -0600603 .width = swapchainExtent.width,
604 .height = swapchainExtent.height,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800605 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600606 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600607 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800608 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600609 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
610 .queueFamilyCount = 0,
611 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600612 .presentMode = swapchainPresentMode,
Ian Elliotte2688a52015-10-16 18:02:43 -0600613 .oldSwapchain = oldSwapchain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600614 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800615 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600616 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800617
Ian Elliotte2688a52015-10-16 18:02:43 -0600618 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, &demo->swapchain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800619 assert(!err);
620
Ian Elliotte2688a52015-10-16 18:02:43 -0600621 // If we just re-created an existing swapchain, we should destroy the old
622 // swapchain at this point.
623 // Note: destroying the swapchain also cleans up all its associated
624 // presentable images once the platform is done with them.
625 if (oldSwapchain.handle != VK_NULL_HANDLE) {
626 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain);
627 }
628
629 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600630 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600631 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800632
Ian Elliott338dedb2015-08-21 15:09:33 -0600633 VkImage* swapchainImages =
634 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
635 assert(swapchainImages);
Ian Elliotte2688a52015-10-16 18:02:43 -0600636 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliott338dedb2015-08-21 15:09:33 -0600637 &demo->swapchainImageCount,
638 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600639 assert(!err);
640
Ian Elliott338dedb2015-08-21 15:09:33 -0600641 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600642 assert(demo->buffers);
643
Ian Elliott338dedb2015-08-21 15:09:33 -0600644 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600645 VkImageViewCreateInfo color_attachment_view = {
646 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800647 .pNext = NULL,
648 .format = demo->format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600649 .channels = {
650 .r = VK_CHANNEL_SWIZZLE_R,
651 .g = VK_CHANNEL_SWIZZLE_G,
652 .b = VK_CHANNEL_SWIZZLE_B,
653 .a = VK_CHANNEL_SWIZZLE_A,
654 },
655 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600656 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600657 .baseMipLevel = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600658 .numLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600659 .baseArrayLayer = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600660 .numLayers = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600661 },
662 .viewType = VK_IMAGE_VIEW_TYPE_2D,
663 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800664 };
665
Ian Elliott338dedb2015-08-21 15:09:33 -0600666 demo->buffers[i].image = swapchainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500667
Tony Barbour40292ae2015-10-21 10:14:48 -0600668 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
669 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600670 demo_set_image_layout(demo, demo->buffers[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600671 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600672 VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour40292ae2015-10-21 10:14:48 -0600673 VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600674
Chia-I Wuc19795a2014-09-13 11:12:55 +0800675 color_attachment_view.image = demo->buffers[i].image;
676
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600677 err = vkCreateImageView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800678 &color_attachment_view, &demo->buffers[i].view);
679 assert(!err);
680 }
Piers Daniell886be472015-02-23 16:23:13 -0700681
682 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800683}
684
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800685static void demo_prepare_depth(struct demo *demo)
686{
Tony Barbour8205d902015-04-16 15:59:00 -0600687 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600688 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600689 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800690 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600691 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800692 .format = depth_format,
693 .extent = { demo->width, demo->height, 1 },
694 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600695 .arrayLayers = 1,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800696 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600697 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600698 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800699 .flags = 0,
700 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600701 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600702 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500703 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800704 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600705 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800706 };
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600707 VkImageViewCreateInfo view = {
708 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800709 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600710 .image.handle = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600711 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600712 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600713 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600714 .baseMipLevel = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600715 .numLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600716 .baseArrayLayer = 0,
Courtney Goeltzenleuchter63f0ead2015-10-16 09:46:00 -0600717 .numLayers = 1
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600718 },
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800719 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600720 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800721 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700722
Mark Lobodzinski23182612015-05-29 09:32:35 -0500723 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600724 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600725 bool U_ASSERT_ONLY pass;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800726
727 demo->depth.format = depth_format;
728
729 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600730 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800731 &demo->depth.image);
732 assert(!err);
733
Mark Lobodzinski72346292015-07-02 16:49:40 -0600734 /* get memory requirements for this object */
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600735 vkGetImageMemoryRequirements(demo->device, demo->depth.image,
Tony Barbourde4124d2015-07-03 10:33:54 -0600736 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600737
738 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500739 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600740 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600741 mem_reqs.memoryTypeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600742 0, /* No requirements */
Mark Lobodzinski72346292015-07-02 16:49:40 -0600743 &mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600744 assert(pass);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800745
Mark Lobodzinski23182612015-05-29 09:32:35 -0500746 /* allocate memory */
747 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
748 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800749
Mark Lobodzinski23182612015-05-29 09:32:35 -0500750 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600751 err = vkBindImageMemory(demo->device, demo->depth.image,
752 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500753 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800754
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600755 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600756 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600757 VK_IMAGE_LAYOUT_UNDEFINED,
758 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600759
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800760 /* create image view */
761 view.image = demo->depth.image;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600762 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800763 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800764}
765
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700766static void demo_prepare_texture_image(struct demo *demo,
767 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600768 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600769 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600770 VkImageUsageFlags usage,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600771 VkFlags required_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800772{
Tony Barbour8205d902015-04-16 15:59:00 -0600773 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600774 const int32_t tex_width = 2;
775 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600776 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600777 bool U_ASSERT_ONLY pass;
Chia-I Wub043fe32014-10-06 15:30:33 +0800778
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600779 tex_obj->tex_width = tex_width;
780 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700781
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600782 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600783 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700784 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600785 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700786 .format = tex_format,
787 .extent = { tex_width, tex_height, 1 },
788 .mipLevels = 1,
Courtney Goeltzenleuchter2ebc2342015-10-21 17:57:31 -0600789 .arrayLayers = 1,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700790 .samples = 1,
791 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600792 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700793 .flags = 0,
794 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600795 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600796 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500797 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700798 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600799 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700800 };
801
Mark Lobodzinski23182612015-05-29 09:32:35 -0500802 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700803
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600804 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600805 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700806 assert(!err);
807
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600808 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600809
810 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600811 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &mem_alloc.memoryTypeIndex);
812 assert(pass);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700813
Mark Lobodzinski23182612015-05-29 09:32:35 -0500814 /* allocate memory */
815 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
816 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700817
Mark Lobodzinski23182612015-05-29 09:32:35 -0500818 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600819 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500820 tex_obj->mem, 0);
821 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700822
Tony Barbourf1eceb92015-10-15 12:42:56 -0600823 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600824 const VkImageSubresource subres = {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600825 .aspect = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700826 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600827 .arrayLayer = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700828 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600829 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700830 void *data;
831 int32_t x, y;
832
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600833 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700834
Dominik Witczake82d5b62015-09-22 18:25:33 +0200835 err = vkMapMemory(demo->device, tex_obj->mem, 0, mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700836 assert(!err);
837
838 for (y = 0; y < tex_height; y++) {
839 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
840 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700841 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700842 }
843
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600844 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700845 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600846
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600847 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600848 demo_set_image_layout(demo, tex_obj->image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600849 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600850 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600851 tex_obj->imageLayout);
852 /* 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 -0700853}
854
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500855static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700856{
857 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600858 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600859 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700860}
861
862static void demo_prepare_textures(struct demo *demo)
863{
Tony Barbour8205d902015-04-16 15:59:00 -0600864 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600865 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700866 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
867 { 0xffff0000, 0xff00ff00 },
868 };
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700869 uint32_t i;
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600870 VkResult err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700871
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600872 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700873
874 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600875 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700876 /* Device can texture using linear textures */
877 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600878 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600879 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700880 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600881 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700882
883 memset(&staging_texture, 0, sizeof(staging_texture));
884 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600885 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700886
887 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600888 VK_IMAGE_TILING_OPTIMAL,
889 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
890 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700891
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600892 demo_set_image_layout(demo, staging_texture.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600893 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600894 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600895 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700896
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600897 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600898 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600899 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600900 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700901
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600902 VkImageCopy copy_region = {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600903 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700904 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600905 .destSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700906 .destOffset = { 0, 0, 0 },
907 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
908 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600909 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600910 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
911 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600912 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700913
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600914 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600915 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600916 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600917 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700918
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600919 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700920
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600921 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700922 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600923 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700924 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700925 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700926
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600927 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600928 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800929 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600930 .magFilter = VK_TEX_FILTER_NEAREST,
931 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600932 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter51624412015-09-10 14:08:50 -0600933 .addressModeU = VK_TEX_ADDRESS_MODE_WRAP,
934 .addressModeV = VK_TEX_ADDRESS_MODE_WRAP,
935 .addressModeW = VK_TEX_ADDRESS_MODE_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800936 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700937 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600938 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800939 .minLod = 0.0f,
940 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600941 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600942 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800943 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600944 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600945 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800946 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600947 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600948 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700949 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600950 .channels = { VK_CHANNEL_SWIZZLE_R,
951 VK_CHANNEL_SWIZZLE_G,
952 VK_CHANNEL_SWIZZLE_B,
953 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -0600954 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600955 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +0800956 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700957
Chia-I Wub043fe32014-10-06 15:30:33 +0800958 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600959 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800960 &demo->textures[i].sampler);
961 assert(!err);
962
Chia-I Wub043fe32014-10-06 15:30:33 +0800963 /* create image view */
964 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600965 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700966 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800967 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800968 }
969}
970
Chia-I Wu99621bc2014-10-08 11:52:22 +0800971static void demo_prepare_vertices(struct demo *demo)
972{
973 const float vb[3][5] = {
974 /* position texcoord */
Cody Northropa9efa052015-10-13 11:28:23 -0600975 { -1.0f, -1.0f, 0.25f, 0.0f, 0.0f },
Chia-I Wue2504cb2015-04-22 14:20:52 +0800976 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800977 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
978 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600979 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600980 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800981 .pNext = NULL,
982 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600983 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800984 .flags = 0,
985 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600986 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600987 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500988 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800989 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600990 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800991 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500992 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600993 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600994 bool U_ASSERT_ONLY pass;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800995 void *data;
996
997 memset(&demo->vertices, 0, sizeof(demo->vertices));
998
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600999 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001000 assert(!err);
1001
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -06001002 vkGetBufferMemoryRequirements(demo->device,
Tony Barbourde4124d2015-07-03 10:33:54 -06001003 demo->vertices.buf, &mem_reqs);
Tony Barbourc1098272015-10-23 10:53:30 -06001004 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001005
Mark Lobodzinski72346292015-07-02 16:49:40 -06001006 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001007 pass = memory_type_from_properties(demo,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001008 mem_reqs.memoryTypeBits,
1009 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1010 &mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -06001011 assert(pass);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001012
Mark Lobodzinski23182612015-05-29 09:32:35 -05001013 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
1014 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001015
Dominik Witczake82d5b62015-09-22 18:25:33 +02001016 err = vkMapMemory(demo->device, demo->vertices.mem, 0, mem_alloc.allocationSize, 0, &data);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001017 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001018
Mark Lobodzinski23182612015-05-29 09:32:35 -05001019 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +08001020
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001021 vkUnmapMemory(demo->device, demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001022
Tony Barbourde4124d2015-07-03 10:33:54 -06001023 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001024 demo->vertices.mem, 0);
1025 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001026
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001027 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001028 demo->vertices.vi.pNext = NULL;
1029 demo->vertices.vi.bindingCount = 1;
1030 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
1031 demo->vertices.vi.attributeCount = 2;
1032 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
1033
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001034 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001035 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001036 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001037
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001038 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
1039 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -06001040 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001041 demo->vertices.vi_attrs[0].offsetInBytes = 0;
1042
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001043 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
1044 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -06001045 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +08001046 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001047}
1048
Chia-I Wuf8385062015-01-04 16:27:24 +08001049static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +08001050{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001051 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001052 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001053 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001054 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001055 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +08001056 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001057 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001058 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001059 .pNext = NULL,
1060 .count = 1,
1061 .pBinding = &layout_binding,
1062 };
Tony Barbour22a30862015-04-22 09:02:32 -06001063 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +08001064
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001065 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001066 &descriptor_layout, &demo->desc_layout);
1067 assert(!err);
1068
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001069 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1070 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1071 .pNext = NULL,
1072 .descriptorSetCount = 1,
1073 .pSetLayouts = &demo->desc_layout,
1074 };
1075
1076 err = vkCreatePipelineLayout(demo->device,
1077 &pPipelineLayoutCreateInfo,
1078 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001079 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001080}
1081
Chia-I Wu76cd4222015-07-08 13:34:24 +08001082static void demo_prepare_render_pass(struct demo *demo)
1083{
Chia-I Wuc278df82015-07-07 11:50:03 +08001084 const VkAttachmentDescription attachments[2] = {
1085 [0] = {
1086 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1087 .pNext = NULL,
1088 .format = demo->format,
1089 .samples = 1,
1090 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1091 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1092 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1093 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1094 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1095 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1096 },
1097 [1] = {
1098 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1099 .pNext = NULL,
1100 .format = demo->depth.format,
1101 .samples = 1,
1102 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1103 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1104 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1105 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1106 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1107 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1108 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001109 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001110 const VkAttachmentReference color_reference = {
1111 .attachment = 0,
1112 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1113 };
1114 const VkSubpassDescription subpass = {
1115 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1116 .pNext = NULL,
1117 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1118 .flags = 0,
1119 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001120 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001121 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001122 .pColorAttachments = &color_reference,
1123 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001124 .depthStencilAttachment = {
1125 .attachment = 1,
1126 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1127 },
1128 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001129 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001130 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001131 const VkRenderPassCreateInfo rp_info = {
1132 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1133 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001134 .attachmentCount = 2,
1135 .pAttachments = attachments,
1136 .subpassCount = 1,
1137 .pSubpasses = &subpass,
1138 .dependencyCount = 0,
1139 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001140 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001141 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001142
1143 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1144 assert(!err);
1145}
1146
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001147static VkShader demo_prepare_shader(struct demo *demo,
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001148 VkShaderStageFlagBits stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001149 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001150 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001151 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001152{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001153 VkShaderModuleCreateInfo moduleCreateInfo;
1154 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001155 VkShader shader;
Tony Barbour4a6692d2015-10-08 13:45:45 -06001156 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001157
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001158
1159 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1160 moduleCreateInfo.pNext = NULL;
1161
1162 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1163 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001164
Cody Northrop75db0322015-05-28 11:27:16 -06001165 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001166 moduleCreateInfo.codeSize = size;
1167 moduleCreateInfo.pCode = code;
1168 moduleCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001169 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001170 assert(!err);
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001171
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001172 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001173 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001174 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001175 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001176 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001177 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001178 } else {
1179 // Create fake SPV structure to feed GLSL
1180 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001181 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1182 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1183 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001184
Cody Northrop75db0322015-05-28 11:27:16 -06001185 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001186 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1187 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1188 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1189 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001190
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001191 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001192 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001193
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001194 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001195 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001196 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001197 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001198 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001199 assert(!err);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001200 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001201 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001202 return shader;
1203}
1204
Cody Northrop75db0322015-05-28 11:27:16 -06001205char *demo_read_spv(const char *filename, size_t *psize)
1206{
1207 long int size;
1208 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001209 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001210
1211 FILE *fp = fopen(filename, "rb");
1212 if (!fp) return NULL;
1213
1214 fseek(fp, 0L, SEEK_END);
1215 size = ftell(fp);
1216
1217 fseek(fp, 0L, SEEK_SET);
1218
1219 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001220 retVal = fread(shader_code, size, 1, fp);
1221 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001222
1223 *psize = size;
1224
1225 return shader_code;
1226}
1227
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001228static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001229{
Cody Northrop75db0322015-05-28 11:27:16 -06001230 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001231 VkShader shader;
1232 void *vertShaderCode;
1233 size_t size;
Cody Northrop75db0322015-05-28 11:27:16 -06001234
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001235 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001236
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001237 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT,
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001238 &demo->vert_shader_module,
1239 vertShaderCode, size);
1240 free(vertShaderCode);
1241 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001242 } else {
1243 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001244 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001245 "#extension GL_ARB_separate_shader_objects : enable\n"
1246 "#extension GL_ARB_shading_language_420pack : enable\n"
1247 "layout (location = 0) in vec4 pos;\n"
1248 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001249 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001250 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001251 " texcoord = attr;\n"
1252 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001253 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001254
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001255 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX_BIT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001256 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001257 (const void *) vertShaderText,
1258 strlen(vertShaderText));
1259 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001260}
1261
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001262static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001263{
Cody Northrop75db0322015-05-28 11:27:16 -06001264 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001265 VkShader shader;
1266 void *fragShaderCode;
1267 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001268
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001269 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001270
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001271 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT,
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001272 &demo->frag_shader_module,
1273 fragShaderCode, size);
1274
1275 free(fragShaderCode);
1276 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001277 } else {
1278 static const char *fragShaderText =
1279 "#version 140\n"
1280 "#extension GL_ARB_separate_shader_objects : enable\n"
1281 "#extension GL_ARB_shading_language_420pack : enable\n"
1282 "layout (binding = 0) uniform sampler2D tex;\n"
1283 "layout (location = 0) in vec2 texcoord;\n"
1284 "layout (location = 0) out vec4 uFragColor;\n"
1285 "void main() {\n"
1286 " uFragColor = texture(tex, texcoord);\n"
1287 "}\n";
1288
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001289 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT_BIT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001290 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001291 (const void *) fragShaderText,
1292 strlen(fragShaderText));
1293 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001294}
1295
1296static void demo_prepare_pipeline(struct demo *demo)
1297{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001298 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001299 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001300
Tony Barboure307f582015-07-10 15:29:03 -06001301 VkPipelineVertexInputStateCreateInfo vi;
1302 VkPipelineInputAssemblyStateCreateInfo ia;
1303 VkPipelineRasterStateCreateInfo rs;
1304 VkPipelineColorBlendStateCreateInfo cb;
1305 VkPipelineDepthStencilStateCreateInfo ds;
1306 VkPipelineViewportStateCreateInfo vp;
1307 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniell811eb742015-09-29 13:01:09 -06001308 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1309 VkPipelineDynamicStateCreateInfo dynamicState;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001310
Tony Barbour22a30862015-04-22 09:02:32 -06001311 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001312
Piers Daniell811eb742015-09-29 13:01:09 -06001313 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1314 memset(&dynamicState, 0, sizeof dynamicState);
1315 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1316 dynamicState.pDynamicStates = dynamicStateEnables;
1317
Chia-I Wuc19795a2014-09-13 11:12:55 +08001318 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001319 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001320 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001321
Chia-I Wu8d29d022014-10-08 12:14:39 +08001322 vi = demo->vertices.vi;
1323
Chia-I Wuc19795a2014-09-13 11:12:55 +08001324 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001325 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001326 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001327
1328 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001329 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001330 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001331 rs.cullMode = VK_CULL_MODE_BACK;
1332 rs.frontFace = VK_FRONT_FACE_CW;
Courtney Goeltzenleuchterc0f9fa72015-10-15 12:57:38 -06001333 rs.depthClampEnable = VK_FALSE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001334 rs.rasterizerDiscardEnable = VK_FALSE;
1335 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001336
1337 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001338 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1339 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001340 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001341 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001342 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001343 cb.attachmentCount = 1;
1344 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001345
Tony Barbourfa6cac72015-01-16 14:27:35 -07001346 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001347 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001348 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001349 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1350 vp.scissorCount = 1;
1351 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001352
1353 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001354 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001355 ds.depthTestEnable = VK_TRUE;
1356 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001357 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001358 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001359 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1360 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001361 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001362 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001363 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001364
Tony Barbourfa6cac72015-01-16 14:27:35 -07001365 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001366 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001367 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001368 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001369
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001370 // Two stages: vs and fs
1371 pipeline.stageCount = 2;
1372 VkPipelineShaderStageCreateInfo shaderStages[2];
1373 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1374
1375 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001376 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001377 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001378
1379 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Courtney Goeltzenleuchter8e2f0972015-10-21 17:08:06 -06001380 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001381 shaderStages[1].shader = demo_prepare_fs(demo);
1382
Tony Barboure307f582015-07-10 15:29:03 -06001383 pipeline.pVertexInputState = &vi;
1384 pipeline.pInputAssemblyState = &ia;
1385 pipeline.pRasterState = &rs;
1386 pipeline.pColorBlendState = &cb;
1387 pipeline.pMultisampleState = &ms;
1388 pipeline.pViewportState = &vp;
1389 pipeline.pDepthStencilState = &ds;
1390 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001391 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001392 pipeline.pDynamicState = &dynamicState;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001393
Jon Ashburn0d60d272015-07-09 15:02:25 -06001394 memset(&pipelineCache, 0, sizeof(pipelineCache));
1395 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1396
1397 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1398 assert(!err);
1399 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001400 assert(!err);
1401
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001402 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001403
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001404 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001405 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001406 }
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001407 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1408 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001409}
1410
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001411static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001412{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001413 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001414 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001415 .count = DEMO_TEXTURE_COUNT,
1416 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001417 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001418 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001419 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001420 .maxSets = 1,
Chia-I Wuf8385062015-01-04 16:27:24 +08001421 .count = 1,
1422 .pTypeCount = &type_count,
1423 };
Tony Barbour22a30862015-04-22 09:02:32 -06001424 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001425
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001426 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001427 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001428 assert(!err);
1429}
1430
1431static void demo_prepare_descriptor_set(struct demo *demo)
1432{
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001433 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001434 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001435 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001436 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001437
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001438 VkDescriptorSetAllocInfo alloc_info = {
1439 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO,
1440 .pNext = NULL,
1441 .descriptorPool = demo->desc_pool,
1442 .count = 1,
1443 .pSetLayouts = &demo->desc_layout
1444 };
1445 err = vkAllocDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northropc8aa4a52015-08-03 12:47:29 -06001446 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001447
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001448 memset(&tex_descs, 0, sizeof(tex_descs));
1449 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001450 tex_descs[i].sampler = demo->textures[i].sampler;
1451 tex_descs[i].imageView = demo->textures[i].view;
1452 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001453 }
1454
1455 memset(&write, 0, sizeof(write));
1456 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1457 write.destSet = demo->desc_set;
1458 write.count = DEMO_TEXTURE_COUNT;
1459 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchter34aa5c82015-10-23 13:38:14 -06001460 write.pImageInfo = tex_descs;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001461
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001462 vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001463}
1464
Chia-I Wu76cd4222015-07-08 13:34:24 +08001465static void demo_prepare_framebuffers(struct demo *demo)
1466{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001467 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001468 attachments[1] = demo->depth.view;
1469
Chia-I Wu76cd4222015-07-08 13:34:24 +08001470 const VkFramebufferCreateInfo fb_info = {
1471 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1472 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001473 .renderPass = demo->render_pass,
1474 .attachmentCount = 2,
1475 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001476 .width = demo->width,
1477 .height = demo->height,
1478 .layers = 1,
1479 };
1480 VkResult U_ASSERT_ONLY err;
1481 uint32_t i;
1482
Tony Barbour5aabff52015-10-08 14:26:24 -06001483 demo->framebuffers = (VkFramebuffer *) malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1484 assert(demo->framebuffers);
1485
1486 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001487 attachments[0]= demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001488 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1489 assert(!err);
1490 }
1491}
1492
Chia-I Wuc19795a2014-09-13 11:12:55 +08001493static void demo_prepare(struct demo *demo)
1494{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001495 VkResult U_ASSERT_ONLY err;
1496
1497 const VkCmdPoolCreateInfo cmd_pool_info = {
1498 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1499 .pNext = NULL,
1500 .queueFamilyIndex = demo->graphics_queue_node_index,
1501 .flags = 0,
1502 };
1503 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1504 assert(!err);
1505
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001506 const VkCmdBufferAllocInfo cmd = {
1507 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001508 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001509 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001510 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001511 .count = 1,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001512 };
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -06001513 err = vkAllocCommandBuffers(demo->device, &cmd, &demo->draw_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001514 assert(!err);
1515
Ian Elliotte2688a52015-10-16 18:02:43 -06001516 demo->swapchain.handle = VK_NULL_HANDLE;
1517
Chia-I Wuc19795a2014-09-13 11:12:55 +08001518 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001519 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001520 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001521 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001522 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001523 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001524 demo_prepare_pipeline(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001525
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001526 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001527 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001528
1529 demo_prepare_framebuffers(demo);
1530
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001531 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001532}
1533
Ian Elliotte14e9f92015-04-16 15:23:05 -06001534#ifdef _WIN32
1535static void demo_run(struct demo *demo)
1536{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001537 if (!demo->prepared)
1538 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001539 demo_draw(demo);
Tony Barbour4a6692d2015-10-08 13:45:45 -06001540
1541 if (demo->depthStencil > 0.99f)
1542 demo->depthIncrement = -0.001f;
1543 if (demo->depthStencil < 0.8f)
1544 demo->depthIncrement = 0.001f;
1545
1546 demo->depthStencil += demo->depthIncrement;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001547}
1548
1549// On MS-Windows, make this a global, so it's available to WndProc()
1550struct demo demo;
1551
1552// MS-Windows event handling function:
1553LRESULT CALLBACK WndProc(HWND hWnd,
1554 UINT uMsg,
1555 WPARAM wParam,
1556 LPARAM lParam)
1557{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001558 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001559
1560 switch(uMsg)
1561 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001562 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001563 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001564 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001565 PostQuitMessage(0);
1566 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001567 case WM_PAINT:
Cody Northrop67ff0a42015-09-09 10:21:49 -06001568 if (demo.prepared) {
1569 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001570 break;
Cody Northrop67ff0a42015-09-09 10:21:49 -06001571 }
Ian Elliottf3f80822015-10-21 15:14:07 -06001572 case WM_SIZE:
1573 demo_resize(&demo);
1574 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001575 default:
1576 break;
1577 }
1578 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1579}
1580
1581static void demo_create_window(struct demo *demo)
1582{
1583 WNDCLASSEX win_class;
1584
1585 // Initialize the window class structure:
1586 win_class.cbSize = sizeof(WNDCLASSEX);
1587 win_class.style = CS_HREDRAW | CS_VREDRAW;
1588 win_class.lpfnWndProc = WndProc;
1589 win_class.cbClsExtra = 0;
1590 win_class.cbWndExtra = 0;
1591 win_class.hInstance = demo->connection; // hInstance
1592 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1593 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1594 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1595 win_class.lpszMenuName = NULL;
1596 win_class.lpszClassName = demo->name;
1597 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1598 // Register window class:
1599 if (!RegisterClassEx(&win_class)) {
1600 // It didn't work, so try to give a useful error:
1601 printf("Unexpected error trying to start the application!\n");
1602 fflush(stdout);
1603 exit(1);
1604 }
1605 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001606 RECT wr = { 0, 0, demo->width, demo->height };
1607 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001608 demo->window = CreateWindowEx(0,
1609 demo->name, // class name
1610 demo->name, // app name
1611 WS_OVERLAPPEDWINDOW | // window style
1612 WS_VISIBLE |
1613 WS_SYSMENU,
1614 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001615 wr.right-wr.left, // width
1616 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001617 NULL, // handle to parent
1618 NULL, // handle to menu
1619 demo->connection, // hInstance
1620 NULL); // no extra parameters
1621 if (!demo->window) {
1622 // It didn't work, so try to give a useful error:
1623 printf("Cannot create a window in which to draw!\n");
1624 fflush(stdout);
1625 exit(1);
1626 }
1627}
1628#else // _WIN32
1629
Chia-I Wuc19795a2014-09-13 11:12:55 +08001630static void demo_handle_event(struct demo *demo,
1631 const xcb_generic_event_t *event)
1632{
1633 switch (event->response_type & 0x7f) {
1634 case XCB_EXPOSE:
1635 demo_draw(demo);
1636 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001637 case XCB_CLIENT_MESSAGE:
1638 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1639 (*demo->atom_wm_delete_window).atom) {
1640 demo->quit = true;
1641 }
1642 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001643 case XCB_KEY_RELEASE:
1644 {
1645 const xcb_key_release_event_t *key =
1646 (const xcb_key_release_event_t *) event;
1647
1648 if (key->detail == 0x9)
1649 demo->quit = true;
1650 }
1651 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001652 case XCB_DESTROY_NOTIFY:
1653 demo->quit = true;
1654 break;
Ian Elliotte2688a52015-10-16 18:02:43 -06001655 case XCB_CONFIGURE_NOTIFY:
1656 {
1657 const xcb_configure_notify_event_t *cfg =
1658 (const xcb_configure_notify_event_t *) event;
1659 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
1660 demo_resize(demo);
1661 }
1662 }
1663 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001664 default:
1665 break;
1666 }
1667}
1668
1669static void demo_run(struct demo *demo)
1670{
1671 xcb_flush(demo->connection);
1672
1673 while (!demo->quit) {
1674 xcb_generic_event_t *event;
1675
Tony Barbour4a6692d2015-10-08 13:45:45 -06001676 event = xcb_poll_for_event(demo->connection);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001677 if (event) {
1678 demo_handle_event(demo, event);
1679 free(event);
1680 }
Tony Barbour4a6692d2015-10-08 13:45:45 -06001681
1682 demo_draw(demo);
1683
1684 if (demo->depthStencil > 0.99f)
1685 demo->depthIncrement = -0.001f;
1686 if (demo->depthStencil < 0.8f)
1687 demo->depthIncrement = 0.001f;
1688
1689 demo->depthStencil += demo->depthIncrement;
1690
1691 // Wait for work to finish before updating MVP.
1692 vkDeviceWaitIdle(demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001693 }
1694}
1695
1696static void demo_create_window(struct demo *demo)
1697{
1698 uint32_t value_mask, value_list[32];
1699
1700 demo->window = xcb_generate_id(demo->connection);
1701
1702 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1703 value_list[0] = demo->screen->black_pixel;
1704 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001705 XCB_EVENT_MASK_EXPOSURE |
1706 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001707
1708 xcb_create_window(demo->connection,
1709 XCB_COPY_FROM_PARENT,
1710 demo->window, demo->screen->root,
1711 0, 0, demo->width, demo->height, 0,
1712 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1713 demo->screen->root_visual,
1714 value_mask, value_list);
1715
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001716 /* Magic code that will send notification when window is destroyed */
1717 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1718 "WM_PROTOCOLS");
1719 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1720
1721 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1722 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1723
1724 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1725 demo->window, (*reply).atom, 4, 32, 1,
1726 &(*demo->atom_wm_delete_window).atom);
1727 free(reply);
1728
Chia-I Wuc19795a2014-09-13 11:12:55 +08001729 xcb_map_window(demo->connection, demo->window);
1730}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001731#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001732
Ian Elliotte36b2082015-07-06 14:27:58 -06001733/*
1734 * Return 1 (true) if all layer names specified in check_names
1735 * can be found in given layer properties.
1736 */
1737static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1738 uint32_t layer_count, VkLayerProperties *layers)
1739{
1740 for (uint32_t i = 0; i < check_count; i++) {
1741 VkBool32 found = 0;
1742 for (uint32_t j = 0; j < layer_count; j++) {
1743 if (!strcmp(check_names[i], layers[j].layerName)) {
1744 found = 1;
1745 }
1746 }
1747 if (!found) {
1748 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1749 return 0;
1750 }
1751 }
1752 return 1;
1753}
1754
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001755void* VKAPI myalloc(
1756 void* pUserData,
1757 size_t size,
1758 size_t alignment,
1759 VkSystemAllocType allocType)
1760{
1761 return malloc(size);
1762}
1763void VKAPI myfree(
1764 void* pUserData,
1765 void* pMem)
1766{
Piers Daniell811eb742015-09-29 13:01:09 -06001767 free(pMem);
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001768}
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001769static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001770{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001771 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001772 char *extension_names[64];
1773 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001774 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001775 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001776 VkLayerProperties *instance_layers;
1777 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001778 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001779 uint32_t instance_layer_count = 0;
1780 uint32_t enabled_extension_count = 0;
1781 uint32_t enabled_layer_count = 0;
1782
Ian Elliotte36b2082015-07-06 14:27:58 -06001783 char *instance_validation_layers[] = {
1784 "MemTracker",
1785 };
1786
1787 char *device_validation_layers[] = {
1788 "MemTracker",
1789 };
1790
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001791 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001792 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001793 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001794 assert(!err);
1795
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001796 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001797 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001798 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001799
1800 if (demo->validate) {
1801 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1802 instance_layer_count, instance_layers);
1803 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001804 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001805 "required validation layer.\n\n"
1806 "Please look at the Getting Started guide for additional "
1807 "information.\n",
1808 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001809 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001810 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001811 }
1812
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001813 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001814 assert(!err);
1815
Tony Barbourd9955e42015-10-08 13:59:42 -06001816 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001817 memset(extension_names, 0, sizeof(extension_names));
1818 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001819 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001820 assert(!err);
1821 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001822 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001823 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001824 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001825 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001826 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001827 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001828 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001829 }
1830 }
1831 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001832 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001833 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001834 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001835 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001836 "Vulkan installable client driver (ICD) installed?\nPlease "
1837 "look at the Getting Started guide for additional "
1838 "information.\n",
1839 "vkCreateInstance Failure");
1840 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001841 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001842 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001843 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001844 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001845 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001846 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001847 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001848 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001849 };
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001850 VkAllocCallbacks cb = {
1851 .pUserData = NULL,
1852 .pfnAlloc = myalloc,
1853 .pfnFree = myfree,
1854 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001855 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001856 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001857 .pNext = NULL,
1858 .pAppInfo = &app,
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001859 .pAllocCb = &cb,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001860 .layerCount = enabled_layer_count,
1861 .ppEnabledLayerNames = (const char *const*) layer_names,
1862 .extensionCount = enabled_extension_count,
1863 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001864 };
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06001865 float queue_priorities[1] = { 0.0 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001866 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchterea975642015-09-16 16:23:55 -06001867 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1868 .pNext = NULL,
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001869 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001870 .queueCount = 1,
Courtney Goeltzenleuchterd3a8d362015-10-23 10:37:02 -06001871 .pQueuePriorities = queue_priorities
Chia-I Wuc19795a2014-09-13 11:12:55 +08001872 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001873 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001874
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001875 err = vkCreateInstance(&inst_info, &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++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001944 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
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,
1964 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001965 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1966 .extensionCount = enabled_extension_count,
1967 .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
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001996 err = vkCreateDevice(demo->gpu, &device, &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++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002207 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2208 }
Tony Barbour5aabff52015-10-08 14:26:24 -06002209 free(demo->framebuffers);
Tony Barbourde4124d2015-07-03 10:33:54 -06002210 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
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);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002216 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002217
Tony Barbourde4124d2015-07-03 10:33:54 -06002218 vkDestroyPipeline(demo->device, demo->pipeline);
2219 vkDestroyRenderPass(demo->device, demo->render_pass);
2220 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2221 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002222
Tony Barbourde4124d2015-07-03 10:33:54 -06002223 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002224 vkFreeMemory(demo->device, demo->vertices.mem);
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++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002227 vkDestroyImageView(demo->device, demo->textures[i].view);
2228 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002229 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002230 vkDestroySampler(demo->device, demo->textures[i].sampler);
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++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002234 vkDestroyImageView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002235 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002236
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002237 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbourde4124d2015-07-03 10:33:54 -06002238 vkDestroyImage(demo->device, demo->depth.image);
2239 vkFreeMemory(demo->device, demo->depth.mem);
2240
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
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002244 vkDestroyDevice(demo->device);
2245 vkDestroyInstance(demo->inst);
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
2260 // In order to properly resize the window, we must re-create the swapchain
2261 // AND redo the command buffers, etc.
2262 //
2263 // First, perform part of the demo_cleanup() function:
2264 demo->prepared = false;
2265
2266 for (i = 0; i < demo->swapchainImageCount; i++) {
2267 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2268 }
2269 free(demo->framebuffers);
2270 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
2271
2272 if (demo->setup_cmd) {
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002273 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->setup_cmd);
Ian Elliotte2688a52015-10-16 18:02:43 -06002274 }
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002275 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->draw_cmd);
Ian Elliotte2688a52015-10-16 18:02:43 -06002276 vkDestroyCommandPool(demo->device, demo->cmd_pool);
2277
2278 vkDestroyPipeline(demo->device, demo->pipeline);
2279 vkDestroyRenderPass(demo->device, demo->render_pass);
2280 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2281 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
2282
2283 vkDestroyBuffer(demo->device, demo->vertices.buf);
2284 vkFreeMemory(demo->device, demo->vertices.mem);
2285
2286 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2287 vkDestroyImageView(demo->device, demo->textures[i].view);
2288 vkDestroyImage(demo->device, demo->textures[i].image);
2289 vkFreeMemory(demo->device, demo->textures[i].mem);
2290 vkDestroySampler(demo->device, demo->textures[i].sampler);
2291 }
2292
2293 for (i = 0; i < demo->swapchainImageCount; i++) {
2294 vkDestroyImageView(demo->device, demo->buffers[i].view);
2295 }
2296
2297 vkDestroyImageView(demo->device, demo->depth.view);
2298 vkDestroyImage(demo->device, demo->depth.image);
2299 vkFreeMemory(demo->device, demo->depth.mem);
2300
Ian Elliott74bb2eb2015-10-27 11:06:33 -06002301 free(demo->buffers);
2302
Ian Elliotte2688a52015-10-16 18:02:43 -06002303 // Second, re-perform the demo_prepare() function, which will re-create the
2304 // swapchain:
2305 demo_prepare(demo);
2306}
2307
Ian Elliotte14e9f92015-04-16 15:23:05 -06002308#ifdef _WIN32
2309int APIENTRY WinMain(HINSTANCE hInstance,
2310 HINSTANCE hPrevInstance,
2311 LPSTR pCmdLine,
2312 int nCmdShow)
2313{
2314 MSG msg; // message
2315 bool done; // flag saying when app is complete
2316
2317 demo_init(&demo, hInstance, pCmdLine);
2318 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002319 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002320
2321 demo_prepare(&demo);
2322
2323 done = false; //initialize loop condition variable
2324 /* main message loop*/
2325 while(!done)
2326 {
Ian Elliott421107f2015-04-28 15:50:36 -06002327 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002328 if (msg.message == WM_QUIT) //check for a quit message
2329 {
2330 done = true; //if found, quit app
2331 }
2332 else
2333 {
2334 /* Translate and dispatch to event queue*/
2335 TranslateMessage(&msg);
2336 DispatchMessage(&msg);
2337 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002338 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002339 }
2340
2341 demo_cleanup(&demo);
2342
Tony Barboura938abb2015-04-22 11:36:22 -06002343 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002344}
2345#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002346int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002347{
2348 struct demo demo;
2349
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002350 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002351 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002352 demo_init_vk_swapchain(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002353
2354 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002355 demo_run(&demo);
2356
2357 demo_cleanup(&demo);
2358
Chia-I Wuc19795a2014-09-13 11:12:55 +08002359 return 0;
2360}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002361#endif // _WIN32