blob: 03a3ccbea3024438651ad64b2b8cbb210922c74e [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 Elliotte36b2082015-07-06 14:27:58 -060046#include <vk_wsi_swapchain.h>
47#include <vk_wsi_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 Wuc19795a2014-09-13 11:12:55 +080051#define DEMO_BUFFER_COUNT 2
Chia-I Wub043fe32014-10-06 15:30:33 +080052#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060053#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060054#define APP_SHORT_NAME "tri"
55#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080056
Ian Elliotte36b2082015-07-06 14:27:58 -060057#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
58
Tony Barbour22a30862015-04-22 09:02:32 -060059#if defined(NDEBUG) && defined(__GNUC__)
60#define U_ASSERT_ONLY __attribute__((unused))
61#else
62#define U_ASSERT_ONLY
63#endif
64
Ian Elliottcaa9f272015-04-28 11:35:02 -060065#ifdef _WIN32
66#define ERR_EXIT(err_msg, err_class) \
67 do { \
68 MessageBox(NULL, err_msg, err_class, MB_OK); \
69 exit(1); \
70 } while (0)
Ian Elliottcaa9f272015-04-28 11:35:02 -060071#else // _WIN32
72
73#define ERR_EXIT(err_msg, err_class) \
74 do { \
75 printf(err_msg); \
76 fflush(stdout); \
77 exit(1); \
78 } while (0)
79#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060080
Ian Elliotte36b2082015-07-06 14:27:58 -060081#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
82{ \
83 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
84 if (demo->fp##entrypoint == NULL) { \
85 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
86 "vkGetInstanceProcAddr Failure"); \
87 } \
88}
89
Ian Elliott1b6de092015-06-22 15:07:49 -060090#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
91{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060092 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060093 if (demo->fp##entrypoint == NULL) { \
94 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
95 "vkGetDeviceProcAddr Failure"); \
96 } \
97}
98
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060099struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600100 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700101
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600102 VkImage image;
103 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600104
Mark Lobodzinski23182612015-05-29 09:32:35 -0500105 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600106 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700107 int32_t tex_width, tex_height;
108};
109
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600110void dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600111 VkFlags msgFlags,
112 VkDbgObjectType objType,
113 uint64_t srcObject,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600114 size_t location,
115 int32_t msgCode,
116 const char* pLayerPrefix,
117 const char* pMsg,
118 void* pUserData)
119{
120 char *message = (char *) malloc(strlen(pMsg)+100);
121
122 assert (message);
123
124 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
125 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
126 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
127 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
128 } else {
129 return;
130 }
131
132#ifdef _WIN32
133 MessageBox(NULL, message, "Alert", MB_OK);
134#else
135 printf("%s\n",message);
136 fflush(stdout);
137#endif
138 free(message);
139}
140
Ian Elliotte36b2082015-07-06 14:27:58 -0600141typedef struct _SwapChainBuffers {
142 VkImage image;
143 VkCmdBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600144 VkImageView view;
Ian Elliotte36b2082015-07-06 14:27:58 -0600145} SwapChainBuffers;
146
Chia-I Wuc19795a2014-09-13 11:12:55 +0800147struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600148#ifdef _WIN32
149#define APP_NAME_STR_LEN 80
150 HINSTANCE connection; // hInstance - Windows Instance
151 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
152 HWND window; // hWnd - window handle
153#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800154 xcb_connection_t *connection;
155 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800156 xcb_window_t window;
157 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte36b2082015-07-06 14:27:58 -0600158 VkPlatformHandleXcbWSI platform_handle_xcb;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600159#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700160 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600161 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600162 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800163
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600164 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600165 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600166 VkDevice device;
167 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600168 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600169 VkQueueFamilyProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700170 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800171
172 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600173 VkFormat format;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600174 VkColorSpaceWSI color_space;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800175
Ian Elliotte36b2082015-07-06 14:27:58 -0600176 PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600177 PFN_vkGetSurfacePropertiesWSI fpGetSurfacePropertiesWSI;
178 PFN_vkGetSurfaceFormatsWSI fpGetSurfaceFormatsWSI;
179 PFN_vkGetSurfacePresentModesWSI fpGetSurfacePresentModesWSI;
Jon Ashburncedc15f2015-05-21 18:13:33 -0600180 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
181 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600182 PFN_vkGetSwapChainImagesWSI fpGetSwapChainImagesWSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600183 PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI;
Jon Ashburncedc15f2015-05-21 18:13:33 -0600184 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600185 VkSurfaceDescriptionWindowWSI surface_description;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600186 uint32_t swapChainImageCount;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800187 VkSwapChainWSI swap_chain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600188 SwapChainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800189
Ian Elliotte36b2082015-07-06 14:27:58 -0600190 VkCmdPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800191
Chia-I Wub043fe32014-10-06 15:30:33 +0800192 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600193 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800194
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600195 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500196 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600197 VkImageView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800198 } depth;
199
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600200 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800201
Chia-I Wu99621bc2014-10-08 11:52:22 +0800202 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500204 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800205
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600206 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600207 VkVertexInputBindingDescription vi_bindings[1];
208 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800209 } vertices;
210
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600211 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
212 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500213 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600214 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600215 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800216 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600217 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800218
Cody Northrope4bc6942015-08-26 10:01:32 -0600219 VkDynamicViewportState dynamic_viewport;
220 VkDynamicLineWidthState dynamic_line_width;
221 VkDynamicDepthBiasState dynamic_depth_bias;
222 VkDynamicBlendState dynamic_blend;
223 VkDynamicDepthBoundsState dynamic_depth_bounds;
Cody Northrop2605cb02015-08-18 15:21:16 -0600224 VkDynamicStencilState dynamic_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800225
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -0600226 VkShaderModule vert_shader_module;
227 VkShaderModule frag_shader_module;
228
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600229 VkDescriptorPool desc_pool;
230 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800231
Chia-I Wu76cd4222015-07-08 13:34:24 +0800232 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
233
Mark Lobodzinski72346292015-07-02 16:49:40 -0600234 VkPhysicalDeviceMemoryProperties memory_properties;
235
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600236 bool validate;
237 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Ian Elliotte36b2082015-07-06 14:27:58 -0600238 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600239 VkDbgMsgCallback msg_callback;
240
Chia-I Wuc19795a2014-09-13 11:12:55 +0800241 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600242 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600243 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800244};
245
Mark Lobodzinski72346292015-07-02 16:49:40 -0600246static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
247{
248 // Search memtypes to find first index with those properties
249 for (uint32_t i = 0; i < 32; i++) {
250 if ((typeBits & 1) == 1) {
251 // Type is available, does it match user properties?
252 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
253 *typeIndex = i;
254 return VK_SUCCESS;
255 }
256 }
257 typeBits >>= 1;
258 }
259 // No memory types matched, return failure
260 return VK_UNSUPPORTED;
261}
262
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600263static void demo_flush_init_cmd(struct demo *demo)
264{
Tony Barbour22a30862015-04-22 09:02:32 -0600265 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600266
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600267 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600268 return;
269
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600270 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600271 assert(!err);
272
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600273 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600274 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275
Tony Barbourde4124d2015-07-03 10:33:54 -0600276 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600277 assert(!err);
278
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600279 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600280 assert(!err);
281
Tony Barbourde4124d2015-07-03 10:33:54 -0600282 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600283 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600284}
285
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600286static void demo_set_image_layout(
287 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600288 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400289 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600290 VkImageLayout old_image_layout,
291 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600292{
Tony Barbour22a30862015-04-22 09:02:32 -0600293 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600294
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600295 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600296 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600297 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600298 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600299 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800300 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600301 .flags = 0,
302 };
303
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600304 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600305 assert(!err);
306
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600307 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600308 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600310 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600311 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600312 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600313 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600314 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600315 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600316 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600317 }
318
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600319 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600320 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600321 .pNext = NULL,
322 .outputMask = 0,
323 .inputMask = 0,
324 .oldLayout = old_image_layout,
325 .newLayout = new_image_layout,
326 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400327 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600328 };
329
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600330 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600331 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600332 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600333 }
334
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600335 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600336 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600337 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600338 }
339
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600340 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600341
Tony Barbourc2e987e2015-06-29 16:20:35 -0600342 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
343 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600344
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600345 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600346}
347
Chia-I Wuc19795a2014-09-13 11:12:55 +0800348static void demo_draw_build_cmd(struct demo *demo)
349{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800350 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600351 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600352 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600353 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600354 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600355 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600356 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600357 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn53d27af2014-12-31 17:08:35 -0700358 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800359 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600360 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
361 [1] = { .depthStencil = { 0.9f, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800362 };
363 const VkRenderPassBeginInfo rp_begin = {
364 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
365 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800366 .renderPass = demo->render_pass,
367 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800368 .renderArea.offset.x = 0,
369 .renderArea.offset.y = 0,
370 .renderArea.extent.width = demo->width,
371 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600372 .clearValueCount = 2,
373 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700374 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800375 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800376
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600377 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800378 assert(!err);
379
Chia-I Wuc278df82015-07-07 11:50:03 +0800380 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600381 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800382 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600383 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600384 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800385
Cody Northrope4bc6942015-08-26 10:01:32 -0600386 vkCmdBindDynamicViewportState(demo->draw_cmd, demo->dynamic_viewport);
387 vkCmdBindDynamicLineWidthState(demo->draw_cmd, demo->dynamic_line_width);
388 vkCmdBindDynamicDepthBiasState(demo->draw_cmd, demo->dynamic_depth_bias);
389 vkCmdBindDynamicBlendState(demo->draw_cmd, demo->dynamic_blend);
390 vkCmdBindDynamicDepthBoundsState(demo->draw_cmd, demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -0600391 vkCmdBindDynamicStencilState(demo->draw_cmd, demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800392
Tony Barbour8205d902015-04-16 15:59:00 -0600393 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600394 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800395
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600396 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800397 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800398
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600399 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800400 assert(!err);
401}
402
403static void demo_draw(struct demo *demo)
404{
Tony Barbour22a30862015-04-22 09:02:32 -0600405 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600406 VkSemaphore presentCompleteSemaphore;
407 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
408 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
409 .pNext = NULL,
410 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
411 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800412
Ian Elliotte36b2082015-07-06 14:27:58 -0600413 err = vkCreateSemaphore(demo->device,
414 &presentCompleteSemaphoreCreateInfo,
415 &presentCompleteSemaphore);
416 assert(!err);
417
418 // Get the index of the next available swapchain image:
419 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
420 UINT64_MAX,
421 presentCompleteSemaphore,
422 &demo->current_buffer);
423 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
424 // return codes
425 assert(!err);
426
427 // Wait for the present complete semaphore to be signaled to ensure
428 // that the image won't be rendered to until the presentation
429 // engine has fully released ownership to the application, and it is
430 // okay to render to the image.
431 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
432
433// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Chia-I Wuc19795a2014-09-13 11:12:55 +0800434 demo_draw_build_cmd(demo);
Tony Barbourde4124d2015-07-03 10:33:54 -0600435 VkFence nullFence = { VK_NULL_HANDLE };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800436
Tony Barbourde4124d2015-07-03 10:33:54 -0600437 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800438 assert(!err);
439
Ian Elliotte36b2082015-07-06 14:27:58 -0600440 VkPresentInfoWSI present = {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600441 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
Ian Elliotte36b2082015-07-06 14:27:58 -0600442 .pNext = NULL,
443 .swapChainCount = 1,
444 .swapChains = &demo->swap_chain,
445 .imageIndices = &demo->current_buffer,
446 };
447
448// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburncedc15f2015-05-21 18:13:33 -0600449 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliotte36b2082015-07-06 14:27:58 -0600450 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
451 // return codes
Chia-I Wuc19795a2014-09-13 11:12:55 +0800452 assert(!err);
453
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800454 err = vkQueueWaitIdle(demo->queue);
455 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600456
457 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
458 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800459}
460
461static void demo_prepare_buffers(struct demo *demo)
462{
Ian Elliotte36b2082015-07-06 14:27:58 -0600463 VkResult U_ASSERT_ONLY err;
464
465 // Check the surface proprties and formats
Ian Elliott7fe115d2015-08-07 15:56:59 -0600466 VkSurfacePropertiesWSI surfProperties;
467 err = demo->fpGetSurfacePropertiesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600468 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600469 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600470 assert(!err);
471
Ian Elliott7fe115d2015-08-07 15:56:59 -0600472 uint32_t presentModeCount;
473 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600474 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600475 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600476 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600477 VkPresentModeWSI *presentModes =
478 (VkPresentModeWSI *)malloc(presentModeCount * sizeof(VkPresentModeWSI));
479 assert(presentModes);
480 err = demo->fpGetSurfacePresentModesWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -0600481 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600482 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600483 assert(!err);
484
485 VkExtent2D swapChainExtent;
486 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600487 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600488 {
489 // If the surface size is undefined, the size is set to
490 // the size of the images requested.
491 swapChainExtent.width = demo->width;
492 swapChainExtent.height = demo->height;
493 }
494 else
495 {
496 // If the surface size is defined, the swap chain size must match
Ian Elliott7fe115d2015-08-07 15:56:59 -0600497 swapChainExtent = surfProperties.currentExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600498 }
499
500 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3fc49c22015-07-27 13:53:11 -0600501 // tearing mode. If not, try IMMEDIATE which will usually be available,
502 // and is fastest (though it tears). If not, fall back to FIFO which is
503 // always available.
504 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_FIFO_WSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600505 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600506 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600507 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
508 break;
509 }
Ian Elliott3fc49c22015-07-27 13:53:11 -0600510 if ((swapChainPresentMode != VK_PRESENT_MODE_MAILBOX_WSI) &&
Ian Elliott7fe115d2015-08-07 15:56:59 -0600511 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_WSI)) {
Ian Elliott3fc49c22015-07-27 13:53:11 -0600512 swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
513 }
Ian Elliotte36b2082015-07-06 14:27:58 -0600514 }
515
516 // Determine the number of VkImage's to use in the swap chain (we desire to
517 // own only 1 image at a time, besides the images being displayed and
518 // queued for display):
Ian Elliott7fe115d2015-08-07 15:56:59 -0600519 uint32_t desiredNumberOfSwapChainImages = surfProperties.minImageCount + 1;
520 if ((surfProperties.maxImageCount > 0) &&
521 (desiredNumberOfSwapChainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600522 {
523 // Application must settle for fewer images than desired:
Ian Elliott7fe115d2015-08-07 15:56:59 -0600524 desiredNumberOfSwapChainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600525 }
526
Tony Barbour248080b2015-07-23 10:34:15 -0600527 VkSurfaceTransformWSI preTransform;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600528 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
Ian Elliotte36b2082015-07-06 14:27:58 -0600529 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
530 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600531 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600532 }
533
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800534 const VkSwapChainCreateInfoWSI swap_chain = {
535 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
536 .pNext = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600537 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
538 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800539 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600540 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800541 .imageExtent = {
Ian Elliotte36b2082015-07-06 14:27:58 -0600542 .width = swapChainExtent.width,
543 .height = swapChainExtent.height,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800544 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600545 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600546 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800547 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600548 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
549 .queueFamilyCount = 0,
550 .pQueueFamilyIndices = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600551 .presentMode = swapChainPresentMode,
552 .oldSwapChain.handle = 0,
553 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800554 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600555 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800556
Jon Ashburncedc15f2015-05-21 18:13:33 -0600557 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800558 assert(!err);
559
Ian Elliott7fe115d2015-08-07 15:56:59 -0600560 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
561 &demo->swapChainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600562 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800563
Ian Elliott7fe115d2015-08-07 15:56:59 -0600564 VkImage* swapChainImages =
565 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliotte36b2082015-07-06 14:27:58 -0600566 assert(swapChainImages);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600567 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
568 &demo->swapChainImageCount,
569 swapChainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600570 assert(!err);
571
Ian Elliotte36b2082015-07-06 14:27:58 -0600572 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
573 assert(demo->buffers);
574
575 for (i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600576 VkImageViewCreateInfo color_attachment_view = {
577 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800578 .pNext = NULL,
579 .format = demo->format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600580 .channels = {
581 .r = VK_CHANNEL_SWIZZLE_R,
582 .g = VK_CHANNEL_SWIZZLE_G,
583 .b = VK_CHANNEL_SWIZZLE_B,
584 .a = VK_CHANNEL_SWIZZLE_A,
585 },
586 .subresourceRange = {
587 .aspect = VK_IMAGE_ASPECT_COLOR,
588 .baseMipLevel = 0,
589 .mipLevels = 1,
590 .baseArraySlice = 0,
591 .arraySize = 1
592 },
593 .viewType = VK_IMAGE_VIEW_TYPE_2D,
594 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800595 };
596
Ian Elliott7fe115d2015-08-07 15:56:59 -0600597 demo->buffers[i].image = swapChainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500598
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600599 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400600 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600601 VK_IMAGE_LAYOUT_UNDEFINED,
602 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600603
Chia-I Wuc19795a2014-09-13 11:12:55 +0800604 color_attachment_view.image = demo->buffers[i].image;
605
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600606 err = vkCreateImageView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800607 &color_attachment_view, &demo->buffers[i].view);
608 assert(!err);
609 }
Piers Daniell886be472015-02-23 16:23:13 -0700610
611 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800612}
613
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800614static void demo_prepare_depth(struct demo *demo)
615{
Tony Barbour8205d902015-04-16 15:59:00 -0600616 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600617 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600618 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800619 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600620 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800621 .format = depth_format,
622 .extent = { demo->width, demo->height, 1 },
623 .mipLevels = 1,
624 .arraySize = 1,
625 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600626 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600627 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800628 .flags = 0,
629 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600630 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600631 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500632 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800633 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600634 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800635 };
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600636 VkImageViewCreateInfo view = {
637 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800638 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600639 .image.handle = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600640 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600641 .subresourceRange = {
Cody Northropec2dc652015-09-03 16:09:28 -0600642 .aspect = VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600643 .baseMipLevel = 0,
644 .mipLevels = 1,
645 .baseArraySlice = 0,
646 .arraySize = 1
647 },
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800648 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600649 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800650 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700651
Mark Lobodzinski23182612015-05-29 09:32:35 -0500652 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600653 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800654
655 demo->depth.format = depth_format;
656
657 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600658 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800659 &demo->depth.image);
660 assert(!err);
661
Mark Lobodzinski72346292015-07-02 16:49:40 -0600662 /* get memory requirements for this object */
Tony Barbourde4124d2015-07-03 10:33:54 -0600663 err = vkGetImageMemoryRequirements(demo->device, demo->depth.image,
664 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600665
666 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500667 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600668 err = memory_type_from_properties(demo,
669 mem_reqs.memoryTypeBits,
670 VK_MEMORY_PROPERTY_DEVICE_ONLY,
671 &mem_alloc.memoryTypeIndex);
672 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800673
Mark Lobodzinski23182612015-05-29 09:32:35 -0500674 /* allocate memory */
675 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
676 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800677
Mark Lobodzinski23182612015-05-29 09:32:35 -0500678 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600679 err = vkBindImageMemory(demo->device, demo->depth.image,
680 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500681 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800682
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600683 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400684 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600685 VK_IMAGE_LAYOUT_UNDEFINED,
686 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600687
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800688 /* create image view */
689 view.image = demo->depth.image;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600690 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800691 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800692}
693
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700694static void demo_prepare_texture_image(struct demo *demo,
695 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600696 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600697 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600698 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600699 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800700{
Tony Barbour8205d902015-04-16 15:59:00 -0600701 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600702 const int32_t tex_width = 2;
703 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600704 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800705
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600706 tex_obj->tex_width = tex_width;
707 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700708
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600709 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600710 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700711 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600712 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700713 .format = tex_format,
714 .extent = { tex_width, tex_height, 1 },
715 .mipLevels = 1,
716 .arraySize = 1,
717 .samples = 1,
718 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600719 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700720 .flags = 0,
721 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600722 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600723 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500724 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700725 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600726 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700727 };
728
Mark Lobodzinski23182612015-05-29 09:32:35 -0500729 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700730
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600731 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600732 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700733 assert(!err);
734
Tony Barbourde4124d2015-07-03 10:33:54 -0600735 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600736
737 mem_alloc.allocationSize = mem_reqs.size;
738 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
739 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700740
Mark Lobodzinski23182612015-05-29 09:32:35 -0500741 /* allocate memory */
742 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
743 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700744
Mark Lobodzinski23182612015-05-29 09:32:35 -0500745 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600746 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500747 tex_obj->mem, 0);
748 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700749
Tony Barbour8205d902015-04-16 15:59:00 -0600750 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600751 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600752 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700753 .mipLevel = 0,
754 .arraySlice = 0,
755 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600756 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700757 void *data;
758 int32_t x, y;
759
Tony Barbour426b9052015-06-24 16:06:58 -0600760 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
761 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700762
Mark Lobodzinski23182612015-05-29 09:32:35 -0500763 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700764 assert(!err);
765
766 for (y = 0; y < tex_height; y++) {
767 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
768 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700769 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700770 }
771
Mark Lobodzinski23182612015-05-29 09:32:35 -0500772 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700773 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700774 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600775
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600776 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600777 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400778 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600779 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600780 tex_obj->imageLayout);
781 /* 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 -0700782}
783
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500784static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700785{
786 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600787 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600788 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700789}
790
791static void demo_prepare_textures(struct demo *demo)
792{
Tony Barbour8205d902015-04-16 15:59:00 -0600793 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600794 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700795 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
796 { 0xffff0000, 0xff00ff00 },
797 };
Tony Barbour22a30862015-04-22 09:02:32 -0600798 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700799 uint32_t i;
800
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600801 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700802 assert(!err);
803
804 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600805 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700806 /* Device can texture using linear textures */
807 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600808 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600809 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700810 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600811 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700812
813 memset(&staging_texture, 0, sizeof(staging_texture));
814 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600815 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700816
817 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600818 VK_IMAGE_TILING_OPTIMAL,
819 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
820 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700821
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600822 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400823 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600824 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600825 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700826
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600827 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400828 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600829 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600830 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700831
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600832 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600833 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700834 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600835 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700836 .destOffset = { 0, 0, 0 },
837 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
838 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600839 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600840 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
841 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600842 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700843
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600844 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400845 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600846 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600847 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700848
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600849 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700850
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600851 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700852 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600853 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700854 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700855 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700856
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600857 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600858 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800859 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600860 .magFilter = VK_TEX_FILTER_NEAREST,
861 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600862 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600863 .addressU = VK_TEX_ADDRESS_WRAP,
864 .addressV = VK_TEX_ADDRESS_WRAP,
865 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800866 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700867 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600868 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800869 .minLod = 0.0f,
870 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600871 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600872 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800873 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600874 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600875 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800876 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600877 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600878 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700879 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600880 .channels = { VK_CHANNEL_SWIZZLE_R,
881 VK_CHANNEL_SWIZZLE_G,
882 VK_CHANNEL_SWIZZLE_B,
883 VK_CHANNEL_SWIZZLE_A, },
884 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600885 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +0800886 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700887
Chia-I Wub043fe32014-10-06 15:30:33 +0800888 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600889 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800890 &demo->textures[i].sampler);
891 assert(!err);
892
Chia-I Wub043fe32014-10-06 15:30:33 +0800893 /* create image view */
894 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600895 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700896 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800897 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800898 }
899}
900
Chia-I Wu99621bc2014-10-08 11:52:22 +0800901static void demo_prepare_vertices(struct demo *demo)
902{
903 const float vb[3][5] = {
904 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800905 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
906 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800907 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
908 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600909 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600910 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800911 .pNext = NULL,
912 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600913 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800914 .flags = 0,
915 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600916 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600917 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500918 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800919 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600920 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800921 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500922 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600923 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800924 void *data;
925
926 memset(&demo->vertices, 0, sizeof(demo->vertices));
927
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600928 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800929 assert(!err);
930
Tony Barbourde4124d2015-07-03 10:33:54 -0600931 err = vkGetBufferMemoryRequirements(demo->device,
932 demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800933
Mark Lobodzinski72346292015-07-02 16:49:40 -0600934 mem_alloc.allocationSize = mem_reqs.size;
935 err = memory_type_from_properties(demo,
936 mem_reqs.memoryTypeBits,
937 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
938 &mem_alloc.memoryTypeIndex);
939 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800940
Mark Lobodzinski23182612015-05-29 09:32:35 -0500941 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
942 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800943
Mark Lobodzinski23182612015-05-29 09:32:35 -0500944 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
945 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800946
Mark Lobodzinski23182612015-05-29 09:32:35 -0500947 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800948
Mark Lobodzinski23182612015-05-29 09:32:35 -0500949 err = vkUnmapMemory(demo->device, demo->vertices.mem);
950 assert(!err);
951
Tony Barbourde4124d2015-07-03 10:33:54 -0600952 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500953 demo->vertices.mem, 0);
954 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800955
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600956 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800957 demo->vertices.vi.pNext = NULL;
958 demo->vertices.vi.bindingCount = 1;
959 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
960 demo->vertices.vi.attributeCount = 2;
961 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
962
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600963 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800964 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600965 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800966
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600967 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
968 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600969 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800970 demo->vertices.vi_attrs[0].offsetInBytes = 0;
971
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600972 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
973 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600974 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800975 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800976}
977
Chia-I Wuf8385062015-01-04 16:27:24 +0800978static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800979{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600980 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600981 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800982 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600983 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800984 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800985 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600986 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600987 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800988 .pNext = NULL,
989 .count = 1,
990 .pBinding = &layout_binding,
991 };
Tony Barbour22a30862015-04-22 09:02:32 -0600992 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800993
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600994 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800995 &descriptor_layout, &demo->desc_layout);
996 assert(!err);
997
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500998 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
999 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1000 .pNext = NULL,
1001 .descriptorSetCount = 1,
1002 .pSetLayouts = &demo->desc_layout,
1003 };
1004
1005 err = vkCreatePipelineLayout(demo->device,
1006 &pPipelineLayoutCreateInfo,
1007 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001008 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001009}
1010
Chia-I Wu76cd4222015-07-08 13:34:24 +08001011static void demo_prepare_render_pass(struct demo *demo)
1012{
Chia-I Wuc278df82015-07-07 11:50:03 +08001013 const VkAttachmentDescription attachments[2] = {
1014 [0] = {
1015 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1016 .pNext = NULL,
1017 .format = demo->format,
1018 .samples = 1,
1019 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1020 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1021 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1022 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1023 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1024 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1025 },
1026 [1] = {
1027 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1028 .pNext = NULL,
1029 .format = demo->depth.format,
1030 .samples = 1,
1031 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1032 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1033 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1034 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1035 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1036 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1037 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001038 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001039 const VkAttachmentReference color_reference = {
1040 .attachment = 0,
1041 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1042 };
1043 const VkSubpassDescription subpass = {
1044 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1045 .pNext = NULL,
1046 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1047 .flags = 0,
1048 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001049 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001050 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001051 .pColorAttachments = &color_reference,
1052 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001053 .depthStencilAttachment = {
1054 .attachment = 1,
1055 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1056 },
1057 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001058 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001059 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001060 const VkRenderPassCreateInfo rp_info = {
1061 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1062 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001063 .attachmentCount = 2,
1064 .pAttachments = attachments,
1065 .subpassCount = 1,
1066 .pSubpasses = &subpass,
1067 .dependencyCount = 0,
1068 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001069 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001070 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001071
1072 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1073 assert(!err);
1074}
1075
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001076static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001077 VkShaderStage stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001078 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001079 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001080 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001081{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001082 VkShaderModuleCreateInfo moduleCreateInfo;
1083 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001084 VkShader shader;
1085 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001086
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001087
1088 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1089 moduleCreateInfo.pNext = NULL;
1090
1091 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1092 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001093
Cody Northrop75db0322015-05-28 11:27:16 -06001094 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001095 moduleCreateInfo.codeSize = size;
1096 moduleCreateInfo.pCode = code;
1097 moduleCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001098 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001099 if (err) {
1100 free((void *) moduleCreateInfo.pCode);
1101 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001102
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001103 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001104 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001105 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001106 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001107 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001108 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001109 } else {
1110 // Create fake SPV structure to feed GLSL
1111 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001112 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1113 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1114 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001115
Cody Northrop75db0322015-05-28 11:27:16 -06001116 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001117 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1118 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1119 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1120 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001121
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001122 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001123 if (err) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001124 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001125 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001126
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001127 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001128 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001129 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001130 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001131 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001132 assert(!err);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001133 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001134 return shader;
1135}
1136
Cody Northrop75db0322015-05-28 11:27:16 -06001137char *demo_read_spv(const char *filename, size_t *psize)
1138{
1139 long int size;
1140 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001141 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001142
1143 FILE *fp = fopen(filename, "rb");
1144 if (!fp) return NULL;
1145
1146 fseek(fp, 0L, SEEK_END);
1147 size = ftell(fp);
1148
1149 fseek(fp, 0L, SEEK_SET);
1150
1151 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001152 retVal = fread(shader_code, size, 1, fp);
1153 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001154
1155 *psize = size;
1156
1157 return shader_code;
1158}
1159
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001160static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001161{
Cody Northrop75db0322015-05-28 11:27:16 -06001162 if (!demo->use_glsl) {
1163 void *vertShaderCode;
1164 size_t size;
1165
1166 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
1167
1168 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001169 &demo->vert_shader_module,
1170 vertShaderCode, size);
Cody Northrop75db0322015-05-28 11:27:16 -06001171 } else {
1172 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001173 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001174 "#extension GL_ARB_separate_shader_objects : enable\n"
1175 "#extension GL_ARB_shading_language_420pack : enable\n"
1176 "layout (location = 0) in vec4 pos;\n"
1177 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001178 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001179 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001180 " texcoord = attr;\n"
1181 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001182 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001183
Cody Northrop75db0322015-05-28 11:27:16 -06001184 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001185 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001186 (const void *) vertShaderText,
1187 strlen(vertShaderText));
1188 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001189}
1190
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001191static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001192{
Cody Northrop75db0322015-05-28 11:27:16 -06001193 if (!demo->use_glsl) {
1194 void *fragShaderCode;
1195 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001196
Cody Northrop75db0322015-05-28 11:27:16 -06001197 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
1198
1199 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001200 &demo->frag_shader_module,
1201 fragShaderCode, size);
Cody Northrop75db0322015-05-28 11:27:16 -06001202 } else {
1203 static const char *fragShaderText =
1204 "#version 140\n"
1205 "#extension GL_ARB_separate_shader_objects : enable\n"
1206 "#extension GL_ARB_shading_language_420pack : enable\n"
1207 "layout (binding = 0) uniform sampler2D tex;\n"
1208 "layout (location = 0) in vec2 texcoord;\n"
1209 "layout (location = 0) out vec4 uFragColor;\n"
1210 "void main() {\n"
1211 " uFragColor = texture(tex, texcoord);\n"
1212 "}\n";
1213
1214 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001215 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001216 (const void *) fragShaderText,
1217 strlen(fragShaderText));
1218 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001219}
1220
1221static void demo_prepare_pipeline(struct demo *demo)
1222{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001223 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001224 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001225
Tony Barboure307f582015-07-10 15:29:03 -06001226 VkPipelineVertexInputStateCreateInfo vi;
1227 VkPipelineInputAssemblyStateCreateInfo ia;
1228 VkPipelineRasterStateCreateInfo rs;
1229 VkPipelineColorBlendStateCreateInfo cb;
1230 VkPipelineDepthStencilStateCreateInfo ds;
1231 VkPipelineViewportStateCreateInfo vp;
1232 VkPipelineMultisampleStateCreateInfo ms;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001233
Tony Barbour22a30862015-04-22 09:02:32 -06001234 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001235
1236 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001237 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001238 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001239
Chia-I Wu8d29d022014-10-08 12:14:39 +08001240 vi = demo->vertices.vi;
1241
Chia-I Wuc19795a2014-09-13 11:12:55 +08001242 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001243 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001244 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001245
1246 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001247 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001248 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001249 rs.cullMode = VK_CULL_MODE_BACK;
1250 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001251 rs.depthClipEnable = VK_TRUE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001252 rs.rasterizerDiscardEnable = VK_FALSE;
1253 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001254
1255 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001256 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1257 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001258 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001259 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001260 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001261 cb.attachmentCount = 1;
1262 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001263
Tony Barbourfa6cac72015-01-16 14:27:35 -07001264 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001265 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001266 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001267
1268 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001269 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001270 ds.depthTestEnable = VK_TRUE;
1271 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001272 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001273 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001274 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1275 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001276 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001277 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001278 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001279
Tony Barbourfa6cac72015-01-16 14:27:35 -07001280 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001281 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001282 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001283 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001284
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001285 // Two stages: vs and fs
1286 pipeline.stageCount = 2;
1287 VkPipelineShaderStageCreateInfo shaderStages[2];
1288 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1289
1290 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1291 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1292 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001293
1294 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1295 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1296 shaderStages[1].shader = demo_prepare_fs(demo);
1297
Tony Barboure307f582015-07-10 15:29:03 -06001298 pipeline.pVertexInputState = &vi;
1299 pipeline.pInputAssemblyState = &ia;
1300 pipeline.pRasterState = &rs;
1301 pipeline.pColorBlendState = &cb;
1302 pipeline.pMultisampleState = &ms;
1303 pipeline.pViewportState = &vp;
1304 pipeline.pDepthStencilState = &ds;
1305 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001306 pipeline.renderPass = demo->render_pass;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001307
Jon Ashburn0d60d272015-07-09 15:02:25 -06001308 memset(&pipelineCache, 0, sizeof(pipelineCache));
1309 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1310
1311 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1312 assert(!err);
1313 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001314 assert(!err);
1315
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001316 err = vkDestroyPipelineCache(demo->device, demo->pipelineCache);
1317 assert(!err);
1318
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001319 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001320 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001321 }
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001322 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1323 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001324}
1325
1326static void demo_prepare_dynamic_states(struct demo *demo)
1327{
Tony Barbourde4124d2015-07-03 10:33:54 -06001328 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northrope4bc6942015-08-26 10:01:32 -06001329 VkDynamicLineWidthStateCreateInfo line_width;
1330 VkDynamicDepthBiasStateCreateInfo depth_bias;
1331 VkDynamicBlendStateCreateInfo blend;
1332 VkDynamicDepthBoundsStateCreateInfo depth_bounds;
Cody Northrop2605cb02015-08-18 15:21:16 -06001333 VkDynamicStencilStateCreateInfo stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001334 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001335
Tony Barbourfa6cac72015-01-16 14:27:35 -07001336 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001337 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001338 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001339 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001340 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001341 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001342 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001343 viewport.minDepth = (float) 0.0f;
1344 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001345 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001346 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001347 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001348 scissor.extent.width = demo->width;
1349 scissor.extent.height = demo->height;
1350 scissor.offset.x = 0;
1351 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001352 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001353
Cody Northrope4bc6942015-08-26 10:01:32 -06001354 memset(&line_width, 0, sizeof(line_width));
1355 line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
1356 line_width.lineWidth = 1.0;
Cody Northropf5bd2252015-08-17 11:10:49 -06001357
Cody Northrope4bc6942015-08-26 10:01:32 -06001358 memset(&depth_bias, 0, sizeof(depth_bias));
1359 depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
1360 depth_bias.depthBias = 0.0f;
1361 depth_bias.depthBiasClamp = 0.0f;
1362 depth_bias.slopeScaledDepthBias = 0.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001363
Cody Northrope4bc6942015-08-26 10:01:32 -06001364 memset(&blend, 0, sizeof(blend));
1365 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
1366 blend.blendConst[0] = 1.0f;
1367 blend.blendConst[1] = 1.0f;
1368 blend.blendConst[2] = 1.0f;
1369 blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001370
Cody Northrope4bc6942015-08-26 10:01:32 -06001371 memset(&depth_bounds, 0, sizeof(depth_bounds));
1372 depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
1373 depth_bounds.minDepthBounds = 0.0f;
1374 depth_bounds.maxDepthBounds = 1.0f;
Cody Northrop2605cb02015-08-18 15:21:16 -06001375
1376 memset(&stencil, 0, sizeof(stencil));
1377 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
1378 stencil.stencilReference = 0;
Cody Northrope4bc6942015-08-26 10:01:32 -06001379 stencil.stencilCompareMask = 0xff;
Cody Northrop2605cb02015-08-18 15:21:16 -06001380 stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001381
Cody Northrope4bc6942015-08-26 10:01:32 -06001382 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001383 assert(!err);
1384
Cody Northrope4bc6942015-08-26 10:01:32 -06001385 err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width);
Cody Northropf5bd2252015-08-17 11:10:49 -06001386 assert(!err);
1387
Cody Northrope4bc6942015-08-26 10:01:32 -06001388 err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001389 assert(!err);
1390
Cody Northrope4bc6942015-08-26 10:01:32 -06001391 err = vkCreateDynamicBlendState(demo->device,
1392 &blend, &demo->dynamic_blend);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001393 assert(!err);
1394
Cody Northrope4bc6942015-08-26 10:01:32 -06001395 err = vkCreateDynamicDepthBoundsState(demo->device,
1396 &depth_bounds, &demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -06001397 assert(!err);
1398
1399 err = vkCreateDynamicStencilState(demo->device,
1400 &stencil, &stencil, &demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001401 assert(!err);
1402}
1403
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001404static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001405{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001406 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001407 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001408 .count = DEMO_TEXTURE_COUNT,
1409 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001410 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001411 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001412 .pNext = NULL,
1413 .count = 1,
1414 .pTypeCount = &type_count,
1415 };
Tony Barbour22a30862015-04-22 09:02:32 -06001416 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001417
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001418 err = vkCreateDescriptorPool(demo->device,
1419 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001420 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001421 assert(!err);
1422}
1423
1424static void demo_prepare_descriptor_set(struct demo *demo)
1425{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001426 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1427 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001428 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001429 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001430
Mike Stroyan230e6252015-04-17 12:36:38 -06001431 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001432 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001433 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001434 &demo->desc_set);
1435 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001436
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001437 memset(&tex_descs, 0, sizeof(tex_descs));
1438 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1439 tex_descs[i].sampler = demo->textures[i].sampler;
1440 tex_descs[i].imageView = demo->textures[i].view;
1441 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1442 }
1443
1444 memset(&write, 0, sizeof(write));
1445 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1446 write.destSet = demo->desc_set;
1447 write.count = DEMO_TEXTURE_COUNT;
1448 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1449 write.pDescriptors = tex_descs;
1450
1451 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1452 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001453}
1454
Chia-I Wu76cd4222015-07-08 13:34:24 +08001455static void demo_prepare_framebuffers(struct demo *demo)
1456{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001457 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001458 attachments[1] = demo->depth.view;
1459
Chia-I Wu76cd4222015-07-08 13:34:24 +08001460 const VkFramebufferCreateInfo fb_info = {
1461 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1462 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001463 .renderPass = demo->render_pass,
1464 .attachmentCount = 2,
1465 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001466 .width = demo->width,
1467 .height = demo->height,
1468 .layers = 1,
1469 };
1470 VkResult U_ASSERT_ONLY err;
1471 uint32_t i;
1472
1473 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001474 attachments[0]= demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001475 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1476 assert(!err);
1477 }
1478}
1479
Chia-I Wuc19795a2014-09-13 11:12:55 +08001480static void demo_prepare(struct demo *demo)
1481{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001482 VkResult U_ASSERT_ONLY err;
1483
1484 const VkCmdPoolCreateInfo cmd_pool_info = {
1485 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1486 .pNext = NULL,
1487 .queueFamilyIndex = demo->graphics_queue_node_index,
1488 .flags = 0,
1489 };
1490 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1491 assert(!err);
1492
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001493 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001494 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001495 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001496 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001497 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001498 .flags = 0,
1499 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001500 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1501 assert(!err);
1502
Chia-I Wuc19795a2014-09-13 11:12:55 +08001503 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001504 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001505 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001506 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001507 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001508 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001509 demo_prepare_pipeline(demo);
1510 demo_prepare_dynamic_states(demo);
1511
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001512 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001513 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001514
1515 demo_prepare_framebuffers(demo);
1516
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001517 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001518}
1519
Ian Elliotte14e9f92015-04-16 15:23:05 -06001520#ifdef _WIN32
1521static void demo_run(struct demo *demo)
1522{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001523 if (!demo->prepared)
1524 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001525 demo_draw(demo);
1526}
1527
1528// On MS-Windows, make this a global, so it's available to WndProc()
1529struct demo demo;
1530
1531// MS-Windows event handling function:
1532LRESULT CALLBACK WndProc(HWND hWnd,
1533 UINT uMsg,
1534 WPARAM wParam,
1535 LPARAM lParam)
1536{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001537 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001538
1539 switch(uMsg)
1540 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001541 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001542 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001543 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001544 PostQuitMessage(0);
1545 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001546 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001547 demo_run(&demo);
1548 return 0;
1549 default:
1550 break;
1551 }
1552 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1553}
1554
1555static void demo_create_window(struct demo *demo)
1556{
1557 WNDCLASSEX win_class;
1558
1559 // Initialize the window class structure:
1560 win_class.cbSize = sizeof(WNDCLASSEX);
1561 win_class.style = CS_HREDRAW | CS_VREDRAW;
1562 win_class.lpfnWndProc = WndProc;
1563 win_class.cbClsExtra = 0;
1564 win_class.cbWndExtra = 0;
1565 win_class.hInstance = demo->connection; // hInstance
1566 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1567 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1568 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1569 win_class.lpszMenuName = NULL;
1570 win_class.lpszClassName = demo->name;
1571 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1572 // Register window class:
1573 if (!RegisterClassEx(&win_class)) {
1574 // It didn't work, so try to give a useful error:
1575 printf("Unexpected error trying to start the application!\n");
1576 fflush(stdout);
1577 exit(1);
1578 }
1579 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001580 RECT wr = { 0, 0, demo->width, demo->height };
1581 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001582 demo->window = CreateWindowEx(0,
1583 demo->name, // class name
1584 demo->name, // app name
1585 WS_OVERLAPPEDWINDOW | // window style
1586 WS_VISIBLE |
1587 WS_SYSMENU,
1588 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001589 wr.right-wr.left, // width
1590 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001591 NULL, // handle to parent
1592 NULL, // handle to menu
1593 demo->connection, // hInstance
1594 NULL); // no extra parameters
1595 if (!demo->window) {
1596 // It didn't work, so try to give a useful error:
1597 printf("Cannot create a window in which to draw!\n");
1598 fflush(stdout);
1599 exit(1);
1600 }
1601}
1602#else // _WIN32
1603
Chia-I Wuc19795a2014-09-13 11:12:55 +08001604static void demo_handle_event(struct demo *demo,
1605 const xcb_generic_event_t *event)
1606{
1607 switch (event->response_type & 0x7f) {
1608 case XCB_EXPOSE:
1609 demo_draw(demo);
1610 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001611 case XCB_CLIENT_MESSAGE:
1612 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1613 (*demo->atom_wm_delete_window).atom) {
1614 demo->quit = true;
1615 }
1616 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001617 case XCB_KEY_RELEASE:
1618 {
1619 const xcb_key_release_event_t *key =
1620 (const xcb_key_release_event_t *) event;
1621
1622 if (key->detail == 0x9)
1623 demo->quit = true;
1624 }
1625 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001626 case XCB_DESTROY_NOTIFY:
1627 demo->quit = true;
1628 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001629 default:
1630 break;
1631 }
1632}
1633
1634static void demo_run(struct demo *demo)
1635{
1636 xcb_flush(demo->connection);
1637
1638 while (!demo->quit) {
1639 xcb_generic_event_t *event;
1640
1641 event = xcb_wait_for_event(demo->connection);
1642 if (event) {
1643 demo_handle_event(demo, event);
1644 free(event);
1645 }
1646 }
1647}
1648
1649static void demo_create_window(struct demo *demo)
1650{
1651 uint32_t value_mask, value_list[32];
1652
1653 demo->window = xcb_generate_id(demo->connection);
1654
1655 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1656 value_list[0] = demo->screen->black_pixel;
1657 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001658 XCB_EVENT_MASK_EXPOSURE |
1659 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001660
1661 xcb_create_window(demo->connection,
1662 XCB_COPY_FROM_PARENT,
1663 demo->window, demo->screen->root,
1664 0, 0, demo->width, demo->height, 0,
1665 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1666 demo->screen->root_visual,
1667 value_mask, value_list);
1668
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001669 /* Magic code that will send notification when window is destroyed */
1670 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1671 "WM_PROTOCOLS");
1672 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1673
1674 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1675 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1676
1677 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1678 demo->window, (*reply).atom, 4, 32, 1,
1679 &(*demo->atom_wm_delete_window).atom);
1680 free(reply);
1681
Chia-I Wuc19795a2014-09-13 11:12:55 +08001682 xcb_map_window(demo->connection, demo->window);
1683}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001684#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001685
Ian Elliotte36b2082015-07-06 14:27:58 -06001686/*
1687 * Return 1 (true) if all layer names specified in check_names
1688 * can be found in given layer properties.
1689 */
1690static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1691 uint32_t layer_count, VkLayerProperties *layers)
1692{
1693 for (uint32_t i = 0; i < check_count; i++) {
1694 VkBool32 found = 0;
1695 for (uint32_t j = 0; j < layer_count; j++) {
1696 if (!strcmp(check_names[i], layers[j].layerName)) {
1697 found = 1;
1698 }
1699 }
1700 if (!found) {
1701 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1702 return 0;
1703 }
1704 }
1705 return 1;
1706}
1707
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001708static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001709{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001710 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001711 char *extension_names[64];
1712 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001713 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001714 VkLayerProperties *instance_layers;
1715 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001716 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001717 uint32_t instance_layer_count = 0;
1718 uint32_t enabled_extension_count = 0;
1719 uint32_t enabled_layer_count = 0;
1720
Ian Elliotte36b2082015-07-06 14:27:58 -06001721 char *instance_validation_layers[] = {
1722 "MemTracker",
1723 };
1724
1725 char *device_validation_layers[] = {
1726 "MemTracker",
1727 };
1728
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001729 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001730 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001731 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001732 assert(!err);
1733
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001734 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1735 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1736 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001737
1738 if (demo->validate) {
1739 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1740 instance_layer_count, instance_layers);
1741 if (!validation_found) {
1742 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
1743 "required validation layer.\n\n"
1744 "Please look at the Getting Started guide for additional "
1745 "information.\n",
1746 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001747 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001748 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001749 }
1750
1751 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1752 assert(!err);
1753
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001754 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001755 memset(extension_names, 0, sizeof(extension_names));
1756 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1757 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1758 assert(!err);
1759 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001760 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001761 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06001762 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001763 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001764 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001765 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001766 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001767 }
1768 }
1769 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001770 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001771 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001772 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliotte36b2082015-07-06 14:27:58 -06001773 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001774 "Vulkan installable client driver (ICD) installed?\nPlease "
1775 "look at the Getting Started guide for additional "
1776 "information.\n",
1777 "vkCreateInstance Failure");
1778 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001779 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001780 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001781 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001782 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001783 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001784 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001785 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001786 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001787 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001788 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001789 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001790 .pNext = NULL,
1791 .pAppInfo = &app,
1792 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001793 .layerCount = enabled_layer_count,
1794 .ppEnabledLayerNames = (const char *const*) layer_names,
1795 .extensionCount = enabled_extension_count,
1796 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001797 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001798 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001799 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001800 .queueCount = 1,
1801 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001802 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001803
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001804 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001805 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1806 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001807 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001808 "additional information.\n",
1809 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001810 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1811 ERR_EXIT("Cannot find a specified extension library"
1812 ".\nMake sure your layers path is set appropriately\n",
1813 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001814 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001815 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1816 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001817 "the Getting Started guide for additional information.\n",
1818 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001819 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001820
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001821 free(instance_layers);
1822 free(instance_extensions);
1823
Jon Ashburn07b309a2015-04-15 11:31:12 -06001824 gpu_count = 1;
1825 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001826 assert(!err && gpu_count == 1);
1827
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001828 /* Look for validation layers */
1829 validation_found = 0;
1830 enabled_layer_count = 0;
1831 uint32_t device_layer_count = 0;
1832 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
1833 assert(!err);
1834
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001835 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
1836 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
1837 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001838
1839 if (demo->validate) {
1840 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1841 device_layer_count, device_layers);
1842 if (!validation_found) {
1843 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
1844 "a required validation layer.\n\n"
1845 "Please look at the Getting Started guide for additional "
1846 "information.\n",
1847 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001848 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001849 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001850 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001851
1852 uint32_t device_extension_count = 0;
1853 VkExtensionProperties *device_extensions = NULL;
1854 err = vkGetPhysicalDeviceExtensionProperties(
1855 demo->gpu, NULL, &device_extension_count, NULL);
1856 assert(!err);
1857
1858 WSIextFound = 0;
1859 enabled_extension_count = 0;
1860 memset(extension_names, 0, sizeof(extension_names));
1861 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
1862 err = vkGetPhysicalDeviceExtensionProperties(
1863 demo->gpu, NULL, &device_extension_count, device_extensions);
1864 assert(!err);
1865
1866 for (uint32_t i = 0; i < device_extension_count; i++) {
1867 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
1868 WSIextFound = 1;
1869 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
1870 }
1871 assert(enabled_extension_count < 64);
1872 }
1873 if (!WSIextFound) {
1874 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
1875 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
1876 "Vulkan installable client driver (ICD) installed?\nPlease "
1877 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001878 "information.\n",
1879 "vkCreateInstance Failure");
1880 }
1881
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001882 VkDeviceCreateInfo device = {
1883 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1884 .pNext = NULL,
1885 .queueRecordCount = 1,
1886 .pRequestedQueues = &queue,
1887 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001888 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1889 .extensionCount = enabled_extension_count,
1890 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001891 };
1892
1893 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001894 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001895 if (!demo->dbgCreateMsgCallback) {
1896 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1897 "vkGetProcAddr Failure");
1898 }
1899 err = demo->dbgCreateMsgCallback(
1900 demo->inst,
1901 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1902 dbgFunc, NULL,
1903 &demo->msg_callback);
1904 switch (err) {
1905 case VK_SUCCESS:
1906 break;
1907 case VK_ERROR_INVALID_POINTER:
1908 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1909 "dbgCreateMsgCallback Failure");
1910 break;
1911 case VK_ERROR_OUT_OF_HOST_MEMORY:
1912 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1913 "dbgCreateMsgCallback Failure");
1914 break;
1915 default:
1916 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1917 "dbgCreateMsgCallback Failure");
1918 break;
1919 }
1920 }
1921
Ian Elliotte36b2082015-07-06 14:27:58 -06001922
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001923 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001924 assert(!err);
1925
Ian Elliotte36b2082015-07-06 14:27:58 -06001926 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06001927 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
1928 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
1929 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06001930 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1931 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1932 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06001933 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliotte36b2082015-07-06 14:27:58 -06001934 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06001935 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
1936
Tony Barbour426b9052015-06-24 16:06:58 -06001937 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001938
Cody Northropef72e2a2015-08-03 17:04:53 -06001939 // Query with NULL data to get count
1940 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001941 assert(!err);
1942
Cody Northropef72e2a2015-08-03 17:04:53 -06001943 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
1944 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001945 assert(!err);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001946 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001947
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001948 // Graphics queue and MemMgr queue can be separate.
1949 // TODO: Add support for separate queues, including synchronization,
1950 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001951}
1952
1953static void demo_init_vk_wsi(struct demo *demo)
1954{
1955 VkResult err;
1956 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06001957
1958 // Construct the WSI surface description:
1959 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
1960 demo->surface_description.pNext = NULL;
1961#ifdef _WIN32
1962 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
1963 demo->surface_description.pPlatformHandle = demo->connection;
1964 demo->surface_description.pPlatformWindow = demo->window;
1965#else // _WIN32
1966 demo->platform_handle_xcb.connection = demo->connection;
1967 demo->platform_handle_xcb.root = demo->screen->root;
1968 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
1969 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
1970 demo->surface_description.pPlatformWindow = &demo->window;
1971#endif // _WIN32
1972
1973 // Iterate over each queue to learn whether it supports presenting to WSI:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001974 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
1975 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001976 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
1977 (VkSurfaceDescriptionWSI *) &demo->surface_description,
1978 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001979 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001980
1981 // Search for a graphics and a present queue in the array of queue
1982 // families, try to find one that supports both
1983 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
1984 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001985 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001986 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
1987 if (graphicsQueueNodeIndex == UINT32_MAX) {
1988 graphicsQueueNodeIndex = i;
1989 }
1990
1991 if (supportsPresent[i] == VK_TRUE) {
1992 graphicsQueueNodeIndex = i;
1993 presentQueueNodeIndex = i;
1994 break;
1995 }
1996 }
1997 }
1998 if (presentQueueNodeIndex == UINT32_MAX) {
1999 // If didn't find a queue that supports both graphics and present, then
2000 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002001 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002002 if (supportsPresent[i] == VK_TRUE) {
2003 presentQueueNodeIndex = i;
2004 break;
2005 }
2006 }
2007 }
2008 free(supportsPresent);
2009
2010 // Generate error if could not find both a graphics and a present queue
2011 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2012 ERR_EXIT("Could not find a graphics and a present queue\n",
2013 "WSI Initialization Failure");
2014 }
2015
2016 // TODO: Add support for separate queues, including presentation,
2017 // synchronization, and appropriate tracking for QueueSubmit
2018 // While it is possible for an application to use a separate graphics and a
2019 // present queues, this demo program assumes it is only using one:
2020 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2021 ERR_EXIT("Could not find a common graphics and a present queue\n",
2022 "WSI Initialization Failure");
2023 }
2024
2025 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002026
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002027 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08002028 0, &demo->queue);
2029 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06002030
Ian Elliotte36b2082015-07-06 14:27:58 -06002031 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002032 uint32_t formatCount;
2033 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002034 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002035 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002036 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002037 VkSurfaceFormatWSI *surfFormats =
2038 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
2039 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002040 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002041 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002042 assert(!err);
2043 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2044 // the surface has no preferred format. Otherwise, at least one
2045 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002046 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2047 {
2048 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2049 }
2050 else
2051 {
2052 assert(formatCount >= 1);
2053 demo->format = surfFormats[0].format;
2054 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002055 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002056
Mark Lobodzinski72346292015-07-02 16:49:40 -06002057 // Get Memory information and properties
2058 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2059 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002060}
2061
2062static void demo_init_connection(struct demo *demo)
2063{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002064#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002065 const xcb_setup_t *setup;
2066 xcb_screen_iterator_t iter;
2067 int scr;
2068
2069 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002070 if (demo->connection == NULL) {
2071 printf("Cannot find a compatible Vulkan installable client driver "
2072 "(ICD).\nExiting ...\n");
2073 fflush(stdout);
2074 exit(1);
2075 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002076
2077 setup = xcb_get_setup(demo->connection);
2078 iter = xcb_setup_roots_iterator(setup);
2079 while (scr-- > 0)
2080 xcb_screen_next(&iter);
2081
2082 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002083#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002084}
2085
Ian Elliotte14e9f92015-04-16 15:23:05 -06002086#ifdef _WIN32
2087static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2088#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002089static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002090#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002091{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002092 bool argv_error = false;
2093
Chia-I Wuc19795a2014-09-13 11:12:55 +08002094 memset(demo, 0, sizeof(*demo));
2095
Ian Elliotte14e9f92015-04-16 15:23:05 -06002096#ifdef _WIN32
2097 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002098 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002099
2100 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2101 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002102 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2103 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002104 else if (strlen(pCmdLine) != 0) {
2105 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2106 argv_error = true;
2107 }
2108#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002109 for (int i = 0; i < argc; i++) {
2110 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2111 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002112 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2113 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002114 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002115#endif // _WIN32
2116 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002117 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002118 fflush(stderr);
2119 exit(1);
2120 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002121
Chia-I Wuc19795a2014-09-13 11:12:55 +08002122 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002123 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002124
2125 demo->width = 300;
2126 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002127}
2128
2129static void demo_cleanup(struct demo *demo)
2130{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002131 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002132
Tony Barbourde4124d2015-07-03 10:33:54 -06002133 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
2134 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2135 }
Tony Barbourb857d312015-07-10 10:50:45 -06002136 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06002137 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002138
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002139 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002140 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002141 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002142 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002143 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002144
Cody Northrope4bc6942015-08-26 10:01:32 -06002145 vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport);
2146 vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width);
2147 vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias);
2148 vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend);
2149 vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -06002150 vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002151
Tony Barbourde4124d2015-07-03 10:33:54 -06002152 vkDestroyPipeline(demo->device, demo->pipeline);
2153 vkDestroyRenderPass(demo->device, demo->render_pass);
2154 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2155 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002156
Tony Barbourde4124d2015-07-03 10:33:54 -06002157 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002158 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002159
Chia-I Wub043fe32014-10-06 15:30:33 +08002160 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002161 vkDestroyImageView(demo->device, demo->textures[i].view);
2162 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002163 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002164 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08002165 }
2166
Mark Lobodzinski8a190642015-08-07 10:17:13 -06002167 for (i = 0; i < demo->swapChainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002168 vkDestroyImageView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002169 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002170
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002171 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbourde4124d2015-07-03 10:33:54 -06002172 vkDestroyImage(demo->device, demo->depth.image);
2173 vkFreeMemory(demo->device, demo->depth.mem);
2174
Ian Elliotte36b2082015-07-06 14:27:58 -06002175 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
2176 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002177
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002178 vkDestroyDevice(demo->device);
2179 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002180
Ian Elliotte14e9f92015-04-16 15:23:05 -06002181#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002182 xcb_destroy_window(demo->connection, demo->window);
2183 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002184#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002185}
2186
Ian Elliotte14e9f92015-04-16 15:23:05 -06002187#ifdef _WIN32
2188int APIENTRY WinMain(HINSTANCE hInstance,
2189 HINSTANCE hPrevInstance,
2190 LPSTR pCmdLine,
2191 int nCmdShow)
2192{
2193 MSG msg; // message
2194 bool done; // flag saying when app is complete
2195
2196 demo_init(&demo, hInstance, pCmdLine);
2197 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002198 demo_init_vk_wsi(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002199
2200 demo_prepare(&demo);
2201
2202 done = false; //initialize loop condition variable
2203 /* main message loop*/
2204 while(!done)
2205 {
Ian Elliott421107f2015-04-28 15:50:36 -06002206 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002207 if (msg.message == WM_QUIT) //check for a quit message
2208 {
2209 done = true; //if found, quit app
2210 }
2211 else
2212 {
2213 /* Translate and dispatch to event queue*/
2214 TranslateMessage(&msg);
2215 DispatchMessage(&msg);
2216 }
2217 }
2218
2219 demo_cleanup(&demo);
2220
Tony Barboura938abb2015-04-22 11:36:22 -06002221 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002222}
2223#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002224int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002225{
2226 struct demo demo;
2227
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002228 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002229 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002230 demo_init_vk_wsi(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002231
2232 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002233 demo_run(&demo);
2234
2235 demo_cleanup(&demo);
2236
Chia-I Wuc19795a2014-09-13 11:12:55 +08002237 return 0;
2238}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002239#endif // _WIN32