blob: 30f95c967a2de4228f5005bf7ae8198970e88a19 [file] [log] [blame]
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001/*
Ian Elliott7595eee2015-04-28 10:33:11 -06002 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24/*
Chia-I Wuf5caeb02014-10-25 12:11:27 +080025 * Draw a textured triangle with depth testing. This is written against Intel
26 * ICD. It does not do state transition nor object memory binding like it
27 * should. It also does no error checking.
28 */
29
Chia-I Wuc19795a2014-09-13 11:12:55 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
35
Ian Elliotte14e9f92015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
38#include <windows.h>
39#define APP_NAME_STR_LEN 80
40#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +080041#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060042#endif // _WIN32
43
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060044#include <vulkan.h>
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060045#include "vk_debug_report_lunarg.h"
Ian Elliott338dedb2015-08-21 15:09:33 -060046#include <vk_ext_khr_swapchain.h>
47#include <vk_ext_khr_device_swapchain.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080048
Cody Northropd4e020a2015-03-17 14:54:35 -060049#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060050
Chia-I 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 Elliott338dedb2015-08-21 15:09:33 -0600141typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600142 VkImage image;
143 VkCmdBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600144 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600145} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600146
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 Elliott338dedb2015-08-21 15:09:33 -0600158 VkPlatformHandleXcbKHR 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 Elliott338dedb2015-08-21 15:09:33 -0600174 VkColorSpaceKHR color_space;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800175
Ian Elliott338dedb2015-08-21 15:09:33 -0600176 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
177 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
178 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
179 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
180 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
181 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
182 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
183 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
184 PFN_vkQueuePresentKHR fpQueuePresentKHR;
185 VkSurfaceDescriptionWindowKHR surface_description;
186 uint32_t swapchainImageCount;
187 VkSwapchainKHR swap_chain;
188 SwapchainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800189
Ian Elliotte36b2082015-07-06 14:27:58 -0600190 VkCmdPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800191
Chia-I Wub043fe32014-10-06 15:30:33 +0800192 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600193 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800194
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600195 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500196 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600197 VkImageView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800198 } depth;
199
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600200 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800201
Chia-I Wu99621bc2014-10-08 11:52:22 +0800202 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500204 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800205
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600206 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600207 VkVertexInputBindingDescription vi_bindings[1];
208 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800209 } vertices;
210
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600211 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
212 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500213 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600214 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600215 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800216 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600217 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800218
Cody Northrope4bc6942015-08-26 10:01:32 -0600219 VkDynamicViewportState dynamic_viewport;
220 VkDynamicLineWidthState dynamic_line_width;
221 VkDynamicDepthBiasState dynamic_depth_bias;
222 VkDynamicBlendState dynamic_blend;
223 VkDynamicDepthBoundsState dynamic_depth_bounds;
Cody Northrop2605cb02015-08-18 15:21:16 -0600224 VkDynamicStencilState dynamic_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800225
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -0600226 VkShaderModule vert_shader_module;
227 VkShaderModule frag_shader_module;
228
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600229 VkDescriptorPool desc_pool;
230 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800231
Chia-I Wu76cd4222015-07-08 13:34:24 +0800232 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
233
Mark Lobodzinski72346292015-07-02 16:49:40 -0600234 VkPhysicalDeviceMemoryProperties memory_properties;
235
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600236 bool validate;
237 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Ian Elliotte36b2082015-07-06 14:27:58 -0600238 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600239 VkDbgMsgCallback msg_callback;
240
Chia-I Wuc19795a2014-09-13 11:12:55 +0800241 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600242 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600243 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800244};
245
Mark Lobodzinski72346292015-07-02 16:49:40 -0600246static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
247{
248 // Search memtypes to find first index with those properties
249 for (uint32_t i = 0; i < 32; i++) {
250 if ((typeBits & 1) == 1) {
251 // Type is available, does it match user properties?
252 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
253 *typeIndex = i;
254 return VK_SUCCESS;
255 }
256 }
257 typeBits >>= 1;
258 }
259 // No memory types matched, return failure
260 return VK_UNSUPPORTED;
261}
262
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600263static void demo_flush_init_cmd(struct demo *demo)
264{
Tony Barbour22a30862015-04-22 09:02:32 -0600265 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600266
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600267 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600268 return;
269
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600270 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600271 assert(!err);
272
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600273 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600274 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275
Tony Barbourde4124d2015-07-03 10:33:54 -0600276 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600277 assert(!err);
278
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600279 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600280 assert(!err);
281
Tony Barbourde4124d2015-07-03 10:33:54 -0600282 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600283 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600284}
285
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600286static void demo_set_image_layout(
287 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600288 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400289 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600290 VkImageLayout old_image_layout,
291 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600292{
Tony Barbour22a30862015-04-22 09:02:32 -0600293 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600294
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600295 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600296 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600297 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600298 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600299 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800300 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600301 .flags = 0,
302 };
303
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600304 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600305 assert(!err);
306
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600307 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600308 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600310 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600311 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600312 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600313 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600314 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600315 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600316 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600317 }
318
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600319 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600320 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600321 .pNext = NULL,
322 .outputMask = 0,
323 .inputMask = 0,
324 .oldLayout = old_image_layout,
325 .newLayout = new_image_layout,
326 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400327 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600328 };
329
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600330 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600331 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600332 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600333 }
334
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600335 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600336 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600337 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600338 }
339
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600340 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600341
Tony Barbourc2e987e2015-06-29 16:20:35 -0600342 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
343 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600344
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600345 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600346}
347
Chia-I Wuc19795a2014-09-13 11:12:55 +0800348static void demo_draw_build_cmd(struct demo *demo)
349{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800350 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600351 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600352 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600353 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600354 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600355 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600356 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600357 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn53d27af2014-12-31 17:08:35 -0700358 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800359 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600360 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
361 [1] = { .depthStencil = { 0.9f, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800362 };
363 const VkRenderPassBeginInfo rp_begin = {
364 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
365 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800366 .renderPass = demo->render_pass,
367 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800368 .renderArea.offset.x = 0,
369 .renderArea.offset.y = 0,
370 .renderArea.extent.width = demo->width,
371 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600372 .clearValueCount = 2,
373 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700374 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800375 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800376
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600377 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800378 assert(!err);
379
Chia-I Wuc278df82015-07-07 11:50:03 +0800380 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600381 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800382 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600383 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600384 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800385
Cody Northrope4bc6942015-08-26 10:01:32 -0600386 vkCmdBindDynamicViewportState(demo->draw_cmd, demo->dynamic_viewport);
387 vkCmdBindDynamicLineWidthState(demo->draw_cmd, demo->dynamic_line_width);
388 vkCmdBindDynamicDepthBiasState(demo->draw_cmd, demo->dynamic_depth_bias);
389 vkCmdBindDynamicBlendState(demo->draw_cmd, demo->dynamic_blend);
390 vkCmdBindDynamicDepthBoundsState(demo->draw_cmd, demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -0600391 vkCmdBindDynamicStencilState(demo->draw_cmd, demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800392
Tony Barbour8205d902015-04-16 15:59:00 -0600393 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600394 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800395
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600396 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800397 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800398
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600399 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800400 assert(!err);
401}
402
403static void demo_draw(struct demo *demo)
404{
Tony Barbour22a30862015-04-22 09:02:32 -0600405 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600406 VkSemaphore presentCompleteSemaphore;
407 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
408 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
409 .pNext = NULL,
410 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
411 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800412
Ian Elliotte36b2082015-07-06 14:27:58 -0600413 err = vkCreateSemaphore(demo->device,
414 &presentCompleteSemaphoreCreateInfo,
415 &presentCompleteSemaphore);
416 assert(!err);
417
418 // Get the index of the next available swapchain image:
Ian Elliott338dedb2015-08-21 15:09:33 -0600419 err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600420 UINT64_MAX,
421 presentCompleteSemaphore,
422 &demo->current_buffer);
Ian Elliott338dedb2015-08-21 15:09:33 -0600423 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliotte36b2082015-07-06 14:27:58 -0600424 // 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
Ian Elliott338dedb2015-08-21 15:09:33 -0600433// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
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 Elliott338dedb2015-08-21 15:09:33 -0600440 VkPresentInfoKHR present = {
441 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600442 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600443 .swapchainCount = 1,
444 .swapchains = &demo->swap_chain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600445 .imageIndices = &demo->current_buffer,
446 };
447
448// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600449 err = demo->fpQueuePresentKHR(demo->queue, &present);
450 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliotte36b2082015-07-06 14:27:58 -0600451 // return codes
Chia-I Wuc19795a2014-09-13 11:12:55 +0800452 assert(!err);
453
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800454 err = vkQueueWaitIdle(demo->queue);
455 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600456
457 err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
458 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800459}
460
461static void demo_prepare_buffers(struct demo *demo)
462{
Ian Elliotte36b2082015-07-06 14:27:58 -0600463 VkResult U_ASSERT_ONLY err;
464
465 // Check the surface proprties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600466 VkSurfacePropertiesKHR surfProperties;
467 err = demo->fpGetSurfacePropertiesKHR(demo->device,
468 (const VkSurfaceDescriptionKHR *)&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;
Ian Elliott338dedb2015-08-21 15:09:33 -0600473 err = demo->fpGetSurfacePresentModesKHR(demo->device,
474 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600475 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600476 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600477 VkPresentModeKHR *presentModes =
478 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600479 assert(presentModes);
Ian Elliott338dedb2015-08-21 15:09:33 -0600480 err = demo->fpGetSurfacePresentModesKHR(demo->device,
481 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600482 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600483 assert(!err);
484
Ian Elliott338dedb2015-08-21 15:09:33 -0600485 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600486 // 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.
Ian Elliott338dedb2015-08-21 15:09:33 -0600491 swapchainExtent.width = demo->width;
492 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600493 }
494 else
495 {
496 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -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.
Ian Elliott338dedb2015-08-21 15:09:33 -0600504 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600505 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -0600506 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
507 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600508 break;
509 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600510 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
511 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
512 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3fc49c22015-07-27 13:53:11 -0600513 }
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 Elliott338dedb2015-08-21 15:09:33 -0600519 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600520 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600521 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600522 {
523 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600524 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600525 }
526
Ian Elliott338dedb2015-08-21 15:09:33 -0600527 VkSurfaceTransformKHR preTransform;
528 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
529 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600530 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600531 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600532 }
533
Ian Elliott338dedb2015-08-21 15:09:33 -0600534 const VkSwapchainCreateInfoKHR swap_chain = {
535 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800536 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600537 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&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 Elliott338dedb2015-08-21 15:09:33 -0600542 .width = swapchainExtent.width,
543 .height = swapchainExtent.height,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800544 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600545 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600546 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800547 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600548 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
549 .queueFamilyCount = 0,
550 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600551 .presentMode = swapchainPresentMode,
552 .oldSwapchain.handle = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600553 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800554 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600555 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800556
Ian Elliott338dedb2015-08-21 15:09:33 -0600557 err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800558 assert(!err);
559
Ian Elliott338dedb2015-08-21 15:09:33 -0600560 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
561 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600562 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800563
Ian Elliott338dedb2015-08-21 15:09:33 -0600564 VkImage* swapchainImages =
565 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
566 assert(swapchainImages);
567 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
568 &demo->swapchainImageCount,
569 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600570 assert(!err);
571
Ian Elliott338dedb2015-08-21 15:09:33 -0600572 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600573 assert(demo->buffers);
574
Ian Elliott338dedb2015-08-21 15:09:33 -0600575 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600576 VkImageViewCreateInfo color_attachment_view = {
577 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800578 .pNext = NULL,
579 .format = demo->format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600580 .channels = {
581 .r = VK_CHANNEL_SWIZZLE_R,
582 .g = VK_CHANNEL_SWIZZLE_G,
583 .b = VK_CHANNEL_SWIZZLE_B,
584 .a = VK_CHANNEL_SWIZZLE_A,
585 },
586 .subresourceRange = {
587 .aspect = VK_IMAGE_ASPECT_COLOR,
588 .baseMipLevel = 0,
589 .mipLevels = 1,
590 .baseArraySlice = 0,
591 .arraySize = 1
592 },
593 .viewType = VK_IMAGE_VIEW_TYPE_2D,
594 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800595 };
596
Ian Elliott338dedb2015-08-21 15:09:33 -0600597 demo->buffers[i].image = swapchainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500598
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600599 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400600 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600601 VK_IMAGE_LAYOUT_UNDEFINED,
602 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600603
Chia-I Wuc19795a2014-09-13 11:12:55 +0800604 color_attachment_view.image = demo->buffers[i].image;
605
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600606 err = vkCreateImageView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800607 &color_attachment_view, &demo->buffers[i].view);
608 assert(!err);
609 }
Piers Daniell886be472015-02-23 16:23:13 -0700610
611 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800612}
613
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800614static void demo_prepare_depth(struct demo *demo)
615{
Tony Barbour8205d902015-04-16 15:59:00 -0600616 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600617 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600618 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800619 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600620 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800621 .format = depth_format,
622 .extent = { demo->width, demo->height, 1 },
623 .mipLevels = 1,
624 .arraySize = 1,
625 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600626 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600627 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800628 .flags = 0,
629 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600630 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600631 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500632 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800633 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600634 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800635 };
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600636 VkImageViewCreateInfo view = {
637 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800638 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600639 .image.handle = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600640 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600641 .subresourceRange = {
Cody Northropec2dc652015-09-03 16:09:28 -0600642 .aspect = VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600643 .baseMipLevel = 0,
644 .mipLevels = 1,
645 .baseArraySlice = 0,
646 .arraySize = 1
647 },
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800648 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600649 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800650 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700651
Mark Lobodzinski23182612015-05-29 09:32:35 -0500652 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600653 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800654
655 demo->depth.format = depth_format;
656
657 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600658 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800659 &demo->depth.image);
660 assert(!err);
661
Mark Lobodzinski72346292015-07-02 16:49:40 -0600662 /* get memory requirements for this object */
Tony Barbourde4124d2015-07-03 10:33:54 -0600663 err = vkGetImageMemoryRequirements(demo->device, demo->depth.image,
664 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600665
666 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500667 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600668 err = memory_type_from_properties(demo,
669 mem_reqs.memoryTypeBits,
670 VK_MEMORY_PROPERTY_DEVICE_ONLY,
671 &mem_alloc.memoryTypeIndex);
672 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800673
Mark Lobodzinski23182612015-05-29 09:32:35 -0500674 /* allocate memory */
675 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
676 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800677
Mark Lobodzinski23182612015-05-29 09:32:35 -0500678 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600679 err = vkBindImageMemory(demo->device, demo->depth.image,
680 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500681 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800682
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600683 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400684 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600685 VK_IMAGE_LAYOUT_UNDEFINED,
686 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600687
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800688 /* create image view */
689 view.image = demo->depth.image;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600690 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800691 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800692}
693
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700694static void demo_prepare_texture_image(struct demo *demo,
695 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600696 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600697 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600698 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600699 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800700{
Tony Barbour8205d902015-04-16 15:59:00 -0600701 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600702 const int32_t tex_width = 2;
703 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600704 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800705
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600706 tex_obj->tex_width = tex_width;
707 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700708
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600709 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600710 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700711 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600712 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700713 .format = tex_format,
714 .extent = { tex_width, tex_height, 1 },
715 .mipLevels = 1,
716 .arraySize = 1,
717 .samples = 1,
718 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600719 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700720 .flags = 0,
721 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600722 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600723 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500724 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700725 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600726 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700727 };
728
Mark Lobodzinski23182612015-05-29 09:32:35 -0500729 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700730
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600731 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600732 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700733 assert(!err);
734
Tony Barbourde4124d2015-07-03 10:33:54 -0600735 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600736
737 mem_alloc.allocationSize = mem_reqs.size;
738 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
739 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700740
Mark Lobodzinski23182612015-05-29 09:32:35 -0500741 /* allocate memory */
742 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
743 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700744
Mark Lobodzinski23182612015-05-29 09:32:35 -0500745 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600746 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500747 tex_obj->mem, 0);
748 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700749
Tony Barbour8205d902015-04-16 15:59:00 -0600750 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600751 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600752 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700753 .mipLevel = 0,
754 .arraySlice = 0,
755 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600756 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700757 void *data;
758 int32_t x, y;
759
Tony Barbour426b9052015-06-24 16:06:58 -0600760 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
761 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700762
Mark Lobodzinski23182612015-05-29 09:32:35 -0500763 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700764 assert(!err);
765
766 for (y = 0; y < tex_height; y++) {
767 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
768 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700769 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700770 }
771
Mark Lobodzinski23182612015-05-29 09:32:35 -0500772 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700773 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700774 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600775
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600776 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600777 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400778 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600779 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600780 tex_obj->imageLayout);
781 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700782}
783
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500784static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700785{
786 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600787 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600788 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700789}
790
791static void demo_prepare_textures(struct demo *demo)
792{
Tony Barbour8205d902015-04-16 15:59:00 -0600793 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600794 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700795 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
796 { 0xffff0000, 0xff00ff00 },
797 };
Tony Barbour22a30862015-04-22 09:02:32 -0600798 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700799 uint32_t i;
800
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600801 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700802 assert(!err);
803
804 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600805 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700806 /* Device can texture using linear textures */
807 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600808 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600809 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700810 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600811 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700812
813 memset(&staging_texture, 0, sizeof(staging_texture));
814 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600815 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700816
817 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600818 VK_IMAGE_TILING_OPTIMAL,
819 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
820 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700821
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600822 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400823 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600824 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600825 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700826
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600827 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400828 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600829 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600830 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700831
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600832 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600833 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700834 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600835 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700836 .destOffset = { 0, 0, 0 },
837 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
838 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600839 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600840 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
841 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600842 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700843
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600844 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400845 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600846 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600847 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700848
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600849 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700850
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600851 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700852 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600853 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700854 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700855 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700856
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600857 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600858 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800859 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600860 .magFilter = VK_TEX_FILTER_NEAREST,
861 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600862 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600863 .addressU = VK_TEX_ADDRESS_WRAP,
864 .addressV = VK_TEX_ADDRESS_WRAP,
865 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800866 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700867 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600868 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800869 .minLod = 0.0f,
870 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600871 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600872 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800873 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600874 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600875 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800876 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600877 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600878 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700879 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600880 .channels = { VK_CHANNEL_SWIZZLE_R,
881 VK_CHANNEL_SWIZZLE_G,
882 VK_CHANNEL_SWIZZLE_B,
883 VK_CHANNEL_SWIZZLE_A, },
884 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600885 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +0800886 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700887
Chia-I Wub043fe32014-10-06 15:30:33 +0800888 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600889 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800890 &demo->textures[i].sampler);
891 assert(!err);
892
Chia-I Wub043fe32014-10-06 15:30:33 +0800893 /* create image view */
894 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600895 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700896 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800897 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800898 }
899}
900
Chia-I Wu99621bc2014-10-08 11:52:22 +0800901static void demo_prepare_vertices(struct demo *demo)
902{
903 const float vb[3][5] = {
904 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800905 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
906 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800907 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
908 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600909 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600910 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800911 .pNext = NULL,
912 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600913 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800914 .flags = 0,
915 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600916 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600917 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500918 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800919 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600920 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800921 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500922 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600923 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800924 void *data;
925
926 memset(&demo->vertices, 0, sizeof(demo->vertices));
927
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600928 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800929 assert(!err);
930
Tony Barbourde4124d2015-07-03 10:33:54 -0600931 err = vkGetBufferMemoryRequirements(demo->device,
932 demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800933
Mark Lobodzinski72346292015-07-02 16:49:40 -0600934 mem_alloc.allocationSize = mem_reqs.size;
935 err = memory_type_from_properties(demo,
936 mem_reqs.memoryTypeBits,
937 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
938 &mem_alloc.memoryTypeIndex);
939 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800940
Mark Lobodzinski23182612015-05-29 09:32:35 -0500941 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
942 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800943
Mark Lobodzinski23182612015-05-29 09:32:35 -0500944 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
945 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800946
Mark Lobodzinski23182612015-05-29 09:32:35 -0500947 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800948
Mark Lobodzinski23182612015-05-29 09:32:35 -0500949 err = vkUnmapMemory(demo->device, demo->vertices.mem);
950 assert(!err);
951
Tony Barbourde4124d2015-07-03 10:33:54 -0600952 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500953 demo->vertices.mem, 0);
954 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800955
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600956 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800957 demo->vertices.vi.pNext = NULL;
958 demo->vertices.vi.bindingCount = 1;
959 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
960 demo->vertices.vi.attributeCount = 2;
961 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
962
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600963 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800964 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600965 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800966
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600967 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
968 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600969 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800970 demo->vertices.vi_attrs[0].offsetInBytes = 0;
971
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600972 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
973 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600974 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800975 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800976}
977
Chia-I Wuf8385062015-01-04 16:27:24 +0800978static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800979{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600980 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600981 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800982 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600983 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800984 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800985 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600986 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600987 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800988 .pNext = NULL,
989 .count = 1,
990 .pBinding = &layout_binding,
991 };
Tony Barbour22a30862015-04-22 09:02:32 -0600992 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800993
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600994 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800995 &descriptor_layout, &demo->desc_layout);
996 assert(!err);
997
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500998 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
999 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1000 .pNext = NULL,
1001 .descriptorSetCount = 1,
1002 .pSetLayouts = &demo->desc_layout,
1003 };
1004
1005 err = vkCreatePipelineLayout(demo->device,
1006 &pPipelineLayoutCreateInfo,
1007 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001008 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001009}
1010
Chia-I Wu76cd4222015-07-08 13:34:24 +08001011static void demo_prepare_render_pass(struct demo *demo)
1012{
Chia-I Wuc278df82015-07-07 11:50:03 +08001013 const VkAttachmentDescription attachments[2] = {
1014 [0] = {
1015 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1016 .pNext = NULL,
1017 .format = demo->format,
1018 .samples = 1,
1019 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1020 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1021 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1022 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1023 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1024 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1025 },
1026 [1] = {
1027 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1028 .pNext = NULL,
1029 .format = demo->depth.format,
1030 .samples = 1,
1031 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1032 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1033 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1034 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1035 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1036 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1037 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001038 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001039 const VkAttachmentReference color_reference = {
1040 .attachment = 0,
1041 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1042 };
1043 const VkSubpassDescription subpass = {
1044 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1045 .pNext = NULL,
1046 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1047 .flags = 0,
1048 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001049 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001050 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001051 .pColorAttachments = &color_reference,
1052 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001053 .depthStencilAttachment = {
1054 .attachment = 1,
1055 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1056 },
1057 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001058 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001059 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001060 const VkRenderPassCreateInfo rp_info = {
1061 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1062 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001063 .attachmentCount = 2,
1064 .pAttachments = attachments,
1065 .subpassCount = 1,
1066 .pSubpasses = &subpass,
1067 .dependencyCount = 0,
1068 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001069 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001070 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001071
1072 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1073 assert(!err);
1074}
1075
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001076static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001077 VkShaderStage stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001078 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001079 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001080 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001081{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001082 VkShaderModuleCreateInfo moduleCreateInfo;
1083 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001084 VkShader shader;
1085 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001086
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001087
1088 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1089 moduleCreateInfo.pNext = NULL;
1090
1091 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1092 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001093
Cody Northrop75db0322015-05-28 11:27:16 -06001094 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001095 moduleCreateInfo.codeSize = size;
1096 moduleCreateInfo.pCode = code;
1097 moduleCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001098 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001099 if (err) {
1100 free((void *) moduleCreateInfo.pCode);
1101 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001102
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001103 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001104 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001105 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001106 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001107 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001108 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001109 } else {
1110 // Create fake SPV structure to feed GLSL
1111 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001112 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1113 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1114 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001115
Cody Northrop75db0322015-05-28 11:27:16 -06001116 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001117 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1118 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1119 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1120 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001121
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001122 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001123 if (err) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001124 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001125 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001126
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001127 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001128 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001129 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001130 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001131 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001132 assert(!err);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001133 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001134 return shader;
1135}
1136
Cody Northrop75db0322015-05-28 11:27:16 -06001137char *demo_read_spv(const char *filename, size_t *psize)
1138{
1139 long int size;
1140 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001141 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001142
1143 FILE *fp = fopen(filename, "rb");
1144 if (!fp) return NULL;
1145
1146 fseek(fp, 0L, SEEK_END);
1147 size = ftell(fp);
1148
1149 fseek(fp, 0L, SEEK_SET);
1150
1151 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001152 retVal = fread(shader_code, size, 1, fp);
1153 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001154
1155 *psize = size;
1156
1157 return shader_code;
1158}
1159
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001160static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001161{
Cody Northrop75db0322015-05-28 11:27:16 -06001162 if (!demo->use_glsl) {
1163 void *vertShaderCode;
1164 size_t size;
1165
1166 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
1167
1168 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001169 &demo->vert_shader_module,
1170 vertShaderCode, size);
Cody Northrop75db0322015-05-28 11:27:16 -06001171 } else {
1172 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001173 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001174 "#extension GL_ARB_separate_shader_objects : enable\n"
1175 "#extension GL_ARB_shading_language_420pack : enable\n"
1176 "layout (location = 0) in vec4 pos;\n"
1177 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001178 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001179 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001180 " texcoord = attr;\n"
1181 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001182 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001183
Cody Northrop75db0322015-05-28 11:27:16 -06001184 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001185 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001186 (const void *) vertShaderText,
1187 strlen(vertShaderText));
1188 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001189}
1190
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001191static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001192{
Cody Northrop75db0322015-05-28 11:27:16 -06001193 if (!demo->use_glsl) {
1194 void *fragShaderCode;
1195 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001196
Cody Northrop75db0322015-05-28 11:27:16 -06001197 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
1198
1199 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001200 &demo->frag_shader_module,
1201 fragShaderCode, size);
Cody Northrop75db0322015-05-28 11:27:16 -06001202 } else {
1203 static const char *fragShaderText =
1204 "#version 140\n"
1205 "#extension GL_ARB_separate_shader_objects : enable\n"
1206 "#extension GL_ARB_shading_language_420pack : enable\n"
1207 "layout (binding = 0) uniform sampler2D tex;\n"
1208 "layout (location = 0) in vec2 texcoord;\n"
1209 "layout (location = 0) out vec4 uFragColor;\n"
1210 "void main() {\n"
1211 " uFragColor = texture(tex, texcoord);\n"
1212 "}\n";
1213
1214 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001215 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001216 (const void *) fragShaderText,
1217 strlen(fragShaderText));
1218 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001219}
1220
1221static void demo_prepare_pipeline(struct demo *demo)
1222{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001223 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001224 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001225
Tony Barboure307f582015-07-10 15:29:03 -06001226 VkPipelineVertexInputStateCreateInfo vi;
1227 VkPipelineInputAssemblyStateCreateInfo ia;
1228 VkPipelineRasterStateCreateInfo rs;
1229 VkPipelineColorBlendStateCreateInfo cb;
1230 VkPipelineDepthStencilStateCreateInfo ds;
1231 VkPipelineViewportStateCreateInfo vp;
1232 VkPipelineMultisampleStateCreateInfo ms;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001233
Tony Barbour22a30862015-04-22 09:02:32 -06001234 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001235
1236 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001237 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001238 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001239
Chia-I Wu8d29d022014-10-08 12:14:39 +08001240 vi = demo->vertices.vi;
1241
Chia-I Wuc19795a2014-09-13 11:12:55 +08001242 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001243 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001244 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001245
1246 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001247 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001248 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001249 rs.cullMode = VK_CULL_MODE_BACK;
1250 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001251 rs.depthClipEnable = VK_TRUE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001252 rs.rasterizerDiscardEnable = VK_FALSE;
1253 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001254
1255 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001256 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1257 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001258 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001259 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001260 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001261 cb.attachmentCount = 1;
1262 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001263
Tony Barbourfa6cac72015-01-16 14:27:35 -07001264 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001265 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001266 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001267
1268 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001269 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001270 ds.depthTestEnable = VK_TRUE;
1271 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001272 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001273 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001274 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1275 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001276 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001277 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001278 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001279
Tony Barbourfa6cac72015-01-16 14:27:35 -07001280 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001281 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001282 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001283 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001284
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001285 // Two stages: vs and fs
1286 pipeline.stageCount = 2;
1287 VkPipelineShaderStageCreateInfo shaderStages[2];
1288 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1289
1290 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1291 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1292 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001293
1294 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1295 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1296 shaderStages[1].shader = demo_prepare_fs(demo);
1297
Tony Barboure307f582015-07-10 15:29:03 -06001298 pipeline.pVertexInputState = &vi;
1299 pipeline.pInputAssemblyState = &ia;
1300 pipeline.pRasterState = &rs;
1301 pipeline.pColorBlendState = &cb;
1302 pipeline.pMultisampleState = &ms;
1303 pipeline.pViewportState = &vp;
1304 pipeline.pDepthStencilState = &ds;
1305 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001306 pipeline.renderPass = demo->render_pass;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001307
Jon Ashburn0d60d272015-07-09 15:02:25 -06001308 memset(&pipelineCache, 0, sizeof(pipelineCache));
1309 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1310
1311 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1312 assert(!err);
1313 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001314 assert(!err);
1315
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001316 err = vkDestroyPipelineCache(demo->device, demo->pipelineCache);
1317 assert(!err);
1318
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001319 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001320 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001321 }
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001322 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1323 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001324}
1325
1326static void demo_prepare_dynamic_states(struct demo *demo)
1327{
Tony Barbourde4124d2015-07-03 10:33:54 -06001328 VkDynamicViewportStateCreateInfo viewport_create;
Cody Northrope4bc6942015-08-26 10:01:32 -06001329 VkDynamicLineWidthStateCreateInfo line_width;
1330 VkDynamicDepthBiasStateCreateInfo depth_bias;
1331 VkDynamicBlendStateCreateInfo blend;
1332 VkDynamicDepthBoundsStateCreateInfo depth_bounds;
Cody Northrop2605cb02015-08-18 15:21:16 -06001333 VkDynamicStencilStateCreateInfo stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001334 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001335
Tony Barbourfa6cac72015-01-16 14:27:35 -07001336 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001337 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001338 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001339 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001340 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001341 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001342 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001343 viewport.minDepth = (float) 0.0f;
1344 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001345 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001346 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001347 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001348 scissor.extent.width = demo->width;
1349 scissor.extent.height = demo->height;
1350 scissor.offset.x = 0;
1351 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001352 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001353
Cody Northrope4bc6942015-08-26 10:01:32 -06001354 memset(&line_width, 0, sizeof(line_width));
1355 line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO;
1356 line_width.lineWidth = 1.0;
Cody Northropf5bd2252015-08-17 11:10:49 -06001357
Cody Northrope4bc6942015-08-26 10:01:32 -06001358 memset(&depth_bias, 0, sizeof(depth_bias));
1359 depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO;
1360 depth_bias.depthBias = 0.0f;
1361 depth_bias.depthBiasClamp = 0.0f;
1362 depth_bias.slopeScaledDepthBias = 0.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001363
Cody Northrope4bc6942015-08-26 10:01:32 -06001364 memset(&blend, 0, sizeof(blend));
1365 blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO;
1366 blend.blendConst[0] = 1.0f;
1367 blend.blendConst[1] = 1.0f;
1368 blend.blendConst[2] = 1.0f;
1369 blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001370
Cody Northrope4bc6942015-08-26 10:01:32 -06001371 memset(&depth_bounds, 0, sizeof(depth_bounds));
1372 depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO;
1373 depth_bounds.minDepthBounds = 0.0f;
1374 depth_bounds.maxDepthBounds = 1.0f;
Cody Northrop2605cb02015-08-18 15:21:16 -06001375
1376 memset(&stencil, 0, sizeof(stencil));
1377 stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO;
1378 stencil.stencilReference = 0;
Cody Northrope4bc6942015-08-26 10:01:32 -06001379 stencil.stencilCompareMask = 0xff;
Cody Northrop2605cb02015-08-18 15:21:16 -06001380 stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001381
Cody Northrope4bc6942015-08-26 10:01:32 -06001382 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001383 assert(!err);
1384
Cody Northrope4bc6942015-08-26 10:01:32 -06001385 err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width);
Cody Northropf5bd2252015-08-17 11:10:49 -06001386 assert(!err);
1387
Cody Northrope4bc6942015-08-26 10:01:32 -06001388 err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001389 assert(!err);
1390
Cody Northrope4bc6942015-08-26 10:01:32 -06001391 err = vkCreateDynamicBlendState(demo->device,
1392 &blend, &demo->dynamic_blend);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001393 assert(!err);
1394
Cody Northrope4bc6942015-08-26 10:01:32 -06001395 err = vkCreateDynamicDepthBoundsState(demo->device,
1396 &depth_bounds, &demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -06001397 assert(!err);
1398
1399 err = vkCreateDynamicStencilState(demo->device,
1400 &stencil, &stencil, &demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001401 assert(!err);
1402}
1403
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001404static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001405{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001406 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001407 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001408 .count = DEMO_TEXTURE_COUNT,
1409 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001410 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001411 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001412 .pNext = NULL,
1413 .count = 1,
1414 .pTypeCount = &type_count,
1415 };
Tony Barbour22a30862015-04-22 09:02:32 -06001416 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001417
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001418 err = vkCreateDescriptorPool(demo->device,
1419 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001420 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001421 assert(!err);
1422}
1423
1424static void demo_prepare_descriptor_set(struct demo *demo)
1425{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001426 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1427 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001428 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001429 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001430
Mike Stroyan230e6252015-04-17 12:36:38 -06001431 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001432 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001433 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001434 &demo->desc_set);
1435 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001436
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001437 memset(&tex_descs, 0, sizeof(tex_descs));
1438 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1439 tex_descs[i].sampler = demo->textures[i].sampler;
1440 tex_descs[i].imageView = demo->textures[i].view;
1441 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1442 }
1443
1444 memset(&write, 0, sizeof(write));
1445 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1446 write.destSet = demo->desc_set;
1447 write.count = DEMO_TEXTURE_COUNT;
1448 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1449 write.pDescriptors = tex_descs;
1450
1451 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1452 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001453}
1454
Chia-I Wu76cd4222015-07-08 13:34:24 +08001455static void demo_prepare_framebuffers(struct demo *demo)
1456{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001457 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001458 attachments[1] = demo->depth.view;
1459
Chia-I Wu76cd4222015-07-08 13:34:24 +08001460 const VkFramebufferCreateInfo fb_info = {
1461 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1462 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001463 .renderPass = demo->render_pass,
1464 .attachmentCount = 2,
1465 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001466 .width = demo->width,
1467 .height = demo->height,
1468 .layers = 1,
1469 };
1470 VkResult U_ASSERT_ONLY err;
1471 uint32_t i;
1472
1473 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001474 attachments[0]= demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001475 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1476 assert(!err);
1477 }
1478}
1479
Chia-I Wuc19795a2014-09-13 11:12:55 +08001480static void demo_prepare(struct demo *demo)
1481{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001482 VkResult U_ASSERT_ONLY err;
1483
1484 const VkCmdPoolCreateInfo cmd_pool_info = {
1485 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1486 .pNext = NULL,
1487 .queueFamilyIndex = demo->graphics_queue_node_index,
1488 .flags = 0,
1489 };
1490 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1491 assert(!err);
1492
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001493 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001494 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001495 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001496 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001497 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001498 .flags = 0,
1499 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001500 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1501 assert(!err);
1502
Chia-I Wuc19795a2014-09-13 11:12:55 +08001503 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001504 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001505 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001506 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001507 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001508 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001509 demo_prepare_pipeline(demo);
1510 demo_prepare_dynamic_states(demo);
1511
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001512 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001513 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001514
1515 demo_prepare_framebuffers(demo);
1516
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001517 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001518}
1519
Ian Elliotte14e9f92015-04-16 15:23:05 -06001520#ifdef _WIN32
1521static void demo_run(struct demo *demo)
1522{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001523 if (!demo->prepared)
1524 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001525 demo_draw(demo);
1526}
1527
1528// On MS-Windows, make this a global, so it's available to WndProc()
1529struct demo demo;
1530
1531// MS-Windows event handling function:
1532LRESULT CALLBACK WndProc(HWND hWnd,
1533 UINT uMsg,
1534 WPARAM wParam,
1535 LPARAM lParam)
1536{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001537 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001538
1539 switch(uMsg)
1540 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001541 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001542 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001543 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001544 PostQuitMessage(0);
1545 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001546 case WM_PAINT:
Cody Northrop67ff0a42015-09-09 10:21:49 -06001547 if (demo.prepared) {
1548 demo_run(&demo);
1549 return 0;
1550 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001551 default:
1552 break;
1553 }
1554 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1555}
1556
1557static void demo_create_window(struct demo *demo)
1558{
1559 WNDCLASSEX win_class;
1560
1561 // Initialize the window class structure:
1562 win_class.cbSize = sizeof(WNDCLASSEX);
1563 win_class.style = CS_HREDRAW | CS_VREDRAW;
1564 win_class.lpfnWndProc = WndProc;
1565 win_class.cbClsExtra = 0;
1566 win_class.cbWndExtra = 0;
1567 win_class.hInstance = demo->connection; // hInstance
1568 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1569 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1570 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1571 win_class.lpszMenuName = NULL;
1572 win_class.lpszClassName = demo->name;
1573 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1574 // Register window class:
1575 if (!RegisterClassEx(&win_class)) {
1576 // It didn't work, so try to give a useful error:
1577 printf("Unexpected error trying to start the application!\n");
1578 fflush(stdout);
1579 exit(1);
1580 }
1581 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001582 RECT wr = { 0, 0, demo->width, demo->height };
1583 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001584 demo->window = CreateWindowEx(0,
1585 demo->name, // class name
1586 demo->name, // app name
1587 WS_OVERLAPPEDWINDOW | // window style
1588 WS_VISIBLE |
1589 WS_SYSMENU,
1590 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001591 wr.right-wr.left, // width
1592 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001593 NULL, // handle to parent
1594 NULL, // handle to menu
1595 demo->connection, // hInstance
1596 NULL); // no extra parameters
1597 if (!demo->window) {
1598 // It didn't work, so try to give a useful error:
1599 printf("Cannot create a window in which to draw!\n");
1600 fflush(stdout);
1601 exit(1);
1602 }
1603}
1604#else // _WIN32
1605
Chia-I Wuc19795a2014-09-13 11:12:55 +08001606static void demo_handle_event(struct demo *demo,
1607 const xcb_generic_event_t *event)
1608{
1609 switch (event->response_type & 0x7f) {
1610 case XCB_EXPOSE:
1611 demo_draw(demo);
1612 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001613 case XCB_CLIENT_MESSAGE:
1614 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1615 (*demo->atom_wm_delete_window).atom) {
1616 demo->quit = true;
1617 }
1618 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001619 case XCB_KEY_RELEASE:
1620 {
1621 const xcb_key_release_event_t *key =
1622 (const xcb_key_release_event_t *) event;
1623
1624 if (key->detail == 0x9)
1625 demo->quit = true;
1626 }
1627 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001628 case XCB_DESTROY_NOTIFY:
1629 demo->quit = true;
1630 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001631 default:
1632 break;
1633 }
1634}
1635
1636static void demo_run(struct demo *demo)
1637{
1638 xcb_flush(demo->connection);
1639
1640 while (!demo->quit) {
1641 xcb_generic_event_t *event;
1642
1643 event = xcb_wait_for_event(demo->connection);
1644 if (event) {
1645 demo_handle_event(demo, event);
1646 free(event);
1647 }
1648 }
1649}
1650
1651static void demo_create_window(struct demo *demo)
1652{
1653 uint32_t value_mask, value_list[32];
1654
1655 demo->window = xcb_generate_id(demo->connection);
1656
1657 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1658 value_list[0] = demo->screen->black_pixel;
1659 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001660 XCB_EVENT_MASK_EXPOSURE |
1661 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001662
1663 xcb_create_window(demo->connection,
1664 XCB_COPY_FROM_PARENT,
1665 demo->window, demo->screen->root,
1666 0, 0, demo->width, demo->height, 0,
1667 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1668 demo->screen->root_visual,
1669 value_mask, value_list);
1670
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001671 /* Magic code that will send notification when window is destroyed */
1672 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1673 "WM_PROTOCOLS");
1674 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1675
1676 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1677 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1678
1679 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1680 demo->window, (*reply).atom, 4, 32, 1,
1681 &(*demo->atom_wm_delete_window).atom);
1682 free(reply);
1683
Chia-I Wuc19795a2014-09-13 11:12:55 +08001684 xcb_map_window(demo->connection, demo->window);
1685}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001686#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001687
Ian Elliotte36b2082015-07-06 14:27:58 -06001688/*
1689 * Return 1 (true) if all layer names specified in check_names
1690 * can be found in given layer properties.
1691 */
1692static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1693 uint32_t layer_count, VkLayerProperties *layers)
1694{
1695 for (uint32_t i = 0; i < check_count; i++) {
1696 VkBool32 found = 0;
1697 for (uint32_t j = 0; j < layer_count; j++) {
1698 if (!strcmp(check_names[i], layers[j].layerName)) {
1699 found = 1;
1700 }
1701 }
1702 if (!found) {
1703 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1704 return 0;
1705 }
1706 }
1707 return 1;
1708}
1709
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001710void* VKAPI myalloc(
1711 void* pUserData,
1712 size_t size,
1713 size_t alignment,
1714 VkSystemAllocType allocType)
1715{
1716 return malloc(size);
1717}
1718void VKAPI myfree(
1719 void* pUserData,
1720 void* pMem)
1721{
1722 return free(pMem);
1723}
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001724static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001725{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001726 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001727 char *extension_names[64];
1728 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001729 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001730 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001731 VkLayerProperties *instance_layers;
1732 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001733 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001734 uint32_t instance_layer_count = 0;
1735 uint32_t enabled_extension_count = 0;
1736 uint32_t enabled_layer_count = 0;
1737
Ian Elliotte36b2082015-07-06 14:27:58 -06001738 char *instance_validation_layers[] = {
1739 "MemTracker",
1740 };
1741
1742 char *device_validation_layers[] = {
1743 "MemTracker",
1744 };
1745
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001746 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001747 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001748 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001749 assert(!err);
1750
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001751 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1752 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1753 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001754
1755 if (demo->validate) {
1756 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1757 instance_layer_count, instance_layers);
1758 if (!validation_found) {
1759 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
1760 "required validation layer.\n\n"
1761 "Please look at the Getting Started guide for additional "
1762 "information.\n",
1763 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001764 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001765 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001766 }
1767
1768 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1769 assert(!err);
1770
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001771 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001772 memset(extension_names, 0, sizeof(extension_names));
1773 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1774 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1775 assert(!err);
1776 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001777 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001778 WSIextFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001779 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001780 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001781 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001782 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001783 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001784 }
1785 }
1786 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001787 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001788 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001789 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001790 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001791 "Vulkan installable client driver (ICD) installed?\nPlease "
1792 "look at the Getting Started guide for additional "
1793 "information.\n",
1794 "vkCreateInstance Failure");
1795 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001796 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001797 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001798 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001799 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001800 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001801 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001802 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001803 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001804 };
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001805 VkAllocCallbacks cb = {
1806 .pUserData = NULL,
1807 .pfnAlloc = myalloc,
1808 .pfnFree = myfree,
1809 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001810 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001811 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001812 .pNext = NULL,
1813 .pAppInfo = &app,
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001814 .pAllocCb = &cb,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001815 .layerCount = enabled_layer_count,
1816 .ppEnabledLayerNames = (const char *const*) layer_names,
1817 .extensionCount = enabled_extension_count,
1818 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001819 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001820 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001821 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001822 .queueCount = 1,
1823 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001824 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001825
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001826 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001827 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1828 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001829 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001830 "additional information.\n",
1831 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001832 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1833 ERR_EXIT("Cannot find a specified extension library"
1834 ".\nMake sure your layers path is set appropriately\n",
1835 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001836 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001837 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1838 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001839 "the Getting Started guide for additional information.\n",
1840 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001841 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001842
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001843 free(instance_layers);
1844 free(instance_extensions);
1845
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001846 /* Make initial call to query gpu_count, then second call for gpu info*/
1847 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
1848 assert(!err && gpu_count > 0);
1849 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
1850 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
1851 assert(!err);
1852 /* For tri demo we just grab the first physical device */
1853 demo->gpu = physical_devices[0];
1854 free(physical_devices);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001855
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001856 /* Look for validation layers */
1857 validation_found = 0;
1858 enabled_layer_count = 0;
1859 uint32_t device_layer_count = 0;
1860 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
1861 assert(!err);
1862
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001863 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
1864 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
1865 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001866
1867 if (demo->validate) {
1868 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1869 device_layer_count, device_layers);
1870 if (!validation_found) {
1871 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
1872 "a required validation layer.\n\n"
1873 "Please look at the Getting Started guide for additional "
1874 "information.\n",
1875 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001876 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001877 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001878 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001879
1880 uint32_t device_extension_count = 0;
1881 VkExtensionProperties *device_extensions = NULL;
1882 err = vkGetPhysicalDeviceExtensionProperties(
1883 demo->gpu, NULL, &device_extension_count, NULL);
1884 assert(!err);
1885
1886 WSIextFound = 0;
1887 enabled_extension_count = 0;
1888 memset(extension_names, 0, sizeof(extension_names));
1889 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
1890 err = vkGetPhysicalDeviceExtensionProperties(
1891 demo->gpu, NULL, &device_extension_count, device_extensions);
1892 assert(!err);
1893
1894 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001895 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001896 WSIextFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001897 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Ian Elliotte36b2082015-07-06 14:27:58 -06001898 }
1899 assert(enabled_extension_count < 64);
1900 }
1901 if (!WSIextFound) {
1902 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001903 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliotte36b2082015-07-06 14:27:58 -06001904 "Vulkan installable client driver (ICD) installed?\nPlease "
1905 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001906 "information.\n",
1907 "vkCreateInstance Failure");
1908 }
1909
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001910 VkDeviceCreateInfo device = {
1911 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1912 .pNext = NULL,
1913 .queueRecordCount = 1,
1914 .pRequestedQueues = &queue,
1915 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001916 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1917 .extensionCount = enabled_extension_count,
1918 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001919 };
1920
1921 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001922 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001923 if (!demo->dbgCreateMsgCallback) {
1924 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1925 "vkGetProcAddr Failure");
1926 }
1927 err = demo->dbgCreateMsgCallback(
1928 demo->inst,
1929 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1930 dbgFunc, NULL,
1931 &demo->msg_callback);
1932 switch (err) {
1933 case VK_SUCCESS:
1934 break;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001935 case VK_ERROR_OUT_OF_HOST_MEMORY:
1936 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1937 "dbgCreateMsgCallback Failure");
1938 break;
1939 default:
1940 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1941 "dbgCreateMsgCallback Failure");
1942 break;
1943 }
1944 }
1945
Ian Elliotte36b2082015-07-06 14:27:58 -06001946
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001947 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001948 assert(!err);
1949
Ian Elliott338dedb2015-08-21 15:09:33 -06001950 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
1951 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
1952 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
1953 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
1954 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1955 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1956 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
1957 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
1958 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
1959 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliott1b6de092015-06-22 15:07:49 -06001960
Tony Barbour426b9052015-06-24 16:06:58 -06001961 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001962
Cody Northropef72e2a2015-08-03 17:04:53 -06001963 // Query with NULL data to get count
1964 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001965 assert(!err);
1966
Cody Northropef72e2a2015-08-03 17:04:53 -06001967 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
1968 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001969 assert(!err);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001970 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001971
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001972 // Graphics queue and MemMgr queue can be separate.
1973 // TODO: Add support for separate queues, including synchronization,
1974 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001975}
1976
1977static void demo_init_vk_wsi(struct demo *demo)
1978{
1979 VkResult err;
1980 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06001981
1982 // Construct the WSI surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06001983 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001984 demo->surface_description.pNext = NULL;
1985#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06001986 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001987 demo->surface_description.pPlatformHandle = demo->connection;
1988 demo->surface_description.pPlatformWindow = demo->window;
1989#else // _WIN32
1990 demo->platform_handle_xcb.connection = demo->connection;
1991 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06001992 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001993 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
1994 demo->surface_description.pPlatformWindow = &demo->window;
1995#endif // _WIN32
1996
1997 // Iterate over each queue to learn whether it supports presenting to WSI:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001998 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
1999 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06002000 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
2001 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06002002 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002003 }
Ian Elliotte36b2082015-07-06 14:27:58 -06002004
2005 // Search for a graphics and a present queue in the array of queue
2006 // families, try to find one that supports both
2007 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2008 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002009 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002010 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2011 if (graphicsQueueNodeIndex == UINT32_MAX) {
2012 graphicsQueueNodeIndex = i;
2013 }
2014
2015 if (supportsPresent[i] == VK_TRUE) {
2016 graphicsQueueNodeIndex = i;
2017 presentQueueNodeIndex = i;
2018 break;
2019 }
2020 }
2021 }
2022 if (presentQueueNodeIndex == UINT32_MAX) {
2023 // If didn't find a queue that supports both graphics and present, then
2024 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002025 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06002026 if (supportsPresent[i] == VK_TRUE) {
2027 presentQueueNodeIndex = i;
2028 break;
2029 }
2030 }
2031 }
2032 free(supportsPresent);
2033
2034 // Generate error if could not find both a graphics and a present queue
2035 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2036 ERR_EXIT("Could not find a graphics and a present queue\n",
2037 "WSI Initialization Failure");
2038 }
2039
2040 // TODO: Add support for separate queues, including presentation,
2041 // synchronization, and appropriate tracking for QueueSubmit
2042 // While it is possible for an application to use a separate graphics and a
2043 // present queues, this demo program assumes it is only using one:
2044 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2045 ERR_EXIT("Could not find a common graphics and a present queue\n",
2046 "WSI Initialization Failure");
2047 }
2048
2049 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002050
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002051 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08002052 0, &demo->queue);
2053 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06002054
Ian Elliotte36b2082015-07-06 14:27:58 -06002055 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002056 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002057 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2058 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002059 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002060 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002061 VkSurfaceFormatKHR *surfFormats =
2062 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2063 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2064 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002065 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002066 assert(!err);
2067 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2068 // the surface has no preferred format. Otherwise, at least one
2069 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002070 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2071 {
2072 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2073 }
2074 else
2075 {
2076 assert(formatCount >= 1);
2077 demo->format = surfFormats[0].format;
2078 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002079 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002080
Mark Lobodzinski72346292015-07-02 16:49:40 -06002081 // Get Memory information and properties
2082 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2083 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002084}
2085
2086static void demo_init_connection(struct demo *demo)
2087{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002088#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002089 const xcb_setup_t *setup;
2090 xcb_screen_iterator_t iter;
2091 int scr;
2092
2093 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002094 if (demo->connection == NULL) {
2095 printf("Cannot find a compatible Vulkan installable client driver "
2096 "(ICD).\nExiting ...\n");
2097 fflush(stdout);
2098 exit(1);
2099 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002100
2101 setup = xcb_get_setup(demo->connection);
2102 iter = xcb_setup_roots_iterator(setup);
2103 while (scr-- > 0)
2104 xcb_screen_next(&iter);
2105
2106 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002107#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002108}
2109
Ian Elliotte14e9f92015-04-16 15:23:05 -06002110#ifdef _WIN32
2111static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2112#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002113static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002114#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002115{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002116 bool argv_error = false;
2117
Chia-I Wuc19795a2014-09-13 11:12:55 +08002118 memset(demo, 0, sizeof(*demo));
2119
Ian Elliotte14e9f92015-04-16 15:23:05 -06002120#ifdef _WIN32
2121 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002122 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002123
2124 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2125 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002126 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2127 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002128 else if (strlen(pCmdLine) != 0) {
2129 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2130 argv_error = true;
2131 }
2132#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002133 for (int i = 0; i < argc; i++) {
2134 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2135 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002136 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2137 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002138 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002139#endif // _WIN32
2140 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002141 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002142 fflush(stderr);
2143 exit(1);
2144 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002145
Chia-I Wuc19795a2014-09-13 11:12:55 +08002146 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002147 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002148
2149 demo->width = 300;
2150 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002151}
2152
2153static void demo_cleanup(struct demo *demo)
2154{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002155 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002156
Cody Northrop67ff0a42015-09-09 10:21:49 -06002157 demo->prepared = false;
2158
Tony Barbourde4124d2015-07-03 10:33:54 -06002159 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
2160 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2161 }
Tony Barbourb857d312015-07-10 10:50:45 -06002162 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06002163 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002164
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002165 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002166 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002167 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002168 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002169 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002170
Cody Northrope4bc6942015-08-26 10:01:32 -06002171 vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport);
2172 vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width);
2173 vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias);
2174 vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend);
2175 vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds);
Cody Northrop2605cb02015-08-18 15:21:16 -06002176 vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002177
Tony Barbourde4124d2015-07-03 10:33:54 -06002178 vkDestroyPipeline(demo->device, demo->pipeline);
2179 vkDestroyRenderPass(demo->device, demo->render_pass);
2180 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2181 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002182
Tony Barbourde4124d2015-07-03 10:33:54 -06002183 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002184 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002185
Chia-I Wub043fe32014-10-06 15:30:33 +08002186 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002187 vkDestroyImageView(demo->device, demo->textures[i].view);
2188 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002189 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002190 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08002191 }
2192
Ian Elliott338dedb2015-08-21 15:09:33 -06002193 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002194 vkDestroyImageView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002195 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002196
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002197 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbourde4124d2015-07-03 10:33:54 -06002198 vkDestroyImage(demo->device, demo->depth.image);
2199 vkFreeMemory(demo->device, demo->depth.mem);
2200
Ian Elliott338dedb2015-08-21 15:09:33 -06002201 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
Ian Elliotte36b2082015-07-06 14:27:58 -06002202 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002203
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002204 vkDestroyDevice(demo->device);
2205 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002206
Ian Elliotte14e9f92015-04-16 15:23:05 -06002207#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002208 xcb_destroy_window(demo->connection, demo->window);
2209 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002210#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002211}
2212
Ian Elliotte14e9f92015-04-16 15:23:05 -06002213#ifdef _WIN32
2214int APIENTRY WinMain(HINSTANCE hInstance,
2215 HINSTANCE hPrevInstance,
2216 LPSTR pCmdLine,
2217 int nCmdShow)
2218{
2219 MSG msg; // message
2220 bool done; // flag saying when app is complete
2221
2222 demo_init(&demo, hInstance, pCmdLine);
2223 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002224 demo_init_vk_wsi(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002225
2226 demo_prepare(&demo);
2227
2228 done = false; //initialize loop condition variable
2229 /* main message loop*/
2230 while(!done)
2231 {
Ian Elliott421107f2015-04-28 15:50:36 -06002232 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002233 if (msg.message == WM_QUIT) //check for a quit message
2234 {
2235 done = true; //if found, quit app
2236 }
2237 else
2238 {
2239 /* Translate and dispatch to event queue*/
2240 TranslateMessage(&msg);
2241 DispatchMessage(&msg);
2242 }
2243 }
2244
2245 demo_cleanup(&demo);
2246
Tony Barboura938abb2015-04-22 11:36:22 -06002247 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002248}
2249#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002250int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002251{
2252 struct demo demo;
2253
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002254 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002255 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002256 demo_init_vk_wsi(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002257
2258 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002259 demo_run(&demo);
2260
2261 demo_cleanup(&demo);
2262
Chia-I Wuc19795a2014-09-13 11:12:55 +08002263 return 0;
2264}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002265#endif // _WIN32