blob: a749f421392795ab9af82e0b075ee68f07efd1c9 [file] [log] [blame]
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001/*
Ian Elliott7595eee2015-04-28 10:33:11 -06002 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24/*
Chia-I Wuf5caeb02014-10-25 12:11:27 +080025 * Draw a textured triangle with depth testing. This is written against Intel
26 * ICD. It does not do state transition nor object memory binding like it
27 * should. It also does no error checking.
28 */
29
Chia-I Wuc19795a2014-09-13 11:12:55 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
35
Ian Elliotte14e9f92015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
38#include <windows.h>
39#define APP_NAME_STR_LEN 80
40#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +080041#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060042#endif // _WIN32
43
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060044#include <vulkan.h>
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060045#include "vk_debug_report_lunarg.h"
Ian Elliott338dedb2015-08-21 15:09:33 -060046#include <vk_ext_khr_swapchain.h>
47#include <vk_ext_khr_device_swapchain.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080048
Cody Northropd4e020a2015-03-17 14:54:35 -060049#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060050
Chia-I Wub043fe32014-10-06 15:30:33 +080051#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060052#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060053#define APP_SHORT_NAME "tri"
54#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080055
Ian Elliotte36b2082015-07-06 14:27:58 -060056#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
57
Tony Barbour22a30862015-04-22 09:02:32 -060058#if defined(NDEBUG) && defined(__GNUC__)
59#define U_ASSERT_ONLY __attribute__((unused))
60#else
61#define U_ASSERT_ONLY
62#endif
63
Ian Elliottcaa9f272015-04-28 11:35:02 -060064#ifdef _WIN32
65#define ERR_EXIT(err_msg, err_class) \
66 do { \
67 MessageBox(NULL, err_msg, err_class, MB_OK); \
68 exit(1); \
69 } while (0)
Ian Elliottcaa9f272015-04-28 11:35:02 -060070#else // _WIN32
71
72#define ERR_EXIT(err_msg, err_class) \
73 do { \
74 printf(err_msg); \
75 fflush(stdout); \
76 exit(1); \
77 } while (0)
78#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060079
Ian Elliotte36b2082015-07-06 14:27:58 -060080#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
81{ \
82 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
83 if (demo->fp##entrypoint == NULL) { \
84 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
85 "vkGetInstanceProcAddr Failure"); \
86 } \
87}
88
Ian Elliott1b6de092015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060091 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060092 if (demo->fp##entrypoint == NULL) { \
93 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
94 "vkGetDeviceProcAddr Failure"); \
95 } \
96}
97
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060098struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060099 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700100
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600101 VkImage image;
102 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600103
Mark Lobodzinski23182612015-05-29 09:32:35 -0500104 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600105 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700106 int32_t tex_width, tex_height;
107};
108
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600109VkBool32 dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600110 VkFlags msgFlags,
111 VkDbgObjectType objType,
112 uint64_t srcObject,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600113 size_t location,
114 int32_t msgCode,
115 const char* pLayerPrefix,
116 const char* pMsg,
117 void* pUserData)
118{
119 char *message = (char *) malloc(strlen(pMsg)+100);
120
121 assert (message);
122
123 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
124 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
125 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
126 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
127 } else {
Courtney Goeltzenleuchter0d6857f2015-09-04 13:52:24 -0600128 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600129 }
130
131#ifdef _WIN32
132 MessageBox(NULL, message, "Alert", MB_OK);
133#else
134 printf("%s\n",message);
135 fflush(stdout);
136#endif
137 free(message);
Courtney Goeltzenleuchtere4c43132015-09-08 16:57:22 -0600138
139 /*
140 * false indicates that layer should not bail-out of an
141 * API call that had validation failures. This may mean that the
142 * app dies inside the driver due to invalid parameter(s).
143 * That's what would happen without validation layers, so we'll
144 * keep that behavior here.
145 */
146 return false;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600147}
148
Ian Elliott338dedb2015-08-21 15:09:33 -0600149typedef struct _SwapchainBuffers {
Ian Elliotte36b2082015-07-06 14:27:58 -0600150 VkImage image;
151 VkCmdBuffer cmd;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600152 VkImageView view;
Ian Elliott338dedb2015-08-21 15:09:33 -0600153} SwapchainBuffers;
Ian Elliotte36b2082015-07-06 14:27:58 -0600154
Chia-I Wuc19795a2014-09-13 11:12:55 +0800155struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600156#ifdef _WIN32
157#define APP_NAME_STR_LEN 80
158 HINSTANCE connection; // hInstance - Windows Instance
159 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
160 HWND window; // hWnd - window handle
161#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800162 xcb_connection_t *connection;
163 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800164 xcb_window_t window;
165 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott338dedb2015-08-21 15:09:33 -0600166 VkPlatformHandleXcbKHR platform_handle_xcb;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600167#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700168 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600169 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600170 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800171
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600172 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600173 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600174 VkDevice device;
175 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600176 VkPhysicalDeviceProperties gpu_props;
Cody Northropef72e2a2015-08-03 17:04:53 -0600177 VkQueueFamilyProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700178 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800179
180 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600181 VkFormat format;
Ian Elliott338dedb2015-08-21 15:09:33 -0600182 VkColorSpaceKHR color_space;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800183
Ian Elliott338dedb2015-08-21 15:09:33 -0600184 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
185 PFN_vkGetSurfacePropertiesKHR fpGetSurfacePropertiesKHR;
186 PFN_vkGetSurfaceFormatsKHR fpGetSurfaceFormatsKHR;
187 PFN_vkGetSurfacePresentModesKHR fpGetSurfacePresentModesKHR;
188 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
189 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
190 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
191 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
192 PFN_vkQueuePresentKHR fpQueuePresentKHR;
193 VkSurfaceDescriptionWindowKHR surface_description;
194 uint32_t swapchainImageCount;
195 VkSwapchainKHR swap_chain;
196 SwapchainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800197
Ian Elliotte36b2082015-07-06 14:27:58 -0600198 VkCmdPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800199
Chia-I Wub043fe32014-10-06 15:30:33 +0800200 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600201 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800202
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500204 VkDeviceMemory mem;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600205 VkImageView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800206 } depth;
207
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600208 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800209
Chia-I Wu99621bc2014-10-08 11:52:22 +0800210 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600211 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500212 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800213
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600214 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600215 VkVertexInputBindingDescription vi_bindings[1];
216 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800217 } vertices;
218
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600219 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
220 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500221 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600222 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600223 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800224 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600225 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800226
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -0600227 VkShaderModule vert_shader_module;
228 VkShaderModule frag_shader_module;
229
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600230 VkDescriptorPool desc_pool;
231 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800232
Tony Barbour5aabff52015-10-08 14:26:24 -0600233 VkFramebuffer *framebuffers;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800234
Mark Lobodzinski72346292015-07-02 16:49:40 -0600235 VkPhysicalDeviceMemoryProperties memory_properties;
236
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600237 bool validate;
238 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Ian Elliotte36b2082015-07-06 14:27:58 -0600239 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600240 VkDbgMsgCallback msg_callback;
241
Tony Barbour4a6692d2015-10-08 13:45:45 -0600242 float depthStencil;
243 float depthIncrement;
244
Chia-I Wuc19795a2014-09-13 11:12:55 +0800245 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600246 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600247 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800248};
249
Tony Barbourf1eceb92015-10-15 12:42:56 -0600250static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinski72346292015-07-02 16:49:40 -0600251{
252 // Search memtypes to find first index with those properties
253 for (uint32_t i = 0; i < 32; i++) {
254 if ((typeBits & 1) == 1) {
255 // Type is available, does it match user properties?
Tony Barbourf1eceb92015-10-15 12:42:56 -0600256 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinski72346292015-07-02 16:49:40 -0600257 *typeIndex = i;
258 return VK_SUCCESS;
259 }
260 }
261 typeBits >>= 1;
262 }
263 // No memory types matched, return failure
264 return VK_UNSUPPORTED;
265}
266
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600267static void demo_flush_init_cmd(struct demo *demo)
268{
Tony Barbour22a30862015-04-22 09:02:32 -0600269 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600270
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600271 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600272 return;
273
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600274 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275 assert(!err);
276
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600277 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600278 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600279
Tony Barbourde4124d2015-07-03 10:33:54 -0600280 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600281 assert(!err);
282
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600283 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600284 assert(!err);
285
Tony Barbourde4124d2015-07-03 10:33:54 -0600286 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600287 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600288}
289
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600290static void demo_set_image_layout(
291 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600292 VkImage image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600293 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600294 VkImageLayout old_image_layout,
295 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600296{
Tony Barbour22a30862015-04-22 09:02:32 -0600297 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600298
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600299 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600300 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600301 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600302 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600303 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800304 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600305 .flags = 0,
306 };
307
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600308 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309 assert(!err);
310
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600311 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600312 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600313 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600314 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600315 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600316 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600317 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600318 .framebuffer = { VK_NULL_HANDLE },
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600319 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600320 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600321 }
322
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600323 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600324 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600325 .pNext = NULL,
326 .outputMask = 0,
327 .inputMask = 0,
328 .oldLayout = old_image_layout,
329 .newLayout = new_image_layout,
330 .image = image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600331 .subresourceRange = { aspectMask, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600332 };
333
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600334 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600335 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600336 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600337 }
338
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600339 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600340 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600341 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600342 }
343
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600344 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600345
Tony Barbourc2e987e2015-06-29 16:20:35 -0600346 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
347 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600348
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600349 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600350}
351
Chia-I Wuc19795a2014-09-13 11:12:55 +0800352static void demo_draw_build_cmd(struct demo *demo)
353{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800354 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600355 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600356 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600357 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600358 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600359 .renderPass = { VK_NULL_HANDLE },
Cody Northrop16898b02015-08-11 11:35:58 -0600360 .subpass = 0,
Courtney Goeltzenleuchter21b5b312015-08-26 14:48:00 -0600361 .framebuffer = { VK_NULL_HANDLE },
Jon Ashburn53d27af2014-12-31 17:08:35 -0700362 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800363 const VkClearValue clear_values[2] = {
Cody Northrop2563a032015-08-25 15:26:38 -0600364 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
Tony Barbour4a6692d2015-10-08 13:45:45 -0600365 [1] = { .depthStencil = { demo->depthStencil, 0 } },
Chia-I Wuc278df82015-07-07 11:50:03 +0800366 };
367 const VkRenderPassBeginInfo rp_begin = {
368 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
369 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800370 .renderPass = demo->render_pass,
371 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800372 .renderArea.offset.x = 0,
373 .renderArea.offset.y = 0,
374 .renderArea.extent.width = demo->width,
375 .renderArea.extent.height = demo->height,
Cody Northropc332eef2015-08-04 11:51:03 -0600376 .clearValueCount = 2,
377 .pClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700378 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800379 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800380
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600381 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800382 assert(!err);
383
Chia-I Wuc278df82015-07-07 11:50:03 +0800384 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600385 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800386 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600387 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600388 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800389
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600390 VkViewport viewport;
391 memset(&viewport, 0, sizeof(viewport));
392 viewport.height = (float) demo->height;
393 viewport.width = (float) demo->width;
394 viewport.minDepth = (float) 0.0f;
395 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600396 vkCmdSetViewport(demo->draw_cmd, 1, &viewport);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600397
398 VkRect2D scissor;
399 memset(&scissor, 0, sizeof(scissor));
400 scissor.extent.width = demo->width;
401 scissor.extent.height = demo->height;
402 scissor.offset.x = 0;
403 scissor.offset.y = 0;
Courtney Goeltzenleuchter932cdb52015-09-21 11:44:06 -0600404 vkCmdSetScissor(demo->draw_cmd, 1, &scissor);
Courtney Goeltzenleuchter09772bb2015-09-17 15:06:17 -0600405
Tony Barbour8205d902015-04-16 15:59:00 -0600406 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600407 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800408
Courtney Goeltzenleuchter4ff11cc2015-09-23 12:31:50 -0600409 vkCmdDraw(demo->draw_cmd, 3, 1, 0, 0);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800410 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800411
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600412 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800413 assert(!err);
414}
415
416static void demo_draw(struct demo *demo)
417{
Tony Barbour22a30862015-04-22 09:02:32 -0600418 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600419 VkSemaphore presentCompleteSemaphore;
420 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
421 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
422 .pNext = NULL,
Mark Lobodzinski2a253072015-10-08 10:44:07 -0600423 .flags = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600424 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800425
Ian Elliotte36b2082015-07-06 14:27:58 -0600426 err = vkCreateSemaphore(demo->device,
427 &presentCompleteSemaphoreCreateInfo,
428 &presentCompleteSemaphore);
429 assert(!err);
430
431 // Get the index of the next available swapchain image:
Ian Elliott338dedb2015-08-21 15:09:33 -0600432 err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600433 UINT64_MAX,
434 presentCompleteSemaphore,
435 &demo->current_buffer);
Ian Elliott338dedb2015-08-21 15:09:33 -0600436 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliotte36b2082015-07-06 14:27:58 -0600437 // return codes
438 assert(!err);
439
440 // Wait for the present complete semaphore to be signaled to ensure
441 // that the image won't be rendered to until the presentation
442 // engine has fully released ownership to the application, and it is
443 // okay to render to the image.
Tony Barbourede36362015-10-20 13:01:07 -0600444 err = vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
445 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -0600446
Ian Elliott338dedb2015-08-21 15:09:33 -0600447// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR
Chia-I Wuc19795a2014-09-13 11:12:55 +0800448 demo_draw_build_cmd(demo);
Tony Barbourde4124d2015-07-03 10:33:54 -0600449 VkFence nullFence = { VK_NULL_HANDLE };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800450
Tony Barbourde4124d2015-07-03 10:33:54 -0600451 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800452 assert(!err);
453
Ian Elliott338dedb2015-08-21 15:09:33 -0600454 VkPresentInfoKHR present = {
455 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliotte36b2082015-07-06 14:27:58 -0600456 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600457 .swapchainCount = 1,
458 .swapchains = &demo->swap_chain,
Ian Elliotte36b2082015-07-06 14:27:58 -0600459 .imageIndices = &demo->current_buffer,
460 };
461
462// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliott338dedb2015-08-21 15:09:33 -0600463 err = demo->fpQueuePresentKHR(demo->queue, &present);
464 // TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
Ian Elliotte36b2082015-07-06 14:27:58 -0600465 // return codes
Chia-I Wuc19795a2014-09-13 11:12:55 +0800466 assert(!err);
467
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800468 err = vkQueueWaitIdle(demo->queue);
469 assert(err == VK_SUCCESS);
Mike Stroyane187cc42015-08-28 10:58:06 -0600470
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600471 vkDestroySemaphore(demo->device, presentCompleteSemaphore);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800472}
473
474static void demo_prepare_buffers(struct demo *demo)
475{
Ian Elliotte36b2082015-07-06 14:27:58 -0600476 VkResult U_ASSERT_ONLY err;
477
478 // Check the surface proprties and formats
Ian Elliott338dedb2015-08-21 15:09:33 -0600479 VkSurfacePropertiesKHR surfProperties;
480 err = demo->fpGetSurfacePropertiesKHR(demo->device,
481 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600482 &surfProperties);
Ian Elliotte36b2082015-07-06 14:27:58 -0600483 assert(!err);
484
Ian Elliott7fe115d2015-08-07 15:56:59 -0600485 uint32_t presentModeCount;
Ian Elliott338dedb2015-08-21 15:09:33 -0600486 err = demo->fpGetSurfacePresentModesKHR(demo->device,
487 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600488 &presentModeCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600489 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -0600490 VkPresentModeKHR *presentModes =
491 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott7fe115d2015-08-07 15:56:59 -0600492 assert(presentModes);
Ian Elliott338dedb2015-08-21 15:09:33 -0600493 err = demo->fpGetSurfacePresentModesKHR(demo->device,
494 (const VkSurfaceDescriptionKHR *)&demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600495 &presentModeCount, presentModes);
Ian Elliotte36b2082015-07-06 14:27:58 -0600496 assert(!err);
497
Ian Elliott338dedb2015-08-21 15:09:33 -0600498 VkExtent2D swapchainExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600499 // width and height are either both -1, or both not -1.
Ian Elliott7fe115d2015-08-07 15:56:59 -0600500 if (surfProperties.currentExtent.width == -1)
Ian Elliotte36b2082015-07-06 14:27:58 -0600501 {
502 // If the surface size is undefined, the size is set to
503 // the size of the images requested.
Ian Elliott338dedb2015-08-21 15:09:33 -0600504 swapchainExtent.width = demo->width;
505 swapchainExtent.height = demo->height;
Ian Elliotte36b2082015-07-06 14:27:58 -0600506 }
507 else
508 {
509 // If the surface size is defined, the swap chain size must match
Ian Elliott338dedb2015-08-21 15:09:33 -0600510 swapchainExtent = surfProperties.currentExtent;
Ian Elliotte36b2082015-07-06 14:27:58 -0600511 }
512
Ian Elliott338dedb2015-08-21 15:09:33 -0600513 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600514
515 // Determine the number of VkImage's to use in the swap chain (we desire to
516 // own only 1 image at a time, besides the images being displayed and
517 // queued for display):
Ian Elliott338dedb2015-08-21 15:09:33 -0600518 uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1;
Ian Elliott7fe115d2015-08-07 15:56:59 -0600519 if ((surfProperties.maxImageCount > 0) &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600520 (desiredNumberOfSwapchainImages > surfProperties.maxImageCount))
Ian Elliotte36b2082015-07-06 14:27:58 -0600521 {
522 // Application must settle for fewer images than desired:
Ian Elliott338dedb2015-08-21 15:09:33 -0600523 desiredNumberOfSwapchainImages = surfProperties.maxImageCount;
Ian Elliotte36b2082015-07-06 14:27:58 -0600524 }
525
Ian Elliott338dedb2015-08-21 15:09:33 -0600526 VkSurfaceTransformKHR preTransform;
527 if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
528 preTransform = VK_SURFACE_TRANSFORM_NONE_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -0600529 } else {
Ian Elliott7fe115d2015-08-07 15:56:59 -0600530 preTransform = surfProperties.currentTransform;
Ian Elliotte36b2082015-07-06 14:27:58 -0600531 }
532
Ian Elliott338dedb2015-08-21 15:09:33 -0600533 const VkSwapchainCreateInfoKHR swap_chain = {
Ian Elliott434d2222015-09-22 10:20:23 -0600534 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800535 .pNext = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600536 .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description,
537 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800538 .imageFormat = demo->format,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600539 .imageColorSpace = demo->color_space,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800540 .imageExtent = {
Ian Elliott338dedb2015-08-21 15:09:33 -0600541 .width = swapchainExtent.width,
542 .height = swapchainExtent.height,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800543 },
Cody Northrop3b0a3ef2015-08-28 16:22:48 -0600544 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliotte36b2082015-07-06 14:27:58 -0600545 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800546 .imageArraySize = 1,
Ian Elliott7fe115d2015-08-07 15:56:59 -0600547 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
548 .queueFamilyCount = 0,
549 .pQueueFamilyIndices = NULL,
Ian Elliott338dedb2015-08-21 15:09:33 -0600550 .presentMode = swapchainPresentMode,
551 .oldSwapchain.handle = 0,
Ian Elliotte36b2082015-07-06 14:27:58 -0600552 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800553 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600554 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800555
Ian Elliott338dedb2015-08-21 15:09:33 -0600556 err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800557 assert(!err);
558
Ian Elliott338dedb2015-08-21 15:09:33 -0600559 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
560 &demo->swapchainImageCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -0600561 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800562
Ian Elliott338dedb2015-08-21 15:09:33 -0600563 VkImage* swapchainImages =
564 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
565 assert(swapchainImages);
566 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain,
567 &demo->swapchainImageCount,
568 swapchainImages);
Ian Elliotte36b2082015-07-06 14:27:58 -0600569 assert(!err);
570
Ian Elliott338dedb2015-08-21 15:09:33 -0600571 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliotte36b2082015-07-06 14:27:58 -0600572 assert(demo->buffers);
573
Ian Elliott338dedb2015-08-21 15:09:33 -0600574 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600575 VkImageViewCreateInfo color_attachment_view = {
576 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800577 .pNext = NULL,
578 .format = demo->format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600579 .channels = {
580 .r = VK_CHANNEL_SWIZZLE_R,
581 .g = VK_CHANNEL_SWIZZLE_G,
582 .b = VK_CHANNEL_SWIZZLE_B,
583 .a = VK_CHANNEL_SWIZZLE_A,
584 },
585 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600586 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600587 .baseMipLevel = 0,
588 .mipLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600589 .baseArrayLayer = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600590 .arraySize = 1
591 },
592 .viewType = VK_IMAGE_VIEW_TYPE_2D,
593 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800594 };
595
Ian Elliott338dedb2015-08-21 15:09:33 -0600596 demo->buffers[i].image = swapchainImages[i];
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500597
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600598 demo_set_image_layout(demo, demo->buffers[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600599 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600600 VK_IMAGE_LAYOUT_UNDEFINED,
601 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600602
Chia-I Wuc19795a2014-09-13 11:12:55 +0800603 color_attachment_view.image = demo->buffers[i].image;
604
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600605 err = vkCreateImageView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800606 &color_attachment_view, &demo->buffers[i].view);
607 assert(!err);
608 }
Piers Daniell886be472015-02-23 16:23:13 -0700609
610 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800611}
612
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800613static void demo_prepare_depth(struct demo *demo)
614{
Tony Barbour8205d902015-04-16 15:59:00 -0600615 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600616 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600617 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800618 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600619 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800620 .format = depth_format,
621 .extent = { demo->width, demo->height, 1 },
622 .mipLevels = 1,
623 .arraySize = 1,
624 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600625 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchterc3b8eea2015-09-10 14:14:11 -0600626 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800627 .flags = 0,
628 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600629 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600630 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500631 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800632 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600633 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800634 };
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600635 VkImageViewCreateInfo view = {
636 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800637 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600638 .image.handle = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600639 .format = depth_format,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600640 .subresourceRange = {
Cody Northrop95b8bb32015-09-14 13:48:12 -0600641 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600642 .baseMipLevel = 0,
643 .mipLevels = 1,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600644 .baseArrayLayer = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600645 .arraySize = 1
646 },
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800647 .flags = 0,
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600648 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800649 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700650
Mark Lobodzinski23182612015-05-29 09:32:35 -0500651 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600652 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800653
654 demo->depth.format = depth_format;
655
656 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600657 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800658 &demo->depth.image);
659 assert(!err);
660
Mark Lobodzinski72346292015-07-02 16:49:40 -0600661 /* get memory requirements for this object */
Tony Barbourde4124d2015-07-03 10:33:54 -0600662 err = vkGetImageMemoryRequirements(demo->device, demo->depth.image,
663 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600664
665 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500666 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600667 err = memory_type_from_properties(demo,
668 mem_reqs.memoryTypeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600669 0, /* No requirements */
Mark Lobodzinski72346292015-07-02 16:49:40 -0600670 &mem_alloc.memoryTypeIndex);
671 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800672
Mark Lobodzinski23182612015-05-29 09:32:35 -0500673 /* allocate memory */
674 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
675 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800676
Mark Lobodzinski23182612015-05-29 09:32:35 -0500677 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600678 err = vkBindImageMemory(demo->device, demo->depth.image,
679 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500680 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800681
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600682 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600683 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600684 VK_IMAGE_LAYOUT_UNDEFINED,
685 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600686
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800687 /* create image view */
688 view.image = demo->depth.image;
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600689 err = vkCreateImageView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800690 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800691}
692
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700693static void demo_prepare_texture_image(struct demo *demo,
694 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600695 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600696 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600697 VkImageUsageFlags usage,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600698 VkFlags required_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800699{
Tony Barbour8205d902015-04-16 15:59:00 -0600700 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600701 const int32_t tex_width = 2;
702 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600703 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800704
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600705 tex_obj->tex_width = tex_width;
706 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700707
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600708 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600709 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700710 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600711 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700712 .format = tex_format,
713 .extent = { tex_width, tex_height, 1 },
714 .mipLevels = 1,
715 .arraySize = 1,
716 .samples = 1,
717 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600718 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700719 .flags = 0,
720 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600721 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600722 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500723 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700724 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600725 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700726 };
727
Mark Lobodzinski23182612015-05-29 09:32:35 -0500728 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700729
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600730 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600731 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700732 assert(!err);
733
Tony Barbourde4124d2015-07-03 10:33:54 -0600734 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600735
736 mem_alloc.allocationSize = mem_reqs.size;
Tony Barbourf1eceb92015-10-15 12:42:56 -0600737 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &mem_alloc.memoryTypeIndex);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600738 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700739
Mark Lobodzinski23182612015-05-29 09:32:35 -0500740 /* allocate memory */
741 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
742 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700743
Mark Lobodzinski23182612015-05-29 09:32:35 -0500744 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600745 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500746 tex_obj->mem, 0);
747 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700748
Tony Barbourf1eceb92015-10-15 12:42:56 -0600749 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600750 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600751 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700752 .mipLevel = 0,
Courtney Goeltzenleuchter3dee8082015-09-10 16:38:41 -0600753 .arrayLayer = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700754 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600755 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700756 void *data;
757 int32_t x, y;
758
Tony Barbour426b9052015-06-24 16:06:58 -0600759 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
760 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700761
Dominik Witczake82d5b62015-09-22 18:25:33 +0200762 err = vkMapMemory(demo->device, tex_obj->mem, 0, mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700763 assert(!err);
764
765 for (y = 0; y < tex_height; y++) {
766 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
767 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700768 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700769 }
770
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600771 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700772 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600773
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600774 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600775 demo_set_image_layout(demo, tex_obj->image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600776 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600777 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600778 tex_obj->imageLayout);
779 /* 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 -0700780}
781
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500782static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700783{
784 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600785 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600786 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700787}
788
789static void demo_prepare_textures(struct demo *demo)
790{
Tony Barbour8205d902015-04-16 15:59:00 -0600791 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600792 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700793 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
794 { 0xffff0000, 0xff00ff00 },
795 };
Tony Barbour22a30862015-04-22 09:02:32 -0600796 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700797 uint32_t i;
798
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600799 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700800 assert(!err);
801
802 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600803 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700804 /* Device can texture using linear textures */
805 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600806 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600807 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700808 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600809 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700810
811 memset(&staging_texture, 0, sizeof(staging_texture));
812 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600813 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700814
815 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600816 VK_IMAGE_TILING_OPTIMAL,
817 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
818 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700819
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600820 demo_set_image_layout(demo, staging_texture.image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600821 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600822 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600823 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700824
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600825 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600826 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600827 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600828 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700829
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600830 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600831 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700832 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600833 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700834 .destOffset = { 0, 0, 0 },
835 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
836 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600837 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600838 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
839 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600840 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700841
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600842 demo_set_image_layout(demo, demo->textures[i].image,
Cody Northrop95b8bb32015-09-14 13:48:12 -0600843 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600844 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600845 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700846
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600847 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700848
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600849 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700850 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600851 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700852 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700853 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700854
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600855 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600856 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800857 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600858 .magFilter = VK_TEX_FILTER_NEAREST,
859 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600860 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter51624412015-09-10 14:08:50 -0600861 .addressModeU = VK_TEX_ADDRESS_MODE_WRAP,
862 .addressModeV = VK_TEX_ADDRESS_MODE_WRAP,
863 .addressModeW = VK_TEX_ADDRESS_MODE_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800864 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700865 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600866 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800867 .minLod = 0.0f,
868 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600869 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski513acdf2015-09-01 15:42:56 -0600870 .unnormalizedCoordinates = VK_FALSE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800871 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600872 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600873 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800874 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600875 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600876 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700877 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600878 .channels = { VK_CHANNEL_SWIZZLE_R,
879 VK_CHANNEL_SWIZZLE_G,
880 VK_CHANNEL_SWIZZLE_B,
881 VK_CHANNEL_SWIZZLE_A, },
Cody Northrop95b8bb32015-09-14 13:48:12 -0600882 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -0600883 .flags = 0,
Chia-I Wub043fe32014-10-06 15:30:33 +0800884 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700885
Chia-I Wub043fe32014-10-06 15:30:33 +0800886 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600887 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800888 &demo->textures[i].sampler);
889 assert(!err);
890
Chia-I Wub043fe32014-10-06 15:30:33 +0800891 /* create image view */
892 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600893 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700894 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800895 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800896 }
897}
898
Chia-I Wu99621bc2014-10-08 11:52:22 +0800899static void demo_prepare_vertices(struct demo *demo)
900{
901 const float vb[3][5] = {
902 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800903 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
904 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800905 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
906 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600907 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600908 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800909 .pNext = NULL,
910 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600911 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800912 .flags = 0,
913 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600914 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600915 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500916 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800917 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600918 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800919 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500920 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600921 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800922 void *data;
923
924 memset(&demo->vertices, 0, sizeof(demo->vertices));
925
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600926 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800927 assert(!err);
928
Tony Barbourde4124d2015-07-03 10:33:54 -0600929 err = vkGetBufferMemoryRequirements(demo->device,
930 demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800931
Mark Lobodzinski72346292015-07-02 16:49:40 -0600932 mem_alloc.allocationSize = mem_reqs.size;
933 err = memory_type_from_properties(demo,
934 mem_reqs.memoryTypeBits,
935 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
936 &mem_alloc.memoryTypeIndex);
937 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800938
Mark Lobodzinski23182612015-05-29 09:32:35 -0500939 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
940 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800941
Dominik Witczake82d5b62015-09-22 18:25:33 +0200942 err = vkMapMemory(demo->device, demo->vertices.mem, 0, mem_alloc.allocationSize, 0, &data);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500943 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800944
Mark Lobodzinski23182612015-05-29 09:32:35 -0500945 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800946
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600947 vkUnmapMemory(demo->device, demo->vertices.mem);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500948
Tony Barbourde4124d2015-07-03 10:33:54 -0600949 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500950 demo->vertices.mem, 0);
951 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800952
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600953 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800954 demo->vertices.vi.pNext = NULL;
955 demo->vertices.vi.bindingCount = 1;
956 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
957 demo->vertices.vi.attributeCount = 2;
958 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
959
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600960 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800961 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600962 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800963
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600964 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
965 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600966 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800967 demo->vertices.vi_attrs[0].offsetInBytes = 0;
968
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600969 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
970 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600971 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800972 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800973}
974
Chia-I Wuf8385062015-01-04 16:27:24 +0800975static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800976{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600977 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600978 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800979 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600980 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800981 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800982 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600983 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600984 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800985 .pNext = NULL,
986 .count = 1,
987 .pBinding = &layout_binding,
988 };
Tony Barbour22a30862015-04-22 09:02:32 -0600989 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800990
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600991 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800992 &descriptor_layout, &demo->desc_layout);
993 assert(!err);
994
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500995 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
996 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
997 .pNext = NULL,
998 .descriptorSetCount = 1,
999 .pSetLayouts = &demo->desc_layout,
1000 };
1001
1002 err = vkCreatePipelineLayout(demo->device,
1003 &pPipelineLayoutCreateInfo,
1004 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001005 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +08001006}
1007
Chia-I Wu76cd4222015-07-08 13:34:24 +08001008static void demo_prepare_render_pass(struct demo *demo)
1009{
Chia-I Wuc278df82015-07-07 11:50:03 +08001010 const VkAttachmentDescription attachments[2] = {
1011 [0] = {
1012 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1013 .pNext = NULL,
1014 .format = demo->format,
1015 .samples = 1,
1016 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1017 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1018 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1019 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1020 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1021 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1022 },
1023 [1] = {
1024 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1025 .pNext = NULL,
1026 .format = demo->depth.format,
1027 .samples = 1,
1028 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1029 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1030 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1031 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1032 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1033 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1034 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001035 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001036 const VkAttachmentReference color_reference = {
1037 .attachment = 0,
1038 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1039 };
1040 const VkSubpassDescription subpass = {
1041 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1042 .pNext = NULL,
1043 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1044 .flags = 0,
1045 .inputCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001046 .pInputAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001047 .colorCount = 1,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001048 .pColorAttachments = &color_reference,
1049 .pResolveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001050 .depthStencilAttachment = {
1051 .attachment = 1,
1052 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1053 },
1054 .preserveCount = 0,
Cody Northrop6de6b0b2015-08-04 11:16:41 -06001055 .pPreserveAttachments = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001056 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001057 const VkRenderPassCreateInfo rp_info = {
1058 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1059 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001060 .attachmentCount = 2,
1061 .pAttachments = attachments,
1062 .subpassCount = 1,
1063 .pSubpasses = &subpass,
1064 .dependencyCount = 0,
1065 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001066 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001067 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001068
1069 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1070 assert(!err);
1071}
1072
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001073static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001074 VkShaderStage stage,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001075 VkShaderModule* pShaderModule,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001076 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001077 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001078{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001079 VkShaderModuleCreateInfo moduleCreateInfo;
1080 VkShaderCreateInfo shaderCreateInfo;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001081 VkShader shader;
Tony Barbour4a6692d2015-10-08 13:45:45 -06001082 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001083
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001084
1085 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1086 moduleCreateInfo.pNext = NULL;
1087
1088 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1089 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001090
Cody Northrop75db0322015-05-28 11:27:16 -06001091 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001092 moduleCreateInfo.codeSize = size;
1093 moduleCreateInfo.pCode = code;
1094 moduleCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001095 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001096 assert(!err);
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001097
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001098 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001099 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001100 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001101 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001102 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001103 assert(!err);
Cody Northrop75db0322015-05-28 11:27:16 -06001104 } else {
1105 // Create fake SPV structure to feed GLSL
1106 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001107 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1108 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1109 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001110
Cody Northrop75db0322015-05-28 11:27:16 -06001111 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001112 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1113 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1114 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1115 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001116
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001117 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001118 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001119
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001120 shaderCreateInfo.flags = 0;
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001121 shaderCreateInfo.module = *pShaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001122 shaderCreateInfo.pName = "main";
Cody Northrop87ae5e12015-08-24 15:11:10 -06001123 shaderCreateInfo.stage = stage;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001124 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001125 assert(!err);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001126 free((void *) moduleCreateInfo.pCode);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001127 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001128 return shader;
1129}
1130
Cody Northrop75db0322015-05-28 11:27:16 -06001131char *demo_read_spv(const char *filename, size_t *psize)
1132{
1133 long int size;
1134 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001135 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001136
1137 FILE *fp = fopen(filename, "rb");
1138 if (!fp) return NULL;
1139
1140 fseek(fp, 0L, SEEK_END);
1141 size = ftell(fp);
1142
1143 fseek(fp, 0L, SEEK_SET);
1144
1145 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001146 retVal = fread(shader_code, size, 1, fp);
1147 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001148
1149 *psize = size;
1150
1151 return shader_code;
1152}
1153
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001154static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001155{
Cody Northrop75db0322015-05-28 11:27:16 -06001156 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001157 VkShader shader;
1158 void *vertShaderCode;
1159 size_t size;
Cody Northrop75db0322015-05-28 11:27:16 -06001160
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001161 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001162
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001163 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1164 &demo->vert_shader_module,
1165 vertShaderCode, size);
1166 free(vertShaderCode);
1167 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001168 } else {
1169 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001170 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001171 "#extension GL_ARB_separate_shader_objects : enable\n"
1172 "#extension GL_ARB_shading_language_420pack : enable\n"
1173 "layout (location = 0) in vec4 pos;\n"
1174 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001175 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001176 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001177 " texcoord = attr;\n"
1178 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001179 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001180
Cody Northrop75db0322015-05-28 11:27:16 -06001181 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001182 &demo->vert_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001183 (const void *) vertShaderText,
1184 strlen(vertShaderText));
1185 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001186}
1187
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001188static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001189{
Cody Northrop75db0322015-05-28 11:27:16 -06001190 if (!demo->use_glsl) {
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001191 VkShader shader;
1192 void *fragShaderCode;
1193 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001194
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001195 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
Cody Northrop75db0322015-05-28 11:27:16 -06001196
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06001197 shader = demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1198 &demo->frag_shader_module,
1199 fragShaderCode, size);
1200
1201 free(fragShaderCode);
1202 return shader;
Cody Northrop75db0322015-05-28 11:27:16 -06001203 } else {
1204 static const char *fragShaderText =
1205 "#version 140\n"
1206 "#extension GL_ARB_separate_shader_objects : enable\n"
1207 "#extension GL_ARB_shading_language_420pack : enable\n"
1208 "layout (binding = 0) uniform sampler2D tex;\n"
1209 "layout (location = 0) in vec2 texcoord;\n"
1210 "layout (location = 0) out vec4 uFragColor;\n"
1211 "void main() {\n"
1212 " uFragColor = texture(tex, texcoord);\n"
1213 "}\n";
1214
1215 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001216 &demo->frag_shader_module,
Cody Northrop75db0322015-05-28 11:27:16 -06001217 (const void *) fragShaderText,
1218 strlen(fragShaderText));
1219 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001220}
1221
1222static void demo_prepare_pipeline(struct demo *demo)
1223{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001224 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001225 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001226
Tony Barboure307f582015-07-10 15:29:03 -06001227 VkPipelineVertexInputStateCreateInfo vi;
1228 VkPipelineInputAssemblyStateCreateInfo ia;
1229 VkPipelineRasterStateCreateInfo rs;
1230 VkPipelineColorBlendStateCreateInfo cb;
1231 VkPipelineDepthStencilStateCreateInfo ds;
1232 VkPipelineViewportStateCreateInfo vp;
1233 VkPipelineMultisampleStateCreateInfo ms;
Piers Daniell811eb742015-09-29 13:01:09 -06001234 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_NUM];
1235 VkPipelineDynamicStateCreateInfo dynamicState;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001236
Tony Barbour22a30862015-04-22 09:02:32 -06001237 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001238
Piers Daniell811eb742015-09-29 13:01:09 -06001239 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1240 memset(&dynamicState, 0, sizeof dynamicState);
1241 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1242 dynamicState.pDynamicStates = dynamicStateEnables;
1243
Chia-I Wuc19795a2014-09-13 11:12:55 +08001244 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001245 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001246 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001247
Chia-I Wu8d29d022014-10-08 12:14:39 +08001248 vi = demo->vertices.vi;
1249
Chia-I Wuc19795a2014-09-13 11:12:55 +08001250 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001251 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001252 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001253
1254 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001255 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001256 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001257 rs.cullMode = VK_CULL_MODE_BACK;
1258 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001259 rs.depthClipEnable = VK_TRUE;
Cody Northropf5bd2252015-08-17 11:10:49 -06001260 rs.rasterizerDiscardEnable = VK_FALSE;
1261 rs.depthBiasEnable = VK_FALSE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001262
1263 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001264 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1265 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001266 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001267 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001268 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001269 cb.attachmentCount = 1;
1270 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001271
Tony Barbourfa6cac72015-01-16 14:27:35 -07001272 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001273 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001274 vp.viewportCount = 1;
Piers Daniell811eb742015-09-29 13:01:09 -06001275 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1276 vp.scissorCount = 1;
1277 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001278
1279 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001280 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001281 ds.depthTestEnable = VK_TRUE;
1282 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001283 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Cody Northrope4bc6942015-08-26 10:01:32 -06001284 ds.depthBoundsTestEnable = VK_FALSE;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001285 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1286 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001287 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001288 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001289 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001290
Tony Barbourfa6cac72015-01-16 14:27:35 -07001291 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001292 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrope9825b72015-08-04 14:34:54 -06001293 ms.pSampleMask = NULL;
Tony Barboure094edf2015-06-26 10:18:34 -06001294 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001295
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001296 // Two stages: vs and fs
1297 pipeline.stageCount = 2;
1298 VkPipelineShaderStageCreateInfo shaderStages[2];
1299 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1300
1301 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1302 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1303 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001304
1305 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1306 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1307 shaderStages[1].shader = demo_prepare_fs(demo);
1308
Tony Barboure307f582015-07-10 15:29:03 -06001309 pipeline.pVertexInputState = &vi;
1310 pipeline.pInputAssemblyState = &ia;
1311 pipeline.pRasterState = &rs;
1312 pipeline.pColorBlendState = &cb;
1313 pipeline.pMultisampleState = &ms;
1314 pipeline.pViewportState = &vp;
1315 pipeline.pDepthStencilState = &ds;
1316 pipeline.pStages = shaderStages;
Tony Barbourd31ab432015-08-06 14:32:54 -06001317 pipeline.renderPass = demo->render_pass;
Piers Daniell811eb742015-09-29 13:01:09 -06001318 pipeline.pDynamicState = &dynamicState;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001319
Jon Ashburn0d60d272015-07-09 15:02:25 -06001320 memset(&pipelineCache, 0, sizeof(pipelineCache));
1321 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1322
1323 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1324 assert(!err);
1325 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001326 assert(!err);
1327
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001328 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Mark Lobodzinski8a190642015-08-07 10:17:13 -06001329
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001330 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001331 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001332 }
Mark Lobodzinskia43e24c2015-08-10 13:40:32 -06001333 vkDestroyShaderModule(demo->device, demo->frag_shader_module);
1334 vkDestroyShaderModule(demo->device, demo->vert_shader_module);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001335}
1336
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001337static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001338{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001339 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001340 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001341 .count = DEMO_TEXTURE_COUNT,
1342 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001343 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001344 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001345 .pNext = NULL,
Courtney Goeltzenleuchterd9e966a2015-09-16 16:12:45 -06001346 .poolUsage = VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT,
1347 .maxSets = 1,
Chia-I Wuf8385062015-01-04 16:27:24 +08001348 .count = 1,
1349 .pTypeCount = &type_count,
1350 };
Tony Barbour22a30862015-04-22 09:02:32 -06001351 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001352
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001353 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001354 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001355 assert(!err);
1356}
1357
1358static void demo_prepare_descriptor_set(struct demo *demo)
1359{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001360 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1361 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001362 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001363 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001364
Mike Stroyan230e6252015-04-17 12:36:38 -06001365 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001366 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001367 1, &demo->desc_layout,
Cody Northropc8aa4a52015-08-03 12:47:29 -06001368 &demo->desc_set);
1369 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001370
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001371 memset(&tex_descs, 0, sizeof(tex_descs));
1372 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1373 tex_descs[i].sampler = demo->textures[i].sampler;
1374 tex_descs[i].imageView = demo->textures[i].view;
1375 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1376 }
1377
1378 memset(&write, 0, sizeof(write));
1379 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1380 write.destSet = demo->desc_set;
1381 write.count = DEMO_TEXTURE_COUNT;
1382 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1383 write.pDescriptors = tex_descs;
1384
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001385 vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
Chia-I Wuf8385062015-01-04 16:27:24 +08001386}
1387
Chia-I Wu76cd4222015-07-08 13:34:24 +08001388static void demo_prepare_framebuffers(struct demo *demo)
1389{
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06001390 VkImageView attachments[2];
Cody Northropf110c6e2015-08-04 10:47:08 -06001391 attachments[1] = demo->depth.view;
1392
Chia-I Wu76cd4222015-07-08 13:34:24 +08001393 const VkFramebufferCreateInfo fb_info = {
1394 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1395 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001396 .renderPass = demo->render_pass,
1397 .attachmentCount = 2,
1398 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001399 .width = demo->width,
1400 .height = demo->height,
1401 .layers = 1,
1402 };
1403 VkResult U_ASSERT_ONLY err;
1404 uint32_t i;
1405
Tony Barbour5aabff52015-10-08 14:26:24 -06001406 demo->framebuffers = (VkFramebuffer *) malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1407 assert(demo->framebuffers);
1408
1409 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northropf110c6e2015-08-04 10:47:08 -06001410 attachments[0]= demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001411 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1412 assert(!err);
1413 }
1414}
1415
Chia-I Wuc19795a2014-09-13 11:12:55 +08001416static void demo_prepare(struct demo *demo)
1417{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001418 VkResult U_ASSERT_ONLY err;
1419
1420 const VkCmdPoolCreateInfo cmd_pool_info = {
1421 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1422 .pNext = NULL,
1423 .queueFamilyIndex = demo->graphics_queue_node_index,
1424 .flags = 0,
1425 };
1426 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1427 assert(!err);
1428
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001429 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001430 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001431 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001432 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001433 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001434 .flags = 0,
1435 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001436 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1437 assert(!err);
1438
Chia-I Wuc19795a2014-09-13 11:12:55 +08001439 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001440 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001441 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001442 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001443 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001444 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001445 demo_prepare_pipeline(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001446
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001447 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001448 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001449
1450 demo_prepare_framebuffers(demo);
1451
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001452 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001453}
1454
Ian Elliotte14e9f92015-04-16 15:23:05 -06001455#ifdef _WIN32
1456static void demo_run(struct demo *demo)
1457{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001458 if (!demo->prepared)
1459 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001460 demo_draw(demo);
Tony Barbour4a6692d2015-10-08 13:45:45 -06001461
1462 if (demo->depthStencil > 0.99f)
1463 demo->depthIncrement = -0.001f;
1464 if (demo->depthStencil < 0.8f)
1465 demo->depthIncrement = 0.001f;
1466
1467 demo->depthStencil += demo->depthIncrement;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001468}
1469
1470// On MS-Windows, make this a global, so it's available to WndProc()
1471struct demo demo;
1472
1473// MS-Windows event handling function:
1474LRESULT CALLBACK WndProc(HWND hWnd,
1475 UINT uMsg,
1476 WPARAM wParam,
1477 LPARAM lParam)
1478{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001479 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001480
1481 switch(uMsg)
1482 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001483 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001484 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001485 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001486 PostQuitMessage(0);
1487 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001488 case WM_PAINT:
Cody Northrop67ff0a42015-09-09 10:21:49 -06001489 if (demo.prepared) {
1490 demo_run(&demo);
Tony Barbour18b53e72015-10-20 12:49:46 -06001491 break;
Cody Northrop67ff0a42015-09-09 10:21:49 -06001492 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001493 default:
1494 break;
1495 }
1496 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1497}
1498
1499static void demo_create_window(struct demo *demo)
1500{
1501 WNDCLASSEX win_class;
1502
1503 // Initialize the window class structure:
1504 win_class.cbSize = sizeof(WNDCLASSEX);
1505 win_class.style = CS_HREDRAW | CS_VREDRAW;
1506 win_class.lpfnWndProc = WndProc;
1507 win_class.cbClsExtra = 0;
1508 win_class.cbWndExtra = 0;
1509 win_class.hInstance = demo->connection; // hInstance
1510 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1511 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1512 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1513 win_class.lpszMenuName = NULL;
1514 win_class.lpszClassName = demo->name;
1515 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1516 // Register window class:
1517 if (!RegisterClassEx(&win_class)) {
1518 // It didn't work, so try to give a useful error:
1519 printf("Unexpected error trying to start the application!\n");
1520 fflush(stdout);
1521 exit(1);
1522 }
1523 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001524 RECT wr = { 0, 0, demo->width, demo->height };
1525 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001526 demo->window = CreateWindowEx(0,
1527 demo->name, // class name
1528 demo->name, // app name
1529 WS_OVERLAPPEDWINDOW | // window style
1530 WS_VISIBLE |
1531 WS_SYSMENU,
1532 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001533 wr.right-wr.left, // width
1534 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001535 NULL, // handle to parent
1536 NULL, // handle to menu
1537 demo->connection, // hInstance
1538 NULL); // no extra parameters
1539 if (!demo->window) {
1540 // It didn't work, so try to give a useful error:
1541 printf("Cannot create a window in which to draw!\n");
1542 fflush(stdout);
1543 exit(1);
1544 }
1545}
1546#else // _WIN32
1547
Chia-I Wuc19795a2014-09-13 11:12:55 +08001548static void demo_handle_event(struct demo *demo,
1549 const xcb_generic_event_t *event)
1550{
1551 switch (event->response_type & 0x7f) {
1552 case XCB_EXPOSE:
1553 demo_draw(demo);
1554 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001555 case XCB_CLIENT_MESSAGE:
1556 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1557 (*demo->atom_wm_delete_window).atom) {
1558 demo->quit = true;
1559 }
1560 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001561 case XCB_KEY_RELEASE:
1562 {
1563 const xcb_key_release_event_t *key =
1564 (const xcb_key_release_event_t *) event;
1565
1566 if (key->detail == 0x9)
1567 demo->quit = true;
1568 }
1569 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001570 case XCB_DESTROY_NOTIFY:
1571 demo->quit = true;
1572 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001573 default:
1574 break;
1575 }
1576}
1577
1578static void demo_run(struct demo *demo)
1579{
1580 xcb_flush(demo->connection);
1581
1582 while (!demo->quit) {
1583 xcb_generic_event_t *event;
1584
Tony Barbour4a6692d2015-10-08 13:45:45 -06001585 event = xcb_poll_for_event(demo->connection);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001586 if (event) {
1587 demo_handle_event(demo, event);
1588 free(event);
1589 }
Tony Barbour4a6692d2015-10-08 13:45:45 -06001590
1591 demo_draw(demo);
1592
1593 if (demo->depthStencil > 0.99f)
1594 demo->depthIncrement = -0.001f;
1595 if (demo->depthStencil < 0.8f)
1596 demo->depthIncrement = 0.001f;
1597
1598 demo->depthStencil += demo->depthIncrement;
1599
1600 // Wait for work to finish before updating MVP.
1601 vkDeviceWaitIdle(demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001602 }
1603}
1604
1605static void demo_create_window(struct demo *demo)
1606{
1607 uint32_t value_mask, value_list[32];
1608
1609 demo->window = xcb_generate_id(demo->connection);
1610
1611 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1612 value_list[0] = demo->screen->black_pixel;
1613 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001614 XCB_EVENT_MASK_EXPOSURE |
1615 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001616
1617 xcb_create_window(demo->connection,
1618 XCB_COPY_FROM_PARENT,
1619 demo->window, demo->screen->root,
1620 0, 0, demo->width, demo->height, 0,
1621 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1622 demo->screen->root_visual,
1623 value_mask, value_list);
1624
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001625 /* Magic code that will send notification when window is destroyed */
1626 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1627 "WM_PROTOCOLS");
1628 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1629
1630 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1631 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1632
1633 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1634 demo->window, (*reply).atom, 4, 32, 1,
1635 &(*demo->atom_wm_delete_window).atom);
1636 free(reply);
1637
Chia-I Wuc19795a2014-09-13 11:12:55 +08001638 xcb_map_window(demo->connection, demo->window);
1639}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001640#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001641
Ian Elliotte36b2082015-07-06 14:27:58 -06001642/*
1643 * Return 1 (true) if all layer names specified in check_names
1644 * can be found in given layer properties.
1645 */
1646static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1647 uint32_t layer_count, VkLayerProperties *layers)
1648{
1649 for (uint32_t i = 0; i < check_count; i++) {
1650 VkBool32 found = 0;
1651 for (uint32_t j = 0; j < layer_count; j++) {
1652 if (!strcmp(check_names[i], layers[j].layerName)) {
1653 found = 1;
1654 }
1655 }
1656 if (!found) {
1657 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1658 return 0;
1659 }
1660 }
1661 return 1;
1662}
1663
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001664void* VKAPI myalloc(
1665 void* pUserData,
1666 size_t size,
1667 size_t alignment,
1668 VkSystemAllocType allocType)
1669{
1670 return malloc(size);
1671}
1672void VKAPI myfree(
1673 void* pUserData,
1674 void* pMem)
1675{
Piers Daniell811eb742015-09-29 13:01:09 -06001676 free(pMem);
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001677}
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001678static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001679{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001680 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001681 char *extension_names[64];
1682 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001683 VkExtensionProperties *instance_extensions;
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001684 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001685 VkLayerProperties *instance_layers;
1686 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001687 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001688 uint32_t instance_layer_count = 0;
1689 uint32_t enabled_extension_count = 0;
1690 uint32_t enabled_layer_count = 0;
1691
Ian Elliotte36b2082015-07-06 14:27:58 -06001692 char *instance_validation_layers[] = {
1693 "MemTracker",
1694 };
1695
1696 char *device_validation_layers[] = {
1697 "MemTracker",
1698 };
1699
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001700 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001701 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001702 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001703 assert(!err);
1704
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001705 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001706 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001707 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001708
1709 if (demo->validate) {
1710 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1711 instance_layer_count, instance_layers);
1712 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001713 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001714 "required validation layer.\n\n"
1715 "Please look at the Getting Started guide for additional "
1716 "information.\n",
1717 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001718 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001719 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001720 }
1721
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001722 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001723 assert(!err);
1724
Tony Barbourd9955e42015-10-08 13:59:42 -06001725 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001726 memset(extension_names, 0, sizeof(extension_names));
1727 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001728 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001729 assert(!err);
1730 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001731 if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001732 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001733 extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001734 }
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001735 if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001736 if (demo->validate) {
Courtney Goeltzenleuchter846298c2015-07-30 11:32:46 -06001737 extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001738 }
1739 }
1740 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001741 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001742 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001743 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001744 "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001745 "Vulkan installable client driver (ICD) installed?\nPlease "
1746 "look at the Getting Started guide for additional "
1747 "information.\n",
1748 "vkCreateInstance Failure");
1749 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001750 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001751 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001752 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001753 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001754 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001755 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001756 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001757 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001758 };
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001759 VkAllocCallbacks cb = {
1760 .pUserData = NULL,
1761 .pfnAlloc = myalloc,
1762 .pfnFree = myfree,
1763 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001764 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001765 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001766 .pNext = NULL,
1767 .pAppInfo = &app,
Jon Ashburne74fc5d2015-08-28 13:48:40 -06001768 .pAllocCb = &cb,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001769 .layerCount = enabled_layer_count,
1770 .ppEnabledLayerNames = (const char *const*) layer_names,
1771 .extensionCount = enabled_extension_count,
1772 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001773 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001774 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchterea975642015-09-16 16:23:55 -06001775 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
1776 .pNext = NULL,
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001777 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001778 .queueCount = 1,
1779 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001780 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001781
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001782 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001783 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1784 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001785 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001786 "additional information.\n",
1787 "vkCreateInstance Failure");
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06001788 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001789 ERR_EXIT("Cannot find a specified extension library"
1790 ".\nMake sure your layers path is set appropriately\n",
1791 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001792 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001793 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1794 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001795 "the Getting Started guide for additional information.\n",
1796 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001797 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001798
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001799 free(instance_layers);
1800 free(instance_extensions);
1801
Tobin Ehlis4f482a72015-09-07 15:16:39 -06001802 /* Make initial call to query gpu_count, then second call for gpu info*/
1803 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
1804 assert(!err && gpu_count > 0);
1805 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
1806 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
1807 assert(!err);
1808 /* For tri demo we just grab the first physical device */
1809 demo->gpu = physical_devices[0];
1810 free(physical_devices);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001811
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001812 /* Look for validation layers */
1813 validation_found = 0;
1814 enabled_layer_count = 0;
1815 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001816 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001817 assert(!err);
1818
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001819 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001820 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001821 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001822
1823 if (demo->validate) {
1824 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1825 device_layer_count, device_layers);
1826 if (!validation_found) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001827 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Ian Elliotte36b2082015-07-06 14:27:58 -06001828 "a required validation layer.\n\n"
1829 "Please look at the Getting Started guide for additional "
1830 "information.\n",
1831 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001832 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001833 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001834 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001835
1836 uint32_t device_extension_count = 0;
1837 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001838 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001839 demo->gpu, NULL, &device_extension_count, NULL);
1840 assert(!err);
1841
Tony Barbourd9955e42015-10-08 13:59:42 -06001842 swapchainExtFound = 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001843 enabled_extension_count = 0;
1844 memset(extension_names, 0, sizeof(extension_names));
1845 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001846 err = vkEnumerateDeviceExtensionProperties(
Ian Elliotte36b2082015-07-06 14:27:58 -06001847 demo->gpu, NULL, &device_extension_count, device_extensions);
1848 assert(!err);
1849
1850 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001851 if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) {
Tony Barbourd9955e42015-10-08 13:59:42 -06001852 swapchainExtFound = 1;
Ian Elliott338dedb2015-08-21 15:09:33 -06001853 extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain";
Ian Elliotte36b2082015-07-06 14:27:58 -06001854 }
1855 assert(enabled_extension_count < 64);
1856 }
Tony Barbourd9955e42015-10-08 13:59:42 -06001857 if (!swapchainExtFound) {
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001858 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliott338dedb2015-08-21 15:09:33 -06001859 "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliotte36b2082015-07-06 14:27:58 -06001860 "Vulkan installable client driver (ICD) installed?\nPlease "
1861 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001862 "information.\n",
1863 "vkCreateInstance Failure");
1864 }
1865
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001866 VkDeviceCreateInfo device = {
1867 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1868 .pNext = NULL,
1869 .queueRecordCount = 1,
1870 .pRequestedQueues = &queue,
1871 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001872 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1873 .extensionCount = enabled_extension_count,
1874 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001875 };
1876
1877 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001878 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001879 if (!demo->dbgCreateMsgCallback) {
1880 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1881 "vkGetProcAddr Failure");
1882 }
1883 err = demo->dbgCreateMsgCallback(
1884 demo->inst,
1885 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1886 dbgFunc, NULL,
1887 &demo->msg_callback);
1888 switch (err) {
1889 case VK_SUCCESS:
1890 break;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001891 case VK_ERROR_OUT_OF_HOST_MEMORY:
1892 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1893 "dbgCreateMsgCallback Failure");
1894 break;
1895 default:
1896 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1897 "dbgCreateMsgCallback Failure");
1898 break;
1899 }
1900 }
1901
Ian Elliotte36b2082015-07-06 14:27:58 -06001902
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001903 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001904 assert(!err);
1905
Ian Elliott338dedb2015-08-21 15:09:33 -06001906 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
1907 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR);
1908 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR);
1909 GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR);
1910 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1911 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
1912 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
1913 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
1914 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
1915 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliott1b6de092015-06-22 15:07:49 -06001916
Tony Barbour426b9052015-06-24 16:06:58 -06001917 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001918
Cody Northropef72e2a2015-08-03 17:04:53 -06001919 // Query with NULL data to get count
1920 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001921 assert(!err);
1922
Cody Northropef72e2a2015-08-03 17:04:53 -06001923 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
1924 err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001925 assert(!err);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001926 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001927
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001928 // Graphics queue and MemMgr queue can be separate.
1929 // TODO: Add support for separate queues, including synchronization,
1930 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001931}
1932
Tony Barbourd9955e42015-10-08 13:59:42 -06001933static void demo_init_vk_swapchain(struct demo *demo)
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001934{
Tony Barbour4a6692d2015-10-08 13:45:45 -06001935 VkResult U_ASSERT_ONLY err;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001936 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06001937
Tony Barbourd9955e42015-10-08 13:59:42 -06001938 // Construct the surface description:
Ian Elliott338dedb2015-08-21 15:09:33 -06001939 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001940 demo->surface_description.pNext = NULL;
1941#ifdef _WIN32
Ian Elliott338dedb2015-08-21 15:09:33 -06001942 demo->surface_description.platform = VK_PLATFORM_WIN32_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001943 demo->surface_description.pPlatformHandle = demo->connection;
1944 demo->surface_description.pPlatformWindow = demo->window;
1945#else // _WIN32
1946 demo->platform_handle_xcb.connection = demo->connection;
1947 demo->platform_handle_xcb.root = demo->screen->root;
Ian Elliott338dedb2015-08-21 15:09:33 -06001948 demo->surface_description.platform = VK_PLATFORM_XCB_KHR;
Ian Elliotte36b2082015-07-06 14:27:58 -06001949 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
1950 demo->surface_description.pPlatformWindow = &demo->window;
1951#endif // _WIN32
1952
Tony Barbourd9955e42015-10-08 13:59:42 -06001953 // Iterate over each queue to learn whether it supports presenting:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001954 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
1955 for (i = 0; i < demo->queue_count; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -06001956 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
1957 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliotte36b2082015-07-06 14:27:58 -06001958 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001959 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001960
1961 // Search for a graphics and a present queue in the array of queue
1962 // families, try to find one that supports both
1963 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
1964 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001965 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001966 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
1967 if (graphicsQueueNodeIndex == UINT32_MAX) {
1968 graphicsQueueNodeIndex = i;
1969 }
1970
1971 if (supportsPresent[i] == VK_TRUE) {
1972 graphicsQueueNodeIndex = i;
1973 presentQueueNodeIndex = i;
1974 break;
1975 }
1976 }
1977 }
1978 if (presentQueueNodeIndex == UINT32_MAX) {
1979 // If didn't find a queue that supports both graphics and present, then
1980 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001981 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001982 if (supportsPresent[i] == VK_TRUE) {
1983 presentQueueNodeIndex = i;
1984 break;
1985 }
1986 }
1987 }
1988 free(supportsPresent);
1989
1990 // Generate error if could not find both a graphics and a present queue
1991 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
1992 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06001993 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06001994 }
1995
1996 // TODO: Add support for separate queues, including presentation,
1997 // synchronization, and appropriate tracking for QueueSubmit
1998 // While it is possible for an application to use a separate graphics and a
1999 // present queues, this demo program assumes it is only using one:
2000 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2001 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourd9955e42015-10-08 13:59:42 -06002002 "Swapchain Initialization Failure");
Ian Elliotte36b2082015-07-06 14:27:58 -06002003 }
2004
2005 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002006
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002007 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08002008 0, &demo->queue);
2009 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06002010
Ian Elliotte36b2082015-07-06 14:27:58 -06002011 // Get the list of VkFormat's that are supported:
Ian Elliott7fe115d2015-08-07 15:56:59 -06002012 uint32_t formatCount;
Ian Elliott338dedb2015-08-21 15:09:33 -06002013 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2014 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002015 &formatCount, NULL);
Ian Elliotte36b2082015-07-06 14:27:58 -06002016 assert(!err);
Ian Elliott338dedb2015-08-21 15:09:33 -06002017 VkSurfaceFormatKHR *surfFormats =
2018 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
2019 err = demo->fpGetSurfaceFormatsKHR(demo->device,
2020 (VkSurfaceDescriptionKHR *) &demo->surface_description,
Ian Elliott7fe115d2015-08-07 15:56:59 -06002021 &formatCount, surfFormats);
Ian Elliotte36b2082015-07-06 14:27:58 -06002022 assert(!err);
2023 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2024 // the surface has no preferred format. Otherwise, at least one
2025 // supported format will be returned.
Ian Elliotte36b2082015-07-06 14:27:58 -06002026 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2027 {
2028 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2029 }
2030 else
2031 {
2032 assert(formatCount >= 1);
2033 demo->format = surfFormats[0].format;
2034 }
Ian Elliott7fe115d2015-08-07 15:56:59 -06002035 demo->color_space = surfFormats[0].colorSpace;
Ian Elliott32536f92015-04-21 16:41:02 -06002036
Mark Lobodzinski72346292015-07-02 16:49:40 -06002037 // Get Memory information and properties
2038 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2039 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002040}
2041
2042static void demo_init_connection(struct demo *demo)
2043{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002044#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002045 const xcb_setup_t *setup;
2046 xcb_screen_iterator_t iter;
2047 int scr;
2048
2049 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002050 if (demo->connection == NULL) {
2051 printf("Cannot find a compatible Vulkan installable client driver "
2052 "(ICD).\nExiting ...\n");
2053 fflush(stdout);
2054 exit(1);
2055 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002056
2057 setup = xcb_get_setup(demo->connection);
2058 iter = xcb_setup_roots_iterator(setup);
2059 while (scr-- > 0)
2060 xcb_screen_next(&iter);
2061
2062 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002063#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002064}
2065
Ian Elliotte14e9f92015-04-16 15:23:05 -06002066#ifdef _WIN32
2067static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2068#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002069static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002070#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002071{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002072 bool argv_error = false;
2073
Chia-I Wuc19795a2014-09-13 11:12:55 +08002074 memset(demo, 0, sizeof(*demo));
2075
Ian Elliotte14e9f92015-04-16 15:23:05 -06002076#ifdef _WIN32
2077 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002078 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002079
2080 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2081 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002082 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2083 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002084 else if (strlen(pCmdLine) != 0) {
2085 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2086 argv_error = true;
2087 }
2088#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002089 for (int i = 0; i < argc; i++) {
2090 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2091 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002092 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2093 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002094 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002095#endif // _WIN32
2096 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002097 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002098 fflush(stderr);
2099 exit(1);
2100 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002101
Chia-I Wuc19795a2014-09-13 11:12:55 +08002102 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002103 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002104
2105 demo->width = 300;
2106 demo->height = 300;
Tony Barbour4a6692d2015-10-08 13:45:45 -06002107 demo->depthStencil = 1.0;
2108 demo->depthIncrement = -0.01f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002109}
2110
2111static void demo_cleanup(struct demo *demo)
2112{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002113 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002114
Cody Northrop67ff0a42015-09-09 10:21:49 -06002115 demo->prepared = false;
2116
Tony Barbour5aabff52015-10-08 14:26:24 -06002117 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002118 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2119 }
Tony Barbour5aabff52015-10-08 14:26:24 -06002120 free(demo->framebuffers);
Tony Barbourde4124d2015-07-03 10:33:54 -06002121 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002122
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002123 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002124 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002125 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002126 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002127 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002128
Tony Barbourde4124d2015-07-03 10:33:54 -06002129 vkDestroyPipeline(demo->device, demo->pipeline);
2130 vkDestroyRenderPass(demo->device, demo->render_pass);
2131 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2132 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002133
Tony Barbourde4124d2015-07-03 10:33:54 -06002134 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002135 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002136
Chia-I Wub043fe32014-10-06 15:30:33 +08002137 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002138 vkDestroyImageView(demo->device, demo->textures[i].view);
2139 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002140 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002141 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08002142 }
2143
Ian Elliott338dedb2015-08-21 15:09:33 -06002144 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002145 vkDestroyImageView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002146 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002147
Courtney Goeltzenleuchter1856d6f2015-09-01 17:30:39 -06002148 vkDestroyImageView(demo->device, demo->depth.view);
Tony Barbourde4124d2015-07-03 10:33:54 -06002149 vkDestroyImage(demo->device, demo->depth.image);
2150 vkFreeMemory(demo->device, demo->depth.mem);
2151
Ian Elliott338dedb2015-08-21 15:09:33 -06002152 demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain);
Ian Elliotte36b2082015-07-06 14:27:58 -06002153 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002154
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002155 vkDestroyDevice(demo->device);
2156 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002157
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002158 free(demo->queue_props);
2159
Ian Elliotte14e9f92015-04-16 15:23:05 -06002160#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002161 xcb_destroy_window(demo->connection, demo->window);
2162 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter591ac872015-09-24 17:27:08 -06002163 free(demo->atom_wm_delete_window);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002164#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002165}
2166
Ian Elliotte14e9f92015-04-16 15:23:05 -06002167#ifdef _WIN32
2168int APIENTRY WinMain(HINSTANCE hInstance,
2169 HINSTANCE hPrevInstance,
2170 LPSTR pCmdLine,
2171 int nCmdShow)
2172{
2173 MSG msg; // message
2174 bool done; // flag saying when app is complete
2175
2176 demo_init(&demo, hInstance, pCmdLine);
2177 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002178 demo_init_vk_swapchain(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002179
2180 demo_prepare(&demo);
2181
2182 done = false; //initialize loop condition variable
2183 /* main message loop*/
2184 while(!done)
2185 {
Ian Elliott421107f2015-04-28 15:50:36 -06002186 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002187 if (msg.message == WM_QUIT) //check for a quit message
2188 {
2189 done = true; //if found, quit app
2190 }
2191 else
2192 {
2193 /* Translate and dispatch to event queue*/
2194 TranslateMessage(&msg);
2195 DispatchMessage(&msg);
2196 }
Tony Barbour18b53e72015-10-20 12:49:46 -06002197 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002198 }
2199
2200 demo_cleanup(&demo);
2201
Tony Barboura938abb2015-04-22 11:36:22 -06002202 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002203}
2204#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002205int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002206{
2207 struct demo demo;
2208
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002209 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002210 demo_create_window(&demo);
Tony Barbourd9955e42015-10-08 13:59:42 -06002211 demo_init_vk_swapchain(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002212
2213 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002214 demo_run(&demo);
2215
2216 demo_cleanup(&demo);
2217
Chia-I Wuc19795a2014-09-13 11:12:55 +08002218 return 0;
2219}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002220#endif // _WIN32