blob: 09491c3983fdb66d5d2d4d375869b1eaeee26a6f [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;
144 VkAttachmentView view;
145} 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;
Chia-I Wuc278df82015-07-07 11:50:03 +0800197 VkAttachmentView 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
Tony Barbour248080b2015-07-23 10:34:15 -0600454 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Ian Elliotte36b2082015-07-06 14:27:58 -0600455 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800456
457 err = vkQueueWaitIdle(demo->queue);
458 assert(err == VK_SUCCESS);
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 },
Ian Elliotte36b2082015-07-06 14:27:58 -0600545 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800546 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600547 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
548 .queueFamilyCount = 0,
549 .pQueueFamilyIndices = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600550 .presentMode = swapChainPresentMode,
551 .oldSwapChain.handle = 0,
552 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800553 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600554 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800555
Jon Ashburncedc15f2015-05-21 18:13:33 -0600556 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800557 assert(!err);
558
Ian Elliott7fe115d2015-08-07 15:56:59 -0600559 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
560 &demo->swapChainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600561 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800562
Ian Elliott7fe115d2015-08-07 15:56:59 -0600563 VkImage* swapChainImages =
564 (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage));
Ian Elliotte36b2082015-07-06 14:27:58 -0600565 assert(swapChainImages);
Ian Elliott7fe115d2015-08-07 15:56:59 -0600566 err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain,
567 &demo->swapChainImageCount,
568 swapChainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600569 assert(!err);
570
Ian Elliotte36b2082015-07-06 14:27:58 -0600571 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
572 assert(demo->buffers);
573
574 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800575 VkAttachmentViewCreateInfo color_attachment_view = {
576 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800577 .pNext = NULL,
578 .format = demo->format,
579 .mipLevel = 0,
580 .baseArraySlice = 0,
581 .arraySize = 1,
582 };
583
Ian Elliott7fe115d2015-08-07 15:56:59 -0600584 demo->buffers[i].image = swapChainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500585
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600586 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400587 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600588 VK_IMAGE_LAYOUT_UNDEFINED,
589 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600590
Chia-I Wuc19795a2014-09-13 11:12:55 +0800591 color_attachment_view.image = demo->buffers[i].image;
592
Chia-I Wuc278df82015-07-07 11:50:03 +0800593 err = vkCreateAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800594 &color_attachment_view, &demo->buffers[i].view);
595 assert(!err);
596 }
Piers Daniell886be472015-02-23 16:23:13 -0700597
598 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800599}
600
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800601static void demo_prepare_depth(struct demo *demo)
602{
Tony Barbour8205d902015-04-16 15:59:00 -0600603 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600604 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600605 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800606 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600607 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800608 .format = depth_format,
609 .extent = { demo->width, demo->height, 1 },
610 .mipLevels = 1,
611 .arraySize = 1,
612 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600613 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600614 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800615 .flags = 0,
616 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600617 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600618 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500619 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800620 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600621 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800622 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800623 VkAttachmentViewCreateInfo view = {
624 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800625 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600626 .image.handle = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600627 .format = depth_format,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800628 .mipLevel = 0,
629 .baseArraySlice = 0,
630 .arraySize = 1,
631 .flags = 0,
632 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700633
Mark Lobodzinski23182612015-05-29 09:32:35 -0500634 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600635 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800636
637 demo->depth.format = depth_format;
638
639 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600640 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800641 &demo->depth.image);
642 assert(!err);
643
Mark Lobodzinski72346292015-07-02 16:49:40 -0600644 /* get memory requirements for this object */
Tony Barbourde4124d2015-07-03 10:33:54 -0600645 err = vkGetImageMemoryRequirements(demo->device, demo->depth.image,
646 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600647
648 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500649 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600650 err = memory_type_from_properties(demo,
651 mem_reqs.memoryTypeBits,
652 VK_MEMORY_PROPERTY_DEVICE_ONLY,
653 &mem_alloc.memoryTypeIndex);
654 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800655
Mark Lobodzinski23182612015-05-29 09:32:35 -0500656 /* allocate memory */
657 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
658 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800659
Mark Lobodzinski23182612015-05-29 09:32:35 -0500660 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600661 err = vkBindImageMemory(demo->device, demo->depth.image,
662 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500663 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800664
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600665 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400666 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600667 VK_IMAGE_LAYOUT_UNDEFINED,
668 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600669
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800670 /* create image view */
671 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800672 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800673 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800674}
675
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700676static void demo_prepare_texture_image(struct demo *demo,
677 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600678 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600679 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600680 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600681 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800682{
Tony Barbour8205d902015-04-16 15:59:00 -0600683 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600684 const int32_t tex_width = 2;
685 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600686 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800687
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600688 tex_obj->tex_width = tex_width;
689 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700690
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600691 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600692 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700693 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600694 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700695 .format = tex_format,
696 .extent = { tex_width, tex_height, 1 },
697 .mipLevels = 1,
698 .arraySize = 1,
699 .samples = 1,
700 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600701 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700702 .flags = 0,
703 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600704 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600705 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500706 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700707 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600708 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700709 };
710
Mark Lobodzinski23182612015-05-29 09:32:35 -0500711 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700712
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600713 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600714 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700715 assert(!err);
716
Tony Barbourde4124d2015-07-03 10:33:54 -0600717 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600718
719 mem_alloc.allocationSize = mem_reqs.size;
720 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
721 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700722
Mark Lobodzinski23182612015-05-29 09:32:35 -0500723 /* allocate memory */
724 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
725 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700726
Mark Lobodzinski23182612015-05-29 09:32:35 -0500727 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600728 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500729 tex_obj->mem, 0);
730 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700731
Tony Barbour8205d902015-04-16 15:59:00 -0600732 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600733 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600734 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700735 .mipLevel = 0,
736 .arraySlice = 0,
737 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600738 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700739 void *data;
740 int32_t x, y;
741
Tony Barbour426b9052015-06-24 16:06:58 -0600742 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
743 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700744
Mark Lobodzinski23182612015-05-29 09:32:35 -0500745 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700746 assert(!err);
747
748 for (y = 0; y < tex_height; y++) {
749 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
750 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700751 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700752 }
753
Mark Lobodzinski23182612015-05-29 09:32:35 -0500754 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700755 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700756 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600757
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600758 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600759 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400760 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600761 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600762 tex_obj->imageLayout);
763 /* 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 -0700764}
765
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500766static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700767{
768 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600769 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600770 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700771}
772
773static void demo_prepare_textures(struct demo *demo)
774{
Tony Barbour8205d902015-04-16 15:59:00 -0600775 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600776 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700777 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
778 { 0xffff0000, 0xff00ff00 },
779 };
Tony Barbour22a30862015-04-22 09:02:32 -0600780 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700781 uint32_t i;
782
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600783 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700784 assert(!err);
785
786 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600787 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700788 /* Device can texture using linear textures */
789 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600790 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600791 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700792 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600793 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700794
795 memset(&staging_texture, 0, sizeof(staging_texture));
796 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600797 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700798
799 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600800 VK_IMAGE_TILING_OPTIMAL,
801 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
802 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700803
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600804 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400805 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600806 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600807 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700808
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600809 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400810 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600811 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600812 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700813
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600814 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600815 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700816 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600817 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700818 .destOffset = { 0, 0, 0 },
819 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
820 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600821 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600822 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
823 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600824 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700825
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600826 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400827 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600828 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600829 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700830
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600831 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700832
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600833 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700834 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600835 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700836 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700837 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700838
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600839 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600840 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800841 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600842 .magFilter = VK_TEX_FILTER_NEAREST,
843 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600844 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600845 .addressU = VK_TEX_ADDRESS_WRAP,
846 .addressV = VK_TEX_ADDRESS_WRAP,
847 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800848 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700849 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600850 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800851 .minLod = 0.0f,
852 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600853 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Cody Northrop0bf4e1d2015-08-11 15:50:55 -0600854 .texelCoords = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800855 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600856 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600857 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800858 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600859 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600860 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700861 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600862 .channels = { VK_CHANNEL_SWIZZLE_R,
863 VK_CHANNEL_SWIZZLE_G,
864 VK_CHANNEL_SWIZZLE_B,
865 VK_CHANNEL_SWIZZLE_A, },
866 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800867 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700868
Chia-I Wub043fe32014-10-06 15:30:33 +0800869 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600870 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800871 &demo->textures[i].sampler);
872 assert(!err);
873
Chia-I Wub043fe32014-10-06 15:30:33 +0800874 /* create image view */
875 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600876 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700877 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800878 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800879 }
880}
881
Chia-I Wu99621bc2014-10-08 11:52:22 +0800882static void demo_prepare_vertices(struct demo *demo)
883{
884 const float vb[3][5] = {
885 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800886 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
887 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800888 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
889 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600890 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600891 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800892 .pNext = NULL,
893 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600894 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800895 .flags = 0,
896 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600897 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600898 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500899 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800900 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600901 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800902 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500903 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600904 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800905 void *data;
906
907 memset(&demo->vertices, 0, sizeof(demo->vertices));
908
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600909 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800910 assert(!err);
911
Tony Barbourde4124d2015-07-03 10:33:54 -0600912 err = vkGetBufferMemoryRequirements(demo->device,
913 demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800914
Mark Lobodzinski72346292015-07-02 16:49:40 -0600915 mem_alloc.allocationSize = mem_reqs.size;
916 err = memory_type_from_properties(demo,
917 mem_reqs.memoryTypeBits,
918 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
919 &mem_alloc.memoryTypeIndex);
920 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800921
Mark Lobodzinski23182612015-05-29 09:32:35 -0500922 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
923 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800924
Mark Lobodzinski23182612015-05-29 09:32:35 -0500925 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
926 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800927
Mark Lobodzinski23182612015-05-29 09:32:35 -0500928 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800929
Mark Lobodzinski23182612015-05-29 09:32:35 -0500930 err = vkUnmapMemory(demo->device, demo->vertices.mem);
931 assert(!err);
932
Tony Barbourde4124d2015-07-03 10:33:54 -0600933 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500934 demo->vertices.mem, 0);
935 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800936
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600937 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800938 demo->vertices.vi.pNext = NULL;
939 demo->vertices.vi.bindingCount = 1;
940 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
941 demo->vertices.vi.attributeCount = 2;
942 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
943
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600944 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800945 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600946 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800947
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600948 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
949 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600950 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800951 demo->vertices.vi_attrs[0].offsetInBytes = 0;
952
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600953 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
954 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600955 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800956 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800957}
958
Chia-I Wuf8385062015-01-04 16:27:24 +0800959static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800960{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600961 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600962 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800963 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600964 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800965 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800966 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600967 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600968 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800969 .pNext = NULL,
970 .count = 1,
971 .pBinding = &layout_binding,
972 };
Tony Barbour22a30862015-04-22 09:02:32 -0600973 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800974
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600975 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800976 &descriptor_layout, &demo->desc_layout);
977 assert(!err);
978
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500979 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
980 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
981 .pNext = NULL,
982 .descriptorSetCount = 1,
983 .pSetLayouts = &demo->desc_layout,
984 };
985
986 err = vkCreatePipelineLayout(demo->device,
987 &pPipelineLayoutCreateInfo,
988 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800989 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800990}
991
Chia-I Wu76cd4222015-07-08 13:34:24 +0800992static void demo_prepare_render_pass(struct demo *demo)
993{
Chia-I Wuc278df82015-07-07 11:50:03 +0800994 const VkAttachmentDescription attachments[2] = {
995 [0] = {
996 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
997 .pNext = NULL,
998 .format = demo->format,
999 .samples = 1,
1000 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1001 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1002 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1003 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1004 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1005 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1006 },
1007 [1] = {
1008 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1009 .pNext = NULL,
1010 .format = demo->depth.format,
1011 .samples = 1,
1012 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1013 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1014 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1015 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1016 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1017 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1018 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001019 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001020 const VkAttachmentReference color_reference = {
1021 .attachment = 0,
1022 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1023 };
1024 const VkSubpassDescription subpass = {
1025 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1026 .pNext = NULL,
1027 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1028 .flags = 0,
1029 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001030 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001031 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001032 .pColorAttachments = &color_reference,
1033 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001034 .depthStencilAttachment = {
1035 .attachment = 1,
1036 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1037 },
1038 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001039 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001040 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001041 const VkRenderPassCreateInfo rp_info = {
1042 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1043 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001044 .attachmentCount = 2,
1045 .pAttachments = attachments,
1046 .subpassCount = 1,
1047 .pSubpasses = &subpass,
1048 .dependencyCount = 0,
1049 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001050 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001051 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001052
1053 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1054 assert(!err);
1055}
1056
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001057static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001058 VkShaderStage stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001059 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001060 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001061 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001062{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001063 VkShaderModuleCreateInfo moduleCreateInfo;
1064 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001065 VkShader shader;
1066 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001067
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001068
1069 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1070 moduleCreateInfo.pNext = NULL;
1071
1072 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1073 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001074
Cody Northrop75db0322015-05-28 11:27:16 -06001075 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001076 moduleCreateInfo.codeSize = size;
1077 moduleCreateInfo.pCode = code;
1078 moduleCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001079 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001080 if (err) {
1081 free((void *) moduleCreateInfo.pCode);
1082 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001083
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001084 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001085 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001086 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001087 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001088 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001089 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001090 } else {
1091 // Create fake SPV structure to feed GLSL
1092 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001093 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1094 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1095 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001096
Cody Northrop75db0322015-05-28 11:27:16 -06001097 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001098 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1099 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1100 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1101 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001102
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001103 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001104 if (err) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001105 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001106 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001107
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001108 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001109 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001110 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001111 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001112 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001113 assert(!err);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001114 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001115 return shader;
1116}
1117
Cody Northrop75db0322015-05-28 11:27:16 -06001118char *demo_read_spv(const char *filename, size_t *psize)
1119{
1120 long int size;
1121 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001122 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001123
1124 FILE *fp = fopen(filename, "rb");
1125 if (!fp) return NULL;
1126
1127 fseek(fp, 0L, SEEK_END);
1128 size = ftell(fp);
1129
1130 fseek(fp, 0L, SEEK_SET);
1131
1132 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001133 retVal = fread(shader_code, size, 1, fp);
1134 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001135
1136 *psize = size;
1137
1138 return shader_code;
1139}
1140
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001141static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001142{
Cody Northrop75db0322015-05-28 11:27:16 -06001143 if (!demo->use_glsl) {
1144 void *vertShaderCode;
1145 size_t size;
1146
1147 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
1148
1149 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001150 &demo->vert_shader_module,
1151 vertShaderCode, size);
Cody Northrop75db0322015-05-28 11:27:16 -06001152 } else {
1153 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001154 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001155 "#extension GL_ARB_separate_shader_objects : enable\n"
1156 "#extension GL_ARB_shading_language_420pack : enable\n"
1157 "layout (location = 0) in vec4 pos;\n"
1158 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001159 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001160 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001161 " texcoord = attr;\n"
1162 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001163 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001164
Cody Northrop75db0322015-05-28 11:27:16 -06001165 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001166 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001167 (const void *) vertShaderText,
1168 strlen(vertShaderText));
1169 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001170}
1171
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001172static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001173{
Cody Northrop75db0322015-05-28 11:27:16 -06001174 if (!demo->use_glsl) {
1175 void *fragShaderCode;
1176 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001177
Cody Northrop75db0322015-05-28 11:27:16 -06001178 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
1179
1180 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001181 &demo->frag_shader_module,
1182 fragShaderCode, size);
Cody Northrop75db0322015-05-28 11:27:16 -06001183 } else {
1184 static const char *fragShaderText =
1185 "#version 140\n"
1186 "#extension GL_ARB_separate_shader_objects : enable\n"
1187 "#extension GL_ARB_shading_language_420pack : enable\n"
1188 "layout (binding = 0) uniform sampler2D tex;\n"
1189 "layout (location = 0) in vec2 texcoord;\n"
1190 "layout (location = 0) out vec4 uFragColor;\n"
1191 "void main() {\n"
1192 " uFragColor = texture(tex, texcoord);\n"
1193 "}\n";
1194
1195 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001196 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001197 (const void *) fragShaderText,
1198 strlen(fragShaderText));
1199 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001200}
1201
1202static void demo_prepare_pipeline(struct demo *demo)
1203{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001204 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001205 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001206
Tony Barboure307f582015-07-10 15:29:03 -06001207 VkPipelineVertexInputStateCreateInfo vi;
1208 VkPipelineInputAssemblyStateCreateInfo ia;
1209 VkPipelineRasterStateCreateInfo rs;
1210 VkPipelineColorBlendStateCreateInfo cb;
1211 VkPipelineDepthStencilStateCreateInfo ds;
1212 VkPipelineViewportStateCreateInfo vp;
1213 VkPipelineMultisampleStateCreateInfo ms;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001214
Tony Barbour22a30862015-04-22 09:02:32 -06001215 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001216
1217 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001218 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001219 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001220
Chia-I Wu8d29d022014-10-08 12:14:39 +08001221 vi = demo->vertices.vi;
1222
Chia-I Wuc19795a2014-09-13 11:12:55 +08001223 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001224 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001225 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001226
1227 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001228 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001229 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001230 rs.cullMode = VK_CULL_MODE_BACK;
1231 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001232 rs.depthClipEnable = VK_TRUE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001233 rs.rasterizerDiscardEnable = VK_FALSE;
1234 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001235
1236 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001237 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1238 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001239 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001240 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001241 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001242 cb.attachmentCount = 1;
1243 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001244
Tony Barbourfa6cac72015-01-16 14:27:35 -07001245 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001246 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001247 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001248
1249 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001250 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001251 ds.depthTestEnable = VK_TRUE;
1252 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001253 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001254 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001255 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1256 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001257 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001258 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001259 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001260
Tony Barbourfa6cac72015-01-16 14:27:35 -07001261 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001262 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001263 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001264 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001265
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001266 // Two stages: vs and fs
1267 pipeline.stageCount = 2;
1268 VkPipelineShaderStageCreateInfo shaderStages[2];
1269 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1270
1271 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1272 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1273 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001274
1275 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1276 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1277 shaderStages[1].shader = demo_prepare_fs(demo);
1278
Tony Barboure307f582015-07-10 15:29:03 -06001279 pipeline.pVertexInputState = &vi;
1280 pipeline.pInputAssemblyState = &ia;
1281 pipeline.pRasterState = &rs;
1282 pipeline.pColorBlendState = &cb;
1283 pipeline.pMultisampleState = &ms;
1284 pipeline.pViewportState = &vp;
1285 pipeline.pDepthStencilState = &ds;
1286 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001287 pipeline.renderPass = demo->render_pass;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001288
Jon Ashburn0d60d272015-07-09 15:02:25 -06001289 memset(&pipelineCache, 0, sizeof(pipelineCache));
1290 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1291
1292 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1293 assert(!err);
1294 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001295 assert(!err);
1296
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001297 err = vkDestroyPipelineCache(demo->device, demo->pipelineCache);
1298 assert(!err);
1299
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001300 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001301 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001302 }
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001303 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1304 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001305}
1306
1307static void demo_prepare_dynamic_states(struct demo *demo)
1308{
Tony Barbourde4124d2015-07-03 10:33:54 -06001309 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northrope4bc6942015-08-26 10:01:32 -06001310 VkDynamicLineWidthStateCreateInfo line_width;
1311 VkDynamicDepthBiasStateCreateInfo depth_bias;
1312 VkDynamicBlendStateCreateInfo blend;
1313 VkDynamicDepthBoundsStateCreateInfo depth_bounds;
Cody Northrop2605cb02015-08-18 15:21:16 -06001314 VkDynamicStencilStateCreateInfo stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001315 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001316
Tony Barbourfa6cac72015-01-16 14:27:35 -07001317 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001318 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001319 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001320 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001321 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001322 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001323 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001324 viewport.minDepth = (float) 0.0f;
1325 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001326 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001327 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001328 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001329 scissor.extent.width = demo->width;
1330 scissor.extent.height = demo->height;
1331 scissor.offset.x = 0;
1332 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001333 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001334
Cody Northrope4bc6942015-08-26 10:01:32 -06001335 memset(&line_width, 0, sizeof(line_width));
1336 line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
1337 line_width.lineWidth = 1.0;
Cody Northropf5bd2252015-08-17 11:10:49 -06001338
Cody Northrope4bc6942015-08-26 10:01:32 -06001339 memset(&depth_bias, 0, sizeof(depth_bias));
1340 depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
1341 depth_bias.depthBias = 0.0f;
1342 depth_bias.depthBiasClamp = 0.0f;
1343 depth_bias.slopeScaledDepthBias = 0.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001344
Cody Northrope4bc6942015-08-26 10:01:32 -06001345 memset(&blend, 0, sizeof(blend));
1346 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
1347 blend.blendConst[0] = 1.0f;
1348 blend.blendConst[1] = 1.0f;
1349 blend.blendConst[2] = 1.0f;
1350 blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001351
Cody Northrope4bc6942015-08-26 10:01:32 -06001352 memset(&depth_bounds, 0, sizeof(depth_bounds));
1353 depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
1354 depth_bounds.minDepthBounds = 0.0f;
1355 depth_bounds.maxDepthBounds = 1.0f;
Cody Northrop2605cb02015-08-18 15:21:16 -06001356
1357 memset(&stencil, 0, sizeof(stencil));
1358 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
1359 stencil.stencilReference = 0;
Cody Northrope4bc6942015-08-26 10:01:32 -06001360 stencil.stencilCompareMask = 0xff;
Cody Northrop2605cb02015-08-18 15:21:16 -06001361 stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001362
Cody Northrope4bc6942015-08-26 10:01:32 -06001363 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001364 assert(!err);
1365
Cody Northrope4bc6942015-08-26 10:01:32 -06001366 err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width);
Cody Northropf5bd2252015-08-17 11:10:49 -06001367 assert(!err);
1368
Cody Northrope4bc6942015-08-26 10:01:32 -06001369 err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001370 assert(!err);
1371
Cody Northrope4bc6942015-08-26 10:01:32 -06001372 err = vkCreateDynamicBlendState(demo->device,
1373 &blend, &demo->dynamic_blend);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001374 assert(!err);
1375
Cody Northrope4bc6942015-08-26 10:01:32 -06001376 err = vkCreateDynamicDepthBoundsState(demo->device,
1377 &depth_bounds, &demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -06001378 assert(!err);
1379
1380 err = vkCreateDynamicStencilState(demo->device,
1381 &stencil, &stencil, &demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001382 assert(!err);
1383}
1384
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001385static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001386{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001387 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001388 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001389 .count = DEMO_TEXTURE_COUNT,
1390 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001391 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001392 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001393 .pNext = NULL,
1394 .count = 1,
1395 .pTypeCount = &type_count,
1396 };
Tony Barbour22a30862015-04-22 09:02:32 -06001397 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001398
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001399 err = vkCreateDescriptorPool(demo->device,
1400 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001401 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001402 assert(!err);
1403}
1404
1405static void demo_prepare_descriptor_set(struct demo *demo)
1406{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001407 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1408 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001409 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001410 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001411
Mike Stroyan230e6252015-04-17 12:36:38 -06001412 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001413 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001414 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001415 &demo->desc_set);
1416 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001417
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001418 memset(&tex_descs, 0, sizeof(tex_descs));
1419 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1420 tex_descs[i].sampler = demo->textures[i].sampler;
1421 tex_descs[i].imageView = demo->textures[i].view;
1422 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1423 }
1424
1425 memset(&write, 0, sizeof(write));
1426 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1427 write.destSet = demo->desc_set;
1428 write.count = DEMO_TEXTURE_COUNT;
1429 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1430 write.pDescriptors = tex_descs;
1431
1432 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1433 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001434}
1435
Chia-I Wu76cd4222015-07-08 13:34:24 +08001436static void demo_prepare_framebuffers(struct demo *demo)
1437{
Cody Northropf110c6e2015-08-04 10:47:08 -06001438 VkAttachmentView attachments[2];
1439 attachments[1] = demo->depth.view;
1440
Chia-I Wu76cd4222015-07-08 13:34:24 +08001441 const VkFramebufferCreateInfo fb_info = {
1442 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1443 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001444 .renderPass = demo->render_pass,
1445 .attachmentCount = 2,
1446 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001447 .width = demo->width,
1448 .height = demo->height,
1449 .layers = 1,
1450 };
1451 VkResult U_ASSERT_ONLY err;
1452 uint32_t i;
1453
1454 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001455 attachments[0]= demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001456 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1457 assert(!err);
1458 }
1459}
1460
Chia-I Wuc19795a2014-09-13 11:12:55 +08001461static void demo_prepare(struct demo *demo)
1462{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001463 VkResult U_ASSERT_ONLY err;
1464
1465 const VkCmdPoolCreateInfo cmd_pool_info = {
1466 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1467 .pNext = NULL,
1468 .queueFamilyIndex = demo->graphics_queue_node_index,
1469 .flags = 0,
1470 };
1471 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1472 assert(!err);
1473
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001474 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001475 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001476 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001477 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001478 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001479 .flags = 0,
1480 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001481 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1482 assert(!err);
1483
Chia-I Wuc19795a2014-09-13 11:12:55 +08001484 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001485 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001486 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001487 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001488 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001489 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001490 demo_prepare_pipeline(demo);
1491 demo_prepare_dynamic_states(demo);
1492
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001493 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001494 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001495
1496 demo_prepare_framebuffers(demo);
1497
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001498 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001499}
1500
Ian Elliotte14e9f92015-04-16 15:23:05 -06001501#ifdef _WIN32
1502static void demo_run(struct demo *demo)
1503{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001504 if (!demo->prepared)
1505 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001506 demo_draw(demo);
1507}
1508
1509// On MS-Windows, make this a global, so it's available to WndProc()
1510struct demo demo;
1511
1512// MS-Windows event handling function:
1513LRESULT CALLBACK WndProc(HWND hWnd,
1514 UINT uMsg,
1515 WPARAM wParam,
1516 LPARAM lParam)
1517{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001518 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001519
1520 switch(uMsg)
1521 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001522 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001523 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001524 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001525 PostQuitMessage(0);
1526 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001527 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001528 demo_run(&demo);
1529 return 0;
1530 default:
1531 break;
1532 }
1533 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1534}
1535
1536static void demo_create_window(struct demo *demo)
1537{
1538 WNDCLASSEX win_class;
1539
1540 // Initialize the window class structure:
1541 win_class.cbSize = sizeof(WNDCLASSEX);
1542 win_class.style = CS_HREDRAW | CS_VREDRAW;
1543 win_class.lpfnWndProc = WndProc;
1544 win_class.cbClsExtra = 0;
1545 win_class.cbWndExtra = 0;
1546 win_class.hInstance = demo->connection; // hInstance
1547 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1548 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1549 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1550 win_class.lpszMenuName = NULL;
1551 win_class.lpszClassName = demo->name;
1552 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1553 // Register window class:
1554 if (!RegisterClassEx(&win_class)) {
1555 // It didn't work, so try to give a useful error:
1556 printf("Unexpected error trying to start the application!\n");
1557 fflush(stdout);
1558 exit(1);
1559 }
1560 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001561 RECT wr = { 0, 0, demo->width, demo->height };
1562 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001563 demo->window = CreateWindowEx(0,
1564 demo->name, // class name
1565 demo->name, // app name
1566 WS_OVERLAPPEDWINDOW | // window style
1567 WS_VISIBLE |
1568 WS_SYSMENU,
1569 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001570 wr.right-wr.left, // width
1571 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001572 NULL, // handle to parent
1573 NULL, // handle to menu
1574 demo->connection, // hInstance
1575 NULL); // no extra parameters
1576 if (!demo->window) {
1577 // It didn't work, so try to give a useful error:
1578 printf("Cannot create a window in which to draw!\n");
1579 fflush(stdout);
1580 exit(1);
1581 }
1582}
1583#else // _WIN32
1584
Chia-I Wuc19795a2014-09-13 11:12:55 +08001585static void demo_handle_event(struct demo *demo,
1586 const xcb_generic_event_t *event)
1587{
1588 switch (event->response_type & 0x7f) {
1589 case XCB_EXPOSE:
1590 demo_draw(demo);
1591 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001592 case XCB_CLIENT_MESSAGE:
1593 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1594 (*demo->atom_wm_delete_window).atom) {
1595 demo->quit = true;
1596 }
1597 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001598 case XCB_KEY_RELEASE:
1599 {
1600 const xcb_key_release_event_t *key =
1601 (const xcb_key_release_event_t *) event;
1602
1603 if (key->detail == 0x9)
1604 demo->quit = true;
1605 }
1606 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001607 case XCB_DESTROY_NOTIFY:
1608 demo->quit = true;
1609 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001610 default:
1611 break;
1612 }
1613}
1614
1615static void demo_run(struct demo *demo)
1616{
1617 xcb_flush(demo->connection);
1618
1619 while (!demo->quit) {
1620 xcb_generic_event_t *event;
1621
1622 event = xcb_wait_for_event(demo->connection);
1623 if (event) {
1624 demo_handle_event(demo, event);
1625 free(event);
1626 }
1627 }
1628}
1629
1630static void demo_create_window(struct demo *demo)
1631{
1632 uint32_t value_mask, value_list[32];
1633
1634 demo->window = xcb_generate_id(demo->connection);
1635
1636 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1637 value_list[0] = demo->screen->black_pixel;
1638 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001639 XCB_EVENT_MASK_EXPOSURE |
1640 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001641
1642 xcb_create_window(demo->connection,
1643 XCB_COPY_FROM_PARENT,
1644 demo->window, demo->screen->root,
1645 0, 0, demo->width, demo->height, 0,
1646 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1647 demo->screen->root_visual,
1648 value_mask, value_list);
1649
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001650 /* Magic code that will send notification when window is destroyed */
1651 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1652 "WM_PROTOCOLS");
1653 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1654
1655 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1656 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1657
1658 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1659 demo->window, (*reply).atom, 4, 32, 1,
1660 &(*demo->atom_wm_delete_window).atom);
1661 free(reply);
1662
Chia-I Wuc19795a2014-09-13 11:12:55 +08001663 xcb_map_window(demo->connection, demo->window);
1664}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001665#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001666
Ian Elliotte36b2082015-07-06 14:27:58 -06001667/*
1668 * Return 1 (true) if all layer names specified in check_names
1669 * can be found in given layer properties.
1670 */
1671static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1672 uint32_t layer_count, VkLayerProperties *layers)
1673{
1674 for (uint32_t i = 0; i < check_count; i++) {
1675 VkBool32 found = 0;
1676 for (uint32_t j = 0; j < layer_count; j++) {
1677 if (!strcmp(check_names[i], layers[j].layerName)) {
1678 found = 1;
1679 }
1680 }
1681 if (!found) {
1682 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1683 return 0;
1684 }
1685 }
1686 return 1;
1687}
1688
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001689static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001690{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001691 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001692 char *extension_names[64];
1693 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001694 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001695 VkLayerProperties *instance_layers;
1696 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001697 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001698 uint32_t instance_layer_count = 0;
1699 uint32_t enabled_extension_count = 0;
1700 uint32_t enabled_layer_count = 0;
1701
Ian Elliotte36b2082015-07-06 14:27:58 -06001702 char *instance_validation_layers[] = {
1703 "MemTracker",
1704 };
1705
1706 char *device_validation_layers[] = {
1707 "MemTracker",
1708 };
1709
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001710 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001711 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001712 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001713 assert(!err);
1714
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001715 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1716 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1717 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001718
1719 if (demo->validate) {
1720 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1721 instance_layer_count, instance_layers);
1722 if (!validation_found) {
1723 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
1724 "required validation layer.\n\n"
1725 "Please look at the Getting Started guide for additional "
1726 "information.\n",
1727 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001728 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001729 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001730 }
1731
1732 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1733 assert(!err);
1734
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001735 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001736 memset(extension_names, 0, sizeof(extension_names));
1737 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1738 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1739 assert(!err);
1740 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001741 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001742 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06001743 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001744 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001745 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001746 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001747 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001748 }
1749 }
1750 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001751 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001752 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001753 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliotte36b2082015-07-06 14:27:58 -06001754 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001755 "Vulkan installable client driver (ICD) installed?\nPlease "
1756 "look at the Getting Started guide for additional "
1757 "information.\n",
1758 "vkCreateInstance Failure");
1759 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001760 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001761 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001762 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001763 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001764 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001765 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001766 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001767 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001768 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001769 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001770 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001771 .pNext = NULL,
1772 .pAppInfo = &app,
1773 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001774 .layerCount = enabled_layer_count,
1775 .ppEnabledLayerNames = (const char *const*) layer_names,
1776 .extensionCount = enabled_extension_count,
1777 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001778 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001779 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001780 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001781 .queueCount = 1,
1782 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001783 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001784
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001785 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001786 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1787 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001788 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001789 "additional information.\n",
1790 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001791 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1792 ERR_EXIT("Cannot find a specified extension library"
1793 ".\nMake sure your layers path is set appropriately\n",
1794 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001795 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001796 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1797 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001798 "the Getting Started guide for additional information.\n",
1799 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001800 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001801
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001802 free(instance_layers);
1803 free(instance_extensions);
1804
Jon Ashburn07b309a2015-04-15 11:31:12 -06001805 gpu_count = 1;
1806 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001807 assert(!err && gpu_count == 1);
1808
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001809 /* Look for validation layers */
1810 validation_found = 0;
1811 enabled_layer_count = 0;
1812 uint32_t device_layer_count = 0;
1813 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
1814 assert(!err);
1815
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001816 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
1817 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
1818 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001819
1820 if (demo->validate) {
1821 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1822 device_layer_count, device_layers);
1823 if (!validation_found) {
1824 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
1825 "a required validation layer.\n\n"
1826 "Please look at the Getting Started guide for additional "
1827 "information.\n",
1828 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001829 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001830 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001831 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001832
1833 uint32_t device_extension_count = 0;
1834 VkExtensionProperties *device_extensions = NULL;
1835 err = vkGetPhysicalDeviceExtensionProperties(
1836 demo->gpu, NULL, &device_extension_count, NULL);
1837 assert(!err);
1838
1839 WSIextFound = 0;
1840 enabled_extension_count = 0;
1841 memset(extension_names, 0, sizeof(extension_names));
1842 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
1843 err = vkGetPhysicalDeviceExtensionProperties(
1844 demo->gpu, NULL, &device_extension_count, device_extensions);
1845 assert(!err);
1846
1847 for (uint32_t i = 0; i < device_extension_count; i++) {
1848 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
1849 WSIextFound = 1;
1850 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
1851 }
1852 assert(enabled_extension_count < 64);
1853 }
1854 if (!WSIextFound) {
1855 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
1856 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
1857 "Vulkan installable client driver (ICD) installed?\nPlease "
1858 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001859 "information.\n",
1860 "vkCreateInstance Failure");
1861 }
1862
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001863 VkDeviceCreateInfo device = {
1864 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1865 .pNext = NULL,
1866 .queueRecordCount = 1,
1867 .pRequestedQueues = &queue,
1868 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001869 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1870 .extensionCount = enabled_extension_count,
1871 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001872 };
1873
1874 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001875 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001876 if (!demo->dbgCreateMsgCallback) {
1877 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1878 "vkGetProcAddr Failure");
1879 }
1880 err = demo->dbgCreateMsgCallback(
1881 demo->inst,
1882 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1883 dbgFunc, NULL,
1884 &demo->msg_callback);
1885 switch (err) {
1886 case VK_SUCCESS:
1887 break;
1888 case VK_ERROR_INVALID_POINTER:
1889 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1890 "dbgCreateMsgCallback Failure");
1891 break;
1892 case VK_ERROR_OUT_OF_HOST_MEMORY:
1893 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1894 "dbgCreateMsgCallback Failure");
1895 break;
1896 default:
1897 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1898 "dbgCreateMsgCallback Failure");
1899 break;
1900 }
1901 }
1902
Ian Elliotte36b2082015-07-06 14:27:58 -06001903
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001904 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001905 assert(!err);
1906
Ian Elliotte36b2082015-07-06 14:27:58 -06001907 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06001908 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI);
1909 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI);
1910 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06001911 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1912 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1913 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
Ian Elliott7fe115d2015-08-07 15:56:59 -06001914 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI);
Ian Elliotte36b2082015-07-06 14:27:58 -06001915 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06001916 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
1917
Tony Barbour426b9052015-06-24 16:06:58 -06001918 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001919
Cody Northropef72e2a2015-08-03 17:04:53 -06001920 // Query with NULL data to get count
1921 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001922 assert(!err);
1923
Cody Northropef72e2a2015-08-03 17:04:53 -06001924 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
1925 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001926 assert(!err);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001927 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001928
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001929 // Graphics queue and MemMgr queue can be separate.
1930 // TODO: Add support for separate queues, including synchronization,
1931 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001932}
1933
1934static void demo_init_vk_wsi(struct demo *demo)
1935{
1936 VkResult err;
1937 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06001938
1939 // Construct the WSI surface description:
1940 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
1941 demo->surface_description.pNext = NULL;
1942#ifdef _WIN32
1943 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
1944 demo->surface_description.pPlatformHandle = demo->connection;
1945 demo->surface_description.pPlatformWindow = demo->window;
1946#else // _WIN32
1947 demo->platform_handle_xcb.connection = demo->connection;
1948 demo->platform_handle_xcb.root = demo->screen->root;
1949 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
1950 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
1951 demo->surface_description.pPlatformWindow = &demo->window;
1952#endif // _WIN32
1953
1954 // Iterate over each queue to learn whether it supports presenting to WSI:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001955 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
1956 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001957 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
1958 (VkSurfaceDescriptionWSI *) &demo->surface_description,
1959 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001960 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001961
1962 // Search for a graphics and a present queue in the array of queue
1963 // families, try to find one that supports both
1964 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
1965 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001966 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001967 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
1968 if (graphicsQueueNodeIndex == UINT32_MAX) {
1969 graphicsQueueNodeIndex = i;
1970 }
1971
1972 if (supportsPresent[i] == VK_TRUE) {
1973 graphicsQueueNodeIndex = i;
1974 presentQueueNodeIndex = i;
1975 break;
1976 }
1977 }
1978 }
1979 if (presentQueueNodeIndex == UINT32_MAX) {
1980 // If didn't find a queue that supports both graphics and present, then
1981 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001982 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001983 if (supportsPresent[i] == VK_TRUE) {
1984 presentQueueNodeIndex = i;
1985 break;
1986 }
1987 }
1988 }
1989 free(supportsPresent);
1990
1991 // Generate error if could not find both a graphics and a present queue
1992 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
1993 ERR_EXIT("Could not find a graphics and a present queue\n",
1994 "WSI Initialization Failure");
1995 }
1996
1997 // TODO: Add support for separate queues, including presentation,
1998 // synchronization, and appropriate tracking for QueueSubmit
1999 // While it is possible for an application to use a separate graphics and a
2000 // present queues, this demo program assumes it is only using one:
2001 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2002 ERR_EXIT("Could not find a common graphics and a present queue\n",
2003 "WSI Initialization Failure");
2004 }
2005
2006 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002007
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002008 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08002009 0, &demo->queue);
2010 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06002011
Ian Elliotte36b2082015-07-06 14:27:58 -06002012 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002013 uint32_t formatCount;
2014 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002015 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002016 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002017 assert(!err);
Ian Elliott7fe115d2015-08-07 15:56:59 -06002018 VkSurfaceFormatWSI *surfFormats =
2019 (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI));
2020 err = demo->fpGetSurfaceFormatsWSI(demo->device,
Ian Elliotte36b2082015-07-06 14:27:58 -06002021 (VkSurfaceDescriptionWSI *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002022 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002023 assert(!err);
2024 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2025 // the surface has no preferred format. Otherwise, at least one
2026 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002027 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2028 {
2029 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2030 }
2031 else
2032 {
2033 assert(formatCount >= 1);
2034 demo->format = surfFormats[0].format;
2035 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002036 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002037
Mark Lobodzinski72346292015-07-02 16:49:40 -06002038 // Get Memory information and properties
2039 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2040 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002041}
2042
2043static void demo_init_connection(struct demo *demo)
2044{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002045#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002046 const xcb_setup_t *setup;
2047 xcb_screen_iterator_t iter;
2048 int scr;
2049
2050 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002051 if (demo->connection == NULL) {
2052 printf("Cannot find a compatible Vulkan installable client driver "
2053 "(ICD).\nExiting ...\n");
2054 fflush(stdout);
2055 exit(1);
2056 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002057
2058 setup = xcb_get_setup(demo->connection);
2059 iter = xcb_setup_roots_iterator(setup);
2060 while (scr-- > 0)
2061 xcb_screen_next(&iter);
2062
2063 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002064#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002065}
2066
Ian Elliotte14e9f92015-04-16 15:23:05 -06002067#ifdef _WIN32
2068static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2069#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002070static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002071#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002072{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002073 bool argv_error = false;
2074
Chia-I Wuc19795a2014-09-13 11:12:55 +08002075 memset(demo, 0, sizeof(*demo));
2076
Ian Elliotte14e9f92015-04-16 15:23:05 -06002077#ifdef _WIN32
2078 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002079 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002080
2081 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2082 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002083 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2084 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002085 else if (strlen(pCmdLine) != 0) {
2086 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2087 argv_error = true;
2088 }
2089#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002090 for (int i = 0; i < argc; i++) {
2091 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2092 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002093 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2094 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002095 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002096#endif // _WIN32
2097 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002098 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002099 fflush(stderr);
2100 exit(1);
2101 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002102
Chia-I Wuc19795a2014-09-13 11:12:55 +08002103 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002104 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002105
2106 demo->width = 300;
2107 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002108}
2109
2110static void demo_cleanup(struct demo *demo)
2111{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002112 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002113
Tony Barbourde4124d2015-07-03 10:33:54 -06002114 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
2115 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2116 }
Tony Barbourb857d312015-07-10 10:50:45 -06002117 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06002118 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002119
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002120 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002121 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002122 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002123 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002124 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002125
Cody Northrope4bc6942015-08-26 10:01:32 -06002126 vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport);
2127 vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width);
2128 vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias);
2129 vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend);
2130 vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -06002131 vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002132
Tony Barbourde4124d2015-07-03 10:33:54 -06002133 vkDestroyPipeline(demo->device, demo->pipeline);
2134 vkDestroyRenderPass(demo->device, demo->render_pass);
2135 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2136 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002137
Tony Barbourde4124d2015-07-03 10:33:54 -06002138 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002139 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002140
Chia-I Wub043fe32014-10-06 15:30:33 +08002141 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002142 vkDestroyImageView(demo->device, demo->textures[i].view);
2143 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002144 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002145 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08002146 }
2147
Mark Lobodzinski8a190642015-08-07 10:17:13 -06002148 for (i = 0; i < demo->swapChainImageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002149 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002150 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002151
2152 vkDestroyAttachmentView(demo->device, demo->depth.view);
2153 vkDestroyImage(demo->device, demo->depth.image);
2154 vkFreeMemory(demo->device, demo->depth.mem);
2155
Ian Elliotte36b2082015-07-06 14:27:58 -06002156 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
2157 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002158
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002159 vkDestroyDevice(demo->device);
2160 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002161
Ian Elliotte14e9f92015-04-16 15:23:05 -06002162#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002163 xcb_destroy_window(demo->connection, demo->window);
2164 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002165#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002166}
2167
Ian Elliotte14e9f92015-04-16 15:23:05 -06002168#ifdef _WIN32
2169int APIENTRY WinMain(HINSTANCE hInstance,
2170 HINSTANCE hPrevInstance,
2171 LPSTR pCmdLine,
2172 int nCmdShow)
2173{
2174 MSG msg; // message
2175 bool done; // flag saying when app is complete
2176
2177 demo_init(&demo, hInstance, pCmdLine);
2178 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002179 demo_init_vk_wsi(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002180
2181 demo_prepare(&demo);
2182
2183 done = false; //initialize loop condition variable
2184 /* main message loop*/
2185 while(!done)
2186 {
Ian Elliott421107f2015-04-28 15:50:36 -06002187 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002188 if (msg.message == WM_QUIT) //check for a quit message
2189 {
2190 done = true; //if found, quit app
2191 }
2192 else
2193 {
2194 /* Translate and dispatch to event queue*/
2195 TranslateMessage(&msg);
2196 DispatchMessage(&msg);
2197 }
2198 }
2199
2200 demo_cleanup(&demo);
2201
Tony Barboura938abb2015-04-22 11:36:22 -06002202 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002203}
2204#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002205int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002206{
2207 struct demo demo;
2208
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002209 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002210 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002211 demo_init_vk_wsi(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002212
2213 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002214 demo_run(&demo);
2215
2216 demo_cleanup(&demo);
2217
Chia-I Wuc19795a2014-09-13 11:12:55 +08002218 return 0;
2219}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002220#endif // _WIN32