blob: 2154420084acda779b655a76a3de2374d83172ee [file] [log] [blame]
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001/*
Ian Elliott7595eee2015-04-28 10:33:11 -06002 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24/*
Chia-I Wuf5caeb02014-10-25 12:11:27 +080025 * Draw a textured triangle with depth testing. This is written against Intel
26 * ICD. It does not do state transition nor object memory binding like it
27 * should. It also does no error checking.
28 */
29
Chia-I Wuc19795a2014-09-13 11:12:55 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
35
Ian Elliotte14e9f92015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
38#include <windows.h>
39#define APP_NAME_STR_LEN 80
40#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +080041#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060042#endif // _WIN32
43
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060044#include <vulkan.h>
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060045#include "vk_debug_report_lunarg.h"
Ian Elliotte36b2082015-07-06 14:27:58 -060046#include <vk_wsi_swapchain.h>
47#include <vk_wsi_device_swapchain.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080048
Cody Northropd4e020a2015-03-17 14:54:35 -060049#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060050
Chia-I Wuc19795a2014-09-13 11:12:55 +080051#define DEMO_BUFFER_COUNT 2
Chia-I Wub043fe32014-10-06 15:30:33 +080052#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060053#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060054#define APP_SHORT_NAME "tri"
55#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080056
Ian Elliotte36b2082015-07-06 14:27:58 -060057#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
58
Tony Barbour22a30862015-04-22 09:02:32 -060059#if defined(NDEBUG) && defined(__GNUC__)
60#define U_ASSERT_ONLY __attribute__((unused))
61#else
62#define U_ASSERT_ONLY
63#endif
64
Ian Elliottcaa9f272015-04-28 11:35:02 -060065#ifdef _WIN32
66#define ERR_EXIT(err_msg, err_class) \
67 do { \
68 MessageBox(NULL, err_msg, err_class, MB_OK); \
69 exit(1); \
70 } while (0)
71
72// NOTE: If the following values (copied from "loader_platform.h") change, they
73// need to change here as well:
74#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
75#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
76#else // _WIN32
77
78#define ERR_EXIT(err_msg, err_class) \
79 do { \
80 printf(err_msg); \
81 fflush(stdout); \
82 exit(1); \
83 } while (0)
84#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060085
Ian Elliotte36b2082015-07-06 14:27:58 -060086#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
87{ \
88 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
89 if (demo->fp##entrypoint == NULL) { \
90 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
91 "vkGetInstanceProcAddr Failure"); \
92 } \
93}
94
Ian Elliott1b6de092015-06-22 15:07:49 -060095#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
96{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060097 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060098 if (demo->fp##entrypoint == NULL) { \
99 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
100 "vkGetDeviceProcAddr Failure"); \
101 } \
102}
103
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600104struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600105 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700106
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600107 VkImage image;
108 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600109
Mark Lobodzinski23182612015-05-29 09:32:35 -0500110 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600111 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700112 int32_t tex_width, tex_height;
113};
114
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600115void dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600116 VkFlags msgFlags,
117 VkDbgObjectType objType,
118 uint64_t srcObject,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600119 size_t location,
120 int32_t msgCode,
121 const char* pLayerPrefix,
122 const char* pMsg,
123 void* pUserData)
124{
125 char *message = (char *) malloc(strlen(pMsg)+100);
126
127 assert (message);
128
129 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
130 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
131 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
132 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
133 } else {
134 return;
135 }
136
137#ifdef _WIN32
138 MessageBox(NULL, message, "Alert", MB_OK);
139#else
140 printf("%s\n",message);
141 fflush(stdout);
142#endif
143 free(message);
144}
145
Ian Elliotte36b2082015-07-06 14:27:58 -0600146typedef struct _SwapChainBuffers {
147 VkImage image;
148 VkCmdBuffer cmd;
149 VkAttachmentView view;
150} SwapChainBuffers;
151
Chia-I Wuc19795a2014-09-13 11:12:55 +0800152struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600153#ifdef _WIN32
154#define APP_NAME_STR_LEN 80
155 HINSTANCE connection; // hInstance - Windows Instance
156 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
157 HWND window; // hWnd - window handle
158#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800159 xcb_connection_t *connection;
160 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800161 xcb_window_t window;
162 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte36b2082015-07-06 14:27:58 -0600163 VkPlatformHandleXcbWSI platform_handle_xcb;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600164#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700165 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600166 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600167 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800168
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600169 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600170 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600171 VkDevice device;
172 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600173 VkPhysicalDeviceProperties gpu_props;
Tony Barbour8205d902015-04-16 15:59:00 -0600174 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700175 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800176
177 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600178 VkFormat format;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800179
Ian Elliotte36b2082015-07-06 14:27:58 -0600180 PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI;
181 PFN_vkGetSurfaceInfoWSI fpGetSurfaceInfoWSI;
Jon Ashburncedc15f2015-05-21 18:13:33 -0600182 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
183 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
184 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600185 PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI;
Jon Ashburncedc15f2015-05-21 18:13:33 -0600186 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Ian Elliotte36b2082015-07-06 14:27:58 -0600187 VkSurfaceDescriptionWindowWSI surface_description;
188 size_t swapChainImageCount;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800189 VkSwapChainWSI swap_chain;
Ian Elliotte36b2082015-07-06 14:27:58 -0600190 SwapChainBuffers *buffers;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800191
Ian Elliotte36b2082015-07-06 14:27:58 -0600192 VkCmdPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800193
Chia-I Wub043fe32014-10-06 15:30:33 +0800194 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600195 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800196
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600197 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500198 VkDeviceMemory mem;
Chia-I Wuc278df82015-07-07 11:50:03 +0800199 VkAttachmentView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800200 } depth;
201
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600202 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800203
Chia-I Wu99621bc2014-10-08 11:52:22 +0800204 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600205 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500206 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800207
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600208 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600209 VkVertexInputBindingDescription vi_bindings[1];
210 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800211 } vertices;
212
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600213 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
214 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500215 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600216 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600217 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800218 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600219 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800220
Tony Barbourde4124d2015-07-03 10:33:54 -0600221 VkDynamicViewportState viewport;
222 VkDynamicRasterState raster;
223 VkDynamicColorBlendState color_blend;
224 VkDynamicDepthStencilState depth_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800225
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600226 VkDescriptorPool desc_pool;
227 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800228
Chia-I Wu76cd4222015-07-08 13:34:24 +0800229 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
230
Mark Lobodzinski72346292015-07-02 16:49:40 -0600231 VkPhysicalDeviceMemoryProperties memory_properties;
232
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600233 bool validate;
234 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Ian Elliotte36b2082015-07-06 14:27:58 -0600235 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600236 VkDbgMsgCallback msg_callback;
237
Chia-I Wuc19795a2014-09-13 11:12:55 +0800238 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600239 uint32_t current_buffer;
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600240 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800241};
242
Mark Lobodzinski72346292015-07-02 16:49:40 -0600243static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
244{
245 // Search memtypes to find first index with those properties
246 for (uint32_t i = 0; i < 32; i++) {
247 if ((typeBits & 1) == 1) {
248 // Type is available, does it match user properties?
249 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
250 *typeIndex = i;
251 return VK_SUCCESS;
252 }
253 }
254 typeBits >>= 1;
255 }
256 // No memory types matched, return failure
257 return VK_UNSUPPORTED;
258}
259
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600260static void demo_flush_init_cmd(struct demo *demo)
261{
Tony Barbour22a30862015-04-22 09:02:32 -0600262 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600263
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600264 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600265 return;
266
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600267 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600268 assert(!err);
269
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600270 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600271 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600272
Tony Barbourde4124d2015-07-03 10:33:54 -0600273 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600274 assert(!err);
275
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600276 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600277 assert(!err);
278
Tony Barbourde4124d2015-07-03 10:33:54 -0600279 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600280 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600281}
282
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600283static void demo_set_image_layout(
284 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600285 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400286 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600287 VkImageLayout old_image_layout,
288 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600289{
Tony Barbour22a30862015-04-22 09:02:32 -0600290 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600291
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600292 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600293 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600294 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600295 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600296 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800297 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600298 .flags = 0,
299 };
300
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600301 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600302 assert(!err);
303
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600304 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600305 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600306 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600307 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600308 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600310 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600311 }
312
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600313 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600314 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600315 .pNext = NULL,
316 .outputMask = 0,
317 .inputMask = 0,
318 .oldLayout = old_image_layout,
319 .newLayout = new_image_layout,
320 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400321 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600322 };
323
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600324 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600325 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600326 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600327 }
328
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600329 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600330 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600331 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600332 }
333
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600334 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600335
Tony Barbourc2e987e2015-06-29 16:20:35 -0600336 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
337 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600338
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600339 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600340}
341
Chia-I Wuc19795a2014-09-13 11:12:55 +0800342static void demo_draw_build_cmd(struct demo *demo)
343{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800344 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600345 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600346 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600347 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600348 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700349 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800350 const VkClearValue clear_values[2] = {
351 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
352 [1] = { .ds = { 0.9f, 0 } },
353 };
354 const VkRenderPassBeginInfo rp_begin = {
355 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
356 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800357 .renderPass = demo->render_pass,
358 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800359 .renderArea.offset.x = 0,
360 .renderArea.offset.y = 0,
361 .renderArea.extent.width = demo->width,
362 .renderArea.extent.height = demo->height,
363 .attachmentCount = 2,
364 .pAttachmentClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700365 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800366 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800367
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600368 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800369 assert(!err);
370
Chia-I Wuc278df82015-07-07 11:50:03 +0800371 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600372 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800373 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600374 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600375 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800376
Tony Barbourde4124d2015-07-03 10:33:54 -0600377 vkCmdBindDynamicViewportState(demo->draw_cmd, demo->viewport);
378 vkCmdBindDynamicRasterState(demo->draw_cmd, demo->raster);
379 vkCmdBindDynamicColorBlendState(demo->draw_cmd, demo->color_blend);
380 vkCmdBindDynamicDepthStencilState(demo->draw_cmd, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800381
Tony Barbour8205d902015-04-16 15:59:00 -0600382 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600383 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800384
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600385 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800386 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800387
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600388 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800389 assert(!err);
390}
391
392static void demo_draw(struct demo *demo)
393{
Tony Barbour22a30862015-04-22 09:02:32 -0600394 VkResult U_ASSERT_ONLY err;
Ian Elliotte36b2082015-07-06 14:27:58 -0600395 VkSemaphore presentCompleteSemaphore;
396 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
397 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
398 .pNext = NULL,
399 .flags = VK_FENCE_CREATE_SIGNALED_BIT,
400 };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800401
Ian Elliotte36b2082015-07-06 14:27:58 -0600402 err = vkCreateSemaphore(demo->device,
403 &presentCompleteSemaphoreCreateInfo,
404 &presentCompleteSemaphore);
405 assert(!err);
406
407 // Get the index of the next available swapchain image:
408 err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain,
409 UINT64_MAX,
410 presentCompleteSemaphore,
411 &demo->current_buffer);
412 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
413 // return codes
414 assert(!err);
415
416 // Wait for the present complete semaphore to be signaled to ensure
417 // that the image won't be rendered to until the presentation
418 // engine has fully released ownership to the application, and it is
419 // okay to render to the image.
420 vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore);
421
422// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI
Chia-I Wuc19795a2014-09-13 11:12:55 +0800423 demo_draw_build_cmd(demo);
Tony Barbourde4124d2015-07-03 10:33:54 -0600424 VkFence nullFence = { VK_NULL_HANDLE };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800425
Tony Barbourde4124d2015-07-03 10:33:54 -0600426 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800427 assert(!err);
428
Ian Elliotte36b2082015-07-06 14:27:58 -0600429 VkPresentInfoWSI present = {
430 .sType = VK_STRUCTURE_TYPE_QUEUE_PRESENT_INFO_WSI,
431 .pNext = NULL,
432 .swapChainCount = 1,
433 .swapChains = &demo->swap_chain,
434 .imageIndices = &demo->current_buffer,
435 };
436
437// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Jon Ashburncedc15f2015-05-21 18:13:33 -0600438 err = demo->fpQueuePresentWSI(demo->queue, &present);
Ian Elliotte36b2082015-07-06 14:27:58 -0600439 // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI
440 // return codes
Chia-I Wuc19795a2014-09-13 11:12:55 +0800441 assert(!err);
442
Ian Elliotte36b2082015-07-06 14:27:58 -0600443// FIXME: UNCOMMENT THE FOLLOWING LINE ONCE WE HAVE A NEW-ENOUGH "vulkan.h" HEADER:
444// err = vkDestroySemaphore(demo->device, presentCompleteSemaphore);
445 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800446
447 err = vkQueueWaitIdle(demo->queue);
448 assert(err == VK_SUCCESS);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800449}
450
451static void demo_prepare_buffers(struct demo *demo)
452{
Ian Elliotte36b2082015-07-06 14:27:58 -0600453 VkResult U_ASSERT_ONLY err;
454
455 // Check the surface proprties and formats
456 size_t capsSize;
457 size_t presentModesSize;
458 err = demo->fpGetSurfaceInfoWSI(demo->device,
459 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
460 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, NULL);
461 assert(!err);
462 err = demo->fpGetSurfaceInfoWSI(demo->device,
463 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
464 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, NULL);
465 assert(!err);
466
467 VkSurfacePropertiesWSI *surfProperties =
468 (VkSurfacePropertiesWSI *)malloc(capsSize);
469 VkSurfacePresentModePropertiesWSI *presentModes =
470 (VkSurfacePresentModePropertiesWSI *)malloc(presentModesSize);
471
472 err = demo->fpGetSurfaceInfoWSI(demo->device,
473 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
474 VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, surfProperties);
475 assert(!err);
476 err = demo->fpGetSurfaceInfoWSI(demo->device,
477 (const VkSurfaceDescriptionWSI *)&demo->surface_description,
478 VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, presentModes);
479 assert(!err);
480
481 VkExtent2D swapChainExtent;
482 // width and height are either both -1, or both not -1.
483 if (surfProperties->currentExtent.width == -1)
484 {
485 // If the surface size is undefined, the size is set to
486 // the size of the images requested.
487 swapChainExtent.width = demo->width;
488 swapChainExtent.height = demo->height;
489 }
490 else
491 {
492 // If the surface size is defined, the swap chain size must match
493 swapChainExtent = surfProperties->currentExtent;
494 }
495
496 // If mailbox mode is available, use it, as is the lowest-latency non-
497 // tearing mode. If not, fall back to IMMEDIATE which should always be
498 // available.
499 VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI;
500 size_t presentModeCount = presentModesSize / sizeof(VkSurfacePresentModePropertiesWSI);
501 for (size_t i = 0; i < presentModeCount; i++) {
502 if (presentModes[i].presentMode == VK_PRESENT_MODE_MAILBOX_WSI) {
503 swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI;
504 break;
505 }
506 }
507
508 // Determine the number of VkImage's to use in the swap chain (we desire to
509 // own only 1 image at a time, besides the images being displayed and
510 // queued for display):
511 uint32_t desiredNumberOfSwapChainImages = surfProperties->minImageCount + 1;
512 if ((surfProperties->maxImageCount > 0) &&
513 (desiredNumberOfSwapChainImages > surfProperties->maxImageCount))
514 {
515 // Application must settle for fewer images than desired:
516 desiredNumberOfSwapChainImages = surfProperties->maxImageCount;
517 }
518
519 VkSurfaceTransformFlagBitsWSI preTransform;
520 if (surfProperties->supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) {
521 preTransform = VK_SURFACE_TRANSFORM_NONE_WSI;
522 } else {
523 preTransform = surfProperties->currentTransform;
524 }
525
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800526 const VkSwapChainCreateInfoWSI swap_chain = {
527 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
528 .pNext = NULL,
Ian Elliotte36b2082015-07-06 14:27:58 -0600529 .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description,
530 .minImageCount = desiredNumberOfSwapChainImages,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800531 .imageFormat = demo->format,
532 .imageExtent = {
Ian Elliotte36b2082015-07-06 14:27:58 -0600533 .width = swapChainExtent.width,
534 .height = swapChainExtent.height,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800535 },
Ian Elliotte36b2082015-07-06 14:27:58 -0600536 .preTransform = preTransform,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800537 .imageArraySize = 1,
Ian Elliotte36b2082015-07-06 14:27:58 -0600538 .presentMode = swapChainPresentMode,
539 .oldSwapChain.handle = 0,
540 .clipped = true,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800541 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600542 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800543
Jon Ashburncedc15f2015-05-21 18:13:33 -0600544 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800545 assert(!err);
546
Ian Elliotte36b2082015-07-06 14:27:58 -0600547 size_t swapChainImagesSize;
548 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
549 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
550 &swapChainImagesSize, NULL);
551 assert(!err);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800552
Ian Elliotte36b2082015-07-06 14:27:58 -0600553 VkSwapChainImagePropertiesWSI* swapChainImages = (VkSwapChainImagePropertiesWSI*)malloc(swapChainImagesSize);
554 assert(swapChainImages);
555 err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain,
556 VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI,
557 &swapChainImagesSize, swapChainImages);
558 assert(!err);
559
560 // The number of images within the swap chain is determined based on the
561 // size of the info returned
562 demo->swapChainImageCount = swapChainImagesSize / sizeof(VkSwapChainImagePropertiesWSI);
563
564 demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount);
565 assert(demo->buffers);
566
567 for (i = 0; i < demo->swapChainImageCount; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800568 VkAttachmentViewCreateInfo color_attachment_view = {
569 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800570 .pNext = NULL,
571 .format = demo->format,
572 .mipLevel = 0,
573 .baseArraySlice = 0,
574 .arraySize = 1,
575 };
576
Ian Elliotte36b2082015-07-06 14:27:58 -0600577 demo->buffers[i].image = swapChainImages[i].image;
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500578
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600579 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400580 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600581 VK_IMAGE_LAYOUT_UNDEFINED,
582 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600583
Chia-I Wuc19795a2014-09-13 11:12:55 +0800584 color_attachment_view.image = demo->buffers[i].image;
585
Chia-I Wuc278df82015-07-07 11:50:03 +0800586 err = vkCreateAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800587 &color_attachment_view, &demo->buffers[i].view);
588 assert(!err);
589 }
Piers Daniell886be472015-02-23 16:23:13 -0700590
591 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800592}
593
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800594static void demo_prepare_depth(struct demo *demo)
595{
Tony Barbour8205d902015-04-16 15:59:00 -0600596 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600597 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600598 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800599 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600600 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800601 .format = depth_format,
602 .extent = { demo->width, demo->height, 1 },
603 .mipLevels = 1,
604 .arraySize = 1,
605 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600606 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600607 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800608 .flags = 0,
609 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600610 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600611 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500612 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800613 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600614 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800615 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800616 VkAttachmentViewCreateInfo view = {
617 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800618 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600619 .image.handle = VK_NULL_HANDLE,
Piers Daniell1cf7fe12015-07-16 09:35:35 -0600620 .format = depth_format,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800621 .mipLevel = 0,
622 .baseArraySlice = 0,
623 .arraySize = 1,
624 .flags = 0,
625 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700626
Mark Lobodzinski23182612015-05-29 09:32:35 -0500627 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600628 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800629
630 demo->depth.format = depth_format;
631
632 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600633 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800634 &demo->depth.image);
635 assert(!err);
636
Mark Lobodzinski72346292015-07-02 16:49:40 -0600637 /* get memory requirements for this object */
Tony Barbourde4124d2015-07-03 10:33:54 -0600638 err = vkGetImageMemoryRequirements(demo->device, demo->depth.image,
639 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600640
641 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500642 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600643 err = memory_type_from_properties(demo,
644 mem_reqs.memoryTypeBits,
645 VK_MEMORY_PROPERTY_DEVICE_ONLY,
646 &mem_alloc.memoryTypeIndex);
647 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800648
Mark Lobodzinski23182612015-05-29 09:32:35 -0500649 /* allocate memory */
650 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
651 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800652
Mark Lobodzinski23182612015-05-29 09:32:35 -0500653 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600654 err = vkBindImageMemory(demo->device, demo->depth.image,
655 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500656 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800657
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600658 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400659 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600660 VK_IMAGE_LAYOUT_UNDEFINED,
661 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600662
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800663 /* create image view */
664 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800665 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800666 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800667}
668
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700669static void demo_prepare_texture_image(struct demo *demo,
670 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600671 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600672 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600673 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600674 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800675{
Tony Barbour8205d902015-04-16 15:59:00 -0600676 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600677 const int32_t tex_width = 2;
678 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600679 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800680
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600681 tex_obj->tex_width = tex_width;
682 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700683
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600684 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600685 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700686 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600687 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700688 .format = tex_format,
689 .extent = { tex_width, tex_height, 1 },
690 .mipLevels = 1,
691 .arraySize = 1,
692 .samples = 1,
693 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600694 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700695 .flags = 0,
696 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600697 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600698 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500699 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700700 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600701 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700702 };
703
Mark Lobodzinski23182612015-05-29 09:32:35 -0500704 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700705
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600706 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600707 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700708 assert(!err);
709
Tony Barbourde4124d2015-07-03 10:33:54 -0600710 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600711
712 mem_alloc.allocationSize = mem_reqs.size;
713 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
714 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700715
Mark Lobodzinski23182612015-05-29 09:32:35 -0500716 /* allocate memory */
717 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
718 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700719
Mark Lobodzinski23182612015-05-29 09:32:35 -0500720 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600721 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500722 tex_obj->mem, 0);
723 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700724
Tony Barbour8205d902015-04-16 15:59:00 -0600725 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600726 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600727 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700728 .mipLevel = 0,
729 .arraySlice = 0,
730 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600731 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700732 void *data;
733 int32_t x, y;
734
Tony Barbour426b9052015-06-24 16:06:58 -0600735 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
736 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700737
Mark Lobodzinski23182612015-05-29 09:32:35 -0500738 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700739 assert(!err);
740
741 for (y = 0; y < tex_height; y++) {
742 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
743 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700744 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700745 }
746
Mark Lobodzinski23182612015-05-29 09:32:35 -0500747 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700748 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700749 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600750
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600751 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600752 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400753 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600754 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600755 tex_obj->imageLayout);
756 /* 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 -0700757}
758
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500759static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700760{
761 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600762 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600763 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700764}
765
766static void demo_prepare_textures(struct demo *demo)
767{
Tony Barbour8205d902015-04-16 15:59:00 -0600768 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600769 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700770 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
771 { 0xffff0000, 0xff00ff00 },
772 };
Tony Barbour22a30862015-04-22 09:02:32 -0600773 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700774 uint32_t i;
775
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600776 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700777 assert(!err);
778
779 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600780 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700781 /* Device can texture using linear textures */
782 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600783 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600784 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700785 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600786 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700787
788 memset(&staging_texture, 0, sizeof(staging_texture));
789 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600790 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700791
792 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600793 VK_IMAGE_TILING_OPTIMAL,
794 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
795 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700796
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600797 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400798 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600799 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600800 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700801
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600802 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400803 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600804 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600805 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700806
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600807 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600808 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700809 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600810 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700811 .destOffset = { 0, 0, 0 },
812 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
813 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600814 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600815 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
816 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600817 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700818
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600819 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400820 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600821 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600822 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700823
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600824 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700825
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600826 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700827 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600828 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700829 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700830 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700831
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600832 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600833 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800834 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600835 .magFilter = VK_TEX_FILTER_NEAREST,
836 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600837 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600838 .addressU = VK_TEX_ADDRESS_WRAP,
839 .addressV = VK_TEX_ADDRESS_WRAP,
840 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800841 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700842 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600843 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800844 .minLod = 0.0f,
845 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600846 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800847 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600848 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600849 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800850 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600851 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600852 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700853 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600854 .channels = { VK_CHANNEL_SWIZZLE_R,
855 VK_CHANNEL_SWIZZLE_G,
856 VK_CHANNEL_SWIZZLE_B,
857 VK_CHANNEL_SWIZZLE_A, },
858 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800859 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700860
Chia-I Wub043fe32014-10-06 15:30:33 +0800861 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600862 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800863 &demo->textures[i].sampler);
864 assert(!err);
865
Chia-I Wub043fe32014-10-06 15:30:33 +0800866 /* create image view */
867 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600868 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700869 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800870 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800871 }
872}
873
Chia-I Wu99621bc2014-10-08 11:52:22 +0800874static void demo_prepare_vertices(struct demo *demo)
875{
876 const float vb[3][5] = {
877 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800878 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
879 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800880 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
881 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600882 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600883 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800884 .pNext = NULL,
885 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600886 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800887 .flags = 0,
888 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600889 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600890 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500891 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800892 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600893 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800894 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500895 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600896 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800897 void *data;
898
899 memset(&demo->vertices, 0, sizeof(demo->vertices));
900
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600901 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800902 assert(!err);
903
Tony Barbourde4124d2015-07-03 10:33:54 -0600904 err = vkGetBufferMemoryRequirements(demo->device,
905 demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800906
Mark Lobodzinski72346292015-07-02 16:49:40 -0600907 mem_alloc.allocationSize = mem_reqs.size;
908 err = memory_type_from_properties(demo,
909 mem_reqs.memoryTypeBits,
910 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
911 &mem_alloc.memoryTypeIndex);
912 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800913
Mark Lobodzinski23182612015-05-29 09:32:35 -0500914 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
915 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800916
Mark Lobodzinski23182612015-05-29 09:32:35 -0500917 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
918 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800919
Mark Lobodzinski23182612015-05-29 09:32:35 -0500920 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800921
Mark Lobodzinski23182612015-05-29 09:32:35 -0500922 err = vkUnmapMemory(demo->device, demo->vertices.mem);
923 assert(!err);
924
Tony Barbourde4124d2015-07-03 10:33:54 -0600925 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500926 demo->vertices.mem, 0);
927 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800928
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600929 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800930 demo->vertices.vi.pNext = NULL;
931 demo->vertices.vi.bindingCount = 1;
932 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
933 demo->vertices.vi.attributeCount = 2;
934 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
935
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600936 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800937 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600938 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800939
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600940 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
941 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600942 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800943 demo->vertices.vi_attrs[0].offsetInBytes = 0;
944
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600945 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
946 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600947 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800948 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800949}
950
Chia-I Wuf8385062015-01-04 16:27:24 +0800951static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800952{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600953 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600954 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800955 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600956 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800957 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800958 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600959 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600960 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800961 .pNext = NULL,
962 .count = 1,
963 .pBinding = &layout_binding,
964 };
Tony Barbour22a30862015-04-22 09:02:32 -0600965 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800966
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600967 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800968 &descriptor_layout, &demo->desc_layout);
969 assert(!err);
970
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500971 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
972 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
973 .pNext = NULL,
974 .descriptorSetCount = 1,
975 .pSetLayouts = &demo->desc_layout,
976 };
977
978 err = vkCreatePipelineLayout(demo->device,
979 &pPipelineLayoutCreateInfo,
980 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800981 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800982}
983
Chia-I Wu76cd4222015-07-08 13:34:24 +0800984static void demo_prepare_render_pass(struct demo *demo)
985{
Chia-I Wuc278df82015-07-07 11:50:03 +0800986 const VkAttachmentDescription attachments[2] = {
987 [0] = {
988 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
989 .pNext = NULL,
990 .format = demo->format,
991 .samples = 1,
992 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
993 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
994 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
995 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
996 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
997 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
998 },
999 [1] = {
1000 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1001 .pNext = NULL,
1002 .format = demo->depth.format,
1003 .samples = 1,
1004 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1005 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1006 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1007 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1008 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1009 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1010 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001011 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001012 const VkAttachmentReference color_reference = {
1013 .attachment = 0,
1014 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1015 };
1016 const VkSubpassDescription subpass = {
1017 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1018 .pNext = NULL,
1019 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1020 .flags = 0,
1021 .inputCount = 0,
1022 .inputAttachments = NULL,
1023 .colorCount = 1,
1024 .colorAttachments = &color_reference,
1025 .resolveAttachments = NULL,
1026 .depthStencilAttachment = {
1027 .attachment = 1,
1028 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1029 },
1030 .preserveCount = 0,
1031 .preserveAttachments = NULL,
1032 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001033 const VkRenderPassCreateInfo rp_info = {
1034 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1035 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001036 .attachmentCount = 2,
1037 .pAttachments = attachments,
1038 .subpassCount = 1,
1039 .pSubpasses = &subpass,
1040 .dependencyCount = 0,
1041 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001042 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001043 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001044
1045 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1046 assert(!err);
1047}
1048
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001049static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001050 VkShaderStage stage,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001051 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001052 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001053{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001054 VkShaderModuleCreateInfo moduleCreateInfo;
1055 VkShaderCreateInfo shaderCreateInfo;
1056 VkShaderModule shaderModule;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001057 VkShader shader;
1058 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001059
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001060
1061 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1062 moduleCreateInfo.pNext = NULL;
1063
1064 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1065 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001066
Cody Northrop75db0322015-05-28 11:27:16 -06001067 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001068 moduleCreateInfo.codeSize = size;
1069 moduleCreateInfo.pCode = code;
1070 moduleCreateInfo.flags = 0;
1071 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
1072 if (err) {
1073 free((void *) moduleCreateInfo.pCode);
1074 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001075
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001076 shaderCreateInfo.flags = 0;
1077 shaderCreateInfo.module = shaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001078 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001079 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001080 } else {
1081 // Create fake SPV structure to feed GLSL
1082 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001083 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1084 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1085 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001086
Cody Northrop75db0322015-05-28 11:27:16 -06001087 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001088 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1089 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1090 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1091 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001092
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001093 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001094 if (err) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001095 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001096 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001097
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001098 shaderCreateInfo.flags = 0;
1099 shaderCreateInfo.module = shaderModule;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001100 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001101 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
1102 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001103 return shader;
1104}
1105
Cody Northrop75db0322015-05-28 11:27:16 -06001106char *demo_read_spv(const char *filename, size_t *psize)
1107{
1108 long int size;
1109 void *shader_code;
Tony Barbour9687cb12015-07-14 13:34:05 -06001110 size_t retVal;
Cody Northrop75db0322015-05-28 11:27:16 -06001111
1112 FILE *fp = fopen(filename, "rb");
1113 if (!fp) return NULL;
1114
1115 fseek(fp, 0L, SEEK_END);
1116 size = ftell(fp);
1117
1118 fseek(fp, 0L, SEEK_SET);
1119
1120 shader_code = malloc(size);
Tony Barbour9687cb12015-07-14 13:34:05 -06001121 retVal = fread(shader_code, size, 1, fp);
1122 if (!retVal) return NULL;
Cody Northrop75db0322015-05-28 11:27:16 -06001123
1124 *psize = size;
1125
1126 return shader_code;
1127}
1128
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001129static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001130{
Cody Northrop75db0322015-05-28 11:27:16 -06001131 if (!demo->use_glsl) {
1132 void *vertShaderCode;
1133 size_t size;
1134
1135 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
1136
1137 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1138 vertShaderCode, size);
1139 } else {
1140 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -05001141 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -06001142 "#extension GL_ARB_separate_shader_objects : enable\n"
1143 "#extension GL_ARB_shading_language_420pack : enable\n"
1144 "layout (location = 0) in vec4 pos;\n"
1145 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001146 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001147 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001148 " texcoord = attr;\n"
1149 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001150 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001151
Cody Northrop75db0322015-05-28 11:27:16 -06001152 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1153 (const void *) vertShaderText,
1154 strlen(vertShaderText));
1155 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001156}
1157
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001158static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001159{
Cody Northrop75db0322015-05-28 11:27:16 -06001160 if (!demo->use_glsl) {
1161 void *fragShaderCode;
1162 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001163
Cody Northrop75db0322015-05-28 11:27:16 -06001164 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
1165
1166 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1167 fragShaderCode, size);
1168 } else {
1169 static const char *fragShaderText =
1170 "#version 140\n"
1171 "#extension GL_ARB_separate_shader_objects : enable\n"
1172 "#extension GL_ARB_shading_language_420pack : enable\n"
1173 "layout (binding = 0) uniform sampler2D tex;\n"
1174 "layout (location = 0) in vec2 texcoord;\n"
1175 "layout (location = 0) out vec4 uFragColor;\n"
1176 "void main() {\n"
1177 " uFragColor = texture(tex, texcoord);\n"
1178 "}\n";
1179
1180 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1181 (const void *) fragShaderText,
1182 strlen(fragShaderText));
1183 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001184}
1185
1186static void demo_prepare_pipeline(struct demo *demo)
1187{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001188 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001189 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001190
Tony Barboure307f582015-07-10 15:29:03 -06001191 VkPipelineVertexInputStateCreateInfo vi;
1192 VkPipelineInputAssemblyStateCreateInfo ia;
1193 VkPipelineRasterStateCreateInfo rs;
1194 VkPipelineColorBlendStateCreateInfo cb;
1195 VkPipelineDepthStencilStateCreateInfo ds;
1196 VkPipelineViewportStateCreateInfo vp;
1197 VkPipelineMultisampleStateCreateInfo ms;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001198
Tony Barbour22a30862015-04-22 09:02:32 -06001199 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001200
1201 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001202 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001203 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001204
Chia-I Wu8d29d022014-10-08 12:14:39 +08001205 vi = demo->vertices.vi;
1206
Chia-I Wuc19795a2014-09-13 11:12:55 +08001207 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001208 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001209 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001210
1211 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001212 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001213 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001214 rs.cullMode = VK_CULL_MODE_BACK;
1215 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001216 rs.depthClipEnable = VK_TRUE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001217
1218 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001219 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1220 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001221 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001222 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001223 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001224 cb.attachmentCount = 1;
1225 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001226
Tony Barbourfa6cac72015-01-16 14:27:35 -07001227 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001228 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001229 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001230
1231 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001232 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001233 ds.depthTestEnable = VK_TRUE;
1234 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001235 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001236 ds.depthBoundsEnable = VK_FALSE;
1237 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1238 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001239 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001240 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001241 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001242
Tony Barbourfa6cac72015-01-16 14:27:35 -07001243 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001244 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001245 ms.sampleMask = 1;
Tony Barboure094edf2015-06-26 10:18:34 -06001246 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001247
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001248 // Two stages: vs and fs
1249 pipeline.stageCount = 2;
1250 VkPipelineShaderStageCreateInfo shaderStages[2];
1251 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1252
1253 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1254 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1255 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001256
1257 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1258 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1259 shaderStages[1].shader = demo_prepare_fs(demo);
1260
Tony Barboure307f582015-07-10 15:29:03 -06001261 pipeline.pVertexInputState = &vi;
1262 pipeline.pInputAssemblyState = &ia;
1263 pipeline.pRasterState = &rs;
1264 pipeline.pColorBlendState = &cb;
1265 pipeline.pMultisampleState = &ms;
1266 pipeline.pViewportState = &vp;
1267 pipeline.pDepthStencilState = &ds;
1268 pipeline.pStages = shaderStages;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001269
Jon Ashburn0d60d272015-07-09 15:02:25 -06001270 memset(&pipelineCache, 0, sizeof(pipelineCache));
1271 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1272
1273 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1274 assert(!err);
1275 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001276 assert(!err);
1277
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001278 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001279 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001280 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001281}
1282
1283static void demo_prepare_dynamic_states(struct demo *demo)
1284{
Tony Barbourde4124d2015-07-03 10:33:54 -06001285 VkDynamicViewportStateCreateInfo viewport_create;
1286 VkDynamicRasterStateCreateInfo raster;
1287 VkDynamicColorBlendStateCreateInfo color_blend;
1288 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001289 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001290
Tony Barbourfa6cac72015-01-16 14:27:35 -07001291 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001292 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001293 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001294 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001295 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001296 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001297 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001298 viewport.minDepth = (float) 0.0f;
1299 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001300 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001301 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001302 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001303 scissor.extent.width = demo->width;
1304 scissor.extent.height = demo->height;
1305 scissor.offset.x = 0;
1306 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001307 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001308
1309 memset(&raster, 0, sizeof(raster));
Tony Barbourde4124d2015-07-03 10:33:54 -06001310 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001311 raster.lineWidth = 1.0;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001312
1313 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourde4124d2015-07-03 10:33:54 -06001314 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001315 color_blend.blendConst[0] = 1.0f;
1316 color_blend.blendConst[1] = 1.0f;
1317 color_blend.blendConst[2] = 1.0f;
1318 color_blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001319
1320 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourde4124d2015-07-03 10:33:54 -06001321 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001322 depth_stencil.minDepthBounds = 0.0f;
1323 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001324 depth_stencil.stencilBackRef = 0;
1325 depth_stencil.stencilFrontRef = 0;
1326 depth_stencil.stencilReadMask = 0xff;
1327 depth_stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001328
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001329 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001330 assert(!err);
1331
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001332 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001333 assert(!err);
1334
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001335 err = vkCreateDynamicColorBlendState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001336 &color_blend, &demo->color_blend);
1337 assert(!err);
1338
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001339 err = vkCreateDynamicDepthStencilState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001340 &depth_stencil, &demo->depth_stencil);
1341 assert(!err);
1342}
1343
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001344static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001345{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001346 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001347 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001348 .count = DEMO_TEXTURE_COUNT,
1349 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001350 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001351 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001352 .pNext = NULL,
1353 .count = 1,
1354 .pTypeCount = &type_count,
1355 };
Tony Barbour22a30862015-04-22 09:02:32 -06001356 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001357
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001358 err = vkCreateDescriptorPool(demo->device,
1359 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001360 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001361 assert(!err);
1362}
1363
1364static void demo_prepare_descriptor_set(struct demo *demo)
1365{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001366 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1367 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001368 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001369 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001370 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001371
Mike Stroyan230e6252015-04-17 12:36:38 -06001372 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001373 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001374 1, &demo->desc_layout,
1375 &demo->desc_set, &count);
1376 assert(!err && count == 1);
1377
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001378 memset(&tex_descs, 0, sizeof(tex_descs));
1379 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1380 tex_descs[i].sampler = demo->textures[i].sampler;
1381 tex_descs[i].imageView = demo->textures[i].view;
1382 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1383 }
1384
1385 memset(&write, 0, sizeof(write));
1386 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1387 write.destSet = demo->desc_set;
1388 write.count = DEMO_TEXTURE_COUNT;
1389 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1390 write.pDescriptors = tex_descs;
1391
1392 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1393 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001394}
1395
Chia-I Wu76cd4222015-07-08 13:34:24 +08001396static void demo_prepare_framebuffers(struct demo *demo)
1397{
Chia-I Wuc278df82015-07-07 11:50:03 +08001398 VkAttachmentBindInfo attachments[2] = {
1399 [0] = {
Tony Barbourde4124d2015-07-03 10:33:54 -06001400 .view.handle = VK_NULL_HANDLE,
Chia-I Wuc278df82015-07-07 11:50:03 +08001401 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1402 },
1403 [1] = {
1404 .view = demo->depth.view,
1405 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1406 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001407 };
1408 const VkFramebufferCreateInfo fb_info = {
1409 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1410 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001411 .renderPass = demo->render_pass,
1412 .attachmentCount = 2,
1413 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001414 .width = demo->width,
1415 .height = demo->height,
1416 .layers = 1,
1417 };
1418 VkResult U_ASSERT_ONLY err;
1419 uint32_t i;
1420
1421 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +08001422 attachments[0].view = demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001423 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1424 assert(!err);
1425 }
1426}
1427
Chia-I Wuc19795a2014-09-13 11:12:55 +08001428static void demo_prepare(struct demo *demo)
1429{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001430 VkResult U_ASSERT_ONLY err;
1431
1432 const VkCmdPoolCreateInfo cmd_pool_info = {
1433 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1434 .pNext = NULL,
1435 .queueFamilyIndex = demo->graphics_queue_node_index,
1436 .flags = 0,
1437 };
1438 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1439 assert(!err);
1440
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001441 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001442 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001443 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001444 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001445 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001446 .flags = 0,
1447 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001448 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1449 assert(!err);
1450
Chia-I Wuc19795a2014-09-13 11:12:55 +08001451 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001452 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001453 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001454 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001455 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001456 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001457 demo_prepare_pipeline(demo);
1458 demo_prepare_dynamic_states(demo);
1459
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001460 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001461 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001462
1463 demo_prepare_framebuffers(demo);
1464
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001465 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001466}
1467
Ian Elliotte14e9f92015-04-16 15:23:05 -06001468#ifdef _WIN32
1469static void demo_run(struct demo *demo)
1470{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001471 if (!demo->prepared)
1472 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001473 demo_draw(demo);
1474}
1475
1476// On MS-Windows, make this a global, so it's available to WndProc()
1477struct demo demo;
1478
1479// MS-Windows event handling function:
1480LRESULT CALLBACK WndProc(HWND hWnd,
1481 UINT uMsg,
1482 WPARAM wParam,
1483 LPARAM lParam)
1484{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001485 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001486
1487 switch(uMsg)
1488 {
Ian Elliotte36b2082015-07-06 14:27:58 -06001489 case WM_CREATE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001490 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001491 case WM_CLOSE:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001492 PostQuitMessage(0);
1493 return 0;
Ian Elliotte36b2082015-07-06 14:27:58 -06001494 case WM_PAINT:
Ian Elliotte14e9f92015-04-16 15:23:05 -06001495 demo_run(&demo);
1496 return 0;
1497 default:
1498 break;
1499 }
1500 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1501}
1502
1503static void demo_create_window(struct demo *demo)
1504{
1505 WNDCLASSEX win_class;
1506
1507 // Initialize the window class structure:
1508 win_class.cbSize = sizeof(WNDCLASSEX);
1509 win_class.style = CS_HREDRAW | CS_VREDRAW;
1510 win_class.lpfnWndProc = WndProc;
1511 win_class.cbClsExtra = 0;
1512 win_class.cbWndExtra = 0;
1513 win_class.hInstance = demo->connection; // hInstance
1514 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1515 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1516 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1517 win_class.lpszMenuName = NULL;
1518 win_class.lpszClassName = demo->name;
1519 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1520 // Register window class:
1521 if (!RegisterClassEx(&win_class)) {
1522 // It didn't work, so try to give a useful error:
1523 printf("Unexpected error trying to start the application!\n");
1524 fflush(stdout);
1525 exit(1);
1526 }
1527 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001528 RECT wr = { 0, 0, demo->width, demo->height };
1529 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001530 demo->window = CreateWindowEx(0,
1531 demo->name, // class name
1532 demo->name, // app name
1533 WS_OVERLAPPEDWINDOW | // window style
1534 WS_VISIBLE |
1535 WS_SYSMENU,
1536 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001537 wr.right-wr.left, // width
1538 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001539 NULL, // handle to parent
1540 NULL, // handle to menu
1541 demo->connection, // hInstance
1542 NULL); // no extra parameters
1543 if (!demo->window) {
1544 // It didn't work, so try to give a useful error:
1545 printf("Cannot create a window in which to draw!\n");
1546 fflush(stdout);
1547 exit(1);
1548 }
1549}
1550#else // _WIN32
1551
Chia-I Wuc19795a2014-09-13 11:12:55 +08001552static void demo_handle_event(struct demo *demo,
1553 const xcb_generic_event_t *event)
1554{
1555 switch (event->response_type & 0x7f) {
1556 case XCB_EXPOSE:
1557 demo_draw(demo);
1558 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001559 case XCB_CLIENT_MESSAGE:
1560 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1561 (*demo->atom_wm_delete_window).atom) {
1562 demo->quit = true;
1563 }
1564 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001565 case XCB_KEY_RELEASE:
1566 {
1567 const xcb_key_release_event_t *key =
1568 (const xcb_key_release_event_t *) event;
1569
1570 if (key->detail == 0x9)
1571 demo->quit = true;
1572 }
1573 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001574 case XCB_DESTROY_NOTIFY:
1575 demo->quit = true;
1576 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001577 default:
1578 break;
1579 }
1580}
1581
1582static void demo_run(struct demo *demo)
1583{
1584 xcb_flush(demo->connection);
1585
1586 while (!demo->quit) {
1587 xcb_generic_event_t *event;
1588
1589 event = xcb_wait_for_event(demo->connection);
1590 if (event) {
1591 demo_handle_event(demo, event);
1592 free(event);
1593 }
1594 }
1595}
1596
1597static void demo_create_window(struct demo *demo)
1598{
1599 uint32_t value_mask, value_list[32];
1600
1601 demo->window = xcb_generate_id(demo->connection);
1602
1603 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1604 value_list[0] = demo->screen->black_pixel;
1605 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001606 XCB_EVENT_MASK_EXPOSURE |
1607 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001608
1609 xcb_create_window(demo->connection,
1610 XCB_COPY_FROM_PARENT,
1611 demo->window, demo->screen->root,
1612 0, 0, demo->width, demo->height, 0,
1613 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1614 demo->screen->root_visual,
1615 value_mask, value_list);
1616
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001617 /* Magic code that will send notification when window is destroyed */
1618 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1619 "WM_PROTOCOLS");
1620 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1621
1622 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1623 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1624
1625 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1626 demo->window, (*reply).atom, 4, 32, 1,
1627 &(*demo->atom_wm_delete_window).atom);
1628 free(reply);
1629
Chia-I Wuc19795a2014-09-13 11:12:55 +08001630 xcb_map_window(demo->connection, demo->window);
1631}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001632#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001633
Ian Elliotte36b2082015-07-06 14:27:58 -06001634/*
1635 * Return 1 (true) if all layer names specified in check_names
1636 * can be found in given layer properties.
1637 */
1638static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
1639 uint32_t layer_count, VkLayerProperties *layers)
1640{
1641 for (uint32_t i = 0; i < check_count; i++) {
1642 VkBool32 found = 0;
1643 for (uint32_t j = 0; j < layer_count; j++) {
1644 if (!strcmp(check_names[i], layers[j].layerName)) {
1645 found = 1;
1646 }
1647 }
1648 if (!found) {
1649 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1650 return 0;
1651 }
1652 }
1653 return 1;
1654}
1655
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001656static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001657{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001658 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001659 char *extension_names[64];
1660 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001661 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001662 VkLayerProperties *instance_layers;
1663 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001664 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001665 uint32_t instance_layer_count = 0;
1666 uint32_t enabled_extension_count = 0;
1667 uint32_t enabled_layer_count = 0;
1668
Ian Elliotte36b2082015-07-06 14:27:58 -06001669 char *instance_validation_layers[] = {
1670 "MemTracker",
1671 };
1672
1673 char *device_validation_layers[] = {
1674 "MemTracker",
1675 };
1676
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001677 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001678 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001679 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001680 assert(!err);
1681
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001682 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1683 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1684 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001685
1686 if (demo->validate) {
1687 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1688 instance_layer_count, instance_layers);
1689 if (!validation_found) {
1690 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
1691 "required validation layer.\n\n"
1692 "Please look at the Getting Started guide for additional "
1693 "information.\n",
1694 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001695 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001696 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001697 }
1698
1699 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1700 assert(!err);
1701
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001702 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001703 memset(extension_names, 0, sizeof(extension_names));
1704 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1705 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1706 assert(!err);
1707 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001708 if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001709 WSIextFound = 1;
Ian Elliotte36b2082015-07-06 14:27:58 -06001710 extension_names[enabled_extension_count++] = "VK_WSI_swapchain";
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001711 }
1712 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
1713 if (demo->validate) {
1714 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
1715 }
1716 }
1717 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001718 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001719 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001720 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliotte36b2082015-07-06 14:27:58 -06001721 "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001722 "Vulkan installable client driver (ICD) installed?\nPlease "
1723 "look at the Getting Started guide for additional "
1724 "information.\n",
1725 "vkCreateInstance Failure");
1726 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001727 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001728 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001729 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001730 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001731 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001732 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001733 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001734 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001735 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001736 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001737 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001738 .pNext = NULL,
1739 .pAppInfo = &app,
1740 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001741 .layerCount = enabled_layer_count,
1742 .ppEnabledLayerNames = (const char *const*) layer_names,
1743 .extensionCount = enabled_extension_count,
1744 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001745 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001746 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001747 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001748 .queueCount = 1,
1749 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001750 uint32_t gpu_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001751
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001752 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001753 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1754 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001755 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001756 "additional information.\n",
1757 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001758 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1759 ERR_EXIT("Cannot find a specified extension library"
1760 ".\nMake sure your layers path is set appropriately\n",
1761 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001762 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001763 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1764 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001765 "the Getting Started guide for additional information.\n",
1766 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001767 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001768
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001769 free(instance_layers);
1770 free(instance_extensions);
1771
Jon Ashburn07b309a2015-04-15 11:31:12 -06001772 gpu_count = 1;
1773 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001774 assert(!err && gpu_count == 1);
1775
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001776 /* Look for validation layers */
1777 validation_found = 0;
1778 enabled_layer_count = 0;
1779 uint32_t device_layer_count = 0;
1780 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
1781 assert(!err);
1782
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001783 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
1784 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
1785 assert(!err);
Ian Elliotte36b2082015-07-06 14:27:58 -06001786
1787 if (demo->validate) {
1788 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
1789 device_layer_count, device_layers);
1790 if (!validation_found) {
1791 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
1792 "a required validation layer.\n\n"
1793 "Please look at the Getting Started guide for additional "
1794 "information.\n",
1795 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001796 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001797 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001798 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001799
1800 uint32_t device_extension_count = 0;
1801 VkExtensionProperties *device_extensions = NULL;
1802 err = vkGetPhysicalDeviceExtensionProperties(
1803 demo->gpu, NULL, &device_extension_count, NULL);
1804 assert(!err);
1805
1806 WSIextFound = 0;
1807 enabled_extension_count = 0;
1808 memset(extension_names, 0, sizeof(extension_names));
1809 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
1810 err = vkGetPhysicalDeviceExtensionProperties(
1811 demo->gpu, NULL, &device_extension_count, device_extensions);
1812 assert(!err);
1813
1814 for (uint32_t i = 0; i < device_extension_count; i++) {
1815 if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) {
1816 WSIextFound = 1;
1817 extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain";
1818 }
1819 assert(enabled_extension_count < 64);
1820 }
1821 if (!WSIextFound) {
1822 ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the "
1823 "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible "
1824 "Vulkan installable client driver (ICD) installed?\nPlease "
1825 "look at the Getting Started guide for additional "
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001826 "information.\n",
1827 "vkCreateInstance Failure");
1828 }
1829
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001830 VkDeviceCreateInfo device = {
1831 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1832 .pNext = NULL,
1833 .queueRecordCount = 1,
1834 .pRequestedQueues = &queue,
1835 .layerCount = enabled_layer_count,
Ian Elliotte36b2082015-07-06 14:27:58 -06001836 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
1837 .extensionCount = enabled_extension_count,
1838 .ppEnabledExtensionNames = (const char *const*) extension_names,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001839 .flags = 0,
1840 };
1841
1842 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001843 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001844 if (!demo->dbgCreateMsgCallback) {
1845 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1846 "vkGetProcAddr Failure");
1847 }
1848 err = demo->dbgCreateMsgCallback(
1849 demo->inst,
1850 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1851 dbgFunc, NULL,
1852 &demo->msg_callback);
1853 switch (err) {
1854 case VK_SUCCESS:
1855 break;
1856 case VK_ERROR_INVALID_POINTER:
1857 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1858 "dbgCreateMsgCallback Failure");
1859 break;
1860 case VK_ERROR_OUT_OF_HOST_MEMORY:
1861 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1862 "dbgCreateMsgCallback Failure");
1863 break;
1864 default:
1865 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1866 "dbgCreateMsgCallback Failure");
1867 break;
1868 }
1869 }
1870
Ian Elliotte36b2082015-07-06 14:27:58 -06001871
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001872 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001873 assert(!err);
1874
Ian Elliotte36b2082015-07-06 14:27:58 -06001875 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI);
1876 GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceInfoWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06001877 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1878 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1879 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
1880 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
Ian Elliotte36b2082015-07-06 14:27:58 -06001881 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI);
Ian Elliott1b6de092015-06-22 15:07:49 -06001882 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
1883
Tony Barbour426b9052015-06-24 16:06:58 -06001884 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001885
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001886 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &demo->queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001887 assert(!err);
1888
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001889 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(demo->queue_count * sizeof(VkPhysicalDeviceQueueProperties));
1890 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, demo->queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001891 assert(!err);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001892 assert(demo->queue_count >= 1);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001893
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001894 // Graphics queue and MemMgr queue can be separate.
1895 // TODO: Add support for separate queues, including synchronization,
1896 // and appropriate tracking for QueueSubmit
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001897}
1898
1899static void demo_init_vk_wsi(struct demo *demo)
1900{
1901 VkResult err;
1902 uint32_t i;
Ian Elliotte36b2082015-07-06 14:27:58 -06001903
1904 // Construct the WSI surface description:
1905 demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI;
1906 demo->surface_description.pNext = NULL;
1907#ifdef _WIN32
1908 demo->surface_description.platform = VK_PLATFORM_WIN32_WSI;
1909 demo->surface_description.pPlatformHandle = demo->connection;
1910 demo->surface_description.pPlatformWindow = demo->window;
1911#else // _WIN32
1912 demo->platform_handle_xcb.connection = demo->connection;
1913 demo->platform_handle_xcb.root = demo->screen->root;
1914 demo->surface_description.platform = VK_PLATFORM_XCB_WSI;
1915 demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb;
1916 demo->surface_description.pPlatformWindow = &demo->window;
1917#endif // _WIN32
1918
1919 // Iterate over each queue to learn whether it supports presenting to WSI:
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001920 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
1921 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001922 demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i,
1923 (VkSurfaceDescriptionWSI *) &demo->surface_description,
1924 &supportsPresent[i]);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001925 }
Ian Elliotte36b2082015-07-06 14:27:58 -06001926
1927 // Search for a graphics and a present queue in the array of queue
1928 // families, try to find one that supports both
1929 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
1930 uint32_t presentQueueNodeIndex = UINT32_MAX;
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001931 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001932 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
1933 if (graphicsQueueNodeIndex == UINT32_MAX) {
1934 graphicsQueueNodeIndex = i;
1935 }
1936
1937 if (supportsPresent[i] == VK_TRUE) {
1938 graphicsQueueNodeIndex = i;
1939 presentQueueNodeIndex = i;
1940 break;
1941 }
1942 }
1943 }
1944 if (presentQueueNodeIndex == UINT32_MAX) {
1945 // If didn't find a queue that supports both graphics and present, then
1946 // find a separate present queue.
Piers Daniell1cf7fe12015-07-16 09:35:35 -06001947 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliotte36b2082015-07-06 14:27:58 -06001948 if (supportsPresent[i] == VK_TRUE) {
1949 presentQueueNodeIndex = i;
1950 break;
1951 }
1952 }
1953 }
1954 free(supportsPresent);
1955
1956 // Generate error if could not find both a graphics and a present queue
1957 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
1958 ERR_EXIT("Could not find a graphics and a present queue\n",
1959 "WSI Initialization Failure");
1960 }
1961
1962 // TODO: Add support for separate queues, including presentation,
1963 // synchronization, and appropriate tracking for QueueSubmit
1964 // While it is possible for an application to use a separate graphics and a
1965 // present queues, this demo program assumes it is only using one:
1966 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
1967 ERR_EXIT("Could not find a common graphics and a present queue\n",
1968 "WSI Initialization Failure");
1969 }
1970
1971 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001972
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001973 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001974 0, &demo->queue);
1975 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06001976
Ian Elliotte36b2082015-07-06 14:27:58 -06001977 // Get the list of VkFormat's that are supported:
1978 size_t formatsSize;
1979 err = demo->fpGetSurfaceInfoWSI(demo->device,
1980 (VkSurfaceDescriptionWSI *) &demo->surface_description,
1981 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
1982 &formatsSize, NULL);
1983 assert(!err);
1984 VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize);
1985 err = demo->fpGetSurfaceInfoWSI(demo->device,
1986 (VkSurfaceDescriptionWSI *) &demo->surface_description,
1987 VK_SURFACE_INFO_TYPE_FORMATS_WSI,
1988 &formatsSize, surfFormats);
1989 assert(!err);
1990 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
1991 // the surface has no preferred format. Otherwise, at least one
1992 // supported format will be returned.
1993 size_t formatCount = formatsSize / sizeof(VkSurfaceFormatPropertiesWSI);
1994 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
1995 {
1996 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
1997 }
1998 else
1999 {
2000 assert(formatCount >= 1);
2001 demo->format = surfFormats[0].format;
2002 }
Ian Elliott32536f92015-04-21 16:41:02 -06002003
Mark Lobodzinski72346292015-07-02 16:49:40 -06002004 // Get Memory information and properties
2005 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2006 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002007}
2008
2009static void demo_init_connection(struct demo *demo)
2010{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002011#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002012 const xcb_setup_t *setup;
2013 xcb_screen_iterator_t iter;
2014 int scr;
2015
2016 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002017 if (demo->connection == NULL) {
2018 printf("Cannot find a compatible Vulkan installable client driver "
2019 "(ICD).\nExiting ...\n");
2020 fflush(stdout);
2021 exit(1);
2022 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08002023
2024 setup = xcb_get_setup(demo->connection);
2025 iter = xcb_setup_roots_iterator(setup);
2026 while (scr-- > 0)
2027 xcb_screen_next(&iter);
2028
2029 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002030#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002031}
2032
Ian Elliotte14e9f92015-04-16 15:23:05 -06002033#ifdef _WIN32
2034static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
2035#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002036static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06002037#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002038{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002039 bool argv_error = false;
2040
Chia-I Wuc19795a2014-09-13 11:12:55 +08002041 memset(demo, 0, sizeof(*demo));
2042
Ian Elliotte14e9f92015-04-16 15:23:05 -06002043#ifdef _WIN32
2044 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06002045 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002046
2047 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2048 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002049 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
2050 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002051 else if (strlen(pCmdLine) != 0) {
2052 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2053 argv_error = true;
2054 }
2055#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002056 for (int i = 0; i < argc; i++) {
2057 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2058 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06002059 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
2060 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002061 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06002062#endif // _WIN32
2063 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06002064 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002065 fflush(stderr);
2066 exit(1);
2067 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002068
Chia-I Wuc19795a2014-09-13 11:12:55 +08002069 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002070 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002071
2072 demo->width = 300;
2073 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002074}
2075
2076static void demo_cleanup(struct demo *demo)
2077{
Mark Lobodzinski23182612015-05-29 09:32:35 -05002078 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08002079
Tony Barbourde4124d2015-07-03 10:33:54 -06002080 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
2081 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
2082 }
Tony Barbourb857d312015-07-10 10:50:45 -06002083 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06002084 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002085
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002086 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002087 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06002088 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002089 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002090 vkDestroyCommandPool(demo->device, demo->cmd_pool);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002091
Tony Barbourde4124d2015-07-03 10:33:54 -06002092 vkDestroyDynamicViewportState(demo->device, demo->viewport);
2093 vkDestroyDynamicRasterState(demo->device, demo->raster);
2094 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
2095 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002096
Tony Barbourde4124d2015-07-03 10:33:54 -06002097 vkDestroyPipeline(demo->device, demo->pipeline);
2098 vkDestroyRenderPass(demo->device, demo->render_pass);
2099 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
2100 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08002101
Tony Barbourde4124d2015-07-03 10:33:54 -06002102 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002103 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08002104
Chia-I Wub043fe32014-10-06 15:30:33 +08002105 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002106 vkDestroyImageView(demo->device, demo->textures[i].view);
2107 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05002108 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06002109 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08002110 }
2111
Chia-I Wuc19795a2014-09-13 11:12:55 +08002112 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06002113 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002114 }
Tony Barbourde4124d2015-07-03 10:33:54 -06002115
2116 vkDestroyAttachmentView(demo->device, demo->depth.view);
2117 vkDestroyImage(demo->device, demo->depth.image);
2118 vkFreeMemory(demo->device, demo->depth.mem);
2119
Ian Elliotte36b2082015-07-06 14:27:58 -06002120 demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain);
2121 free(demo->buffers);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002122
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002123 vkDestroyDevice(demo->device);
2124 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002125
Ian Elliotte14e9f92015-04-16 15:23:05 -06002126#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002127 xcb_destroy_window(demo->connection, demo->window);
2128 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002129#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08002130}
2131
Ian Elliotte14e9f92015-04-16 15:23:05 -06002132#ifdef _WIN32
2133int APIENTRY WinMain(HINSTANCE hInstance,
2134 HINSTANCE hPrevInstance,
2135 LPSTR pCmdLine,
2136 int nCmdShow)
2137{
2138 MSG msg; // message
2139 bool done; // flag saying when app is complete
2140
2141 demo_init(&demo, hInstance, pCmdLine);
2142 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002143 demo_init_vk_wsi(&demo);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002144
2145 demo_prepare(&demo);
2146
2147 done = false; //initialize loop condition variable
2148 /* main message loop*/
2149 while(!done)
2150 {
Ian Elliott421107f2015-04-28 15:50:36 -06002151 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002152 if (msg.message == WM_QUIT) //check for a quit message
2153 {
2154 done = true; //if found, quit app
2155 }
2156 else
2157 {
2158 /* Translate and dispatch to event queue*/
2159 TranslateMessage(&msg);
2160 DispatchMessage(&msg);
2161 }
2162 }
2163
2164 demo_cleanup(&demo);
2165
Tony Barboura938abb2015-04-22 11:36:22 -06002166 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002167}
2168#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002169int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08002170{
2171 struct demo demo;
2172
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07002173 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002174 demo_create_window(&demo);
Piers Daniell1cf7fe12015-07-16 09:35:35 -06002175 demo_init_vk_wsi(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002176
2177 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08002178 demo_run(&demo);
2179
2180 demo_cleanup(&demo);
2181
Chia-I Wuc19795a2014-09-13 11:12:55 +08002182 return 0;
2183}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002184#endif // _WIN32